INF: Passing an int from C to MASM by Value & Returning an int

ID Number: Q76887

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 an int from a program

written in Microsoft C to a function written with the Microsoft Macro

Assembler (MASM). The MASM function will also return an int 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 int far MasmSub (int) ;

main ()

{

int var = 1;

printf ("%d\n", var) ;

printf ("%d", MasmSub(var)) ;

}

Sample Code 2

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

; Filename: MASMSUB.ASM

; Assemble options needed: /mx /ml

.MODEL LARGE, C

.CODE

PUBLIC MasmSub

MasmSub PROC FAR

PUSH BP

MOV BP, SP

MOV AX, [BP+6] ; Load the int into AX.

ADD AX, 32766 ; Because the function returns an int (a 2-byte

POP BP ; value), C will get the return value from AX.

RET

MasmSub ENDP

END

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