我需要增加一个数字,以便代码永远增加,但它保持为零。
这是我的代码:
section .data
FORMAT: db '%c', 0
FORMATDBG: db '%d', 10, 0
EQUAL: db "is equal", 10, 0
repeat:
push ecx ; just to print
push FORMATDBG ; just to print
call printf ; just to print
add esp, 8 ; add the spaces
inc ecx ; increment ecx
cmp ecx, 0 ; compare ecx to zero
ja repeat ; if not equal to zero loop again
最佳答案
repeat:
xor ecx, ecx
push ecx ; just to print
push FORMATDBG ; just to print
call printf ; just to print
add esp, 8 ; add the spaces
inc ecx ; increment ecx
cmp ecx, 0 ; compare ecx to zero
ja repeat ; if not equal to zero loop again
xor ecx, ecx
将 ecx
设置为零。我不确定你是否知道这一点。您可能不希望每次迭代都发生这种情况。此外,您的循环条件 ja Repeat
当前仅在 ecx > 0
时才会导致循环,这可能不是您想要的(或者是吗?)。
最后一件事,printf
可能会破坏ecx
(我假设cdecl
或stdcall
)。阅读调用约定(不确定您使用的编译器/操作系统)并查看哪些寄存器保证在函数调用中保留。
就您的代码而言,您可能想要更接近此的代码:
xor ebx, ebx
repeat:
push ebx ; just to print
push FORMATDBG ; just to print
call printf ; just to print
add esp, 8 ; add the spaces
inc ebx ; increment ecx
cmp ebx, 0 ; compare ecx to zero
ja repeat ; if not equal to zero loop again
但这不会导致无限循环。当 ebx 达到最大值时,其值将回绕到 0,这将导致循环条件 (ebx>0
) 计算为 false 并退出循环.
关于assembly - 汇编语言中的 ecx 递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10423465/