Loops 由于可能存在不正确的比较,无法在程序集中输入for循环
我试图在汇编中编写代码,以打印出正确的序列,直到达到输入的迭代。我无法进入for循环,我不确定为什么,但是,这可能是由于对Loops 由于可能存在不正确的比较,无法在程序集中输入for循环,loops,for-loop,assembly,x86,Loops,For Loop,Assembly,X86,我试图在汇编中编写代码,以打印出正确的序列,直到达到输入的迭代。我无法进入for循环,我不确定为什么,但是,这可能是由于对cmp,调用使用不当造成的-我认为这不是原因,但可能是原因 “我的数据”部分如下所示: .data stmt1 db "Nathanial Mendes", 0Ah, 0 prompt1 db "Enter maximum sequence count: ", 0 fmt db "%d", 0 sum db "Fib(%d) = %d",
cmp,
调用使用不当造成的-我认为这不是原因,但可能是原因
“我的数据”部分如下所示:
.data
stmt1 db "Nathanial Mendes", 0Ah, 0
prompt1 db "Enter maximum sequence count: ", 0
fmt db "%d", 0
sum db "Fib(%d) = %d", 0
seq dd 0
这就是我的代码部分的样子
.code
main PROC c
push offset stmt1
call printf
;Prompt for first int
push offset prompt1
call printf
add esp, 4
push offset seq
push offset fmt
call scanf
add esp, 8
;Set up loop vars
mov eax, 0 ; set i = 0
mov ebx, 0
mov ecx, 0
mov edx, 0
;#############################
beginloop:
cmp eax, seq ; comparing the counter (eax) to the max iteration (seq)
jle endloop ; if it is false jump to the end of the loop
;#######
;code for Fib here
;print statement (prints out the current Fib, looks like Fib(iteration) = Fib_sum
push eax
push ebx
push offset sum
call printf
add esp, 8
;next term calculations for the fib seq.
add ecx, edx
mov ebx, ecx
mov ecx, edx
mov edx, ebx
;#######
; set up next iteration of for-loop
add eax, 1 ; add one to the iterator
jmp beginloop
;#######
;when the for loop is false
endloop:
;Print (final) result
push eax
push ebx
push offset sum
call printf
add esp, 8
;##########################################
INVOKE ExitProcess,0
main endp
end
为什么我的for循环从不经过compare语句而直接进入endloop调用?
cmp eax,seq
/jle endloop
是如果(我认为它应该输入一次,但其余的代码是错误的。add esp,8
应该是12
,因为您传递了3个参数。另外printf
允许更改eax
,ecx
和edx
,因此您的所有局部变量都将被覆盖。