delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/12/07/14:29:02

Sender: nate AT cartsys DOT com
Message-ID: <366C2B61.1331A1A5@cartsys.com>
Date: Mon, 07 Dec 1998 11:24:17 -0800
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.0.35 i486)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: Passing variables from DJGPP to extern functions made in NASM
References: <Pine DOT SOL DOT 4 DOT 05 DOT 9812042355290 DOT 16251-100000 AT sunburn DOT ccs DOT yorku DOT ca>
Reply-To: djgpp AT delorie DOT com

SynthHack wrote:
> 
> HI,
> 
> How can I pass arguments (or variables) from DJGPP to a function I made in
> NASM? (And created a coff file with it)...

The best way is probably to make your NASM function conform to GCC's
calling conventions.  This means:

* Stack layout is as follows:

   Last argument   
   ...
   Second argument [esp+8]
   First argument  [esp+4]
   Return address  [esp]

This assumes all arguments are `int', pointers, or `float'.  A `double'
will occupy two such slots (`double's are not by default aligned on the
stack).  You should avoid passing structs on the stack; it gets very
complicated.

* The return value, if an integer or pointer, should be placed in eax. 
If a floating point value, leave it in the FPU top-of-stack register. 
Don't try to return structures, it too is complicated.

* You may modify registers eax, ecx, edx and the FPU registers.  Others
should be saved and restored.

Then declare the function appropriately in your C header.  (If using
C++, use `extern "C"'.)

If you want to use your own calling conventions, you'll have to use
inline asm from GCC to call the function.  So, if an asm function `frob'
is passed a value in ebx and modifies it in place, clobbering ecx:

int n;
...
asm("call frob" : "=b" (n) : "0" (n) : "ecx");

This is probably not the best solution.  The first way is better, IMHO,
unless there are special considerations.
-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


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