Skip to main content

8086 TASM

8086 Assembly: Handling the External Timer Interrupt (INT 08h)

A complete guide to handling the 8086 external timer interrupt INT 08h. Covers the interrupt vector table, how the CPU dispatches interrupts, saving and restoring the original BIOS handler, writing a far ISR that increments a tick counter, chains correctly, and exits safely — with a common-mistakes table.

8086 Assembly: PUSH, POP, CALL, and RET – Stack Operations Explained

A complete guide to 8086 assembly stack operations. Covers how the stack works (SP, SS, LIFO), what PUSH and POP do to memory, how CALL saves a return address and RET retrieves it, and a fully annotated working program that demonstrates all four instructions together.

8086 Assembly Program to Compute the Power of a Number Using Exponentiation by Squaring

This blog post details an 8086-assembly program that computes the power of a number using the Exponentiation by Squaring algorithm (O(log n) efficiency). While a standard iterative approach multiplies the base n times (taking O(n) time), exponentiation by squaring—also known as binary exponentiation—works by breaking the exponent down into its binary components. By squaring the base in each step and only multiplying it into the result when a bit in the exponent is set, we drastically reduce the computational load. For example, calculating x32 requires only 5 multiplications instead of 31. This example demonstrates advanced assembly concepts like bitwise manipulation, conditional branching, and efficient arithmetic optimization. Let’s get started! Logic Breakdown: The algorithm follows the mathematical identity of Binary Exponentiation: Check Exponent: If it's zero, stop. Odd Case: If the current exponent is odd, multiply the running result by the current base. Square and Halve: Regardless of odd/even, square the base and divide the exponent by 2. Loop: Continue until the exponent is exhausted. Let's visualize it's working for 53. StepBase ExponentResult ActionInitial531Start loopIter 153 (Odd)51 X 5 = 5Square251552 = 25, 3/2 = 1Iter 2251 (Odd)1255 X 25 = 125Square6250125252 = 625,1/2 = 0Exit-0125Loop terminates

8086 Assembly Program to Compute the Power of a Number

This blog post details an 8086-assembly program that computes the power of a number, specifically baseexponent. This example demonstrates fundamental assembly concepts like loops, multiplication, and register manipulation to perform iterative arithmetic. Let’s get started! data segment base dw 0003h exponent dw 0004h result dd ? data ends code segment assume cs:code, ds:data start: ; Initialize data segment mov ax, data mov ds, ax ; Initialize result to 1 (R = B^0) mov word ptr result, 0001h mov word ptr result+2, 0000h ; Load base and exponent mov cx, exponent ; CX = exponent (loop counter) mov bx, base ; BX = base cmp cx, 0000h je exit ; If exponent is 0, result is already 1. Jump to exit. power_loop: ; Multiply result by base ; The result is a 32-bit number in result[0] (lower word) and result[2] (upper word) ; Multiplication requires careful handling of the 32-bit result with a 16-bit multiplier. ; 1. Multiply the lower word of result by base mov ax, word ptr result ; AX = result[0] mul bx ; AX * BX -> DX:AX (32-bit product) push dx ; Save the higher word (DX) of the product mov word ptr result, ax ; Store the lower word (AX) of the product as the new result[0] ; 2. Multiply the upper word of result by base mov ax, word ptr result+2 ; AX = result[2] mul bx ; AX * BX -> DX:AX (32-bit product) ; 3. Add the two 16-bit 'carry' terms pop cx ; Retrieve the saved higher word (DX) from step 1 into CX add ax, cx ; AX = AX + CX (Sum of two high words) adc dx, 0000h ; DX = DX + 0 + Carry from the previous ADD (final carry from the 32-bit multiplication) ; 4. Store the final upper word mov word ptr result+2, ax ; Store AX as the new result[2] ; Handle the 32-bit overflow (DX is the final carry from the 32-bit multiplication) ; For a 32-bit result storage, this program assumes the result fits in 32-bits. ; If the power is large, overflow might occur, which is a limitation of this 32-bit storage approach. loop power_loop ; Decrement CX and jump back to power_loop if CX != 0 exit: int 3 ; Program termination code ends end start

8086 Assembly Program to Compute Factorial of an Integer Using Recursion

This blog post will dive into a more advanced concept in 8086 assembly: computing the factorial of a number using recursion. Unlike iterative approaches using loops, recursion involves a procedure calling itself. This example highlights crucial concepts of stack management (PUSH and POP), procedure calls (CALL and RET), and conditional jumping to handle base cases. Let’s explore how the stack handles the "memory" of recursive calls! ; Program to calculate Factorial of a number using Recursion ; Input: num (e.g., 5) ; Output: result (e.g., 5! = 120 or 78h) data segment num dw 0005h ; The number to calculate factorial for (16-bit word) result dw ? ; Variable to store the final 16-bit result data ends code segment assume cs:code, ds:data start: mov ax, data mov ds, ax ; Initialize Data Segment mov cx, num ; Load the input number into CX register used as counter/argument mov ax, 0001h ; Initialize accumulator AX to 1 (needed for multiplication) call factorial ; Call the recursive procedure mov result, ax ; Store final result from AX into memory variable int 3 ; Breakpoint to halt and check registers ;--- Recursive Procedure Definition --- factorial proc near cmp cx, 1 ; BASE CASE: Check if number in CX is <= 1 jbe base_case ; If CX is 0 or 1, jump to base_case to return push cx ; RECURSIVE STEP: Save current state of CX on STACK dec cx ; Decrement CX to move towards base case (N-1) call factorial ; Recursive Call: factorial(N-1) pop cx ; UNWINDING: Restore the saved value of CX from STACK mul cx ; AX = AX * CX. (Current Result * Current N) ret ; Return to caller base_case: mov ax, 1 ; Base case returns 1 (as 0! = 1 and 1! = 1) ret ; Return to caller factorial endp ;-------------------------------------- code ends end start

8086 Assembly Program to Generate the Fibonacci Sequence

This blog post will walk you through an 8086 assembly program designed to generate the Fibonacci sequence. While this may sound simple, it’s an excellent example for understanding looping, arithmetic operations, and register management in assembly language. The Fibonacci Series The Fibonacci series is a sequence where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, … In this program, we’ll generate the first 10 Fibonacci numbers and store them in memory. Code ; 8086 Program to Generate 10 Fibonacci Numbers data segment COUNT equ 10 ; Number of terms to generate FIB_SERIES db COUNT dup(?) ; Array to store the series data ends code segment assume cs:code, ds:data start: ; Initialize Data Segment (DS) register mov ax, data mov ds, ax ; Use SI as a pointer to the array mov si, offset FIB_SERIES ; Seed the first two Fibonacci numbers ; Fib(0) = 0 mov byte ptr [si], 00h ; Fib(1) = 1 inc si mov byte ptr [si], 01h ; Set up loop counter. We already have 2 numbers, ; so we need to generate (COUNT - 2) more. mov cx, COUNT sub cx, 2 L1: ; AL = Fib(n-1) (e.g., [si]) mov al, [si] ; BL = Fib(n-2) (e.g., [si-1]) mov bl, [si-1] ; AL = Fib(n-1) + Fib(n-2) add al, bl ; Move pointer to next position Fib(n) inc si ; Store the new term: [si] = AL mov [si], al ; Decrement CX, loop if CX is not zero loop L1 ; Halt execution for debugging int 3 code ends end start

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

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!