Mix (C++ and Assembly) Program to Find Square/Cube/Factorial of a Number

This program demonstrates how to calculate the square, cube, or factorial of a number using 8086 inline assembly in C++. It presents a menu-based approach for the user to choose an operation and performs the calculation using the mul instruction in assembly.

#include<iostream.h>
#include<conio.h>

void main() {
    clrscr();
    int a, b, ch, c;
    char x;

    do {
        cout << "Select an option:" << "\n";
        cout << "1.SQUARE" << "\n";
        cout << "2.CUBE" << "\n";
        cout << "3.FACT" << "\n";
        cin >> ch;

        switch(ch) {
            case 1:
                cout << "Enter a number:";
                cin >> a;
                asm mov ax, a
                asm mul ax
                asm mov c, ax
                cout << "The square is:" << c << "\n";
                cout << "Do you want to continue?" << endl;
                cin >> x;
                break;

            case 2:
                cout << "Enter a number:";
                cin >> a;
                asm mov ax, a
                asm mov bx, a
                for(int i = 0; i <= 1; i++) {
                    asm mul bx
                }
                asm mov c, ax
                cout << "The cube is:" << c << "\n";
                cout << "Do you want to continue?" << endl;
                cin >> x;
                break;

            case 3:
                cout << "Enter a number:";
                cin >> a;
                b = a - 1;
                asm mov ax, a
                for(; b > 0; b--) {
                    asm mov bx, b
                    asm mul bx
                }
                asm mov c, ax
                cout << "The fact is:" << c << "\n";
                cout << "Do you want to continue?" << endl;
                cin >> x;
        }

    } while(x == 'y' || x == 'Y');

    getch();
}

Understanding the Code

Options Provided:

  • Option 1 (SQUARE):
    • Prompts the user for a number.
    • Loads the value into AX register and squares it using the MUL instruction (AX * AX).
    • The result is stored back in variable c.
  • Option 2 (CUBE):
    • Prompts the user for a number.
    • Loads the number into AX and BX, then multiplies it twice using a loop to compute AX = A * A * A.
    • The result is saved in c.
  • Option 3 (FACTORIAL):
    • Prompts the user for a number.
    • Sets a loop from the input number down to 1, multiplying each decrementing value.
    • Effectively calculates the factorial AX = A * (A-1) * (A-2) * ... * 1.

Assembly Instructions Used:

  • mov ax, a – Loads the input value into the AX register.
  • mov bx, b – Loads a secondary value into the BX register.
  • mul bx – Multiplies AX with BX and stores result in AX.
  • mov c, ax – Moves result back to a C++ variable for display.

Output Example

Select an option:
1.SQUARE
2.CUBE
3.FACT
1
Enter a number:2
The square is:4
Do you want to continue? y

Select an option:
1.SQUARE
2.CUBE
3.FACT
2
Enter a number:3
The cube is:27
Do you want to continue? y

Select an option:
1.SQUARE
2.CUBE
3.FACT
3
Enter a number:5
The fact is:120
Do you want to continue? n

Output Explanation

  • Square of 2: 2 * 2 = 4
  • Cube of 3: 3 * 3 * 3 = 27
  • Factorial of 5: 5 * 4 * 3 * 2 * 1 = 120

This simple interactive program is a great way to learn how arithmetic operations can be performed with inline assembly directly in C++ code. It offers insight into how low-level instructions work behind high-level operations.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.