Calling C/C++ Functions (Cont.)
Function Return Values
The following list shows how Microsoft C++ functions return values:
bool
and char
values are returned in AL
.
short int
values are returned in AX
.
int
and long int
values are returned in EAX
.
- Pointers are returned in
EAX
.
float
, double
, and long double
values are pushed on the floating-point stack as 4-, 8-, and 10-byte values, respectively.
Code Example—Finding all Factors
The following application prompts the user for an integer, finds all its factors, and displays each factor with leading padded spaces.
The assembly language module will contain calls to two functions, for input-output, written in C++.
The program will be launched from C++, main.cpp
:
- The C++ function
main( )
is the entry point of main.cpp
.
It ensures the execution of required C++ language initialization code.
- The assembly procedure
DisplayFactors
performs the following tasks:
- Calls
askForInteger
that inputs an integer n
from the user.
- Uses a loop to repeatedly divide the input by the index from
n
to 1.
- Displays the divisor by calling
showInt
if the remainder is zero.
ECX
must be pushed and popped before calling showInt
because Visual C++ functions may use general-purpose registers and do not save and restore them.
- The C++ function
askForInteger( )
returns its result in the EAX
register.
- The C++ function
showInt
displays an integer with a specified width by using setw.