5

1.

TEST EAX,EAX
JNZ SHORT program.00407190

2.

XOR EAX,EAX
JNZ SHORT program.00407190

Kindly correct me if I'm wrong. Thank you! :)

From my understanding so far:

JNZ is jump if not equal to zero, it will jump or not depending on whether ZF is set to 1 or not. If it's 1, it will not jump. Else, it will jump.

From my understanding for code #1, TEST EAX,EAX will check whether it's zero or not. If it's not equal to zero(ZF is 0), it will jump to address 00407190.

For code #2
XOR EAX,EAX will set EAX register to 0. Does it set any flags? If not, how does JNZ instruction determine to jump or not?

Lastly, why would people want to check if EAX is 0 or not? Kindly please assist me in a easier and detailed explanation, I'm still a beginner.

Isaac D. Cohen
  • 773
  • 2
  • 9
  • 23
ysj
  • 329
  • 1
  • 5
  • 12
  • 2
    As for why you'd want to check if `EAX` is 0 or not; it depends on the context. Let's say that you're writing a Windows application and you called some WinAPI function, like `CreateWindow` or `HeapAlloc` or whatever. The result of the function (whether it's a window handle, a pointer to an allocated memory block, or something else) will be returned in `EAX`, and it makes sense to check whether the result was zero (invalid) or non-zero (valid), in order to determine how to proceed in your program. – Michael Sep 25 '13 at 11:56
  • Thank you! This is definitely a good example for beginners to relate. – ysj Sep 26 '13 at 11:53
  • possible duplicate of [x86 Assembly - 'testl' eax against eax?](http://stackoverflow.com/questions/147173/x86-assembly-testl-eax-against-eax) for `test eax`, and http://stackoverflow.com/questions/1396527/any-reason-to-do-a-xor-eax-eax for `xor` – Ciro Santilli新疆棉花TRUMP BAN BAD Aug 12 '15 at 15:36

1 Answers1

8

TEST and XOR are logical instructions used to perform logical operations on the operands.

TEST INSTRUCTION (comparing the operands)

TEST destiny, source

It performs a conjunction, bit by bit, of the operands, but differing from AND, this instruction does not place the result in the destination operand, it only has effect on the state of the flags.

Source Destiny | Destiny
--------------------------
1      1       | 1      
1      0       | 0
0      1       | 0
0      0       | 0    <---

XOR INSTRUCTION (Exclusive OR)

XOR destiny, source 

Its function is to perform the logical exclusive disjunction of the two operands bit by bit.

Source Destiny | Destiny
--------------------------
1      1       | 0    <---
1      0       | 1
0      1       | 1
0      0       | 0    <---

As you see in the tables:

XOR EAX,EAX will set the EAX register to zero. The ZF will be set if the result of the XOR is zero. So in this case: (ZF=1)

TEST EAX,EAX does not place the result on the register, it only has effect on the state of the ZF. In this case if EAX == 0, then (ZF=1)


JNZ (JNE) INSTRUCTION (Conditional jump)

JNZ label

It jumps to label if it is not equal or zero. The jump will be done if ZF is deactivated. (ZF=0)

Vahid Hallaji
  • 6,026
  • 4
  • 39
  • 48
  • Thanks for the explanation but you missed out some of my questions. For code #2 XOR EAX,EAX will set EAX register to 0. Does it set any flags? If not, how does JNZ instruction determine to jump or not? Lastly, why would people want to check if EAX is 0 or not? – ysj Sep 25 '13 at 05:53
  • @ysj `JNE` will be jumped if `ZF=0`. `XOR EAX,EAX` set `ZF=1`. Checking `EAX=0` is depend on your purpose. – Vahid Hallaji Sep 25 '13 at 06:16
  • @ysj `JNE` and `JNZ` are just different names for a conditional jump when `ZF` is equal to `0` – Vahid Hallaji Sep 25 '13 at 06:33