28

I'm looking at some small assembler codes and I'm having trouble understanding the TEST instruction and its use. I'm looking at the following code at the end of a loop:

8048531:    84 c0                   test   al,al
8048533:    75 dc                   jne    8048511 <function+0x2d>

The way i understand TEST is that it works a bit like the AND operator and it sets some flags. I guess I don't really understand how the flags work. test al,al to me looks like it checks the same lower bits and will always get the same results.

Can someone explain?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
danielhc
  • 479
  • 1
  • 5
  • 12

1 Answers1

19

It tests the register against itself, just to set the flags. The result will be different for a zero and a non-zero value.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 1
    So to clarify, when does the TEST produce a result that will make the JNE jump? – danielhc May 14 '11 at 13:31
  • 1
    It sets ZF if the tested register is zero, otherwise it clears the flag. The JNE jumps (or not) depending on that flag. – Bo Persson May 14 '11 at 13:38
  • 7
    @danielhc: The JNE instruction is also known as JNZ - "Jump if Not Zero". In your case, the jump will be taken if the register al is not zero. – user200783 May 14 '11 at 13:45