I'm finding myself unable to read the zero flag (ZF) bit correctly. I'm quite confused, because this approach appears to work fine for other flags such as the carry flag (CF) or overflow flag (OF). My strategy is to push the EFLAGS
register onto the stack with the instruction pushf
, then select the ZF bit with a bitwise AND.
In the code below, I set EAX to 0xFFFFFFFE, so after incrementing once it should be 0xFFFFFFFF and ZF should be zero. After incrementing it twice then EAX should be 0x0 and ZF should be one, if my understanding is correct. However, my program outputs:
Value of Zero Flag: 0
Value of Zero Flag: 0
Where I expect it to output:
Value of Zero Flag: 0
Value of Zero Flag: 64
My code is this:
.text
.global main
main:
movl $0xfffffffe, %eax
addl $1, %eax
pushf
pop %ebx
andl $0x40, %ebx
pushl %ebx
pushl $strzf
pushl $string
call printf
pop %ebx
pop %ebx
pop %ebx
addl $1, %eax
pushf
pop %ebx
andl $0x40, %ebx
pushl %ebx
pushl $strzf
pushl $string
call printf
pop %ebx
pop %ebx
pop %ebx
leave
ret
.section .rodata
string:
.string "Value of %s: %d\n"
strzf:
.string "ZF"
User contributions licensed under CC BY-SA 3.0