我有这个循环基本上添加了两个动态宽度整数(实际上,它有点展开,但这并不重要)。注册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
恢复它,但是如果有的话我想避免一种解决方法。
答案 0 :(得分:2)
您可以将上面%r8
和%rcx
的角色交换为:
clc
.p2align 4 # just a thought...
.Lloop0:
jrcxz .Lloop0_end
...
leaq -1(%rcx), %rcx
jmp .Lloop0
.Lloop0_end: