NASM Forum > Using NASM

How does the test instruction work?

(1/2) > >>

ThatGuy22:
I was wondering how the test instruction works, I know it does almost the same thing as and. Does the test instruction take a value you give it and tell you if that bit is set like:
test ax, 2 - would this test ax to see if the second byte is set like 00000010 since, 00000010 in binary is equivalent to 2?
like wise would this test for 3:
test ax, 3 - testing to see if 00000011 is set?

 -In short does the test instruction see if the binary value that makes up the number you give it is set within the first operand?
 -If I am incorrect can someone explain it to me please.

cm:

--- Quote from: ThatGuy22 on December 15, 2010, 02:50:34 PM ---I was wondering how the test instruction works, I know it does almost the same thing as and.
--- End quote ---

The only difference is that "test" only sets the flags (you're primarily interested in the Zero Flag (ZF) here) while "and" sets the flags (ZF) as well as the destination operand.


--- Quote ---test ax, 2 - would this test ax to see if the second byte is set like 00000010 since, 00000010 in binary is equivalent to 2?
--- End quote ---

Second bit; besides that, yes, this instruction would test whether the second bit is set. You can then use a "jz" (jump if zero (ZF set)) or "jnz" (jump if not zero (ZF clear)) conditional jump instruction.


--- Quote ---like wise would this test for 3:
test ax, 3 - testing to see if 00000011 is set?
--- End quote ---

Yes. But you have to understand that it will test if either the first bit (01) or the second bit (10) or both (11) are set. Any of these three states would cause "test" to tell you that the number you tested for is "set" - if you test for multiple bits like this, the test instruction tells you whether at least one of the bits is set.

ThatGuy22:
Ok, thank you this was exactly what I was looking for.

JoyceTruitt:
Nice One

fredericopissarra:

--- Quote from: cm on December 15, 2010, 06:03:13 PM ---
--- Quote from: ThatGuy22 on December 15, 2010, 02:50:34 PM ---I was wondering how the test instruction works, I know it does almost the same thing as and.
--- End quote ---

The only difference is that "test" only sets the flags (you're primarily interested in the Zero Flag (ZF) here) while "and" sets the flags (ZF) as well as the destination operand.
--- End quote ---
Just a tiny correction... TEST is, indeed AND affecting only the flags, but ALL arithmetic/logic related flags are affected... ZF, SF and PF are set/reset depending on the result, and CF=OF=0. The AF flag is undefined.

Navigation

[0] Message Index

[#] Next page

Go to full version