assembly - 汇编语言中的 ecx 递增

标签 assembly x86

我需要增加一个数字,以便代码永远增加,但它保持为零。

这是我的代码:

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, ecxecx 设置为零。我不确定你是否知道这一点。您可能不希望每次迭代都发生这种情况。此外,您的循环条件 ja Repeat 当前仅在 ecx > 0 时才会导致循环,这可能不是您想要的(或者是吗?)。

最后一件事,printf可能会破坏ecx(我假设cdeclstdcall)。阅读调用约定(不确定您使用的编译器/操作系统)并查看哪些寄存器保证在函数调用中保留。

就您的代码而言,您可能想要更接近此的代码:

    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/

相关文章:

c - 系统调用包装器 asm C

c++ - 有人可以帮助我了解 stmdb、ldmia 以及如何用 arm 汇编语言实现此 C++ 代码吗?

gcc - 设置执行原始二进制文件的起始地址

assembly - 微融合和寻址模式

assembly - 使用TEST检查AL的多个位是否全为零或全1

两个数字的 GCD 的汇编代码计算不起作用

c - 英特尔 FMA 指令提供零性能优势

从 c 调用汇编函数

c - C 编译器输出中的冗余移动

assembly - 如何使用 x86 汇编语言将两个 64 位数字相乘?