-2

I am very confused when it comes to status flags. Can someone help me through an example and the steps I need to take to verify how the flags are behaving?

mov ah, 21
mov al, -21
cmp ah,al

Can someone help me determine the behavior of the CF, ZF,SF, and OF?

Thanks

Pixel
  • 31
  • 4
  • 1
    There's an app - erm - manual for that. – 500 - Internal Server Error Nov 15 '14 at 23:52
  • See also [this question](http://stackoverflow.com/questions/26423548/68000-assembly-language-cmpi-b/26425658). While that is for 68k, the logic for the 4 flags (with `N`=`SF` and `V`=`OF`) is the same on x86 too except here the second operand is subtracted from the first. – Jester Nov 16 '14 at 00:10
  • [`cmp` is just `sub`](http://stackoverflow.com/questions/7261535/why-does-cmp-0x84-0x30-trigger-the-overflow-flag), except that its operands are not affected, just the flags register. –  Nov 16 '14 at 00:21

1 Answers1

0

You can look at the eflags register in a debugger (like gdb). There is plenty of readily accessible documentation about eflags, but essentially bits in the register are 0 or 1 depending on whether particular status flags are set.

If it is not enough to just see the values in a debugger, and you need to do something with them:

  1. you can push eflags onto the stack (pushfl works for me) and then pop the stack to a general purpose register.

  2. You can use the jc, jz, jo, and js instructions which jump to a specified label if the carry, zero, overflow, or sign flags (respectively) are set.

For example:

    clc                     # clear carry flag (set CF = 0)
    addl eax, 0xffffffff    # some operation that might change status flags
    jc label1               # if the carry flag is set, jump to label

    ...                     # instructions to execute if carry not set (CF = 0)

    jmp label2

label1:

    ...                     # instructions to execute if carry set (CF = 0)

label2:

    ...                     # resume execution which does not depend on CF
Tony
  • 1,645
  • 1
  • 10
  • 13