Mixed C and MASM with MASM Main Language, No C Run-TimeLast reviewed: August 17, 1995Article ID: Q86815 |
The information in this article applies to:
SUMMARYThe following steps should be used when doing mixed-language programming between the Microsoft Macro Assembler (MASM) and Microsoft C with MASM as the main language and when no C run-time will be used:
MORE INFORMATIONThe following is a mixed-language sample. There is one C module and one assembly module that must be compiled and then linked together. No special link options are needed. The sample declares two words, arg1 and arg2, converts them to dollar-terminated strings, prints 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. NOTE: The EXTRN directive changed to EXTERN in MASM 6.1 All instances of EXTRN should be changed to EXTERN in the sample code below if you are using MASM version 6.1 or later.
Sample Code
/* Compile options needed: /Gs */ /* Uncomment the following line for C 5.1 to ensure that the startup code is not brought in: int _acrtused = 0; */ void ptrswap( int *ptr1, int *ptr2 ){ int temp; temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp;}
; Assemble options needed: /Mx
.MODEL small Display MACRO string ; Print a "$" terminated string MOV ah, 9 MOV dx, offset string INT 21h ENDM .STACK .DATA arg1 DW 1234 arg2 DW 4321outstr1 DB "Arg1: ", "$" outstr2 DB "Arg2: ", "$" newline DB 13, 10, "$" ; Carriage return-linefeed converted DD ? ; Will contain arg1 or arg2 after DB "$" ; they are converted .CODE EXTRN _ptrswap:proc ; External C routine cnvrt PROC ; ******** Convert a word to a dollar-terminated string. ******* PUSH bp ; Establish MOV bp, sp ; stack frame MOV ax, [bp+4] ; Get first argument off stack MOV bx, offset converted MOV cx, 10 MOV si, 3 ; Index for storing in convertednextdigit: MOV dx, 0 DIV cx ; Divide ax by cx ADD dl, "0" ; Convert to ASCII value MOV [bx][si], dl ; Save digit in converted DEC si ; Decrement index CMP si, 0FFFFh ; Are we done? JNZ nextdigit ADD sp, 2 ; Destroy argument RET ; Return cnvrt ENDP; ******************************************************************** start: MOV ax, @data ; Load the data MOV ds, ax ; segment register; Print "Arg1: <value>" where <value> is the value of arg1
Display outstr1 ; Use display macro MOV ax, arg1 PUSH ax ; Push argument CALL cnvrt ; Call the convert procedure Display converted ; Use display macro Display newline ; Use display macro; Print "Arg2: <value>" where <value> is the value of arg2
Display outstr2 ; Use display macro MOV ax, arg2 PUSH ax ; Push argument CALL cnvrt ; Call the convert procedure Display converted ; Use the display macro Display newline ; Use the display macro Display newline ; Use the display macro; Call the C module to swap the values of arg1 and arg2
MOV bx, offset arg2 PUSH bx ; Push the 2nd argument MOV bx, offset arg1 PUSH bx ; Push the 1st argument CALL _ptrswap ; Call the C routine; Print out arg1 again, to show that it has changed
Display outstr1 ; Use display macro MOV ax, arg1 PUSH ax ; Push argument CALL cnvrt ; Call the convert procedure Display converted ; Use display macro Display newline ; Use display macro; Print out arg2 again, to show that it has changed
Display outstr2 ; Use display macro MOV ax, arg2 PUSH ax ; Push argument CALL cnvrt ; Call the convert procedure Display converted ; Use display macro Display newline ; Use display macro MOV ah, 4ch ; Terminate program int 21h END start ; Specify entry point as _start |
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. |