Mix (C++ and Assembly) Program to Find Largest Number from Given Numbers

This C++ program demonstrates how to find the largest element from a set of five integers using 8086 assembly instructions embedded directly into the code. The inline assembly helps perform comparisons and updates the result with the highest value found.

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

void main()
{
    int a[5], x, y, res;
    int i, j;
    y = 0;

    cout << "\n Enter 5 Numbers:";
    for (i = 0; i < 5; i++) {
        cin >> a[i];
    }

    _asm {
        mov bx, y
    }

    // Finding largest
    for (i = 0; i < 5; i++) {
        x = a[i];
        _asm {
            mov ax, x      // Move current number into AX
            mov bx, y      // Load current max value into BX
            cmp ax, bx     // Compare current number with max
            jl nxt         // If current is less, skip
            mov bx, ax     // Else, update max
            mov y, bx      // Save new max to y
        }
    nxt:
    }

    _asm {
        mov res, bx       // Store final max in result
    }

    cout << "\n Largest Element:" << res;
    getch();
}

Understanding the Code

Variable Declarations

  • int a[5] → Array to hold 5 integers.
  • int x, y → Used for comparisons. y holds the current maximum value.
  • int res → Stores the result (largest number).

User Input
The program prompts the user to input 5 integers which are stored in the array a.

Inline Assembly Instructions

  • mov bx, y → Initialize BX with 0 (current max value).

Loop with Inline Assembly for Comparison For each element in the array:

  • mov ax, x → Move the current array element to register AX.
  • mov bx, y → Load current largest value from y.
  • cmp ax, bx → Compare the current number with the current largest.
  • jl nxt → If AX is less than BX, skip update.
  • mov bx, ax → If AX is greater or equal, move it to BX.
  • mov y, bx → Save updated maximum value.

After the loop:

  • mov res, bx → Copy the largest number into res for output.

Output Display
The largest number is printed using cout.


Output

Enter 5 Numbers:10
12
8
24
16
Largest Element:24

Output Explanation

After comparing all five input numbers, the program uses inline assembly to track and store the highest value found in the array. Here, among 10, 12, 8, 24, 16, the largest is 24.

Leave a Reply

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