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
  • 42,023
  • 8
  • 87
  • 158
danielhc
  • 479
  • 1
  • 5
  • 12
  • @Evan: Please to *not* create more tags for specific x86 instructions. We can discuss this on meta if you want, but don't tag more questions until after discussion. – Peter Cordes Apr 17 '18 at 00:19
  • @PeterCordes https://meta.stackoverflow.com/q/366109/124486 – user157251 Apr 17 '18 at 00:38

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
  • 86,087
  • 31
  • 138
  • 198
  • 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