12
  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?

Jongware
  • 22,200
  • 8
  • 54
  • 100
codesmith
  • 561
  • 1
  • 3
  • 17
  • I've spent 30 minutes searching, and I cannot find anything beyond it's existence, and that was on Wikipedia regarding int 13h. – codesmith Jan 29 '13 at 03:08
  • 3
    [This Wiki article](http://en.wikipedia.org/wiki/FLAGS_register) leads to [this official document, Intel 64 and IA-32 Architectures Software Developer's Manual, vol 1](http://download.intel.com/products/processor/manual/253665.pdf), where in section `3.4.3 EFLAGS` you're given enough information to answer your question or enough clues for further searching and reading in the document. – Alexey Frunze Jan 29 '13 at 03:23
  • Ok, thanks for showing me how to get there. I guess I get a bit frustrated with Google when my answer isn't one of the immediate results. – codesmith Jan 29 '13 at 12:02

2 Answers2

13

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Also it is one bit of a larger [FLAGS register](http://en.wikipedia.org/wiki/FLAGS_register) that you can access with the `push` and `popf` instructions. – andrewdotn Jan 29 '13 at 02:42
  • 1
    Or, *much* faster on modern CPUs, `lahf` / `sahf` (load / store AH from/to flags). One Intel uop with 1 cycle latency, vs. `pushf` taking 3, and `popf` taking 9 uops, with one-per-18-cycles throughput. Similarly slow on AMD. – Peter Cordes Jul 22 '15 at 06:35
1

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.