Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if carry flag is set

Using inline assembler [gcc, intel, c], how to check if the carry flag is set after an operation?

like image 849
hans Avatar asked Jun 29 '10 10:06

hans


People also ask

When carry flag will be set?

The carry (C) flag is set when an operation results in a carry, or when a subtraction results in no borrow.

How can check carry flag in 8086?

To conditionally branch on the status of the carry flag (CF), you would use JC or JNC . JC will branch if the carry flag is set (CF == 1), whereas JNC will branch if the carry flag is not set (CF == 0). The mnemonics for these opcodes are simply "Jump if Carry" and "Jump if Not Carry".

When carry flag is set or reset?

Carry Flag (CY) – Carry is generated when performing n bit operations and the result is more than n bits, then this flag becomes set i.e. 1, otherwise, it becomes reset i.e. 0.

What is the status of carry and auxiliary carry flag?

Auxiliary Carry Flag (AF) is one of the six status flags in the 8086 microprocessor. This flag is used in BCD (Binary-coded Decimal) operations. The status of this flag is updated for every arithmetic or logical operation performed by ALU.


Video Answer


2 Answers

sbb %eax,%eax will store -1 in eax if the carry flag is set, 0 if it is clear. There's no need to pre-clear eax to 0; subtracting eax from itself does that for you. This technique can be very powerful since you can use the result as a bitmask to modify the results of computations in place of using conditional jumps.

You should be aware that it is only valid to test the carry flag if it was set by arithmetic performed INSIDE the inline asm block. You can't test carry of a computation that was performed in C code because there are all sorts of ways the compiler could optimize/reorder things that would clobber the carry flag.

like image 110
R.. GitHub STOP HELPING ICE Avatar answered Oct 05 '22 04:10

R.. GitHub STOP HELPING ICE


With conditional jumps jc (jump if carry) or jnc (jump if not carry).

Or you can store the carry flag,

;; Intel syntax
mov eax, 0
adc eax, 0 ; add with carry
like image 20
Nick Dandoulakis Avatar answered Oct 05 '22 02:10

Nick Dandoulakis