Intel x86 Assembly Language & Microarchitecture Optimization Moving Carry flag into a register



If the Carry (C) flag holds a value that you want to put into a register, the naïve way is to do something like this:

    mov  al, 1
    jc   NotZero
    mov  al, 0

Use 'sbb'

A more direct way, avoiding the jump, is to use "Subtract with Borrow":

    sbb  al,al    ; Move Carry to al

If C is zero, then al will be zero. Otherwise it will be 0xFF (-1). If you need it to be 0x01, add:

    and  al, 0x01 ; Mask down to 1 or 0


  • About the same size
  • Two or one fewer instructions
  • No expensive jump


  • It's opaque to a reader unfamiliar with the technique
  • It alters other Flags