使用加法和减法设置和清除进位标志 [英] Setting and Clearing the Carry Flag with Addition and Subtraction
问题描述
我在清除进位标志时遇到了困难,但我想出了一种使用减法来清除它的方法,但我想知道是否有人可以与我分享如何使用加法设置和清除进位标志的更好方法和减法.
I was having difficulty clearing the carry flag but I have come up with a way to clear it using subtraction but I was wondering if there was a better way someone could share with me on how to set and clear the carry flag using addition and subtraction.
.data
binNum1 BYTE 11111111b
binNum2 BYTE 00000001b
.code
main PROC
mov al, binNum1 ; AL = 0FFh
add al, binNum2 ; AL = 00h CF = 1
sub al, binNum2 ; AL = FFh CF = 1
sub al, binNum2 ; AL = FEh CF = 0
这是我必须设置和清除进位标志的内容.当我第一次从 AL 中减去 binNum2 时,我得到了 FFh 的原始值,但进位标志仍然设置,直到我再次从 AL 中减去 binNum2.然后将 AL 设置为 FEh 并清除进位标志.
This is what I have to set and clear the carry flag. When I first subtract binNum2 from AL I get my original value of FFh back but the Carry Flag is still set until I subtract binNum2 from AL again. It then sets AL to FEh and clears the Carry Flag.
有没有人有更好的方法来清除进位标志?
Does anyone have a better way of clearing the carry flag?
推荐答案
清除进位标志的最好方法是使用CLC
指令;设置进位标志的最好方法是使用STC
指令.
The best way to clear the carry flag is to use the CLC
instruction; and the best way to set the carry flag is to use the STC
instruction.
如果你因为一些奇怪的原因必须用加法或减法来做;最坏的方法(对于代码大小)可能是 sub eax,eax
清除进位标志,并且 xor eax,eax;sub eax,1
设置进位标志.
If you must do it with addition or subtraction for some bizarre reason; the least worst method (for code size) is likely sub eax,eax
to clear the carry flag, and xor eax,eax; sub eax,1
to set the carry flag.
注意:设置进位标志,stc;sbb eax,eax
会更糟糕,但该解决方案可能会使毫无意义的事情变得太明显.
Note: to set the carry flag, stc; sbb eax,eax
would be even less bad, but that solution probably makes the pointlessness too obvious.
这篇关于使用加法和减法设置和清除进位标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!