Assembly language is still used widely to configure hardware devices and optimize both the speed and code size of programs.
For external procedures in assembly language that can be called from C and C++ programs, such programs consist of at least two modules:
- The first, written in assembly language, contains the external procedure;
- The second contains the C/C++ code that starts and ends the program.
Arguments
Arguments are passed by C/C++ programs from right to left, as they appear in the argument list.
External Names
In the assembly language source, specify the C calling convention in the .MODEL
directive and create a prototype for each procedure called from an external C/C++ program.
For example,
.586
.model flat, C
Encript PROTO, buf:PTR BYTE, count:DWORD, eChar:BYTE
Declaring the Function
In a C program, use the extern
qualifier when declaring an external assembly language procedure.
For example, this is how to declare Encript
:
extern void Encript( char* buf, long count, char eChar );
If the procedure will be called from a C++ program, add a ‘C’ qualifier to prevent C++ name decoration:
extern "C" void Encript( char* buf, long count, char eChar );
Name decoration is a standard C++ compiler technique that involves modifying a function with extra characters that indicate the exact type of each function parameter.