68HC11 Assembly Programming
68HC11 Assembly Programming
68HC11 Assembly Programming
Han-Way Huang
C program
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;
}
Assembly Program
1. Assembler Directives
3. END directive
4. Comments
1. Label field
- is optional
- starts with a letter and followed by letters, digits, or special symbols (_ or .)
- can start from any column if ended with “:” (not true for Motorola freeware as11)
- must start from column 1 if not ended with “:”
2. Operation field
- contains the mnemonic of a machine instruction or a directive
- is separated from the label by at least one space
3. Operand field
- follows the operation field and is separated from the operation field
by at least one space
- contains operands for instructions or arguments for assembler directives
4. Comment field
- a whole line comment starts with a *
- is separated from the operand and operation field for at least one space
Example 2.3
2. ORG
- sets a new value for the location counter of the assembler
- tells the assembler where to put the next byte it generates after the ORG directive
The sequence
ORG $C000
LDAB #$FF
will put the opcode byte for the instruction LDAB #$FF at location $C000.
- causes the assembler to allocate a block of bytes that are initialized to zeros
- syntax is
[<label>] BSZ <expression> [<comment>]
The statement
buffer BSZ 80
The statement
reserves three consecutive memory bytes and initializes their values to $11, $22, and $33.
The directive
will initialize 6 consecutive bytes in memory to $00 $11 $00 $22 $00 $33
The directive
The directive
The directive
will force the freeware assembler to fill each of the 40 memory locations with a 1.
EQU -- equate
- allows the user to use a symbolic name in place of a number
- syntax is
<label> EQU <expression> [<comment>]
The directive
tells the assembler that wherever ROM appears in the program, the value $E000
is to be substituted.
Flowchart
- is a form of program documentation
- is a tool for developing program logic flow
Symbols of Flowchart
Terminal A
Process
Subroutine
Input or
output
B
start
Compile or assemble to
generate machine code
No Is the result
satisfactory?
Yes
Stop
Example 2.4 Write a program to add the values of memory locations at $00, $01, and $02, and
save the result at $03.
Example 2.5 Write a program to subtract 6 from three 8-bit numbers stored at $00, $01, and
$02 respectively.
Example 2.6 Write a program to add the 3-byte numbers stored at $00-$02 and
$03-$05 and save the result at $06-$08.
Solution: The addition starts from the least significant byte.
Example 2.7 Write a program to subtract the 3-byte number stored at $03-$05
from the 3-byte number stored at $00-$02 and save the result at $10-$12.
Solution: The subtraction starts from the LSBs.
LDAA $00
ADDA $01
DAA
STAA $02
adds the BCD numbers stored at $00 and $01 and saves the sum at $02.
Multiplication
Example 2.10 Multiply the two 16-bit numbers stored at M and N and save the
product at location P.
Divide Instructions
1. IDIV: integer division
The 68HC11 provides swap instructions so that further division to the quotient
can be performed.
Example 2.13 Write a program to convert the 16-bit number stored at $00-$01 to BCD
format and store the result at $02-$06. Each BCD digit is stored in one byte.
Solution:
- A binary number can be converted to BCD format by using repeated division by 10.
- The largest 16-bit binary number is 65535 which has five decimal digits.
- The first division by 10 obtains the least significant digit, the second division by 10
obtains the second least significant digit, and so on.
Program Loops
Looping mechanisms:
1. DO statement S forever
3. WHILE C DO statement S
Program loops are implemented by using the conditional branch instructions and
the execution of these instructions depends on the contents of the CCR register.
S X H I N Z V C
- C: carry flag
- V: overflow flag
- Z: zero flag
- N: negative flag
- H: half carry flag
Conditional Branch Instructions that check more than one condition flag
- BGE branch if (N ⊕ V) = 0
- BGT branch if (Z + (N ⊕ V)) = 0
- BHI branch if (C + Z) = 0
- BLE branch if (Z + (N ⊕ V)) = 1
- BLS branch if (C + Z) = 1
- BLT branch if (N ⊕ V) = 1
- DECA: A ← [A] - 1
- DECB: B ← [B] - 1
- DEC opr: mem[opr] ← [mem[opr]] - 1
- DES: SP ← [SP] - 1
- DEX: X ← [X] - 1
- DEY: Y ← [Y] - 1
- INCA: A ← [A] + 1
− INCB: B ← [B] + 1
- INC opr: mem[opr] ← [mem[opr]] + 1
- INS: SP ← [SP] + 1
- INX: X ← [X] + 1
- INY: Y ← [Y] + 1
Note 1. Incrementing and decrementing instructions can be used to update the loop
indices.
Note 2. The memory operand opr is specified in either extended or index addressing mode.
Example 2.15 Write a program to compute 1 + 2 + ... + 20 and save the sum at $00.
Solution:
Example 2.16 Write a program to find the largest number from an array of 20
8-bit numbers. The array is stored at $00-$13. Save the result at $20.
Solution:
Start
yes
array max < array [i] ? array max ← array [i]
i←i+1
no
no
i = array count - 1?
yes
Stop
* The following program uses A to hold the temporary array max and uses B
* as the loop index.
Compare Instructions
Instruction format
[<label>] CBA [<comment>] compare A to B
[<label>] CMPA opr [<comment>] compare A to a memory location or value
[<label>] CMPB opr [<comment>] compare B to a memory location or value
[<label>] CPD opr [<comment>] compare D to a memory location or value
[<label>] CPX opr [<comment>] compare X to a memory location or value
[<label>] CPY opr [<comment>] compare Y to a memory location or value
[<label>] TST opr [<comment>] test a memory location for negative or zero
[<label>] TSTA [<comment>] test A for negative or zero
[<label>] TSTB [<comment>] test B for negative or zero
where
opr specifies the memory location to be checked and must be specified using either
the direct or index addressing mode.
msk is an 8-bit mask that specifies the bits of the memory location to be checked.
The bits of the memory byte to be checked correspond to those bit positions
that are 1s in the mask.
rel is the branch offset and is specified in the relative mode.
ldx #$1000
here brclr $30,X %10000000 here
ldaa $31,X
will force the 68HC11 continue to execute the second instruction until the bit 7 is set to 1.
Example 2.17 Write a program to compute the sum of the odd numbers in an array
with 20 8-bit elements. The array is stored at $00-$13. Save the sum at
$20-$21.
Solution:
Start
sum ← 0
ptr ← 0
bit 0 of mem[ptr] = 0?
no
sum ← sum + [mem[ptr]]
no
ptr ← ptr + 1 ptr = $13?
yes
Stop
N equ $13
org $20
sum rmb 2
org $C000
ldaa #$00
staa sum ; initialize sum to 0
staa sum+1 ; “
ldx #$00 ; point X to array[0]
loop brclr 0,X $01 chkend ; is it an odd number?
ldd sum ; add the odd number to the sum
addb 0,X ; “
adca #0 ; “
std sum ; “
chkend cpx #N ; compare the pointer to the address of the last element
bhs exit ; is this the end?
inx
bra loop ; not yet done, continue
exit end
where opr is specified using the extended or index addressing mode. The
specified memory location is cleared.
Accumulator A is cleared to 0
Accumulator B is cleared to 0
The 68HC11 has shift and rotate instructions that apply to a memory location, accumulators
A, B and D. A memory operand must be specified using the extended or index
addressing mode.
[<label>] ASL opr [<comment>] -- memory location opr is shifted left one place
[<label>] ASLA [<comment>] -- accumulator A is shifted left one place
[<label>] ASLB [<comment>] -- accumulator B is shifted left one place
The operation is
C b7 ----------------- b0 0
The operation is
C b7 ----------------- b0 b7 ----------------- b0 0
accumulator A accumulator B
The 68HC11 has arithmetic shift right instructions that apply to a memory location and
accumulators A and B.
[<label>] ASR opr [<comment>] -- memory location opr is shifted right one place
[<label>] ASRA [<comment>] -- accumulator A is shifted right one place
[<label>] ASRB [<comment>] -- accumulator B is shifted right one place
The operation is
b7 ----------------- b0 C
The 68HC11 has logical shift left instructions that apply to a memory location and
accumulators A and B.
[<label>] LSL opr [<comment>] -- memory location opr is shifted left one place
[<label>] LSLA [<comment>] -- accumulator A is shifted left one place
[<label>] LSLB [<comment>] -- accumulator B is shifted left one place
The operation is
C b7 ----------------- b0 0
The operation is
C b7 ----------------- b0 b7 ----------------- b0 0
accumulator A accumulator B
The 68HC11 has three logical shift right instructions that apply to 8-bit operands.
[<label>] LSR opr [<comment>] -- memory location opr is shifted right one place
[<label>] LSRA [<comment>] -- accumulator A is shifted right one place
[<label>] LSRB [<comment>] -- accumulator B is shifted right one place
The operation is
0 b7 ----------------- b0 C
The operation is
0 b7 ----------------- b0 b7 ----------------- b0 C
accumulator A accumulator B
The 68HC11 has three rotate left instructions that operate on 9-bit operands.
[<label>] ROL opr [<comment>] -- memory location opr is rotated left one place
[<label>] ROLA [<comment>] -- accumulator A is rotated left one place
[<label>] ROLB [<comment>] -- accumulator B is rotated left one place
The operation is
b7 ----------------- b0 C
The 68HC11 has three rotate right instructions that operate on 9-bit operands.
[<label>] ROR opr [<comment>] -- memory location opr is rotated right one place
[<label>] RORA [<comment>] -- accumulator A is rotated right one place
[<label>] RORB [<comment>] -- accumulator B is rotated right one place
The operation is
C b7 ----------------- b0
Example 2.18 Suppose that [A] = $74 and C = 1. Compute the new values of A and C
after the execution of the instruction ASLA.
Solution:
The operation is
0 1 1 1 0 1 0 0 0
0 1 1 1 0 1 0 0 0
C A
Result: [A] = %11101000 C = 0
Example 2.19 Suppose that [mem[$00]] = $F6 and C = 1. Compute the new values of
mem[$00] and the C flag after the execution of the instruction ASR $00.
Solution:
The operation is
1 1 1 1 0 1 1 0
1 1 1 1 1 0 1 1 0
mem[$00] C
Result: [mem[$00]] = %11111011 C = 0
Example 2.20 Suppose that [mem[$00]] = $F6 and C = 1. Compute the new contents of
mem[$00] and the C flag after the execution of the instruction LSR $00.
Solution:
The operation is
0 1 1 1 1 0 1 1 0
0 1 1 1 1 0 1 1 0
mem[$00] C
Result: [mem[$00]] = % 01111011 C = 0
Example 2.21 Suppose that [B] = $BE and C = 1. Compute the new values of
B after the execution of the instruction ROLB.
Solution:
The operation is
1 0 1 1 1 1 1 0 1
0 1 1 1 1 1 0 1 1
B C
Result: [B] = % 01111101 C = 1
Example 2.22 Suppose that [B] = $BE and C = 1. Compute the new values of
mem[$00] after the execution of the instruction RORB.
Solution:
The operation is
1 1 0 1 1 1 1 1 0
0 1 1 0 1 1 1 1 1
C B
Example 2.23 Write a program to count the number of 1s in the 16-bit number stored
at $00-$01 and save the result in $02.
Solution:
* The 16-bit number is shifted to the right up to 16 time or until the shifted value becomes 0.
* If the bit shifted out is a 1 then increment the 1s count by 1.
org $C000
ldaa #$00 ; initialize the 1s count to 0
staa $02 ; “
ldd $00 ; place the number in D
loop lsrd ; shift the lsb of D to the C flag
bcc testzero ; is the C flag a 0?
inc $02 ; increment 1s count if the lsb is a 1
testzero cpd #0 ; check to see if D is already 0
bne loop
end
1. The bit 7 of each byte will receive the bit 0 of its immediate left byte with the
exception of the most significant byte which will receive a 0.
2. Each byte will be shifted to the right by 1 bit. The bit 0 of the least significant byte
will be lost.
1. The bit 0 of each byte will receive the bit 7 of its immediate right byte with the
exception of the least significant byte which will receive a 0.
2. Each byte will be shifted to the left by 1 bit. The bit 7 of the most significant byte
will be lost.
Example 2.24 Write a program to shift the 32-bit number stored at $20-$23 to the
right four places.
Solution:
An easy way to create a delay is to use program loops. Use the instructions in Table 2.3 as
an example.
Table 2.3 Execution times of a sample of instructions
Instruction Execution time (E clock cycles)
BNE <rel> 3
DECB 2
DEX 3
LDAB <imme> 2
LDX <imme> 3
NOP 2
The following instruction sequence takes 5 µs to execute for 2 MHz E clock signal.
ldab #100
outer ldx #20000
inner nop
nop
dex
bne inner
decb
bne outer