INF: Passing a long from C to MASM by Value & Returning a long

ID Number: Q76886

5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.00 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

The sample code below demonstrates how to pass two longs from a

program written in Microsoft C to a function written with Microsoft

Macro Assembler (MASM). The program will also return a long to the C

program.

More Information:

Registers are used to pass the return values of simple data types. Use

the following conventions for returning data to a C program:

char AL

int, short, near AX

long, far DX: High order portion (segment)

AX: Low order portion (offset)

Link the two programs below with the following command:

link cmain masmsub,,, /nod llibce;

Sample Code 1

-------------

// Filename: CMAIN.C

// Compile options needed: /c /AL

#include <stdio.h>

extern long far MasmSub (long, long) ;

main ()

{

long var1 = 98304, var2 = 147456 ;

printf ("%ld + %ld = %ld", var1, var2, MasmSub (var1, var2)) ;

}

Sample Code 2

-------------

; Filename: MASMSUB.ASM

; Assemble options needed: /mx /ml

.MODEL LARGE, C ; Remember that C pushes parameters from right

.CODE ; to left, so the second long will be pushed

PUBLIC MasmSub ; first, and thus be farthest away from the base

MasmSub PROC FAR ; pointer (BP).

PUSH BP

MOV BP, SP

PUSH BX

PUSH CX

MOV DX, [BP+12] ; Load the high word of the second long into DX.

MOV AX, [BP+10] ; Load the low word of the second long into AX.

MOV BX, [BP+8] ; Load the high word of the first long into BX.

MOV CX, [BP+6] ; Load the low word of the first long into CX.

ADD DX, BX ; Add the two high words of the longs together.

ADD AX, CX ; Add the two low words of the longs together.

POP CX

POP BX

POP BP

RET ; Since the function returns a long, C will get

MasmSub ENDP ; the high word of the return value from DX, and

END ; the low word from AX.

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00