You are on page 1of 7

Chapter 3

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

0020 96 LDAA $40 Get operand 1


0021 40
0022 9B ADDA $41 Add operand 2
0023 41
0024 97 STAA $42 Store result
0025 42
0026 7E JMP $0026 Loop forever
0027 00
0028 26

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

Accumulator B can also be used alternatively.


machine
location code ($) mnemonic
$0010 D6 40 LDAB $40 # : immediate addressing
$0012 C4 0F ANDB #%00001111 $ : hexadecimal
$0014 D7 41 STAB $41 %: binary
$0016 7E 00 16 JMP $0016

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

location label mnemonic comment


$0060 CLRA SUM=0
$0061 LDAB $41 COUNT=Length of array
acc. B will be used as counter
$0063 LDX #$0042 IX will be used as pointer
$0066 SUMD ADDA $00,X SUM=SUM+element from the array
$0068 INX update the pointer
$0069 DECB update the counter

23
$006A BNE SUMD if, not and of array (Z=0), go on
adding numbers
$006C STAA $40 store SUM
$006E FOREVER JMP FOREVER

Calculation of offset for label SUMD:

Target address = $66 = current instruction + 2 + relative address (offset)


= $6A+2+offset
offset = $66-$6A-2 = -6

In 2’s complement form, offset = $FA

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.

location label mnemonic


0020 LDAA $40
0022 LDX #$0041
0025 LOOP LDAB $00,X
0027 STAB $10,X
0029 INX
002A DECA
002B BNE LOOP
002D STOP JMP STOP

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?

ACCA: $00, $7F, $01, $FF, $80

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

location label mnemonic comment


$0100 LDAB $41
$0102 DECB initialize the counter
$0103 LDAA $42 set MAX to first number
$0105 LDX #$0043 initialize the pointer
$0108 CONT CMPA $00,X compare acc.A contents with the value
pointed by IX (i.e. (A)-(M) is performed)
$010A BCC NOCHG if AccA is greater go on with NOCHG
$010C LDAA $00,X otherwise update the MAX value
$010E NOCHG INX update the pinterter
$010F DECB update the counter
$0110 BNE CONT if it is not end of array CONTinue
$0112 STAA $40 otherwise save the maximum value
$0114 STOP BRA 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)

Relative address calculations:


$10E = $10A + 2 + offset for label NOCHG ⇒ offset = $02
$108 = $110 + 2 + offset for label CONT ⇒ offset = -10 = $F6 (in 2’s complement)
$114 = $114 + 2 + offset for label STOP ⇒ offset = -2 = $FE (in 2’s complement)

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

Before After C=0


0040 03 COUNT = ($40)
0041 29 24 PTR1 = 41
0042 A4 DC
0043 50 78
... ...
0051 FB (PTR1)=(PTR1)+(PTR1+$10)+C
0052 37
0053 28
PTR1 = PTR1 + 1
COUNT = COUNT - 1

No
IS
COUNT
0?
Yes

STOP

• Use a counter (ACCB) to control the number of bytes


• Use ACCA to hold the current sum.
• Use IY as the pointer

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

For the relative address ‘LOOP’:


0106 = 010E + 2 + offset => offset = F6

Please note that the same program can be written using IX consuming less storage space in the
program memory.

Decimal arithmetic

Ex: $0006+$0007 = $000D  (BCD correction)  13

After ADCA $10,Y insert DAA instruction and correct the BNE address accordingly.

DAA: Decimal Adjust ACCA, is used after the instructions


ABA (add ACCB to ACCA)
ADDA (add (M) to ACCA)
ADCA
to correct ACCA contents for BCD.

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.

Assume that SQTAB contains 0, 1, 4, 9, 16, 25, 36, 49

label mnemonic comment


LDX #SQTAB load the base address of the table
LDAA $40 get the input data
BEQ FOUND if it is zero, stop searching and goto FOUND
CONT INX otherwise; point to the next table entry
DECA decrement A
BNE CONT and repeat for ($40) times
FOUND LDAA $00,X get the corresponding table entry
STAA $41 store the result
STOP BRA STOP

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 .

location label mnemonic comment


$0020 CLRB initialize the counter
$0021 LDAA #$2E hold the trailer value
$0023 LDX #$0041 initialize the pointer
$0026 NPER CMPA $00,X compare data with the trailer
$0028 BEQ DONE stop if period is found (if Z=1)
$002A INCB otherwise update the counter
$002B INX update the pointer
$002C BRA NPER go on with checking
$002E DONE STAB $40 save the string length
$0030 STOP BRA STOP

Relative address calculations:


$2E = $28 + 2 + offset for label DONE ⇒ offset = $04
$26 = $2C + 2 + offset for label NPER ⇒ offset = -8 = $F8 (in 2’s complement)

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.

label mnemonic comment


LDAA #$FF initially the strings are assumed not to be
STAA $40 equal to each other
LDAB $41 initialize the counter
LDX #$0042 initialize the pointer
CONCHK LDAA $00,X get an element from string1
CMPA $10,X compare with the corresponding element
of string2
BNE DONE if they are not equal branch to stop
INX update the pointer
DECB update the counter
BNE CONCHK if not end of the strings go on checking
CLR $40 otherwise write the result, save the string length
DONE BRA DONE

28

You might also like