Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the `test` instruction do? [duplicate]

Tags:

x86

assembly

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?

like image 538
danielhc Avatar asked May 14 '11 13:05

danielhc


People also ask

What does the test instruction do?

In the x86 assembly language, the TEST instruction performs a bitwise AND on two operands. The flags SF , ZF , PF are modified while the result of the AND is discarded. The OF and CF flags are set to 0 , while AF flag is undefined.

What does test %eax %eax do?

eax contains the return value of strcmp. test is like bitwise and except it only sets the flags. Anding a value with itself gives the same value, so test eax, eax sets the flags based on whatever eax contains. ZF is set when the result of an operation is zero.

How does test set the zero flag?

TEST sets the zero flag, ZF , when the result of the AND operation is zero. If two operands are equal, their bitwise AND is zero when both are zero. TEST also sets the sign flag, SF , when the most significant bit is set in the result, and the parity flag, PF , when the number of set bits is even.

What does Movzbl do in assembly?

For MOVZBL, the low 8 bits of the destination are replaced by the source operand. the top 24 bits are set to 0. The source operand is unaffected. For MOVZBW, the low 16 bits of the destination are replaced by the source operand.


1 Answers

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

like image 182
Bo Persson Avatar answered Oct 03 '22 02:10

Bo Persson