You can write assembly language programs that call C++ functions.
There are at least a couple of reasons for doing so:
- Input-output is more flexible under C++, with its rich iostream library.
For example,
- cin represents the standard input stream.
- cout represents the standard output stream.
- C++ has extensive math libraries.
When calling functions from the standard C/C++ library, you must start the program from a C/C++ main( )
procedure to allow library initialization code to run.
Function Prototypes
C++ functions called from assembly language code must be defined with the "C"
and extern
keywords.
Here is the basic syntax:
extern "C" funName( paramlist ) {
... }
Here is an example:
extern "C" int askForInteger( ) {
cout << "Please enter an integer:";
// ...
}
Rather than modifying every function definition, it's easier to group multiple function prototypes inside a block.
Then you can omit extern
and "C"
from the function implementations:
extern "C" {
int askForInteger( );
int showInt( int value, unsigned outWidth );
etc.
}