Tag Archives: C

Mix (C++ and Assembly) Program to Subtract Two 8 bit Numbers

While modern high-level languages like C++ abstract away many low-level operations, sometimes it’s useful to get closer to the hardware to understand how things work under the hood. This example demonstrates how to subtract two 8-bit numbers using inline assembly in a simple C++ program.

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

void main() {
    clrscr();

    short int a, b, c;

    cout << "Enter First Number:";
    cin >> a;

    cout << "Enter Second Number:";
    cin >> b;

    asm mov ax, a      // Move 'a' into AX
    asm mov ah, 00h    // Ensure AH is cleared
    asm mov bx, b      // Move 'b' into BX
    asm mov bh, 00h    // Ensure BH is cleared
    asm sub al, bl     // Subtract BL from AL
    asm mov c, ax      // Store result in 'c'

    cout << "Result:";
    cout << c;

    getch();
}
Continue reading Mix (C++ and Assembly) Program to Subtract Two 8 bit Numbers

Mix (Assembly and C++) Program to Find Greatest of Two Numbers

This program demonstrates how to compare two integers using 8086 inline assembly in C++. By leveraging assembly instructions like sub and conditional jump js, the program determines which number is greater.

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

void main() {
    clrscr();
    short a;
    short b;

    cout << "\n Enter First Number:";
    cin >> a;

    cout << "\n Enter Second Number:";
    cin >> b;

    asm mov ax, 0000h        // Clear AX
    asm mov bx, 0000h        // Clear BX
    asm mov ax, a            // Load first number into AX
    asm mov bx, b            // Load second number into BX
    asm sub ax, bx           // Subtract BX from AX
    asm js true              // Jump if result is negative (AX < BX)

    cout << "\n " << a << " is greater than " << b;
    asm jmp end              // Skip 'true' block

true:
    cout << "\n " << b << " is greater than " << a;

end:
    getch();
}
Continue reading Mix (Assembly and C++) Program to Find Greatest of Two Numbers

Mix Program in Assembly and C++ to Find Factorial of Number

This C++ program calculates the factorial of a number between 0 and 8 using inline 8086 assembly instructions. The multiplication is handled within an assembly loop, showcasing a basic yet insightful use of mul, dec, and jnz instructions.

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

void main() {
    clrscr();
    short a;
    unsigned int c;

    cout << "\n Enter Number between 0 to 8:";
    cin >> a;

    asm mov ax, 0000h
    asm mov al, 01h     // Initialize AX to 1
    asm mov cx, 0000h
    asm mov cx, a       // Set CX loop counter to input value

bac:
    asm mul cx          // Multiply AX by CX
    asm dec cx          // Decrement CX
    asm jnz bac         // Loop until CX reaches zero

    asm mov c, ax       // Move result from AX to variable c

    cout << "\n Factorial of " << a << " is " << c;
    getch();
}
Continue reading Mix Program in Assembly and C++ to Find Factorial of Number