| Previous | Table of Contents | Next |
INTRODUCTION
This section discusses how to make Assembly language code work with a Microsoft C language program. It explains some of the internal workings of the C language and some fundamental ideas about a compiled C language programs data area. It is necessary to understand these concepts in order to write Assembly language routines that can interface efficiently with a C language program. The Microsoft Macro Assembler development system provides many functions for writing Assembly language code to interface to C language code and other high level languages. The Microsoft Assembler system allows for easy development of Assembly language code that links to a C language program.
C LANGUAGE DATA AREAS
The data for C code functions and routines is based on heap and stack structures. To explain this in a simple way, assume that a program is assigned one contiguous block of memory to use upon execution. The code of the program is loaded into the lowest address space of the block of memory. The heap data is loaded directly above the code in lower memory and expands up as necessary for more heap data space. The stack data starts at the top of the memory block and expands down towards the heap data as more stack data space is needed. Let us hope that the heap data and the stack data shall never meet because this may crash a program.
A general method to access data passed to a routine from a standard C function is to use the BP register. The following code segment shows how to define BP at the start of an Assembly language code function and addresses the parameters passed from a C language program.
.MODEL small
.CODE
PUBLIC _Test_Asm
_Test_Asm PROC
push bp ;save old BP value
mov bp,sp ;set BP to index local data
mov ax,[bp+4] ;load AX with parameter 1
add ax,[bp+6] ;add parameter 2 to AX
;exit procedure
pop bp ;restore old BP value
ret ;exit to C program with AX = P1 + P2
_Test_Asm ENDP
END
The following is an example of a C language code routine to execute the _Test_Asm Assembly language function.
extern "C" { int Test_Asm( int, int); }
main() {
int sum;
Sum = Test_Asm(1, 2)
}
MEMORY MODELS
When programs become large and either the code, stack, or data segments grow to exceed 64KB, then certain problems develop. Because of speed along with code size problems and 64KB problems, the Microsoft Macro Assembler system provides several basic memory models for code development. They are: tiny, small, medium, compact, large, huge, and flat. The basic differences between the different memory models have to do with assumptions about the data being addressed with near or far pointers and code routines being called as near routines or as far routines. Many of the problems are solved with special compiler functions provided in a Microsoft C development system.
The following is a list of C data types and the associated Assembler data types.
| char | byte or sbyte |
| unsigned char | byte |
| signed char | sbyte |
| short | sword |
| unsigned short | word |
| int | sword |
| unsigned int | word |
| long | sdword |
| unsigned long | dword |
| float | real4 |
| double | real8 |
| long double | real10 |
| int *datapt | ptr sword |
| int far *datapt | far ptr sword |
The following is a list of C language function return data types and the Assembly language registers that the data types use.
| unsigned char | AL |
| char | AL |
| unsigned short | AX |
| short | AX |
| unsigned int | AX |
| int | AX |
| unsigned long | DX:AX |
| long | DX:AX |
| float | PTR AX or PTR DX:AX |
| double | PTR AX or PTR DX:AX |
| long double | PTR AX or PTR DX:AX |
The Microsoft Assembler development system provides a utility for converting C include files into MASM include files. The utility is called H2INC.
COMPILING C AND ASSEMBLY TOGETHER
The Microsoft Linker provides convenient ways to combine compiled C language programs with Assembly language routines. The programmer can link the two program object files together with a single link statement as in the following example.
In order to use this method, you must have assembled the Assembly code with the /c option on the ML command line. The files source1 and source2 must have .OBJ filename extensions.
UNDERSCORES AND NAMING CONVENTIONS
The Assembly language code needs to add an underscore character to the start of function names to be called from a C language program. For example, if the C language program makes a call to an Assembly language function called asm_fun in the C language program, then the Assembly language program should use the name _asm_fun for the Assembly language code. The name of the Assembly language function that the C language program calls must be case sensitive. The compiler option /ml can be used to make an Assembly language program case sensitive. The Assembly language function name needs to be declared as PUBLIC.
| Previous | Table of Contents | Next |