Category Archives: 8086 TASM

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

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 Assembly Program to Find Smallest Number from Given Numbers

​In this blog post, we’ll explore how to find the smallest number from a given set of values using an 8086 assembly language program. The following code snippet demonstrates this process:

data segment
STRING1 DB 08h,14h,05h,0Fh,09h
res db ?
data ends

code segment
assume cs:code, ds:data
start: mov ax, data
mov ds, ax
mov cx, 04h

mov bl, 79h
LEA SI, STRING1
up:
mov al, [SI]
cmp al, bl
jge nxt
mov bl, al
nxt:
inc si
dec cx
jnz up

mov res,bl
int 3
code ends
end start
Continue reading 8086 Assembly Program to Find Smallest Number from Given Numbers

8086 Assembly Program to Sort Numbers in Descending Order

In this blog post, we will explore an 8086 assembly language program designed to sort a list of numbers in descending order. Sorting is a fundamental operation in computer science, and understanding how to implement it at the assembly level provides valuable insights into low-level programming and processor operations.

The following assembly program sorts an array of numbers in descending order using the bubble sort algorithm:

DATA SEGMENT
STRING1 DB 99H,12H,56H,45H,36H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV CH,04H
UP2: MOV CL,04H
LEA SI,STRING1

UP1:MOV AL,[SI]
MOV BL,[SI+1]
CMP AL,BL
JNC DOWN
MOV DL,[SI+1]
XCHG [SI],DL
MOV [SI+1],DL

DOWN: INC SI
DEC CL
JNZ UP1
DEC CH
JNZ UP2
INT 3
CODE ENDS
END START
Continue reading 8086 Assembly Program to Sort Numbers in Descending Order

8086 Assembly Program to Sort Numbers in Ascending Order

In this blog post, we will explore an 8086 assembly language program designed to sort a list of numbers in ascending order. Sorting is a fundamental operation in computer science, and understanding how to implement it at the assembly level provides valuable insights into low-level programming and processor operations.​

The following assembly program sorts an array of numbers in ascending order using the bubble sort algorithm:

DATA SEGMENT
STRING1 DB 99H,12H,56H,45H,36H
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX

MOV CH,04H

UP2: MOV CL,04H
LEA SI,STRING1

UP1: MOV AL,[SI]
MOV BL,[SI+1]
CMP AL,BL
JC DOWN
MOV DL,[SI+1]
XCHG [SI],DL
MOV [SI+1],DL

DOWN: INC SI
DEC CL
JNZ UP1
DEC CH
JNZ UP2

INT 3
CODE ENDS
END START
Continue reading 8086 Assembly Program to Sort Numbers in Ascending Order

8086 Assembly Program to Count Number of 0’s and 1’s from a String

In this blog post, we’ll explore how to count the number of ‘0’s and ‘1’s in a binary string using an 8086 assembly language program. The following code snippet demonstrates this process:​

DATA SEGMENT
STR1 DB "00011100"
NZ DW ?
NO DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
LEA SI, STR1
MOV BX, 00H
MOV DX, 00H
MOV CX, 08H
UP:
MOV AX, [SI]
ROR AX, 1
JNC ZE
INC BX
JMP DN
ZE:
INC DX
DN:
INC SI
DEC CX
JNZ UP
MOV NZ, DX
MOV NO, BX
INT 3
CODE ENDS
END START
Continue reading 8086 Assembly Program to Count Number of 0’s and 1’s from a String