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
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:
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:
Now, make
esihold the address of the array,ebxhold the value ofi, andeaxhold the value 33. You’d get: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 comparingtotaltoa[i].