www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/11/13/17:02:18

From: Nate Eldredge <neldredge AT hmc DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help interfacing DJGPP and NASM again...
Date: 13 Nov 1999 13:31:22 -0800
Organization: InterWorld Communications
Lines: 54
Message-ID: <833dua9iz9.fsf@mercury.st.hmc.edu>
References: <s2q3mqdehsq93 AT news DOT supernews DOT com>
NNTP-Posting-Host: mercury.st.hmc.edu
X-Trace: nntp1.interworld.net 942528747 83437 134.173.45.219 (13 Nov 1999 21:32:27 GMT)
X-Complaints-To: usenet AT nntp1 DOT interworld DOT net
NNTP-Posting-Date: 13 Nov 1999 21:32:27 GMT
X-Newsreader: Gnus v5.7/Emacs 20.4
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019