Author Topic: How do you retrieve the Zero Flag in assembly?  (Read 7056 times)

Offline ben321

  • Full Member
  • **
  • Posts: 185
How do you retrieve the Zero Flag in assembly?
« 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.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How do you retrieve the Zero Flag in assembly?
« Reply #1 on: October 10, 2022, 08:07:35 AM »
Code: [Select]
pushf
pop eax ; or other reg

Best,
Frank


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How do you retrieve the Zero Flag in assembly?
« Reply #2 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


Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: How do you retrieve the Zero Flag in assembly?
« Reply #3 on: October 10, 2022, 10:30:04 PM »
Code: [Select]
setz al  ; al = 0 if ZF=0, 1 if ZF=1

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How do you retrieve the Zero Flag in assembly?
« Reply #4 on: October 10, 2022, 11:09:16 PM »
We live and learn! Thank you, Fred!

Best,
Frank