Hardware Reference
In-Depth Information
sum
ds.w
1
org
$1500
ldx
#array
; place the starting address of array in X
movw
#0,sum
; initialize sum to 0
ldy
#N
; initialize loop count to N
loop
ldd
1,x 1
; place one number in D and move array pointer
addd
sum
; add to sum
std
sum
; update the sum
dbne
y,loop
; add all numbers to sum yet?
swi
; return to monitor
end
At the fi rst run of this program, sum ($A61F) is also incorrect.
+ md 1010
1010 22 24 26 28 - A6 1F B9 A9 - 2A CA FA DB - AC DA 18 97 “$&(. . . .*. . . . . . .
.
Again, the error can be found by tracing. This time the error will not be found until the
instruction loop ldd 1,x 1 is traced.
+ pc 1500
PP PC
SP
X
Y
D 5 A:B
CCR 5 SXHI
NZVC
38 1500
3C00
1014
0000
A6:1F
1001
1000
xx: 1500
CE1000
LDX
#$1000
+ t
PP PC
SP
X
Y
D 5 A:B
CCR 5 SXHI
NZVC
38 1503
3C00
1000
0000
A6:1F
1001
0000
xx: 1503
180300001014
MOVW
#$0000,$1014
+ t
PP PC
SP
X
Y
D 5 A:B
CCR 5 SXHI
NZVC
38 1509
3C00
1000
0000
A6:1F
1001
0000
xx: 1509
CD0014
LDY
#$0014
+ t
PP PC
SP
X
Y
D 5 A:B
CCR 5 SXHI
NZVC
38 150C
3C00
1000
0014
A6:1F
1001
0000
xx: 150C
EC30
LDD
1,X 1
+ t
PP PC
SP
X
Y
D 5 A:B
CCR 5 SXHI
NZVC
38 150E
3C00
1001
0014
02:04
1001
0000
xx: 150E
F31014
ADDD
$1014
.
We expect this instruction to place the first array element ($02) in D. Instead, it places
$0204 in D. This is obviously wrong! The error is due to the fact that the ldd instruction loads
a word (2 bytes) instead of 1 byte into D. At this point, we should be able to figure out the right
instruction to use and fix the problem.
I NAPPROPRIATE U SE OF I NDEX A DDRESSING M ODE
When the index addressing mode is used to step through an array, one needs to increment or
decrement the index register to reach the next element. Depending on the size of the element,
 
Search WWH ::




Custom Search