codesmith
codesmith

Reputation: 561

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

  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?

Upvotes: 12

Views: 24825

Answers (2)

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.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882646

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.

Upvotes: 13

Related Questions