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
5
votes
1 answer

Determining carry and overflow flag in 6502 emulation in Java?

I'm making a 6502 emulator and I'm stuck (or I think I am at least) already at the beginning (implementing the ADC operation). The problem is that I have to determine if there's a carry or an overflow. The thing is, I can't really grasp the…
ZimZim
  • 3,291
  • 10
  • 49
  • 67
5
votes
2 answers

Carry flag in substraction

I am using MASM32. With this code: mov eax,5 sub eax,10 CF status flag will be set. But using my pencil and paper, I actually see that there is no any carry from MSB. Yes, I know that from subtraction from less number great number set CF. But I…
4
votes
1 answer

Would an Instruction Set Architecture benefit from both an ADC and SBC, or could all carry instructions repeat the previous type?

I'm creating an instruction set with only 16 instructions (4 bits opcode, 4 bits immediate), and as such am wondering if I could replace having both ADC (Add-with-carry) and SBC (Subtract-with-carry) with RWC (Repeat-with-carry). RWC would function…
4
votes
1 answer

Is there an x86 assembly instruction that does both 'xadd' and 'adc reg,0'?

I've recently learnt about XADD and I had no luck at looking up on google. Does an instruction like XADD that also adds the carry flag exist? (ex. XADC) Or am I forced to do it with two different instructions? I'm on Ubuntu and I'm using NASM in 64…
Luxo
  • 95
  • 1
  • 1
  • 3
4
votes
2 answers

I can't understand some instructions in ARM: SBC, RSC

I don't understand SBC and RSC ARM instructions I know that both deal with the carry flag (C) I think it makes sense adding the result with the carry (ADC) like: ADC r1, r2, r3 @ r1 = r2 + r3 + Carry But subtracting/reverse subtracting with…
Spacey
  • 69
  • 1
  • 4
4
votes
1 answer

Assembly ADC (add with carry)

mov eax, ptr_to_num1 ; little endian mov ebx, ptr_to_num2 ; little endian xor ecx, ecx xor edx, edx clc bytes_addition: mov dl, byte [eax+ecx] ; byte from shortest adc dl, byte [ebx+ecx] mov byte [eax+ecx], dl inc ecx cmp ecx, 4…
Andrew
  • 63
  • 1
  • 9
4
votes
3 answers

Subtract and detect underflow, most efficient way? (x86/64 with GCC)

I'm using GCC 4.8.1 to compile C code and I need to detect if underflow occurs in a subtraction on x86/64 architecture. Both are UNSIGNED. I know in assembly is very easy, but I'm wondering if I can do it in C code and have GCC optimize it in a way,…
kktsuri
  • 333
  • 2
  • 11
4
votes
2 answers

Does C hold the carry about bit from a << or a >> bit shift?

I read that C holds the carry-out from shifts and it can be found in processor-specific .h. Is this true and should I use it? or should work out the carry-out bit myself ?
cxzp
  • 652
  • 2
  • 14
  • 28
4
votes
3 answers

Reason to use the carry bit and the overflow bit

I am taking an Introduction to Embedded Systems Class. As I was reading, I encountered an interesting question on the implementations of the carry bit and overflow bit. I know what a carry bit and overflow bit is, however I cannot think of a…
chaitanya.varanasi
  • 960
  • 1
  • 9
  • 26
3
votes
2 answers

6502 Assembly question: Should I have an instance of SEC for each SBC when there are multiple SBC in a string of calculations?

According to this website, under the "major uses" for CLC, it states: If there are to be a series of additions (multiple-byte addition), only the first ADC is preceded by CLC since the carry feature is necessary. Under the "major uses" for SBC it…
3
votes
2 answers

How Intel 8085 actually performs a subtraction with previous borrow?

My understanding is that in order to evaluate X - Y - borrow, we can either perfrom X - (Y + borrow) or (X - Y) - borrow. Examples in the textbooks depicts the former approach. In that case, what will happen is the subtrahend (Y) is FFH and borrow…
codeR
  • 165
  • 1
  • 1
  • 13
3
votes
0 answers

Get a C compiler to use the carry flag result from an x86 shift?

Is it possible to capture the carry flag from an x86 SHL/SHR/SAR instruction in C, without inline assembly? I couldn't find any intrinsic for shifts, and it doesn't seem like compilers pick up patterns where it would be used. Suppose I want the…
zinga
  • 769
  • 7
  • 17
3
votes
2 answers

Using intel inline assembler to code bigint add with carry

I would like to do a fast code for adding 64 bit numbers in big ints: uint64_t ans[n]; uint64_t a[n], b[n]; // assume initialized values.... for (int i = 0; i < n; i++) ans[i] = a[i] + b[i]; but the above does not work with carry. I saw another…
Dov
  • 8,000
  • 8
  • 46
  • 75
3
votes
2 answers

Access the flags without inline assembly?

I have the following method in C that takes two 16-bit short ints and: Adds the two integers If the carry flag is set, add 1 to the result Negate (NOT) all the bits in the final results Return the result: short __declspec(naked) getchecksum(short…
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
3
votes
2 answers

What is a borrow in hexadecimal subtraction? (Assembly)

Quick question, I am reading the textbook entitled "Introduction to 80x86 Assembly Language and Computer Architecture" by Richard C. Detmer and on page 21 and 22 it talks about the concept of what a borrow is, but it doesnt really describe what it…
Code Doggo
  • 2,146
  • 6
  • 33
  • 58