Hardware Reference
In-Depth Information
Example 2.14
Write a program to add an array of N 8-bit numbers and store the sum at memory locations
$1000,$1001. Use the “for i 5 n1 to n2 do” looping construct.
Solution: We will use variable i as the array index. This variable can also be used to keep track
of the current iteration being performed. We will use a 2-byte variable sum to hold the sum of
array elements. The logic flow of the program is illustrated in Figure 2.9. The program is a di-
rect translation of the fl owchart in Figure 2.9.
Start
i 0
sum
0
Yes
i = N?
Stop
No
sum sum + array[i]
i i + 1
Figure 2.9 Logic flow of example 2.14
N
equ
20
; array count
org
$1000
; starting address of on-chip SRAM
sum
rmb
2
; array sum
i
rmb
1
; array index
org
$1500
; starting address of the program
ldaa
#0
staa
i
; initialize loop (array) index to 0
staa
sum
; initialize sum to 0
staa
sum 1 1
; “
loop
ldab
i
cmpb #N
; is i 5 N?
beq
done
; if done, then branch
ldx
#array
; use index register X as a pointer to the array
abx
; compute the address of array[i]
ldab
0,x
; place array[i] in B
ldy
sum
; place sum in Y
aby
; compute sum <- sum 1 array[i]
sty
sum
; update sum
inc
i
; increment the loop count by 1
bra
loop
 
Search WWH ::




Custom Search