Professional Documents
Culture Documents
Programming Examples
Example 1: (simple addition) Write a program which adds the contents of memory locations $0040
and $0041 and stores the sum to memory location $0042. Start the program from location
$0020.
machine
location code ($) mnemonic comments
Example 2: (nibble seperation) Write a program which takes the contents of location $40 and stores
the four least significant bits (LSBs) in location $41, i.e., separate the low order nibble. Start
the program from location $0010.
machine
location code ($) mnemonic comment
$0010 96 LDAA $40 get the operand
$0011 40
$0012 84 ANDA #$0F mask off the most significant nibble
$0013 0F
$0014 97 STAA $41 store the result
$0015 41
$0016 7E JMP $0016 loop forever
$0017 00
$0018 16
Example 3: (disassembly of a word) Write a program which places the four LSBs of location $0040 in
location $0041 and four MSBs of location $0040 in location $0042 as the four LSBs of the
corresponding destination locations. Start the program from $0060.
machine
location m. code ($) mnemonic comment cycle
0060 96 LDAA $40 Get data 3
0061 40
0062 84 ANDA #$0F Mask off 4 MSBs 2
0063 0F
22
0064 97 STAA $41 Store LSBs 3
0065 41
0066 96 LDAA $40 Get data 3
0067 40
0068 44 LSRA Convert MSBs 2
0069 44 LSRA to LSBs 2
006A 44 LSRA 2
006B 44 LSRA 2
006C 97 STAA $42 Store MSBs 3
006D 42
006E 7E JMP $006E Loop forever
006F 00
0070 6E
Total number of cycles: 22. With 2 MHz clock the execution time of the program: 11 µsec.
Example 4: (addition of an array of an numbers) Memory location $41 contains the length (≠0) of a
set of numbers. The set starts at memory location $42. Write a program that stores the sum
(less than 256) of numbers in $40. Start the program at $0060.
START
COUNT=($41)
SUM=0
POINTER=$42
SUM=SUM+(POINTER)
POINTER=POINTER+1
COUNT=COUNT-1
No IS
COUNT
0?
Yes
($40)=SUM
STOP
23
$006A BNE SUMD if, not and of array (Z=0), go on
adding numbers
$006C STAA $40 store SUM
$006E FOREVER JMP FOREVER
Hence the machine codes at memory locations $006A and $006B should be $26 and $FA.
Example 5: (data transfer) The length of the data array is in memory location $40, the data originally
starts in memory location $41, and the destination area for the data starts in memory location
$51. Write a program to transfer the data. Start the program at $0020.
Example 6: (condition code register) Assume ACCA contains the following data before HC11
executes the NEGA instruction. What is the result in ACCA and the N, Z, V, and C bits for
the negation of each byte?
Before After
ACCA ACCA NZVC Comment
$00 $00 0100 Negating 0 gives us 0
$7F $81 1001 Negating +127 gives -127
$01 $FF 1001 Negating +1 gives -1
$FF $01 0001 Negating -1 gives +1
$80 $80 1011 Negating -128 gives overflow
Note that NEGA means ($00-A) at the same time. That is why C (interpreted as borrow in this case) is
set in all negations except $00.
Example 7: (maximum value) Length (≠0) of a memory array, which contains unsigned numbers, is in
$41 and the array starts at $42. Write a program that places the maximum value in the array in
$40. Start the program at $0100.
24
START
MAX=($42)
PTR=43
COUNT=($41)-1
IS
MAX No
MAX = (PTR)
≥ (PTR)
?
Yes
PTR=PTR+1
COUNT=COUNT-1
No IS
COUNT
0?
Yes
($40)=MAX
STOP
CMPA: compare (A) and (M) means, perform (A)-(M) and change the CCR accordingly
→ Z = 1 if (A) and (M) are equal
C = 1 if (M) ≥ (A)
Assume the initial values as ($41) = $03; ($42) = $37; ($43) = $F2; ($44) = $C6, a trace example for
the above program is as follows:
I
25
instruction B A IX C Z ($40)
1 LDAB $41 03 x x x 0 x
2 DECB 02 x x x 0 x
3 LDAA $42 02 37 x x 0 x
4 LDX #$0043 02 37 0043 x 0 x
5 CMPA $00,X 02 37 0043 1 0 x
6 BCC NOCHG 02 37 0043 1 0 x no branch
7 LDAA $00,X 02 F2 0043 1 0 x
8 INX 02 F2 0044 1 0 x
9 DECB 01 F2 0044 1 0 x
10 BNE CONT 01 F2 0044 1 0 x branch
11 CMPA $00,X 01 F2 0044 0 0 x
12 BCC NOCHG 01 F2 0044 0 0 x branch
13 INX 01 F2 0045 0 0 x
14 DECB 00 F2 0045 0 1 x
15 BNE CONT 00 F2 0045 0 1 x no branch
16 STAA $40 00 F2 0045 0 1 F2
17 BRA STOP .......
18 BRA STOP .......
Example 8: (multiple precision arithmetic) Two n-byte numbers will be added. The number of bytes
in the numbers is given in location $0040. First number starts (LSByte first) at $0041 and the
second number at $0051. The sum is to replace the first number.
Ex: n1 = 50 A4 29
n2 = 28 37 FB
+
START
sum = 78 DC 24
No
IS
COUNT
0?
Yes
STOP
26
location labelmnemonic comment
$0100 CLC clear carry
$0101 LDAB $40 initialize the counter
$0103 LDY #$0041 initialize the pointer
$0107 LOOP LDAA $00,Y get 8 bits of number 1
$010A ADCA $10,Y add the corresponding bits of number 2
$010D STAA $00,Y store the result
$0110 INY update the pointer
$0112 DECB update the counter
$0113 BNE LOOP
$0115 STOP BRA STOP
Please note that the same program can be written using IX consuming less storage space in the
program memory.
Decimal arithmetic
After ADCA $10,Y insert DAA instruction and correct the BNE address accordingly.
Example 9: (square from a lookup table) Write a program to find the square of a 3-bit binary number
from a look up table. The table starts at SQTAB, location $40 contains the number whose
square is required and the result will be saved in location $41.
Example 10: (character manipulation) Determine the length of a string of ASCII characters starting at
$41 and ending with period “.”. Store the length of the string to location $40. ASCII
equivalent of “.” is $2E. Start the program at $20.
27
e.g.
0041 54 T
0042 4F O
0043 4F O
0044 20
0045 4C L
0046 41 A
0047 54 T
0048 45 E
0049 2E .
Example 11: (pattern comparison) Two ASCII strings start in memory locations $42 and $52,
respectively. Memory locations $41 contains the length (≠0) of the strings. Write a program
that compares these two strings. If they are equal, the program will place zero to location $40,
otherwise $FF will be placed to $40.
28