[comp.sys.mac.programmer] passing correct var size in MPW C/C++

marc@Apple.COM (Mark Dawson) (07/25/90)

I wrote a routine in assembly that I wanted to call from my C++ code, it
didn't end up working the way I expected--the C++ call kept passing a long,
when my assembly was expecting a short (2 bytes).  I've of course modifed
my assembly to expect a long, but I'd like to know where my thinking went
wrong.

extern "C" void myProc(short x);  // external declaration

....
short vRefNum = -2;
myProc(vRefNum);  // I also tried myProc((short)vRefNum) and got the same res
...
--------
myProc	PROC  EXPORT
        ....
        move.l	(sp)+,A0	// pop return @ off stack
        move.w	(sp)+,D0	// get vRefNum off stack
       ....
        ENDP

--------
When I did a debugger trap in myProc(), the stack always had an $FFFF FFFE,
not the $FFFE that I expected--it seemed to be passing 4 bytes instead of two.
Why?

Thanks,

Mark


-- 
---------------------------------
Mark Dawson                Service Diagnostic Engineering
AppleLink: Dawson.M

Apple says what it says; I say what I say.  We're different
---------------------------------

chi@tybalt.caltech.edu (Curt Hagenlocher) (07/25/90)

In article <43315@apple.Apple.COM> marc@Apple.COM (Mark Dawson) writes:
>I wrote a routine in assembly that I wanted to call from my C++ code, it
>didn't end up working the way I expected--the C++ call kept passing a long,
>when my assembly was expecting a short (2 bytes).  I've of course modifed
>my assembly to expect a long, but I'd like to know where my thinking went
>wrong.
>
>(code deleted)
>
>When I did a debugger trap in myProc(), the stack always had an $FFFF FFFE,
>not the $FFFE that I expected--it seemed to be passing 4 bytes instead of two.
>Why?
>

MPW C always converts everything to 32 bits that's less than
32 bits in length.  This includes characters and shorts.
The information is put into the low (byte/word) and sign extended
to make a long integer.

I imagine this is done so that everything stays longword aligned.

This information can be found in the chapter of the MPW C manual
entitled ``Calling Conventions.''  I believe it is Appendix B.

---
chi@tybalt.caltech.edu
Curt Hagenlocher

beard@ux5.lbl.gov (Patrick C Beard) (07/26/90)

In article <1990Jul24.235007.20545@laguna.ccsf.caltech.edu> chi@tybalt.caltech.edu (Curt Hagenlocher) writes:
#In article <43315@apple.Apple.COM> marc@Apple.COM (Mark Dawson) writes:
#>I wrote a routine in assembly that I wanted to call from my C++ code, it
#>didn't end up working the way I expected--the C++ call kept passing a long,
#>when my assembly was expecting a short (2 bytes).  I've of course modifed
#>my assembly to expect a long, but I'd like to know where my thinking went
#>wrong.
#>
#
#MPW C always converts everything to 32 bits that's less than
#32 bits in length.  This includes characters and shorts.
#The information is put into the low (byte/word) and sign extended
#to make a long integer.

This is correct.  For all routines that use C calling conventions, all
types are pushed as 32-bit quantities.  One way to use smaller size parameters
is to declare a routine with "pascal" calling conventions.  So, if you
really only want to push a word, declare your routine as:

extern pascal void MyRoutine(short arg);

And C++, or C will only push a 2-byte integer on the stack.



--
-------------------------------------------------------------------------------
-  Patrick Beard, Macintosh Programmer                        (beard@lbl.gov) -
-  Berkeley Systems, Inc.  ".......<dead air>.......Good day!" - Paul Harvey  -
-------------------------------------------------------------------------------