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

codesmith picture codesmith · Jan 29, 2013 · Viewed 16.4k times · Source
  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?

Answer

paxdiablo picture paxdiablo · Jan 29, 2013

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.