在x86 / x86_64中,如果不是零,我怎么跳#34;不影响进旗?

时间:2015-02-17 16:40:44

标签: loops assembly x86 compare x86-64

我有这个循环基本上添加了两个动态宽度整数(实际上,它有点展开,但这并不重要)。注册RCX包含目标地址,RDX包含源地址,R8包含数组的长度。

    clc                    # Clear CF flag before beginning
.Lloop0:
    movq    (%rdx), %rax   # Load memory for next addition
    adcq    %rax, (%rcx)   # Perform addition with carry (i.e. using CF flag)
    leaq    8(%rcx), %rcx  # Increment destination address (without affecting CF)
    leaq    8(%rdx), %rdx  # Increment source address (without affecting CF)
    leaq    -1(%r8), %r8   # Decrement length (without affecting CF)
    testq   %r8, %r8       # Test if length is zero (affects CF!)
    jne     Lloop0

问题是TEST指令会清除CF标志,这是下一个ADC所需要的。 CMP指令也会产生类似的效果。

我当然可以在测试之前使用FLAGS复制LAHF寄存器,并在循环开始时使用SAHF恢复它,但是如果有的话我想避免一种解决方法。

1 个答案:

答案 0 :(得分:2)

您可以将上面%r8%rcx的角色交换为:

    clc
    .p2align 4 # just a thought...
.Lloop0:
    jrcxz .Lloop0_end
    ...
    leaq  -1(%rcx), %rcx
    jmp   .Lloop0
.Lloop0_end: