Download MAR – Memory Address Register MDR – Memory Data Register PC

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
MAR – Memory Address Register
MDR – Memory Data Register
PC – Program Counter
IR – Instruction Register
IX, IY – Index Register
SP – Stack Pointer
ACCA – Accumulator A
ACCB – Accumulator B
CCR – Conditional Code Register
ALU – Arithmetic Logic Unit
2

An ALU of Four Basic Operations
3

The result needs to be adjusted if necessary.
 Add $6 to every sum digit greater than $9
 Add $6 to every sum digit that had a carry of 1 to the next higher digit.

The fifth bit of the condition code register (CCR) is the halfcarry, or H flag, that keeps a carry from the lower 4 bits to the
higher 4 bits during the addition.
4

The HCS12 has the DAA instruction that performs a
decimal adjust accumulator A. - automatically adds $6 to
any 4 bits that requires it.

Example 4.2.1 (Example 2.9) – Add the BCD numbers
stored at $1000 and $1001 and save the sum at $1010.
Solution:
LDAA
ADDA
DAA
STAA
$1000
$1001
$1010
5

Example 4.2.2 – Separate two digits of a BCD number
Assume that X = 25 (BCD), we want to get A = 2 and B = 5
Register Transfer
Notation
AX
BA
B  B & $0F
AA/2
AA/2
AA/2
AA/2
Arithmetic
A=X
B=X
B = LSN (X)
A = X >> 1
A = X >> 2
A = X >> 3
A = X >> 4 = MSN(X)
Assembly Code
LDAA X
TAB
ANDB #$0F
LSRA
LSRA
LSRA
LSRA
6
Example 4.2.3, we want convert a BCD number to its equivalent binary number:
6810
=
(0110 1000)BCD = 0110 * 101 + 1000 * 100
The assembly program is as followed:
ORG $0000
bcdval DS.B 1
; BCD value
binval DS.B 1
; binary equivalent number.
ORG $C000
LDAA bcdval
; A <- BCD value
TAB
; B <- A
ANDA #$0F ; A <- LS bits of BCD number
LSRB
; B = B >> 1
LSRB
; B = B >> 2
LSRB
; B = B >> 3
LSRB
; B = B >> 4 = MS bits of BCD number
LSLB
; B <- B * 2
ABA
; A <- A + B = A + 2 * B
LSLB
; B <- B * 4
LSLB
; B <- B * 8
ABA
; A <- A + B = A + 2 * B + 8 * B
STAA binval
;
END
7
For example, we want convert a BCD number to its equivalent binary number:
6810
=
(0110 1000)BCD = 0110 * 101 + 1000 * 100
Alternative solution:
ORG $0000
bcdval DS.B 1
; BCD value
binval DS.B 1
; binary equivalent number.
ORG $C000
LDAA bcdval
; A <- BCD value
TAB
; B <- A
ANDA #$0F ; A <- LS bits of BCD number
ANDB #$F0 ; mask the lower bits of B; or B = 16 * B.
LSRB
; B = B >> 1 = 16 * B / 2 = 8 * B
ABA
; A <- A + B = A + 8 * B
LSRB
; B = B >> 2 = 16 * B / 4 = 4 * B
LSRB
; B = B >> 3 = 16 * B / 8 = 2 * B
ABA
; A <- A + B = A + 8 * B + 2 * B
STAA binval
;
END
8
Assembly language programming is a method of creating
instructions that are the symbolic equivalent of machine
code.
 Let us begin by examining a C program and its equivalent
assembly language code.
main (){
int
i, j, k
; i, j, k are integer variables
i = 75;
; assign 75 to i
j = 10;
; assign 10 to j
k=i+j–6
;
}

9
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)
* Data storage declaration section
ORG $0000
i
DS.B 1
; variable i
j
DS.B 1
; variable j
k
DS.B 1
; variable k
* program instruction section
start:
ORG $C000
; starting address of program
LDAA #75
STAA i
; initialize i to 75
LDAA #10
STAA j
; initialize j to 10
ADDA i
; compute i + j
SUBA #6
; compute i + j -6
STAA k
; store i + j - 6 to k
END
10
An Assembly Program is divided into four sections
1. Assembler Directives
 define data and symbol
 reserve and initialize memory locations
 set assembler and linking condition
 specify output format
 etc.
2. Assembly Language Instructions
3. END directive
 last statement of a program
 any statement after END will be ignored
4. Comments
 explain the function of a single or a group of instructions
11
1.
Label field
 It is optional
 It starts with a letter and
followed by letters, digits,
or special symbols (_ or .)
 It can start from any
column if ended with “:”
(not true for Motorola
freeware as11)
 It must start from column
1 if not ended with “:”
ORG $0000
Var1 DS.B 1
Var2 DS.B 1
ORG $4000
LDAA Var1
BEQ Next
ADDA #$01
Next: ADDA Var2
“Var1,” “Var2,” and
“Next” are labels.
12
Operation field
 It contains the mnemonic of a machine instruction or a
directive
 It is separated from the label by at least one space
2.
In the previous example:
 “ORG” and “DS.B” are directives
 “LDAA,” “BEQ,” and “ADDA” are assembly instructions
13
3.
Operand field
 It follows the operation field and is separated from the
operation field by at least one space
 It contains operands for instructions or arguments for
assembler directives
TRUE
LOOP:
4.
EQU 1
LDAB 0, X
BNE NEQ
; 1 is the operand field
; 0, X is the operand field
; NEQ is the operand field
Comment field
 It is the whole line comment starts with a *
 Any characters following the semicolon (;) are treated
as comment.
14

Identify the four fields of an instruction below
loop: ADDA #$40




; add 40 to accumulator A
(1) “loop” is a label
(2) “ADDA” is an instruction mnemonic
(3) “#$40” is the operand
(4) “add #$40 to accumulator A” is a comment
15

END – it ends a program to be processed by an assembler.
Any statement following the END directive is ignored

ORG – it sets a new value for the location counter of the
assembler. It tells the assembler where to put the next byte it
generates after the ORG directive
The sequence, for example,
ORG $1000
LDAB #$FF
will put the opcode byte for the instruction LDAB #$FF at
location $1000.
16

DC.B (define constant byte)
DB (define byte)
FCB (form constant byte)
- These three directives define the value of a byte or bytes
that will be placed at a given location.
- These directives are often preceded by the org directive.
- For example,
ORG $800
array DC.B $11,$22,$33,$44
string DC.B “Welcome to CPEN231”, $00
17

DC.W (define constant word)
DW(define word)
FDB (form double bytes)
-
Define the value of a word or words that will be placed
at a given location.
The value can be specified by an expression.
For example,
vec_tab
DC.W
$1234, $5678
18
FCC (form constant character) - It generates ASCII code
bytes for the letters in the arguments.
The syntax is
[label]
FCC “<string>“ [<comment>]
For example, the directive
ALPHA
FCC “DEF”
will generate the values $44 (for character D) $45 (for character
E) $46 (for character F) in memory

19

FILL – It fills a block of constant values. Its function is
similar to DCB directive.
syntax
[<label>] FILL <value>,<length>
The directive
spaceLines
FILL $20, 40
will fill 40 bytes with the value of $20 starting from the
memory location referred to by the label spaceLines.
20

DS (define storage)
RMB (reserve memory byte)
DS.B (define storage bytes)
-
Each of these directives reserves a number of bytes
given as the arguments to the directive.
-
For example,
buffer
DS
100
;reserves 100 bytes
21

DS.W (define storage word)
RMW(reserve memory word)
-
Each of these directives increments the location counter
by the value indicated in the number-of-words argument
multiplied by two.
-
For example,
dbuf DS.W 20
reserves 40 bytes starting from the memory location
represented by label dbuf.
22

EQU (equate) – it allows the user to use a symbolic name in
place of a number.
syntax is
<label>
EQU<expression> [<comment>]
The directive
loop_cnt
EQU 40
tells the assembler that wherever loop_cnt appears in the
program, the value 40 is to be substituted.
23
• LOC
-
loop`
This directive increments and produces an internal counter
used in conjunction with the backward tick mark (`). By
using the loc directive and the ` mark one can write program
segments like the following example, without thinking up
new labels:
LOC
LOC
LDAA #2
LDAA #2
DECA
BNE
same as
loop`
LOC
loop`
BRCLR 0,x,$55,loop`
loop001 DECA
BNE loop001
LOC
loop002 BRCLR 0,x,$55,loop002
24
• MACRO, ENDM
-
A name assigned to a group of instructions
Use macro and endm to define a macro.
Example of macro
sumOf3
MACRO
LDAA
ADDA
ADDA
ENDM
arg1,arg2,arg3
arg1
arg2
arg3
- Invoke a defined macro: write down the name and the arguments of the macro
sumOf3 $1000,$1001,$1002
is replaced by
LDAA
ADDA
ADDA
$1000
$1001
$1002
25

Define three variables V1, V2, and V3.
 V1 is 1 byte in length and starts at the address $12EF
 V2 is a constant, 2 bytes in length, with a value of $AFDB
 V3 is a string of “My Name”
Solution:
ORG
V1
DS.B
V2
DC.W
V3
DC.B
$12EF
$01
$AFDB
'My Name', 0
26

Define three variables V4, V5, and V6.
 V4 is 40 bytes in length, is filled with $AF, and starts at the address
$D100
 V5 is 2 bytes in length
 V6 is an array of 100 elements, initialized with $EB
Solution:
ORG $D100
$AF, 40
V4
FILL
V5
V6
DS.B 2
FILL $EB, 100
27
Flowchart – It is a form of program documentation. It is a
tool for developing program logic flow
 Symbols of Flowchart

28





The terminal symbol is used at the beginning and the end of
each program. E.g. the term Start or Stop is used.
The process box describes what must be done at this point in
the program execution.
The input/output box specifies whether data enters or leaves
the computer.
The decision box specifies the logical flow of a program
based on a result flag. It has only two exits.
The on-page connector indicates that the flowchart
continues elsewhere on the same page. The off-page
connector, on the other hand, signifies that the flowchart
continues on another page.
29
30

The assembly process generates an *.S19 file which contains the
machine code to be downloaded into the microcontroller. Here is
an example of assembly code for Lab0 project (main.asm).
S0920000433A5C55736572735C557365725C446F63756D656E74735C476
F6E7A61676120556E69766572736974795C436F75727365735C484353313
2202D204350454E203233314C202D204D6963726F636F6D707574657220
4172636869746563747572655C4C616230415C62696E5C50726F6A656374
2E61627348435331325F53657269616C5F4D6F6E69746F722E616273F6
S1234000790011C6395B1086095A12CF400010EF180BFF025A791000B6
1000180E585858A4
S1124020587B02584281072301877A100020E958
S105FFFE4000BD
S9030000FC
31

The .s19 file is a file consisting of readable characters in the
set of {S,0-9,A-F}. The file contains addresses and byteinformation to be located in memory starting from the
addresses. Addresses are specified by 4, 6, or 8 character
HEX strings, and bytes are specified by 2-character HEX
strings.

Each line in the s19 file contains:
the character ‘S’,
a single-digit code {1-9},
and a 2-character number indicating the number of bytes in the line.
32
1.
Digit Code
Following the “S” character, the first single digit is the code.
E.g.
S1234000790011C6395B1086095A12CF400010EF180BFF
025A791000B61000180E585858A4
“S1” indicates that memory address will be 16 bits.
33
Length of Command Line - The digits in the third and fourth
positions signify the number of digit pairs on the line.
3. Address of Memory Instructions - The next four digits are the
address of the first instruction.
4. Check-Sum - The last two digits on the command line
represent the checksum of the line
2.
S1234000790011C6395B1086095A12CF400010EF180BFF025A7910
00B61000180E585858A4
- “23” in Hex indicates that there will be 35 digit pairs after the fourth
digit.
- “4000” is the memory address of the first instruction
- “A4” is the check sum
34