你在你的代碼的幾個錯誤:在下面的順序反覆設置的斧頭,這是相當無用:
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
作爲指數ECX在LOOP
指令
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 main
_「我該如何糾正這個問題ogram?「_它以什麼方式不按預期工作? – Michael