我对汇编语言相当陌生,我正在努力弄清楚这个程序。我只想知道我是否对这个节目有兴趣。我该如何纠正这个程序?
编写一个循环,计算字节数组中所有元素的总和。打印结果。一些提示:将数组的大小加载到适当的寄存器中。加载数组当前元素的偏移量,并在循环的每次迭代中相应地更改它。
到目前为止,我的情况如下:
INCLUDE Irvine32.inc
.data
val1 BYTE 1,2,3
counter = 0
.code
main PROC
mov ax, 0
mov ax, (LENGTHOF val1)
mov ax, OFFSET Counter
movzx ecx,ax
L1:
add eax, val1[ecx]
inc eax
loop L1
Call WriteDec
exit
END PROC
end main发布于 2016-05-06 12:16:34
您的代码中有几个错误:按照下面的顺序,您重复设置ax,这是非常无用的:
mov ax, 0 ; you set ax to 0
mov ax, (LENGTHOF val1) ; you set ax to 3
mov ax, OFFSET Counter ; you set ax to an address (and try to use a 16-bit register for a 32-bit address然后将此偏移量添加到
movzx ecx,ax
L1:
add eax, val1[ecx] ; add offset(val1)[offset(Counter)] to offset(Counter)毫无疑问,这将给您一个内存错误,因为地址可能在任何地方。然后,将此偏移量增加。
inc eax ; you probably confused this with a counter/index register然后,在ECX中使用这个偏移量,movzx ecx, ax将它作为LOOP指令中ECX中的一个索引。
loop L1 ; decrements ECX and loops if ECX != 0修复所有这些错误后,代码如下所示:
INCLUDE Irvine32.inc
.data
val1 BYTE 1,2,3
counter = 0
.code
main:
xor eax, eax ; 32-bit register for the sum
xor edx, edx ; 32-bit register for the counter/index
mov ecx, LENGTHOF val1 ; number of entries in fixed size array in ECX as maximum limit
L1:
movsx ebx, byte ptr val1[edx] ; extend BYTE at offset val1 + EDX to 32-bit
add eax, ebx ; add extended BYTE value to accumulator
inc edx ; increase index in EDX
loop L1 ; decreases ECX and jumps if not zero
Call WriteDec ; I assume this prints EAX
exit
end mainhttps://stackoverflow.com/questions/37071767
复制相似问题