Question

I'm working on a homework assignment. We're given a pre-compiled binary and we have to use gdb to get assembly dumps, traverse data structures, view values stored in memory, etc. in order to puzzle out what the binary does. Here are a few lines of disassembler dump from a function call:

0x08048e14 <+21>:   test   %esi,%esi
0x08048e16 <+23>:   jne    0x8048e4b <fun6+76>
0x08048e18 <+25>:   jmp    0x8048e5d <fun6+94>

I assumed that test %esi,%esi would always return a result of "equals" (or, rather, the equivalent statement expressed using the register flags, which I believe is only ZF is set?), and that the jne instruction would never execute, and instead the program would execute the instruction at line <+25>. However, after stepping through these instructions, the program jumps to line <+76>! Why does this happen? I am so baffled.

In case it helps explain the answer, here are the register flags after the test instruction at line <+21> (ZF isn't set?)(I still don't know exactly how to interpret the flags):

eflags         0x202    [ IF ]
Was it helpful?

Solution

The test instruction performs a bitwise AND, but does not store the result; it only sets the flags.

And jne is actually "jump if ZF does not equal 0", so here it's testing if esi is zero.

Also see How does the `test` instruction work? and What does the `test` instruction do?

OTHER TIPS

I think I have found my answer:

testl b,a is like computing a&b without setting a destination. ZF set when a&b == 0.

In other words, I was thinking of the cmp instruction, which is different from test, apparently.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top