INF: Passing Common C Array Types to MASM by Reference

ID Number: Q76526

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 C arrays of type char,

int, and long to a Microsoft Macro Assembler (MASM) subprogram by

reference.

More Information:

Link the two programs 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 chararray[4] = "abc\0" ;

int intarray[3] = {1, 2, 3} ;

long longarray[3] = {32768, 32769, 32770} ;

main ()

{

printf ("%s\n", chararray) ;

printf ("%d %d %d\n", intarray[0], intarray[1], intarray[2]) ;

printf ("%ld %ld %ld\n", longarray[0], longarray[1], longarray[2]) ;

MasmSub (chararray, intarray, longarray) ;

printf ("%s\n", chararray) ;

printf ("%d %d %d\n", intarray[0], intarray[1], intarray[2]) ;

printf ("%ld %ld %ld", longarray[0], longarray[1], longarray[2]) ;

}

Sample Code 2

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

; Filename: MASMSUB.ASM

; Assemble options needed: /mx /ml

.MODEL LARGE, C ; Since C pushes parameters from

.CODE ; right to left, the address of

PUBLIC MasmSub ; long array will be pushed first,

MasmSub PROC FAR ; and will thus be farthest from

; the base pointer (BP).

PUSH BP

MOV BP, SP

PUSH SI

MOV AX, [BP+8] ; Load address of char array into

MOV ES, AX ; ES:SI. Since a char is 1 byte

MOV SI, [BP+6] ; long, each successive element in

MOV BYTE PTR ES:[SI], "x" ; the array can be accessed by

MOV BYTE PTR ES:[SI+1], "y" ; adding 1 more to SI.

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

MOV AX, [BP+12] ; Load address of int array into

MOV ES, AX ; ES:SI. Since an int is 2 bytes

MOV SI, [BP+10] ; long, each successive element in

ADD WORD PTR ES:[SI], 7 ; the array can be accessed by

ADD WORD PTR ES:[SI+2], 7 ; adding 2 more to SI.

ADD WORD PTR ES:[SI+4], 7

MOV AX, [BP+16] ; Load address of long array into

MOV ES, AX ; ES:SI. Since a long is 4 bytes

MOV SI, [BP+14] ; long, the low word of each

INC WORD PTR ES:[SI] ; successive element in the array

INC WORD PTR ES:[SI+4] ; can be accessed by adding 4 more

INC WORD PTR ES:[SI+8] ; to SI (or 4 more to SI+2 to access

; the high word of each element).

POP SI

POP BP

RET

MasmSub ENDP

END

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