INF: Passing Common C Types to MASM by Reference

ID Number: Q76507

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

MS-DOS | OS/2

Summary:

The sample code below demonstrates how to pass variables of type char,

int, and long from a Microsoft C program to a Microsoft Macro

Assembler (MASM) program by reference.

More Information:

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 void far MasmSub (char far*, int far*, long far*) ;

char charvar = 'a' ;

int intvar = 1 ;

long longvar = 32768 ;

main ()

{

printf ("%c %d %ld\n", charvar, intvar, longvar) ;

MasmSub (&charvar, &intvar, &longvar) ;

printf ("%c %d %ld", charvar, intvar, longvar) ;

}

Sample Code 2

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

; Filename: MASMSUB.ASM

; Assemble options needed: /mx /ml

.MODEL LARGE, C ; Remember that C pushes parameters

.CODE ; from right to left, so the address

PUBLIC MasmSub ; of the long parameter will be pushed

MasmSub PROC FAR ; first, making it farthest from the

; base pointer (BP);

PUSH BP

MOV BP, SP

PUSH AX

PUSH ES

PUSH SI

MOV AX, [BP+8] ; Load ES:SI with the address of the

MOV ES, AX ; char variable.

MOV SI, [BP+6]

MOV BYTE PTR ES:[SI], "z"

MOV AX, [BP+12] ; Load ES:SI with the address of the

MOV ES, AX ; int variable.

MOV SI, [BP+10]

ADD WORD PTR ES:[SI], 9

MOV AX, [BP+16] ; Load ES:SI with the address of the

MOV ES, AX ; long variable.

MOV SI, [BP+14]

INC WORD PTR ES:[SI] ; Increment the low word of the long

; variable by 1.

POP SI

POP ES

POP AX

POP BP

RET

MasmSub ENDP

END

The following is the output of the program:

a 1 32768

z 10 32769

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