不使用ADC来模拟adc eax,ebx?

问题描述 投票:-2回答:1

问题是:

adc eax, ebx指令将寄存器eax,寄存器ebx和进位标志的内容相加,然后将结果存储回eax。假设不允许使用adc指令,请写几条与adc eax, ebx产生完全相同行为的指令。

会是这样吗?

   add eax, ebx
   add eax, 1
assembly x86 masm carryflag
1个回答
4
投票

您需要做的是使用条件跳转来处理进位标志。您可能有几种方法可以解决此问题。这是我的方法:

    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是多余的。

© www.soinside.com 2019 - 2024. All rights reserved.