[comp.sys.ibm.pc] Calling a "C" function from assembly language

cy@killer.DALLAS.TX.US (Cyrus Foughty) (10/22/88)

	I have a assembly language program from which I want to 
	call a "C" function. I know this is backwards from what 
	everyone else does. BTW, I am using Microsoft Masm and
	"C" 5.1 for this little dodad. THE Microsoft manuals tell
	how to interface an assembly language routine but not the
	other way round. I would be forced to change ALL my assembly
	programs to match what "C" wants and that is not an option.
	I have way to much assembly to change. How do I do it?
-- 
cy
{ames,decwrl}!killer!cy
MaBell: 408.255.5990 
IRS Motto: Reach out and touch someone!                         

everett@hpcvlx.HP.COM (Everett Kaser) (10/25/88)

This is a case of RTFM, I'd say.  Both my 5.01 MASM and my 5.x C have
a manual, or section of a manual (they're both the exact same pages, just
one is a stand-alone manual, and the other is a section of a binder) called
"Mixed Language Programming Guide".  Section 6.6 of that guide is titled
"Calling High-Level Languages from Assembly".  The first paragraph is:

 High-level language routines assume that certain initialization code has
 previously been executed; you can ensure that the proper initialization is
 performed by starting in a high-level language module, and then calling an
 assembly procedure.  THE ASSEMBLY PROCEDURE CAN THEN CALL HIGH-LEVEL
 LANGUAGE ROUTINES AS NEEDED...

It then goes on to explain four guidelines to be observed in making the call.

Everett Kaser
!hplabs!hp-pcd!everett

hollen@spot.megatek.uucp (Dion Hollenbeck) (10/26/88)

From article <5876@killer.DALLAS.TX.US>, by cy@killer.DALLAS.TX.US (Cyrus Foughty):
> 
> 	I have a assembly language program from which I want to 
> 	call a "C" function. I know this is backwards from what 
> 	everyone else does. BTW, I am using Microsoft Masm and
> 	"C" 5.1 for this little dodad. [...stuff deleted...]
> 	How do I do it?

The key is in the mixed language section of the C reference manual,
although it is implied rather than stated exactly.  You need to know
that a C function pushes its parameters on the stack from right to
left.  Byte width parameters are pushed as words with high order
bits zeroed and long width (32-bits) parameters are pushed high
16-bits first and low 16-bits second.  The calling assembly
is responsible for stripping the pushed parms off the stack on
return from the called function.  I recommend using "ADD  SP,xxx"
where xxx = number of words to strip from stack (remember bytes
went on stack as words).



	Dion Hollenbeck             (619) 455-5590 x2814
	Megatek Corporation, 9645 Scranton Road, San Diego, CA  92121

                                seismo!s3sun!megatek!hollen
                                ames!scubed/

pozar@hoptoad.uucp (Tim Pozar) (10/27/88)

In article <5876@killer.DALLAS.TX.US> cy@killer.DALLAS.TX.US (Cyrus Foughty) writes:
>	I have a assembly language program from which I want to 
>	call a "C" function. I know this is backwards from what 
>	everyone else does. BTW, I am using Microsoft Masm and
>	"C" 5.1 for this little dodad. THE Microsoft manuals tell
>	how to interface an assembly language routine but not the
>	other way round. I would be forced to change ALL my assembly
>	programs to match what "C" wants and that is not an option.
>	I have way to much assembly to change. How do I do it?

        You have the information you need.  When Microsoft
	describes how to call assembly functions, what they are
	also saying is how to call C functions too.  How
	arguments are passed to assembly funtions is how they
	work for C functions.
		   Tim

-- 
 ...sun!hoptoad!\                                     Tim Pozar
                 >fidogate!pozar               Fido:  1:125/406
  ...lll-winken!/                            PaBell:  (415) 788-3904
       USNail:  KKSF / 77 Maiden Lane /  San Francisco CA 94108