Tag Archives: MP

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

Implementing JUMP, PUSH, POP, IN & OUT in Assembly Program on 8086

This program is a sandbox for five instructions you’ll encounter constantly in real 8086 code: unconditional jump (JMP), stack push and pop, and port I/O (IN/OUT). Rather than demonstrating each in isolation, the program weaves them together into a two-iteration loop that modifies variables, saves and restores registers through the stack, then reads from and writes to an I/O port at the end. It’s a useful reference for anyone trying to understand how these instructions interact.

Continue reading Implementing JUMP, PUSH, POP, IN & OUT in Assembly Program on 8086

8086 Assembly Program to Display String ‘hello’

There are two ways to print a string in 8086 DOS assembly. One is the clean, one-call approach using INT 21h function 09h. The other — shown here — is the character-by-character loop using function 02h. It’s more verbose, but it gives you a clear picture of how string output actually works at the hardware level: load a character into DL, call INT 21h, move to the next character, repeat. Once you understand this version, the shortcut function 09h makes a lot more sense.

Continue reading 8086 Assembly Program to Display String ‘hello’

8086 Assembly Program for Subtraction of Two 8-bit Numbers

8-bit subtraction on the 8086 follows the same register-first pattern as 16-bit subtraction — but with one extra gotcha to watch: because only AL is involved in the computation, AH retains whatever value it had from the segment setup code. When you later store the full AX into the result variable, that dirty AH comes along for the ride. The emu8086 and NASM versions below fix this with a single xor ah, ah. This post walks through a working implementation in three environments: MASM/TASM, emu8086, and NASM.

Continue reading 8086 Assembly Program for Subtraction of Two 8-bit Numbers

8086 Assembly Program for Multiplication of Two 8-bit Numbers

8-bit multiplication on the 8086 is simpler than its 16-bit counterpart in one important way: the result always fits in a single register pair. MUL with an 8-bit operand multiplies the implicit AL register by that operand and places the full 16-bit result in AX — no separate DX needed. This post walks through a working implementation in three environments: MASM/TASM, emu8086, and NASM.

Continue reading 8086 Assembly Program for Multiplication of Two 8-bit Numbers

8086 Assembly Program for Division of Two 8-bit Numbers

8-bit division in 8086 assembly has a subtle but important difference from 16-bit division: the dividend is always the full 16-bit AX register, not just AL. When you write DIV b with a byte operand, the CPU divides the 16-bit value in AX by that byte — quotient lands in AL, remainder in AH. Getting AH cleared to zero before the divide is therefore essential for a correct result. This post walks through a working implementation in three environments: MASM/TASM, emu8086, and NASM.

Continue reading 8086 Assembly Program for Division of Two 8-bit Numbers

8086 Assembly Program to Add Two 32-bit Numbers

Adding two 32-bit numbers on the 16-bit 8086 requires two passes: first add the lower 16-bit words, then add the upper 16-bit words using ADC (Add with Carry) to fold in any carry from the first pass. The carry propagation between the two halves is what makes multi-word arithmetic correct — skip it and the upper half of your result will be wrong whenever the lower addition overflows. This post walks through a working implementation in three environments: MASM/TASM, emu8086, and NASM.

Continue reading 8086 Assembly Program to Add Two 32-bit Numbers