-1

I was looking through some code and found 2 lines that perplexed me:

add    -0x4(%esi,%ebx,4),%eax
cmp    %eax,(%esi,%ebx,4)

I am accustomed to the standard add src,dst and cmp x1,x2 and I'm not really sure what these lines are actually doing.

I believe that it is compiled with GCC

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Strogenstroff
  • 47
  • 1
  • 9
  • It would help if you edit the question to include more details about the specific assembler you're using. I don't recognize this syntax, though I must add, it's been a while since I've fiddled about with assembler. – John Gaines Jr. Apr 15 '12 at 03:38

1 Answers1

2

That's using the Base + (Index * Scale) + Displacement addressing mode. At least, I think so. I'm not real familiar with the AT&T syntax. I think the Intel syntax would be:

add eax,[esi + ebx*4 - 4]
cmp [esi + ebx*4],eax

This looks like it's indexing into an array of integers (4-byte values). Imagine in C that you want to add the value from some array element to a total, like this:

int a[100];
int i = 10;
int total = 0;
total += a[i-1];

Now, make esi hold the address of the array, ebx hold the value of i, and eax hold the value 33. You'd get:

add eax,[esi + ebx*4 - 4]

The comparison instruction is testing to see if the result (in eax) is equal to the next value in the array. In the C example, that would be equivalent to comparing total to a[i].

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • 1
    You are correct. AT&T syntax basics: http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html – DCoder Apr 15 '12 at 05:24