🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.msdos.programmer
Article #186

Re: Calling C functions from assembly

#186
From: g-tookey Richard Schaut
Date: Tue, 01 Aug 1989 16:07
42 lines
1952 bytes
In article <12324@s.ms.uky.edu> simon@ms.uky.edu (G. Simon Gales) writes:
>I'm trying to call some C functions from assembly, using MSC 5.0 and either
>MASM or TASM.  I could write a dummy _main() in C, if this makes things
>easier, but I'm still not sure what needs to be setup to call a C subroutine.
>
>Can anyone clue me in on how to do this?

Sure.  Given the following C function,

	foo(fparm1,fparm2,fparm3)

you would need the following code to call (assuming the paramaters are ints)

	mov ax,[fparm3]
	push ax
	mov ax,[fparm2]
	push ax
	mov ax,[fparm1]
	push ax
	call foo
	add sp,3*INTSIZE

Note that the parameters are pushed onto the stack in reverse order.  As
for how to retrieve the functions return value, you'l have to concult
you compilers documentation.  There is no set convention, however most
use one of the CPU registers.  If the routine doesn't return a value,
then all you have to worry about is pushing the parameters onto the
stack in reverse order, and restoring the stack when the routine is done.


Rick
Please send e-mail to:
schaut@madnix.UUCP
ArpaNet: madnix!schaut@cs.wisc.edu
UseNet: ...uwvax!astroatc!nicmad!madnix!schaut
             {decvax!att}!

Madison: an alternative to reality

I am posting this through a friend's account.  His consent to my use of his
account in no way implies his consent to responsibility for the opinions
expressed herein.
Technical Details
Message-ID: <8057@spool.cs.wisc.edu>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!uwvax!rocky.cs.wisc.edu!g-tookey
References: <12324@s.ms.uky.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!uwvax!rocky.cs.wisc.edu!g-tookey
From: g-tookey@rocky.cs.wisc.edu. (Richard Schaut)
Newsgroups: alt.msdos.programmer,comp.sys.ibm.pc
Subject: Re: Calling C functions from assembly
Keywords: C ASM Help
Message-ID: <8057@spool.cs.wisc.edu>
Date: 1 Aug 89 16:07:21 GMT
References: <12324@s.ms.uky.edu>
Sender: news@spool.cs.wisc.edu
Reply-To: g-tookey@rocky.CS.WISC.EDU (Richard Schaut)
Organization: U of Wisconsin CS Dept
Lines: 42