Mixed C and MASM with MASM Main Language, C Run-TimeLast reviewed: January 23, 1995Article ID: Q86816 |
The information in this article applies to:
SUMMARYThe following steps should be considered when doing mixed-language programming between the Microsoft Macro Assembler (MASM) and Microsoft C with MASM as the main language, but the C startup code is to be brought in to enable the assembly module to call a C run-time routine:
MORE INFORMATIONThe following is a mixed-language example. There is one C module and one assembly module that must be compiled and then linked together. No special link options are needed. The example declares two words, arg1 and arg2, calls the C run-time routine printf to print out their values to the screen, and passes their addresses to a C routine. The C routine swaps the values of arg1 and arg2. The values of arg1 and arg2 are printed out a second time to show that they have been swapped.
Sample Code
/* Compile options needed: none */ void ptrswap( int *ptr1, int *ptr2 ){ int temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
; Assemble options needed: /Mx
.MODEL small, c
.DATA
arg1 DW 1234
arg2 DW 4321
format1 DB "Arg1: %d", 10, 0 ; Format string for printf
format2 DB "Arg2: %d", 10, 10, 0 ; Format string for printf
EXTRN _acrtused:abs ; Bring in C startup
.CODE
EXTRN ptrswap:proc ; External C routine
EXTRN printf:proc ; External C run-time routine
PUBLIC main ; C startup requires the name _main
main:
MOV ax, arg1
PUSH ax ; Push 2nd argument (C convention)
MOV bx, offset format1
PUSH bx ; Push 1st argument (C convention)
CALL printf ; Call C run-time routine
MOV ax, arg2
PUSH ax ; Push 2nd argument (C convention)
MOV bx, offset format2
PUSH bx ; Push 1st argument (C convention)
CALL printf ; Call C run-time routine
MOV bx, offset arg2
PUSH bx ; Push 2nd argument (C convention)
MOV bx, offset arg1
PUSH bx ; Push 1st argument (C convention)
CALL ptrswap ; Call C routine from module
MOV ax, arg1
PUSH ax ; Push 2nd argument (C convention)
MOV bx, offset format1
PUSH bx ; Push 1st argument (C convention)
CALL printf ; Call C run-time routine
MOV ax, arg2
PUSH ax ; Push 2nd argument (C convention)
MOV bx, offset format2
PUSH bx ; Push 1st argument (C convention)
CALL printf ; Call C run-time routine
MOV ah, 4ch ; Terminate program
int 21h
END ; Entry point will be specified by
; the C startup code
|
Additional reference words: kbinf non-local 5.00 5.10 5.10a 6.00 6.00a
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |