Calling C/C++ Functions from Assembly Language: Multiplication Table Example

Click the blue text
Calling C/C++ Functions from Assembly Language: Multiplication Table Example
Follow us

Source from the Internet, please delete if infringing

Now let’s write a simple application that prompts the user to input an integer and multiplies it by powers of 2 (2¹ to 2ⁿ) using bit shifting, displaying each product with leading spaces. The input-output will use C++. The assembly module will call three functions written in C++ and the program will be initiated by the C++ module.

Assembly Language Module

The assembly module contains a function DisplayTable. It calls the C++ function askForInteger to get an integer from the user. It also uses a loop structure to repeatedly left shift the integer intVal and calls showInt to display it.

; C++ calls ASM function.INCLUDE Irvine32.inc; external C++ function askForInteger PROTO CshowInt PROTO C, value:SDWORD, outWidth:DWORDOUT_WIDTH = 8ENDING_POWER = 10.dataintVal DWORD ?.code;---------------------------------------------SetTextOutColor PROC C,    color:DWORD;; Set text color and clear console window; call Irvine32 library function;---------------------------------------------    mov    eax,color    call    SetTextColor    call    Clrscr    retSetTextOutColor ENDP;---------------------------------------------DisplayTable PROC C;; Input an integer n and display the multiplication table from n * 2^1 to n * 2^10;----------------------------------------------    INVOKE askForInteger                 ; Call C++ function    mov    intVal,eax                    ; Save integer    mov    ecx,ENDING_POWER              ; Loop counterL1:    push ecx                          ; Save loop counter    shl  intVal,1                        ; Multiply by 2    INVOKE showInt,intVal,OUT_WIDTH    call    Crlf    pop    ecx                           ; Restore loop counter    loop    L1    retDisplayTable ENDPEND

In the DisplayTable process, ECX must be pushed onto the stack before calling showInt and newLine, and popped off afterward, as Visual C++ functions do not save and restore general-purpose registers. The function askForInteger returns the result in the EAX register.

DisplayTable does not necessarily have to use INVOKE when calling C++ functions. The PUSH and CALL instructions can achieve the same result. The call to showInt is as follows:

push OUT_WIDTH  ; Last parameter pushed firstpush intValcall showInt             ; Call functionadd esp,8                ; Clear stack

It is necessary to adhere to the C calling convention, where parameters are pushed onto the stack in reverse order, and the caller is responsible for removing the actual parameters from the stack after the call.

C++ Test Program

Below is the C++ module that starts the program. Its entry point is main(), ensuring the required C++ initialization code is executed. It includes prototypes for the external assembly procedures and three output functions:

// main.cpp// Demonstration of C++ program and external assembly module function calls#include <iostream>#include <iomanip>using namespace std;extern "C" {    // External ASM procedures:    void DisplayTable();    void SetTextOutColor( unsigned color );    // Local C++ functions:    int askForInteger();    void showInt( int value, int width );}// Program entryint main(){    SetTextOutColor( 0x1E );       // Blue background yellow text    DisplayTable();                // Call ASM procedure    return 0;}// Prompt user to input an integerint askForInteger(){    int n;    cout &lt;&lt; "Enter an integer between 1 and 90,000: ";    cin &gt;&gt; n;    return n;}// Display a signed integer with a specific widthvoid showInt( int value, int width ){    cout &lt;&lt; setw(width) &lt;&lt; value;}</iomanip></iostream>

Generating the Project

Add the C++ and assembly modules to the Visual Studio project, and select Build Solution from the Project menu.

Program Output

When the user inputs 90,000, the output generated by the multiplication table program is as follows:Calling C/C++ Functions from Assembly Language: Multiplication Table Example

Visual Studio Project Properties

If you use Visual Studio to generate a program that integrates C++ and assembly code and calls the Irvine32 linking library, you need to modify some project settings. For the Multiplication_Table program, for example.In the Project menu, select Properties, then under Configuration Properties on the left, select Linker. In the right panel’s Additional Library Directories entry, enter c:\Irvine.As shown in the figure below. Click OK to close the Project Property Pages window. Now Visual Studio can find the Irvine32 linking library.

Calling C/C++ Functions from Assembly Language: Multiplication Table Example


If you are over 18 and find learning 【C Language】 too difficult? Want to try another programming language? Then I recommend you learn Python, with a current value of 499 yuan Python zero-based course available for free, limited to 10 places!



▲ Scan the QR code - Get it for free


Calling C/C++ Functions from Assembly Language: Multiplication Table Example
Click to read the original text for more information

Leave a Comment