In x86 and x64 assembly, the test instruction takes two operands and it performs a bitwise AND between the two operands. The result of the AND operation is discarded. But, the operation may modify the Sign Flag (SF), the Zero Flag (ZF), or the Parity Flag (PF). The Overflow Flag (OF) and the Carry Flag (CF) are set to zero.
So, if there are two operands and we want to test whether any of the operands is zero, we can use the test instruction. We often execute a test instruction before a conditional jump instruction. For example,
test eax, eax jz 0xABCD0000
Here, the test instruction performs a bitwise AND between the content of the EAX register. If the content of the EAX register is zero, the Zero Flag (ZF) will be set to 1. After that, the jz instruction will get executed and the execution flow will jump to 0xABCD0000 if the ZF flag is set to 1 or the content of the EAX register is zero.
Similarly, we can execute the following assembly instructions:
test eax, eax js 0xABCD0000
Here, the test instruction performs a bitwise AND operation between the content of the EAX register like before. If the result of the AND operation is negative, the Sign Flag (SF) will be set to 1. After that, the js instruction will get executed and the execution flow will jump to 0xABCD0000 if the SF is 1 or the result of the AND operation is negative.










































0 Comments