哎呀!我有一个关于汇编语言的问题。 指令adc eax, ebx
添加寄存器eax
、寄存器ebx
和进位标志的内容,然后将结果存储回eax
。我的问题是,假设不允许adc
指令,那么我们如何编写产生与adc eax, ebx
完全相同的行为的指令。
我在下面写了一些代码,但可能不正确。
add eax, ebx
add eax, 1
您需要做的是使用条件跳转来处理携带标志。可能有几种方法可以做到这一点。这是我的方法:
push ebx ; We want to preserve ebx. This seems lazy but it's
; an easy approach.
jnc carry_handled ; If the carry is set:
add ebx, 1 ; Add 1 to summand. We are using
; ADD here, because INC does not
; set the carry flag.
;
jz carry_overflowed ; If it overflows (ebx is 0), then
; skip the addition. We don't want
; to add 0 which will clear the
; carry flag.
carry_handled: ;
add eax, ebx ; Add the adjusted summand.
carry_overflowed:
pop ebx
需要注意的重要一点是,您希望正确设置 CPU 标志,就像执行adc
一样。在上述方法中,如果您事后不关心进位标志,则jz carry_overflowed
是多余的。