Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What values can the carry flag hold, and how to check its status in x86 assembly?

Tags:

x86

assembly

nasm

  1. What values can the carry flag hold? Is it just 0x00 and 0x01 (boolean) or is it 16 (or 32/64) bits like the rest of the CPU registers?

  2. How do I check its status? Do I just use it like a normal CPU register like cmp cf, 0x00 then jg <jump destination>?

  3. I am writing a mini-OS. Is it good practice to use it for my own purposes, or should it be reserved for exclusive write-permissions for the CPU, and all I do is read from it?

like image 221
codesmith Avatar asked Jan 29 '13 02:01

codesmith


People also ask

How do you check the carrying flag?

1. The carry flag is set if the addition of two numbers causes a carry out of the most significant (leftmost) bits added. 2. The carry (borrow) flag is also set if the subtraction of two numbers requires a borrow into the most significant (leftmost) bits subtracted.

What is the carry flag in assembly?

In computer processors the carry flag (usually indicated as the C flag) is a single bit in a system status register/flag register used to indicate when an arithmetic carry or borrow has been generated out of the most significant arithmetic logic unit (ALU) bit position.

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".

What is status flag in assembly language?

The status flags reflect the outcomes of arithmetic and logical operations performed by the CPU. • The carry flag (CF) is set when the result of an unsigned arithmetic operation is too large to fit into the destination.


2 Answers

It's a flag, it can only hold true or false (technically 1 or 0, but effectively the truth values as shown).

In terms of using it, no, you don't compare it to something and then use jg. It's at the same level of abstraction as other flags so you can just use:

jc somewhere         ; jump if carry flag is set
jnc somewhere_else   ; jump if carry flag is not set

It's set automatically by certain instructions so, for example, to add two values and detect carry, you can use something like:

add ax,bx
jc  too_big

And, while it's mostly set by those instructions, you can also do it manually with stc (set), clc (clear) and cmc (complement). For example, it's often useful to clear it before-hand if you're entering a loop where the value is carried forward to the next iteration.

like image 171
paxdiablo Avatar answered Oct 14 '22 07:10

paxdiablo


there was this little book that once came with borland turbo assembler that listed all x86 instructions, together with their number of cpu cycles and flags affected for every model of processor individually... i suggest you go find one of those books and read it... 2: no, you cannot use cmp etc directly on the flags REGISTER as it's not memory but a register in the cpu, you can however use a couple of designated results or move the whole thing first onto the stack and then into ram or the other way around with the instructions below

CLC (clear (0) carry bit) STC (set carry flag to 1) JC (branch if carry set) JNC (branch if carry not set) PUSHF (pop flags onto stack) POPF (move latest entry on stack into flags register)

if i recall correctly... there probably are some more ways of doing things.

like image 39
HRH Sven Olaf of CyberBunker Avatar answered Oct 14 '22 06:10

HRH Sven Olaf of CyberBunker