Microsoft KB Archive/86815

= Mixed C and MASM with MASM Main Language, No C Run-Time =

Article ID: 86815

Article Last Modified on 10/17/2003

-

APPLIES TO


 * Microsoft Macro Assembler 5.0
 * Microsoft Macro Assembler 5.1 Standard Edition
 * Microsoft Macro Assembler 5.1a
 * Microsoft Macro Assembler 6.0 Standard Edition
 * Microsoft Macro Assembler 6.0a
 * Microsoft Macro Assembler 6.0b
 * Microsoft Macro Assembler 6.1 Standard Edition

-



This article was previously published under Q86815



SUMMARY
The 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:  Include the statement

.MODEL

in the assembly module to ensure that the modules use the same default segments. The will be small, medium, compact, or large. Use the END directive in the assembly module to specify the entry point. Compile the C module with /Gs to avoid generation of calls to the stack checking routine.  If the C compiler is version 5.1, include the following statement in the C module to ensure that the startup code is not brought in: int _acrtused = 0;  Assemble the assembly module with /Mx to preserve the case of nonlocal names. If MASM 6.0 or later is being used, use /Cx to preserve the case of nonlocal names.

Note that the only contributions to the stack segment come from the assembly module.



MORE INFORMATION
The 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 &quot;$&quot; terminated string MOV ah, 9 MOV dx, offset string INT 21h ENDM

.STACK .DATA

arg1   DW 1234 arg2   DW 4321 outstr1 DB &quot;Arg1: &quot;, &quot;$&quot; outstr2 DB &quot;Arg2: &quot;, &quot;$&quot; newline DB 13, 10, &quot;$&quot;           ; Carriage return-linefeed converted DD ? ; Will contain arg1 or arg2 after DB &quot;$&quot;                   ;    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 converted nextdigit: MOV   dx, 0 DIV   cx                 ; Divide ax by cx    ADD    dl, &quot;0&quot;            ; 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 &quot;Arg1: &quot; where 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 &quot;Arg2: &quot; where 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 query words: kbinf non-local 5.00 5.10 5.10a 6.00 6.00a 6.00b s_quickasm s_c s_quickc s_qcwin

Keywords: KB86815

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.