Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x86: ZF not always updated by AND?

Tags:

x86

assembly

I'm debugging my code on x86 and the problem tracks down to AND instruction sometimes does not clear ZF flag if the result is not zero. Here is the problematic piece of code:

0257A9F9  mov         edx,dword ptr [ecx+18h] 
0257A9FC  and         edx,80000h 
0257AA02  int         3    
0257AA03  je          0257AA2A 

I added a breakpoint after AND for debugging. When it stops on the breakpoint EDX==0x80000 and ZF==1. But ZF should be cleared if EDX!=0. The code works perfectly fine when single stepped in debugger, but it fails consistently during normal run.

Here is a screenshot of debugger session.

Any hints?

If that matters the code is generated by JIT, so I'm executing data.

Thank you in advance.

like image 574
danila Avatar asked Jan 22 '09 15:01

danila


3 Answers

Thanks everyone. It was my fault, sorry to bother you. There is a branch to 'int 3' from another place. That's why the flags are inconsistent with instructions before 'int 3'. I was confused by always having edx==0x80000 at this point. Sorry again.

like image 60
danila Avatar answered Oct 29 '22 06:10

danila


You can easily examine the int 3 handler to see if it's returning with a iret (i.e. pop back the callers flags) or if it's returning with retf 2 (i.e. preserve the flags from the handler).

like image 39
Jonas Engström Avatar answered Oct 29 '22 06:10

Jonas Engström


According to the Intel instruction set reference, ZF is always set according to the result. Could something in the int 3 handler be manipulating this?

Edit: After further digging through the manuals (thank god for Intel sending out free copies!), my only ideas are that it's either the int 3 handler setting it somehow, or the processor only looking at dx instead of edx when setting flags. Both seem unlikely, but the latter seems completely implausible. What mode are you running in? (Real, protected, unreal, long?)

like image 27
Serafina Brocious Avatar answered Oct 29 '22 07:10

Serafina Brocious