Mail Archives: djgpp/1999/11/13/17:02:18
"Groman" <groman AT tfz DOT net> writes:
> hello.. could somebody please help me with this?
>
> I have the following NASM function which I call from my DJGPP program:
>
> _testfunc:
> push ebp
> mov edx, [esp+4]
> mov ecx, [esp+8]
> add edx,ecx
> mov eax,edx
> leave
> ret
>
> which is declared in C++ as
> extern long int testfunc(long int x,long int y);
>
> and it should return the sum of the two long ints? right?
No. As it stands, your stack after the `push ebp' will look like
this:
Address Value
esp+12 y
esp+8 x
esp+4 return address
esp pushed ebp
So you're really returning x + the return address.
Also, your stack frame setup is wrong. A standard stack frame setup
goes like:
mov ebp, esp
push ebp
...
; Now reference stack parameters with respect to ebp, which
; points at the return address...
leave ; or pop ebp; mov esp, ebp
So if you're going to restore esp from ebp at the end (which is what
leave does), you'd better have it contain the right value.
However, as you seem to have noticed, it's easy in this example to
access the function's arguments via esp (the register, not the method
of telepathy :). In this case you can ignore ebp altogether and omit
the leave.
Hope this helps.
--
Nate Eldredge
neldredge AT hmc DOT edu
- Raw text -