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(); }