Category Archives: 8086

8086 Assembly Program to Compute the Greatest Common Divisor (GCD) of Two 16-bit Numbers Using Euclidean Algorithm

In this blog post, we’ll explore an 8086 assembly program that computes the Greatest Common Divisor (GCD) of two numbers using the classic Euclidean Algorithm.

The Euclidean method is an efficient and elegant way to find the largest number that divides both inputs without leaving a remainder.

The principle is simple:
GCD(A, B) = GCD(B, A mod B)

This process repeats until the remainder becomes zero. At that point, the divisor is the GCD.

Let’s dive into the code and understand how it works!


Program Code

data segment
    a dw 0012h          ; First number (18 decimal)
    b dw 000Ah          ; Second number (10 decimal)
    gcd_result dw ?     ; Variable to store the GCD
data ends

code segment
    assume cs:code, ds:data
start:
    mov ax, data
    mov ds, ax           ; Initialize data segment

    mov ax, a            ; Load A into AX
    mov bx, b            ; Load B into BX

    cmp bx, 0000h        ; Check if B is zero
    jz store_result

gcd_loop:
    mov dx, 0000h        ; Clear DX before division
    div bx               ; AX = AX / BX, remainder in DX

    cmp dx, 0000h        ; If remainder = 0, GCD found
    je store_result_bx

    mov ax, bx           ; A = B
    mov bx, dx           ; B = remainder
    jmp gcd_loop         ; Repeat loop

store_result_bx:
    mov ax, bx           ; Move GCD into AX

store_result:
    mov gcd_result, ax   ; Store final result
    int 3                ; Stop execution
code ends
end start

Continue reading 8086 Assembly Program to Compute the Greatest Common Divisor (GCD) of Two 16-bit Numbers Using Euclidean Algorithm

8086 Assembly Program to Calculate the Factorial of an Integer Using Loops and Registers

In this post, we’ll walk through an 8086 assembly program that calculates the factorial of a given integer. This program highlights the power of the 8086’s loop instruction and the use of general-purpose registers for iterative calculations. We’ll use the AX register as an accumulator for the result and the CX register as a loop counter, which is its special-purpose function. Let’s get to the code!

data segment
    n   dw 5      ; The number to find the factorial of
    fact dw ?     ; To store the 16-bit result
data ends
 
code segment
    assume ds:data, cs:code
start:
    mov ax, data
    mov ds, ax
 
    mov cx, n     ; Load N (5) into the counter register CX
    mov ax, 1     ; Initialize factorial result (AX) to 1
 
fact_loop:
    mul cx        ; Multiply AX by CX. Result in DX:AX
    loop fact_loop ; Decrement CX, jump to fact_loop if CX != 0
 
    mov fact, ax  ; Store the final result from AX into 'fact'
 
    int 3         ; Halt execution (breakpoint for debugging)
code ends
end start
Continue reading 8086 Assembly Program to Calculate the Factorial of an Integer Using Loops and Registers

Introduction to Memory Segmentation in 8086

In 1978, Intel released the 8086 and changed computing permanently. It was the chip that powered the original IBM PC, seeded the x86 architecture that still runs every Windows and Linux machine today, and introduced the pipelined fetch-execute model that every modern CPU descends from. If you are studying microprocessors — for an exam, for embedded systems work, or out of curiosity about how computers actually work at the hardware level — the 8086 is where the story starts. This post covers the processor’s origin and key features first, then moves into the memory segmentation mechanism that underlies every single memory access it ever makes.

Continue reading Introduction to Memory Segmentation in 8086

8086 MASM Assembly Program for Addition of Two 8-bit Numbers

This blog post will guide you through a MASM (Microsoft Macro Assembler) program that performs the addition of two 8-bit numbers. While this is a fundamental operation, it demonstrates crucial assembly programming concepts such as data handling, register usage, and memory storage. Let’s dive in!

Assembly Code

    .model small
    .data
    a db 09h
    b db 02h
    c dw ?
    
    .code
    main proc
        mov ax, @data
        mov ds, ax
        
        mov al, a
        mov bl, b
        add al, bl
        
        mov ah, 0
        mov c, ax
        
        int 3  ; Breakpoint interrupt
        
        mov ax, 4C00h  ; Exit program
        int 21h
    main endp
    
    end main
Continue reading 8086 MASM Assembly Program for Addition of Two 8-bit Numbers

Understanding INT 3h vs INT 21h in 8086 Assembly

In 8086 assembly programming, interrupts play a crucial role in handling various operations, from debugging to system calls. Two commonly used interrupts are <strong>INT 3h</strong> (Breakpoint Interrupt) and <strong>INT 21h</strong> (DOS Interrupt). While both involve interrupt handling, their functionalities and use cases are completely different.

In this blog post, we will explore the differences between INT 3h and INT 21h, their respective use cases, and practical examples to understand their behavior.

TL;DR

  • <strong>INT 3h</strong> is used for debugging; it stops execution and hands control to the debugger.
  • <strong>INT 21h</strong> is used for system services like displaying messages, reading input, and terminating the program.
  • Key Difference: INT 3h is a 1-byte instruction (CC), while INT 21h requires function numbers in AH to specify system calls.
Continue reading Understanding INT 3h vs INT 21h in 8086 Assembly

8086 Assembly Program to Add Two 16-bit Numbers with Carry Handling

In this blog post, we’ll explore an 8086 assembly language program that adds two 16-bit numbers while also checking for a carry flag.

; Data Segment

data segment
    a dw 0FFFFh      ; Example value that will cause a carry
    b dw 0001h       ; Example value
    c dw ?           ; To store result
    carry_flag db 0  ; To store carry (0 or 1)
data ends

; Code Segment

code segment
assume cs:code, ds:data
start:
    mov ax, data
    mov ds, ax      ; Initialize data segment

    mov ax, a       ; Load first number
    mov bx, b       ; Load second number
    add ax, bx      ; Perform addition

    mov c, ax       ; Store result in 'c'

    jc carry_occurred  ; Jump if carry flag is set
    mov carry_flag, 0  ; No carry, store 0
    jmp end_program    

carry_occurred:
    mov carry_flag, 1  ; Store carry flag as 1

end_program:
    int 3             ; Halt program

code ends
end start
Continue reading 8086 Assembly Program to Add Two 16-bit Numbers with Carry Handling

8086 Data Directives: DB, DW, DD, DQ, DT, DUP, EQU, PTR, and OFFSET

Data directives are assembler instructions, not CPU instructions — they tell the assembler how much memory to reserve and what value to store there at load time. The CPU never executes a DB or DW; it simply finds the bytes already in memory when it accesses that address at runtime. Choosing the right directive matters: use DB for bytes and strings, DW for 16-bit integers and addresses, DD for far pointers and 32-bit values, and DUP to initialise arrays without typing each value individually.

Continue reading 8086 Data Directives: DB, DW, DD, DQ, DT, DUP, EQU, PTR, and OFFSET