How the hell is this code working?

Status
Not open for further replies.
Ok, here's a weird one for ya. I'm trying to learn assembler in linux, bsd, unix, whatever... well, i was playing around, and i wrote a few code snippets in c and c++ to see what they would compile to, and this is waht i got, first the code snippits:
code:<HR>
Code:
add.C:
int add (int a, int b) {
    return a + b;
}
addm.cpp:
#include<iostream>

int add(int, int);

int main() {
    int a, b;
    cin >> a >> b;
    cout << add(a, b) << endl;
return 0;
}
-----------------------------------
and after compiling:
add.S:
        .file   "add.C"
gcc2_compiled.:
___gnu_compiled_cplusplus:
.text
        .align 4
.globl _add__Fii
        .def    _add__Fii;      .scl    2;      .type   32;     .endef
_add__Fii:
        pushl %ebp
        movl %esp,%ebp
        movl 8(%ebp),%eax
        movl 12(%ebp),%ecx
        leal (%ecx,%eax),%edx
        movl %edx,%eax
        jmp L2
        jmp L3
        jmp L2
        .align 4
L3:
L2:
        movl %ebp,%esp
        popl %ebp
        ret

now after compiling it ran just as you would expect, and i ran as on the add.S file to get an a.out, then used g++ to assemble and link the rest. It ran fine... I dont get it though, where in any of that does it do an add? I dont see an add instruction anywhere in there, yet it adds the numbers when you run it, so i'm confused....

Anyone know how this works?

-----------------
Crackez
 

DrPizza

Well-known member
93,870
Now for a little detail (I had to post that just to make sure no-one posted before me whilst writing the detail).

<tt>lea</tt> means "load effective address". It takes two parameters, a source and a destination. The operand takes the address of the source, and stores it in the destination.

Due to the way that x86 assembler works, the way it computes the address can be (ab)used to do other things (it can be used to move, add and multiply, and more to the point, do all of these together). In this case, <tt>lea</tt> is using <tt>(%ecx,%eax)</tt> as the source, and <tt>%edx</tt> as the destination. In this particular assembler syntax, <tt>(%ecx, %eax)</tt> means "add the value in ecx to that in eax, and treat that as a memory address".

So this adds eax and ecx, and stores that result in edx.
 
ok, so whats the deal with the jmp's? I can understand the abuse of lea... but i think there's something i'm missing in general, so far the only real assembly programming i've done has been with MASM 6.11, and i'm wondering is there any signifigance to the l at the end of each instruction? like pushl instead of push? and tell me, are the operands reversed or something? like 'mov orig,dest' or something? cause the last mov there is 'movl %edx,%eax', but doesn't it need to return the answer in eax? so in intel syntax it would be like this right? 'mov eax,edx' since the answer is originally in edx right? i'm just so confused about this....

---------------
Crackez

P.S. anyone know of some good online resources for linux/x86 asm programming? Right now i'm stuck using masm for my class, but i'd really like to have the flexibility of using a linux assembler, i'm looking into NASM, but i've been having problems with it, like getting past '.386' and '.model flat' I assume these macros just aren't supported, but is there anyway around that, btw, i just need to write some simple functions for this class. so if i can get around needing those MS assembler shortcuts, that'd be good too...
 
Status
Not open for further replies.