NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: ben321 on October 10, 2022, 05:45:28 AM

Title: How do you retrieve the Zero Flag in assembly?
Post by: ben321 on October 10, 2022, 05:45:28 AM
If I do a comparison operation like "TEST EAX,EAX", the result of that operation is unfortunately not stored in any normal accessible register like ECX or whatever. Instead it's stored in the Zero Flag bit of the CPU status register. This works great when using a conditional jump instruction (like JNE), which internally looks at such flag bits. However what if I want to just extract ZF bit for further usage that DOESN'T involve a conditional jump, how do I do that? For example, how do I store the ZF bit in the lowest bit of the EAX register? I don't know if there's any kind of GetZeroFlagBit instruction in x86 assembly.
Title: Re: How do you retrieve the Zero Flag in assembly?
Post by: Frank Kotler on October 10, 2022, 08:07:35 AM
Code: [Select]
pushf
pop eax ; or other reg

Best,
Frank

Title: Re: How do you retrieve the Zero Flag in assembly?
Post by: Frank Kotler on October 10, 2022, 08:59:16 PM
Hi again Ben,

I should have said... You probably don't want to do this. Probably better to save the "mystery value" and test it for zero when you're ready to use the result than to test it now and save the result. But if you want to, "pushf"...

Best,
Frank

Title: Re: How do you retrieve the Zero Flag in assembly?
Post by: fredericopissarra on October 10, 2022, 10:30:04 PM
Code: [Select]
setz al  ; al = 0 if ZF=0, 1 if ZF=1
Title: Re: How do you retrieve the Zero Flag in assembly?
Post by: Frank Kotler on October 10, 2022, 11:09:16 PM
We live and learn! Thank you, Fred!

Best,
Frank