Mix (C++ and Assembly) Program to Perform Signed & Unsigned Multiplication and Division

This post walks through a C++ program that demonstrates how to perform basic arithmetic operations like multiplication and division using inline assembly. It allows the user to choose between signed/unsigned multiplication and division in a loop until they decide to exit.

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

void main() {
    clrscr();
    int a, b, c, j;
    char m, Y, N, y, n;

    do {
        cout << "1.unsigned multiplication:" << "\n";
        cout << "2.signed multiplication:" << "\n";
        cout << "3.unsigned division:" << "\n";
        cout << "4.signed divsion:" << "\n";
        cout << "Enter ur choice";
        cin >> j;

        switch(j) {
        case 1:
            cout << "Enter the first no.:" << "\n";
            cin >> a;
            cout << "Enter the second no.:" << "\n";
            cin >> b;
            if(a < 0 || b < 0) {
                cout << " only positive no.s are accepted ";
            } else {
                asm mov ax, a;
                asm mov bx, b;
                asm mul bx;
                asm mov c, ax;
                cout << "result=" << c << "\n";
            }
            break;

        case 2:
            cout << "Enter the first no.";
            cin >> a;
            cout << "Enter the second no.";
            cin >> b;
            asm mov ax, a;
            asm mov bx, b;
            asm imul bx;
            asm mov c, ax;
            cout << "result=" << c << "\n";
            break;

        case 3:
            cout << "Enter the first no.";
            cin >> a;
            cout << "Enter the second no.";
            cin >> b;
            if(a < 0 || b < 0) {
                cout << " only positive no.s are accepted ";
            } else {
                asm mov ax, a;
                asm mov bx, b;
                asm div bx;
                asm mov c, ax;
                cout << "result=" << c << "\n";
            }
            break;

        case 4:
            cout << "Enter the first no.";
            cin >> a;
            cout << "Enter the second no.";
            cin >> b;
            asm mov ax, a;
            asm mov bx, b;
            asm idiv bx;
            asm mov c, ax;
            cout << "result=" << c << "\n";
            break;
        }

        cout << "Do you want to continue?(Y/N):\n";
        cin >> m;
    } while (m == 'y' || m == 'Y');

    getch();
}

Understanding the Code

Variable Declarations

int a, b, c, j; → Declares variables to store user inputs and results. char m is used for loop control.

Loop and Menu Interface

The program uses a do-while loop to repeatedly present a menu and perform selected operations until the user chooses to exit.


Case-by-Case Explanation

Case 1: Unsigned Multiplication

User Input

The program takes two positive integers.

Inline Assembly Operations

  • asm mov ax, a → Moves the value of a into AX.
  • asm mov bx, b → Moves the value of b into BX.
  • asm mul bx → Performs unsigned multiplication of AX and BX. The result is stored in AX.
  • asm mov c, ax → Moves the result to c.

Output Display

Displays the result using cout.


Case 2: Signed Multiplication

User Input

Two integers are accepted, including negative values.

Inline Assembly Operations

  • asm mov ax, a → Loads first number.
  • asm mov bx, b → Loads second number.
  • asm imul bx → Performs signed multiplication.
  • asm mov c, ax → Stores result into c.

Output Display

The signed result is displayed.


Case 3: Unsigned Division

User Input

Only positive numbers are accepted.

Inline Assembly Operations

  • asm mov ax, a → Moves dividend into AX.
  • asm mov bx, b → Moves divisor into BX.
  • asm div bx → Performs unsigned division.
  • asm mov c, ax → Stores quotient in c.

Output Display

Displays the quotient.


Case 4: Signed Division

User Input

Accepts both positive and negative integers.

Inline Assembly Operations

  • asm mov ax, a → Loads dividend.
  • asm mov bx, b → Loads divisor.
  • asm idiv bx → Performs signed division.
  • asm mov c, ax → Stores quotient in c.

Output Display

Displays the signed result.


Sample Output

1.unsigned multiplication:
2.signed multiplication:
3.unsigned division:
4.signed divsion:
Enter ur choice2
Enter the first no.-2
Enter the second no.5
result=-10
Do you want to continue?(Y/N):
Y
1.unsigned multiplication:
2.signed multiplication:
3.unsigned division:
4.signed divsion:
Enter ur choice1
Enter the first no.:
5
Enter the second no.:
7
result=35
Do you want to continue?(Y/N):
N

This interactive program provides a hands-on example of how arithmetic operations can be handled directly with assembly instructions in C++.

Leave a Reply

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