1

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 ]
jayhendren
  • 3,510
  • 29
  • 52

2 Answers2

7

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?

Community
  • 1
  • 1
starrify
  • 13,101
  • 4
  • 32
  • 48
  • 1
    SO automatically inserts question titles for its own links. – rici Oct 07 '13 at 04:24
  • @rici Thank you very much for pointing this out :) However just after my posting this answer it shows "enter link description here" XD, which made me quite confused.. – starrify Oct 07 '13 at 04:31
  • starrify: there are some wierd update issues. If it looks like you've got a previous version, or you're missing a comment you just typed, etc., reload the page and you'll usually get the most recent version. – rici Oct 07 '13 at 04:33
1

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.

jayhendren
  • 3,510
  • 29
  • 52