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
1
An ALU of Four Basic Operations
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.
3
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 $1000
ADDA $1001
DAA
STAA $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 Assembly Code
A = X
B = X
B = LSN (X)
A = X >> 1
A = X >> 2
LDAA X
TAB
ANDB #$0F
LSRA
LSRA
A = X >> 3 LSRA
A = X >> 4 = MSN(X) LSRA
6
Example 4.2.3
, we want convert a BCD number to its equivalent binary number:
68
10
= (0110 1000)
BCD
= 0110 * 10
The assembly program is as followed:
ORG $0000
1 + 1000 * 10 0 bcdval DS.B
1 binval DS.B
1
; BCD value
; binary equivalent number.
ORG $C000
LDAA bcdval ; A <- BCD value
TAB ; B <- A
ANDA #$0F ; A <- LS bits of BCD number
LSRB
LSRB
LSRB
LSRB
LSLB
; B = B >> 1
; B = B >> 2
; B = B >> 3
; B = B >> 4 = MS bits of BCD number
; B <- B * 2
ABA
LSLB
LSLB
ABA
; A <- A + B = A + 2 * B
; B <- B * 4
; B <- B * 8
; 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:
68
10
= (0110 1000)
Alternative solution:
BCD
= 0110 * 10 1 + 1000 * 10 0
ORG $0000 bcdval DS.B
1 ; BCD value binval DS.B
1
ORG $C000
; binary equivalent number.
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
LSRB
LSRB
ABA
; A <- A + B = A + 8 * B
; B = B >> 2 = 16 * B / 4 = 4 * B
; B = B >> 3 = 16 * B / 8 = 2 * B
; 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 = 75; j = 10; k = i + j – 6
}
;
; i, j, k are integer variables
; assign 75 to i
; assign 10 to j
9
(1) * declaration
(2)
(3) i
(4) j
(5) k
ORG
DS.B
DS.B
DS.B
$0000
1
1
1
; variable i
; variable j
; variable k
(7) start: ORG $C000
(8) LDAA #75
(9)
(10)
STAA
LDAA i
#10
(11)
(12)
(13)
(14)
(15)
STAA
ADDA
SUBA
STAA
END j i
#6 k
; starting address of program
; initialize i to 75
; initialize j to 10
; compute i + j
; compute i + j -6
; store i + j - 6 to k
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.
3.
4.
Assembly Language Instructions
END directive
last statement of a program
any statement after END will be ignored
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
2.
Operation field
It contains the mnemonic of a machine instruction or a directive
It is separated from the label by at least one space
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:
EQU 1 ; 1 is the operand field
LDAB 0, X ; 0, X is the operand field
BNE NEQ ; NEQ is the operand field
4.
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
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
LDAA #2 loop` DECA
BNE loop` same as
LOC loop` BRCLR 0,x,$55,loop`
LOC
LDAA #2 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 arg1,arg2,arg3
LDAA arg1
ADDA arg2
ADDA arg3
ENDM
- Invoke a defined macro: write down the name and the arguments of the macro sumOf3 $1000,$1001,$1002 is replaced by
LDAA $1000
ADDA $1001
ADDA $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 $12EF
V2 DC.W $AFDB
V3 DC.B '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
V4 FILL $AF
,
40
V5 DS.B
2
V6 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.
S1 234000790011C6395B1086095A12CF400010EF180BFF
025A791000B61000180E585858A4
“ S1 ” indicates that memory address will be 16 bits.
33
2.
3.
4.
Length of Command Line The digits in the third and fourth positions signify the number of digit pairs on the line.
Address of Memory Instructions The next four digits are the address of the first instruction.
Check-Sum The last two digits on the command line represent the checksum of the line
S1 23 4000 790011C6395B1086095A12CF400010EF180BFF025A7910
00B61000180E585858 A4
“ 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