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 0021 0022 0023 0024 0025 0026 0027 0028 96 40 9B 41 97 42 7E 00 26 LDAA $40 ADDA $41 STAA $42 JMP $0026 Get operand 1 Add operand 2 Store result Loop forever

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 $0012 C4 0F ANDB #%00001111 $0014 D7 41 STAB $41 $0016 7E 00 16 JMP $0016

# : immediate addressing $ : hexadecimal %: binary

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 0065 0066 0067 0068 0069 006A 006B 006C 006D 006E 006F 0070

97 41 96 40 44 44 44 44 97 42 7E 00 6E

STAA $41 LDAA $40 LSRA LSRA LSRA LSRA STAA $42 JMP $006E

Store LSBs Get data Convert MSBs to LSBs Store MSBs Loop forever

3 3 2 2 2 2 3

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 $0060 $0061 $0063 $0066 $0068 $0069 SUMD

mnemonic CLRA LDAB $41

comment SUM=0 COUNT=Length of array acc. B will be used as counter LDX #$0042 IX will be used as pointer ADDA $00,X SUM=SUM+element from the array INX update the pointer DECB update the counter

23

$006A $006C $006E FOREVER

BNE SUMD STAA $40 JMP FOREVER

if, not and of array (Z=0), go on adding numbers store SUM

Calculation of offset for label SUMD: Target address = $66 = current instruction + 2 + relative address (offset) = $6A+2+offset offset = $66-$6A-2 = -6

In 2s 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 0020 0022 0025 0027 0029 002A 002B 002D mnemonic LDAA $40 LDX #$0041 LOOP LDAB $00,X STAB $10,X INX DECA BNE LOOP 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 ACCA $00 $7F $01 $FF $80 After ACCA $00 $81 $FF $01 $80

NZVC 0100 1001 1001 0001 1011

Comment Negating 0 gives us 0 Negating +127 gives -127 Negating +1 gives -1 Negating -1 gives +1 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 (PTR) ? Yes PTR=PTR+1 COUNT=COUNT-1

No

MAX = (PTR)

No

IS COUNT 0? Yes ($40)=MAX STOP

location $0100 $0102 $0103 $0105 $0108 $010A $010C $010E $010F $0110 $0112 $0114

label

CONT

mnemonic LDAB $41 DECB LDAA $42 LDX #$0043 CMPA $00,X BCC NOCHG LDAA $00,X INX DECB BNE CONT STAA $40 BRA STOP

comment initialize the counter set MAX to first number initialize the pointer compare acc.A contents with the value pointed by IX (i.e. (A)-(M) is performed) if AccA is greater go on with NOCHG otherwise update the MAX value update the pinterter update the counter if it is not end of array CONTinue otherwise save the maximum value

NOCHG

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 2s complement) $114 = $114 + 2 + offset for label STOP offset = -2 = $FE (in 2s 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

instruction LDAB $41 DECB LDAA $42 LDX #$0043 CMPA $00,X BCC NOCHG LDAA $00,X INX DECB BNE CONT CMPA $00,X BCC NOCHG INX DECB BNE CONT STAA $40 BRA STOP BRA STOP

B 03 02 02 02 02 02 02 02 01 01 01 01 01 00 00 00 ....... .......

A x x 37 37 37 37 F2 F2 F2 F2 F2 F2 F2 F2 F2 F2

IX x x x 0043 0043 0043 0043 0044 0044 0044 0044 0044 0045 0045 0045 0045

C x x x x 1 1 1 1 1 1 0 0 0 0 0 0

Z 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1

($40) x x x x x x x x x x x x x x x F2

no branch

branch branch no branch

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: + sum = Before 03 29 A4 50 ... FB 37 28 78 DC 24 After 24 DC 78 n1 n2 = = 50 A4 29 28 37 FB
START

0040 0041 0042 0043 ... 0051 0052 0053

C=0 COUNT = ($40) PTR1 = 41

(PTR1)=(PTR1)+(PTR1+$10)+C

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 $0100 $0101 $0103 $0107 $010A $010D $0110 $0112 $0113 $0115

mnemonic CLC LDAB $40 LDY #$0041 LOOP LDAA $00,Y ADCA $10,Y STAA $00,Y INY DECB BNE LOOP STOP BRA STOP

label

comment clear carry initialize the counter initialize the pointer get 8 bits of number 1 add the corresponding bits of number 2 store the result update the pointer update the counter

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

CONT

FOUND 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 0042 0043 0044 0045 0046 0047 0048 0049 location $0020 $0021 $0023 $0026 $0028 $002A $002B $002C $002E $0030 54 4F 4F 20 4C 41 54 45 2E label T O O L A T E . mnemonic CLRB LDAA #$2E LDX #$0041 CMPA $00,X BEQ DONE INCB INX BRA NPER STAB $40 BRA STOP comment initialize the counter hold the trailer value initialize the pointer compare data with the trailer stop if period is found (if Z=1) otherwise update the counter update the pointer go on with checking save the string length

NPER

DONE STOP

Relative address calculations: $2E = $28 + 2 + offset for label DONE offset = $04 $26 = $2C + 2 + offset for label NPER offset = -8 = $F8 (in 2s 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 LDAA #$FF STAA $40 LDAB $41 LDX #$0042 LDAA $00,X CMPA $10,X BNE DONE INX DECB BNE CONCHK CLR $40 BRA DONE comment initially the strings are assumed not to be equal to each other initialize the counter initialize the pointer get an element from string1 compare with the corresponding element of string2 if they are not equal branch to stop update the pointer update the counter if not end of the strings go on checking otherwise write the result, save the string length

CONCHK

DONE

28

You might also like