我正在尝试用 Easy68K 编写一个程序来计算数组元素的总和,但它在 ( ) 处冻结。
也许我没有完全理解a0
、(a0)
和总和的工作原理。有人能帮帮我吗?
这是我的代码:
ORG $8000
array DC 1,2,3,4,5,6,7,8,9,10 ; DC reserves 4 bytes for each element
; $8000 -> 1, $8004 -> 2, etc.
len EQU 10 ; Number of elements in the array
sum DC 0 ; Variable to store the sum
START:
suba.l a0,a0 ; Clear register A0 (set to 0)
clr d1
clr d0
movea array,a0 ; Load the address of 'array' into A0
move len,d1
move sum,d0
LOOP:
TST d1 ; Check if D1 (counter) is zero
beq END ; If zero, exit the loop
move (a0),d5 ; Load the value at address A0 into D5
add d5,d0 ; Add D5 to D0 (accumulate sum)
addq #4,a0 ; Move to the next element in the array (4-byte step)
subq #1,d1 ; Decrease the loop counter by 1
bra LOOP
END:
SIMHALT
END START
不要从指令和说明中省略大小说明符!从屏幕截图可以清楚地看出,裸指令
DC
仅保留2 个字节的字,而不是您在整个程序中声明和使用的 4 个字节。如果您要全部分配它们,则无需清除它们。
不需要单独增加地址寄存器。68k 具有“后增”寻址模式,可以自动执行此操作:
该代码应该可以工作:
add.l (a0)+, d0
将把地址 A0 处的长字添加到 D0,然后将 A0 的内容提升操作的大小(长字表示 4)。bra
。始终尝试“有条件地”跳转到循环顶部。