Question

I have disassembled my test C program, this is a part of the code I'm not sure if I understood correctly.

00401143: 83 F8 01           cmp         eax,1
00401146: 0F 84 F4 00 00 00  je          00401240
0040114C: 85 C0              test        eax,eax
0040114E: 0F 85 A0 00 00 00  jne         004011F4
00401154: 31 C0              xor         eax,eax

So basically, this is what I understand:

  1. Check if eax equals 1, if yes return 0
  2. If 0, jump to 00401240, if not, continue
  3. Test == AND thus I already know it will return 1 because there is no way eax can equal 0 as it would jump on 00401146. I was wondering why should I call this test. Why can't I simply replace test and jne with single jmp (non-conditional jump) which we would reach only if eax equals 1 anyway.
  4. If not 0 (thus 1), jump to 004011F4 -- this will always happen
  5. We will never reach 00401154 from this point (we can assume then that there must be call/jump) somewhere in the code which would navigate us to 00401154

Is this correct or is there something that I haven't understood correctly?

No correct solution

OTHER TIPS

This is likely to be simply badly optimised code by the compiler, but to explain what it has done, while being thorough it added the bitwise "test" to look for any other value except for the 1 you were initially looking for.

The compiler has generated code to evaluate whether EAX is 0 or 1 and strictly no other value.

if(a==1){...}
else if(a<>0){...}

...

00401143: 83 F8 01           cmp         eax,1     ...EAX is 1?
00401146: 0F 84 F4 00 00 00  je          00401240  ...EAX is 1...
0040114C: 85 C0              test        eax,eax   ...EAX is strictly 0? (eax & eax)
0040114E: 0F 85 A0 00 00 00  jne         004011F4  ...EAX is another value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top