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

ID Number: Q76888

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 a char from a program

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

Assembler (MASM). The MASM function also returns a char 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 llibcer;

Sample Code: C

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

// Filename: CMAIN.C

// Compile options needed: /c /AL

#include <stdio.h>

extern char far MasmSub (char) ;

main ()

{

char var = 'a';

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

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

}

Sample Code: MASM

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

; 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 char into the low byte of AX.

ADD AX, 25

POP BP

RET ; Because the function returns a char (a 1-byte

MasmSub ENDP ; value), C will get the return value from the

END ; low byte of AX.

The following is the output of the program:

a

z

Additional reference words: 6.00 6.00a 6.00ax 7.00