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

This C++ program demonstrates how to find the smallest number from an array using inline 8086 assembly language instructions. The logic involves comparing each array element and storing the smallest found so far using cmp and conditional jump instructions.

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

void main()
{
    short a[5], x, y, res;
    short i, j;
    y = 999; // Initialize with a large number

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

    asm {
        mov bx, y
    }

    // Finding smallest
    for (i = 0; i < 5; i++) {
        x = a[i];
        asm {
            mov ax, x
            mov bx, y
            cmp ax, bx
            jnb nxt   // Jump if not below (i.e., current is not smaller)
            mov bx, ax
            mov y, bx
        }
    nxt:
    }

    asm {
        mov res, bx;
    }

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

Understanding the Code

Variable Declarations

  • a[5] → Array to hold five short integers.
  • x, y, res → Temporary variables for holding current and smallest values.
  • i, j → Loop counters.

User Input
The user is prompted to input 5 integer values which are stored in the array a.

Assembly Instructions Explanation

  • mov bx, y → Stores the initial smallest value in register BX.
  • mov ax, x → Loads the current array element into AX.
  • cmp ax, bx → Compares the current element with the current smallest.
  • jnb nxt → If current is not smaller, jump to next iteration.
  • mov bx, ax → Update BX with the new smallest value.
  • mov y, bx → Reflect the change back into variable y.
  • mov res, bx → Move the final smallest value into result variable.

Result Output
After looping through all array elements, the smallest value is displayed using cout.


Output

Enter 5 Numbers:15
20
10
8
24
Smallest Element:8

Output Explanation

From the entered list [15, 20, 10, 8, 24], the smallest number is 8. The inline assembly helps with efficient comparison to determine this result.

Leave a Reply

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