News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

How to check values of status flags?

Started by Victor Stout, January 08, 2011, 10:33:44 PM

Previous topic - Next topic

Victor Stout

Hello. I would like an advice on how to check status flags (CF,PF,AF,ZF,SF and OF) and a simple code example if possible since I'm new to MASM. The reason of this is to get to know the values of data types on a binary level and see how they interact with status flags. Perfect solution to this question would be example that shows steps to be performed on BYTE data type value to affect all (or those that can be affected by manipulation with BYTE) the flags. Thanks.

donkey

Normally you push the values onto the stack and pop them into a register.

PUSHFD ; pushes the EFlags
POP eax

you can use BT (Bit Test) to check each individual bit or use boolean operators such as TEST/AND/OR.

A less efficient way is to use the Jcc group of instructions to test for a flag condition and execute code based on the result. For example you can use JC/JNC to test the carry flag or JP/JNP to test the parity flag.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

clive

It could be a random act of randomness. Those happen a lot as well.

donkey

Hi clive,

Forgot about LAHF :) Does it include the direction and overflow flags ?
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

clive

Quote from: donkey on January 09, 2011, 12:47:31 AM
Hi clive, Forgot about LAHF :)

Hi Edgar<G>

It's a bit of a dinosaur, tend to use other instructions to recover carry, or extend sign.

With the P6 and beyond you have the conditional instructions SETxxx (ie SETNGE, SETNP, etc)
http://faydoc.tripod.com/cpu/setnge.htm

They are a bit more complicated than perhaps what is sought here, but food for thought.
It could be a random act of randomness. Those happen a lot as well.

jj2007

Quote from: donkey on January 09, 2011, 12:47:31 AM
Forgot about LAHF :) Does it include the direction and overflow flags ?

AH := SF ZF xx AF xx PF xx CF

It's a lot faster than the pushfd/pop eax combi.

Intel(R) Celeron(R) M CPU        420  @ 1.60GHz (SSE3)
841     cycles for 100*pushfd
104     cycles for 100*lahf


Make sure you are not running an Athlon64...!

KeepingRealBusy

Quote from: jj2007 on January 09, 2011, 01:27:08 AM
Quote from: donkey on January 09, 2011, 12:47:31 AM
Forgot about LAHF :) Does it include the direction and overflow flags ?

AH := SF ZF xx AF xx PF xx CF

It's a lot faster than the pushfd/pop eax combi.

Intel(R) Celeron(R) M CPU        420  @ 1.60GHz (SSE3)
841     cycles for 100*pushfd
104     cycles for 100*lahf


Make sure you are not running an Athlon64...!

You might want to clarify that to say an "Athlon running in 64 bit mode", works fine in 32 but mode, in 64 bit mode you need to check the status of ecx bit 0 returned by CPUID extended function 8000_0001h to determine if the CPU implementation supports LAHF in 64 bit mode.

Dave.


dedndave

Jochen
i have to ask if the test includes a popfd / sahf paired with the pushfd / lahf
seems to me that pushfd and lahf are not the slow ones
it is one thing to examine a flag - it is another thing to explicitly set it

jj2007

Quote from: dedndave on January 09, 2011, 04:15:13 AM
Jochen
i have to ask if the test includes a popfd / sahf paired with the pushfd / lahf
seems to me that pushfd and lahf are not the slow ones
it is one thing to examine a flag - it is another thing to explicitly set it

See yourself...
invoke Sleep, 100
counter_begin LOOP_COUNT, HIGH_PRIORITY_CLASS
REPEAT 100
pushfd
pop eax
ENDM
counter_end
print str$(eax), 9, "cycles for 100*pushfd", 13, 10
invoke Sleep, 100
counter_begin LOOP_COUNT, HIGH_PRIORITY_CLASS
REPEAT 100
lahf
ENDM
counter_end
print str$(eax), 9, "cycles for 100*lahf", 13, 10

dedndave

thanks for clarifying, JJ   :bg
the code wasn't attached - so couldn't see it

if you are using 32-bit code, LAHF will get you most of the useful flags
it won't get you the DF, however
it is only when the CPU is in long mode that some CPU's don't support it
i have a prescott - and it does not support LAHF/SAHF in long mode - so AMD isn't the only culprit