2

I'm running through some code right now on gdb and I have no clue what these two instructions actually do. If anyone could help me out, I'd really appreciate it.

add  -0x2c(%ebp, %ebx, 4), %eax
cmp  %eax, -0x28(%ebp, %ebx, 4)
Carl Norum
  • 219,201
  • 40
  • 422
  • 469

2 Answers2

4

x86 assembly is usually much easier to understand when you write it in Intel syntax instead of AT&T syntax.

In Intel syntax it would be:

add eax,[ebp+4*ebx-0x2C]
cmp [ebp+4*ebx-0x28],eax

The first instruction (add) adds the value of word stored in the memory address [ebp+4*ebx-0x2C] to the value of eax, and stores the sum in eax.

The second instruction (cmp) compares eax with [ebp+4*ebx-0x28] by subtracting the value of eax from the value of the word stored in the memory address [ebp+4*ebx-0x28], and sets flags (OF, SF, ZF, AF, PF, CF) accordingly but does not save the result anywhere. cmp is exactly the same as sub, the only difference being the fact that in sub the result is saved, in cmp not.

The type of comparison is usually created in the form a conditional jump. In x86 assembly there are a lot of conditional jumps and whether they branch depends on the values of the flags.

nrz
  • 10,435
  • 4
  • 39
  • 71
  • Sorry >.< I just started doing this stuff within the past week so I didn't know how to change between syntax's and such. This does help a lot though. How do I exactly check the value of the flag? –  Sep 08 '12 at 02:13
  • [Intel and AT&T syntaxes](http://www.imada.sdu.dk/Courses/DM18/Litteratur/IntelnATT.htm) A conditional jump after `cmp` jumps to somewhere else in the code if the condition is met. If not, then the execution continues on the next instruction (no jump). There are other possible ways too (a hash table with flags as keys), but conditional jumps solve all the cases. Note that in Intel and AT&T syntaxes the order of operands is reversed, in Intel its `dest,src` whereas in AT&T it's `src,dest`. This affects `cmp` too. See [Intel x86 jump quick ref.](http://www.unixwiz.net/techtips/x86-jumps.html). – nrz Sep 08 '12 at 02:30
1

That's AT&T assembly syntax. Those addressing modes are a bit weird looking, but in pseudocode, they mean:

eax = *(ebp + ebx*4 - 0x2c)

and

compare eax to *(ebp + ebx*4 - 0x28)

Here's a link with complete explanation.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469