Questions tagged [carryflag]

A carry flag usually indicates whether an arithmetic instruction (e.g. addition or subtraction) has resulted in a carry or borrow. This flag can then be used as the basis for a conditional branch or other conditional execution.

On x86 processors, the carry flag is denoted CF and is the low bit of the FLAGS (or EFLAGS or rFLAGS) register.

This tag is for questions about how the carry flag operates, how to use it, and related topics.

112 questions
43
votes
2 answers

Efficient 128-bit addition using carry flag

I'm using a 128 bit integer counter in the very inner loops of my C++ code. (Irrelevant background: The actual application is evaluating finite difference equations on a regular grid, which involves repetitively incrementing large integers, and even…
Randall Meyers
  • 433
  • 1
  • 4
  • 5
37
votes
3 answers

gdb with assembler: Print status of carry flag

I've got an x86 assembler program which I'm debugging with gdb. Is there a way to print the status of the carry flag inside gdb with, like, "print $cf"?
Hinton
  • 2,320
  • 4
  • 26
  • 32
28
votes
2 answers

Overflow and Carry flags on Z80

I have gotten round to implementing the ADD A,r set of opcodes on my Z80 core. I had a bit of confusion about the carry and overflow flags which I think I've nailed, but I wanted to put it to the community to check that I'm right. Basically, from…
PhilPotter1987
  • 1,306
  • 2
  • 12
  • 20
22
votes
2 answers

Assembly - Carry flag VS overflow flag

I have the next code: mov al, -5 add al, 132 add al, 1 As I check it, the overflow flag and the carry flag will set in the first operation, and in the second, only the overflow will set. But I don't understand why: In unsigned number, the result…
Tom O
  • 237
  • 1
  • 4
  • 7
22
votes
8 answers

Assembly ADC (Add with carry) to C++

There is an x86 assembly instruction ADC. I've found this means "Add with carry". What does this mean/do? How would one implement the behavior of this instruction in C++? INFO: Compiled on Windows. I'm using a 32-bit Windows Installation. My…
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
19
votes
12 answers

What actually happens when a Byte overflows?

What actually happens when a Byte overflows? Say we have byte byte1 = 150; // 10010110 byte byte2 = 199; // 11000111 If we now do this addition byte byte3 = byte1 + byte2; I think we'll end up with byte3 = 94 but what actually happens? Did I…
ImJames
  • 881
  • 2
  • 9
  • 14
14
votes
2 answers

carry/overflow & subtraction in x86

I'm trying to wrap my head around overflow & carry flags in x86. As I understand it, for addition of signed 2's complement numbers, the flags can only be generated in one of four ways (my examples are 4-bit numbers): pos+pos = neg (overflow) 0111…
Robz
  • 1,747
  • 5
  • 21
  • 35
14
votes
5 answers

check if carry flag is set

Using inline assembler [gcc, intel, c], how to check if the carry flag is set after an operation?
hans
  • 143
  • 1
  • 1
  • 4
12
votes
2 answers

Could someone please explain what this inline #define assembly is doing?

I'm an occasional C programmer. I've come across this bit of inline assembly code in a Turbo C program #define ADC(dst,src) { asm MOV AX, dst; asm ADD AX, src; \ asm ADC AX, 0; asm MOV dst, AX; } dst and src are both unsigned…
Jim C
  • 165
  • 8
7
votes
3 answers

Confusion about ARM documentation on carry flag

In the ARM documentation here, it says that: A carry occurs: ... if the result of a subtraction is positive or zero ... I know from this answer on SO that the carry flag is set in subtraction when there is unsigned underflow (ie the number being…
takanuva15
  • 1,286
  • 1
  • 15
  • 27
6
votes
1 answer

ADC instruction in asm

In the following code, MOV AL,NUMBER1 ADD AL,NUMBER2 MOV AH, 00H ADC AH, 00H what are lines 3 and 4 for? What do they do? Also, why does the code clear AH? (I assume because AL's "ADD" operation may produce carry.)
parvin
  • 195
  • 2
  • 12
6
votes
2 answers

x86 sbb with same register as first and second operand

I am analyzing a sequence of x86 instructions, and become confused with the following code: 135328495: sbb edx, edx 135328497: neg edx 135328499: test edx, edx 135328503: jz 0x810f31c I understand that sbb equals to des = des - (src + CF), in other…
lllllllllllll
  • 8,519
  • 9
  • 45
  • 80
6
votes
1 answer

Are there ARM intrinsics for add-with-carry in C?

Do there exist intrinsics for ARM C compilers to do add-with-carry operations, or is it necessary to use assembly language? On x86, there is _addcarry_u64 for add-with-carry. (There's also the newer _addcarryx_u64 for special purposes.)
Myria
  • 3,372
  • 1
  • 24
  • 42
6
votes
4 answers

When are the carry flags set by x86 negation (NEG) / subtraction (SUB)?

What is meant by "Applying the NEG instruction to a nonzero operand always sets the Carry flag." Why does substracting 2 from 1 set the carry flag? 00000001 (1) + 11111110 (-2) [in 2-complement form] --------------------- CF:1 …
user2453180
  • 131
  • 1
  • 2
  • 7
5
votes
1 answer

Understanding the difference between overflow and carry flags

I am designing a 16 bit ALU in verilog based on an existing RISC ISA. The ISA says that the carry flag is set when the operation is unsigned, and overflow is set when the operation is signed. The interesting thing is that the ISA implements ADD and…
richbai90
  • 4,994
  • 4
  • 50
  • 85
1
2 3 4 5 6 7 8