[comp.os.msdos.programmer] Calling assembly routines from C

jhall@wpi.WPI.EDU (John Clinton Hall) (12/01/90)

I am writing a program to be separately assembled to be linked with a C
program.  The assembly part prints a string using the undocumented INT 29h
function call (I do know the dangers of using an undocumented function call).

The Microsoft Mixed Language Programming Guide states, "By default, C
parameters are passed by value, except for arrays, which are passed by
reference."  I will assume that C will pass the "reference" of the array to the
function.  What exactly does it mean by "reference?"  Is it a word pointing
into the data segment, or a segment address with a word pointing into the
segment?

Also, how would I define the function with extern in the C program?  Would I
use:

          extern int29( const char * );
-- 
The highest sounds are hardest to hear.  ## ## ## ##### ##  jhall@wpi.wpi.edu
Going forward is a way to retreat.       ## ## ## ## ## ##  Worcester
Great talent shows itself late in life.  ## ## ## ##### ##  Polytechnic
Even a perfect program still has bugs.   ######## ##    ##  Institute

hughes@locusts.Berkeley.EDU (Eric Hughes) (12/02/90)

In article <1990Dec1.050322.15678@wpi.WPI.EDU> jhall@wpi.WPI.EDU (John
Clinton Hall) writes:
>I am writing a program to be separately assembled to be linked with a
>C program.

Remember to prepend an underscore to the name as declared in the
assembler code so that the .OBJ names will match.

Also, if you use MS C 6.0, you don't need a separate assembler and
parameter passing is greatly simplified.

>The Microsoft Mixed Language Programming Guide states, "By default, C
>parameters are passed by value, except for arrays, which are passed
>by reference."  ...  What exactly does it mean by "reference?"

The reference is the address (lvalue) of the first element in the array.
A call by reference to a variable is the same as a call by value to the
address of the variable.  (In MS C, at least.)

>Is it a word pointing into the data segment, or a segment address
>with a word pointing into the segment?

It depends on whether it is a near or far pointer.  If you don't
explicitly declare the pointer size, it defaults to that of the memory
model which the compiler is using.  This is a dangerous practice,
since you might change memory models without remembering the implicit
size dependency.

Therefore, make sure your prototype contains the size of the parameter.

>Also, how would I define the function with extern in the C program?
>segment?  Would I use:
>	extern int29( const char * );

The 'const' keyword is superfluous.  I would use
	extern int29( char _far * ) ;

Why a far pointer?  It will work with any memory model, as the
compiler with generate four-byte pointers in small model in presence
of a prototype.

Eric Hughes
hughes@ocf.berkeley.edu

kdq@demott.COM (Kevin D. Quitt) (12/03/90)

In article <1990Dec1.050322.15678@wpi.WPI.EDU> jhall@wpi.WPI.EDU (John Clinton Hall) writes:
>
>I am writing a program to be separately assembled to be linked with a C
>program.  The assembly part prints a string using the undocumented INT 29h
>function call (I do know the dangers of using an undocumented function call).

    Write a fake version of your assembly routine in C: same calling
parameters, etc.  Make sure your fake code refers to each of the calling
parameters the way you expect to.  Compile the module with the /Fa
option, and the compiler will produce a source assembly file, suitable
for hacking.  All questions about referencing variables will be answered.



-- 
 _
Kevin D. Quitt         demott!kdq   kdq@demott.com
DeMott Electronics Co. 14707 Keswick St.   Van Nuys, CA 91405-1266
VOICE (818) 988-4975   FAX (818) 997-1190  MODEM (818) 997-4496 PEP last

                96.37% of all statistics are made up.

stever@Octopus.COM (Steve Resnick ) (12/04/90)

In article <1990Dec1.050322.15678@wpi.WPI.EDU> jhall@wpi.WPI.EDU (John Clinton Hall) writes:
>
>I am writing a program to be separately assembled to be linked with a C
>program.  The assembly part prints a string using the undocumented INT 29h
>function call (I do know the dangers of using an undocumented function call).
>
>The Microsoft Mixed Language Programming Guide states, "By default, C
>parameters are passed by value, except for arrays, which are passed by
>reference."  I will assume that C will pass the "reference" of the array to the
>function.  What exactly does it mean by "reference?"  Is it a word pointing
>into the data segment, or a segment address with a word pointing into the
>segment?

Passing by reference means the address of the object (pointer) is placed on
the stack rather than copying the object (by value). If you want to pass
an array by value, there's a trick to do it. You define the array as being
a single member in a struct, then pass the struct.

Hope this helps....
Steve

-- 
----------------------------------------------------------------------------
steve.resnick@f105.n143.z1.FIDONET.ORG - or - apple!camphq!105!steve.resnick
Flames, grammar errors, spelling errrors >/dev/nul
The Asylum OS/2 BBS - (408)263-8017 IFNA 1:143/105.0