Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x86 jnz after xor?

After using IDA Pro to disassemble a x86 dll, I found this code (Comments added by me in pusedo-c code. I hope they're correct):

test    ebx, ebx        ; if (ebx == false)
jz      short loc_6385A34B ; Jump to 0x6385a34b
mov     eax, [ebx+84h]  ; eax = *(ebx+0x84)
mov     ecx, [esi+84h]  ; ecx = *(esi+0x84)
mov     al, [eax+30h]   ; al = *(*(ebx+0x84)+0x30)
xor     al, [ecx+30h]   ; al = al XOR *(*(esi+0x84)+0x30)
jnz     loc_6385A453

Lets make it simpler for me to understand:

mov     eax, b3h
xor     eax, d6h
jnz     ...

How does the conditional jump instruction work after a xor instruction?

like image 387
小太郎 Avatar asked Jun 06 '10 11:06

小太郎


3 Answers

Like most instructions, xor sets the processor condition flags depending on the result of the previous operation. In this case, the Z flag will be set if the result of the xor is zero. The jnz instruction tests the Z flag and branches if it is not set.

like image 186
Greg Hewgill Avatar answered Sep 29 '22 01:09

Greg Hewgill


I barely know assembly at all but xor in this context does pretty much the same as cmp I’d say, in addition to setting eax to the result of the xor operation.

In other words, after the xor, eax will be 0 exactly if its previous value was d6h (otherwise, it will be some value != 0). And additionally, the zero flag will be set (as with cmp) so you can jnz to test that flag.

like image 30
Konrad Rudolph Avatar answered Sep 29 '22 01:09

Konrad Rudolph


It will jump if the value in eax doesn't end up as zero.

Your second example doesn't do the code justice since the code you have is using constant values, not values loaded from memory.

In the first example, it loads all those values from memory and performs the xor on that. The memory contents may, unlike your second example, change on each execution depending on what's in [ebx+84h] and [esi+84h].

See xor and jnz for details.

like image 32
paxdiablo Avatar answered Sep 28 '22 23:09

paxdiablo