Q59438: Assembly Main Module Calling a C Function
Article: Q59438
Product(s): See article
Version(s): 5.10
Operating System(s): MS-DOS
Keyword(s): ENDUSER | S_QuickASM | mspl13_masm
Last Modified: 29-MAR-1990
It is possible to call C function from an Assembly main program. It is
not recommended because the functionality of the C code will be
limited. Since most C run-time functions call either the C startup
code or the C stack check routine, it usually is not possible to make
any C run-time calls.
When compiling the C source code, it is important to use the /Gs
switch to disable stack checking and add the following line to the C
source code to tell the compiler/linker to not link in the startup
code.
int _acrtused = 0;
The following is sample code to demonstrate how to write an Assembly
main module calling a C function.
Code Example
------------
; Assembly switch: MASM /Mx
.MODEL MEDIUM,C
EXTRN ptrmsg:PROC
.STACK 100h
.DATA
arg1 DB 65
arg2 DW 5
.CODE
main PROC
mov ax, @data ; Set up DGROUP
mov ds, ax ; DS pointing to DGROUP
xor ax, ax ; Clear ax register
mov ax, OFFSET arg2
push ax ; Push second argument
mov ax, OFFSET arg1
push ax ; Push first argument
call ptrmsg ; Call C function
add sp,4 ; Restore stack pointer
mov ax, 4C00h
int 21h ; Terminate program
main ENDP
END
-------------------------------
/* Compiler line: cl /Gs /c /AM */
int _acrtused = 0; /* Do not bring in the startup code */
void ptrmsg (char *a, int *b)
{
*a += 1;
*b += 1;
}
THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
Copyright Microsoft Corporation 1986-2002.