8086 Tutorial
8086 Tutorial
Write a program to read string using the character reading function and display the
string using character displaying function
title read and display string using character function
.model small
.stack 32
.data
str db 25 dup(?)
newline db 0dh,0ah,'$'
.code
main proc far
mov ax,@data
mov ds,ax
mov cx,25
mov dx,00
mov bx,offset str
mov ah,01h
l1: int 21h
mov [bx],al
inc dx
inc bx
cmp al,0dh
loopne l1
mov cx,dx
mov ah,09h
mov dx,offset newline
int 21h
mov ah,02h
mov bx,offset str
l2: mov dl,[bx]
int 21h
inc bx
loop l2
mov ax,4c00h
int 21h
main endp
end main
2 | P a g e
Write a program to display all ASCII character from 0 to 255 (decimal value)
TITLE PROGRAM TO DISPLAY ASCII CHARACTER FROM 0 TO 255
.MODEL SMALL
.STACK 32 ;32 bytes stack segment defined
.DATA
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX ;DATA SEGMENT REGISTER INITIALIZATION
MOV DL,00
MOV AH,02H ;SINGLE CHARACTER DISPLAY
BACK: INT 21H
INC DL
CMP DL,255
JB BACK
MOV AX,4C00H ;REQUEST FOR NORMAL EXIT
INT 21H
MAIN ENDP ;END PROCEDURE
END MAIN
WAP to read string and display each word in separate lines
TITLE READ STRING FROM USERS & DISPLAY EACH WORD IN SEPARATE LINES
.MODEL SMALL
.STACK 64 ;STACK WITH 64 BYTES SIZE
.DATA
STR DB 30 DUP(?)
NEWLINE DB 13,10,'$' ;OR --> 0DH,0AH,'$'
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX ;DATA SEGMENT INITIALIZATION
MOV CX,30
MOV DX,00
MOV BX,OFFSET STR
MOV AH,01H ;KEYBOARD CHARACTER INPUT FUNCTION NUMBER
L1: INT 21H
3 | P a g e
MOV [BX],AL ;STORE ASCII VALUE OF KEY PRESSED INTO MEMORY
;WITH OFFSET GIVEN BY BX REGISTER
INC BX
INC DX ;COUNTS CHARACTER ENTERED
CMP AL,0DH ;COMPARE WITH ENTER KEY
LOOPNE L1
MOV CX,DX ;COPY N0. OF CHARACTERS TO CX
MOV AH,09H
MOV DX,OFFSET NEWLINE
INT 21H ;INSERT NEWLINE
MOV BX,OFFSET STR
L2: MOV AH,02H
MOV DL,[BX]
CMP DL,' ' ;COMPARE CHARACTER TO BE DISPLAY WITH SPACE KEY VALUE
JNE NOSPC ;JUMPS IF NOT EQUAL RESULT FOR ABOVE COMPARISION
MOV AH,09H
MOV DX,OFFSET NEWLINE
NOSPC: INT 21H
INC BX
LOOP L2
MOV AX,4C00H
INT 21H
MAIN ENDP ;END 'MAIN' PROCEDURE
END MAIN ;TERMINATE PROGRAM
Write a program to count and display its total vowel characters from user entered
string
TITLE READ STRING AND COUNT THE VOWELS AND DISPLAY IT
.MODEL SMALL
.STACK 64H
.DATA
PARALST LABEL BYTE
MAXCHAR DB 60
ACTCHAR DB ?
STR DB 60 DUP(?)
VOWEL DB 0
NEWLINE DB 13,10,'$'
4 | P a g e
CHECKSTR DB 'a','e','i','o','u','A','E','I','O','U'
.CODE
MAIN PROC FAR
MOV AX,@DATA ;for locating the data segment
MOV DS,AX
LEA DX,PARALST ;enter the string
MOV AH,0AH
INT 21H
CALL NEXTLINE ;call procedure "nextline" for newline
MOV CL,ACTCHAR
MOV CH,0
MOV BX,0
L1: PUSH CX
MOV DL,STR[BX]
MOV SI,0
MOV CX,10 ;no. of vowels to be compared with each character
L3: CMP DL,CHECKSTR[SI]
JZ L2
INC SI
LOOP L3
JMP L4
L2: INC VOWEL ;count vowel charater found
MOV AH,02H ;for displaying each vowel character found
INT 21H
L4: INC BX
POP CX ;get back no. of character for checking each
LOOP L1
CALL VOWELCOUNTDISP
MOV AH,01H
INT 21H
MOV AX,4C00H
INT 21H
MAIN ENDP
5 | P a g e
NEXTLINE PROC NEAR
LEA DX,NEWLINE
MOV AH,09H
INT 21H
RET
NEXTLINE ENDP
VOWELCOUNTDISP PROC NEAR
MOV CX,10
MOV AL,VOWEL
MOV AH,0
MOV BX,0
LABEL1: MOV DX,0
DIV CX
ADD DX,30H ;convert vowel number into ASCII format for displaying
PUSH DX
INC BX
CMP AX,0
JA LABEL1
MOV AH,02H
MOV CX,BX ;load total number to be displayed into cx for loop
DISP: POP DX
INT 21H ;display number after converting it into ASCII format
LOOP DISP ;loop continue until number ends
RET
VOWELCOUNTDISP ENDP
END MAIN
WAP to read a string and display it on center of window 2,10 * 22,70
title display string on center of window defined
.model small
.stack 64h
.data
string label byte
maxlen db 40
actlen db ?
input db 40 dup("$")
message db "Please enter a string: ","$"
6 | P a g e
.code
main proc far
mov ax,@data
mov ds,ax
mov ah,09h ;ask for string input
mov dx,offset message
int 21h
mov ah,0ah ;enter string
lea dx,string
int 21h
mov ah,06h ;for complete window clear
mov al,00h
mov bh,07h
mov cx,0000h
mov dx,184fh
int 10h
mov ah,06h ;for clearing defined window area 2,10 * 22,70
mov al,14h
mov bh,30h
mov cx,020ah
mov dx,1646h
int 10h
mov al,actlen ;set the cursor at the center by
shr al,1 ;by dividing the string into half
mov dl,40
sub dl,al ;center column (width)
mov ah,02h
mov bh,00h ;page number (0 for default)
mov dh,12 ;center of row
int 10h
mov ah,09h
mov dx,offset input
int 21h
7 | P a g e
mov ax,4c00h
int 21h
main endp
end main
WAP to take single digit from user and display sum upto that number starting from 0.
title take a single number as input and display the sum upto that number from 0
.model small
.stack 100
.data
input db "Please enter a number: ","$"
output db "The sum is: ","$"
newline db 0dh,0ah,"$"
sum dw 0
.code
main proc far
mov ax,@data
mov ds,ax
mov ah,09h ;ask for input number
lea dx,input
int 21h
mov ah,01h
int 21h ;request for keboard input character
mov ah,00
sub al,30h ;find actual number by converting ASCII
mov cx,ax
l1: add sum,cx ;find sum
loop l1
mov ax,sum
mov dx,0
mov bx,0
mov cx,10
l2: div cx
add dx,30h
push dx
inc bx
mov dx,00
8 | P a g e
cmp ax,00
ja l2
mov ah,09h
lea dx,newline
int 21h
mov ah,09h
ea dx,output
int 21h
mov cx,bx
l3: mov ah,02h
pop dx
int 21h
loop l3
mov ax,4c00h
int 21h
main endp
end main
WAP to add all data between 50 and 150 from the table of data stored in memory
title to add all the data of table between 50 and 150
.model small
.stack 32h
.data
table db 55,12,34,156,234,90,140,200,99,6 ;table of data of each 1 byte
message db "The sum is : ","$"
sum dw 0000
newline db 13,10,"$"
.code
main proc far
mov ax,@data ;initialization of data segment
mov ds,ax
mov si,offset table
mov cx,10 ;counter for 10 data
mov ah,00h
l1: mov al,[si]
9 | P a g e
cmp al,50
jbe l2 ;jump on below or equal
add sum,ax
l2: inc si
loop l1
mov ax,sum ;stored sum is copied to ax register
mov dx,00
mov bx,00
mov cx,10
l3: div cx
add dx,30h
push dx
inc bx ;bx is used to count the number of ASCII character
mov dx,00
cmp ax,00
ja l3
mov ah,09h
lea dx,newline
int 21h ;print one blank line
mov ah,09h
lea dx,message
int 21h ;print message
mov cx,bx
mov ah,02h
l4: pop dx
int 21h ;display result in ASCII format
loop l4
mov ax,4c00h
int 21h
main endp
end main
WAP to sort an array of 10 numbers stored in memory. Display the numbers in screen
after sorting
title sorting 10 numbers stored in memory and disply them afer sorting
.model small
10 | P a g e
.stack 100h
.data
data_arr db 3,5,8,2,5,93,9,10,10h,4
message db 'THE SORTED ORDER IS: ','$'
tablike db ' ','$'
.code
main proc far
mov ax,@data
mov ds,ax
l1: mov si,offset data_arr
mov bl,00h
mov cx,0009h
l2: mov al,[si]
inc si
cmp al,[si]
jbe ahead
;swapping
mov dl,[si]
mov [si],al
dec si
mov [si],dl
inc si
mov bl,01h
ahead: loop l2
dec bl
jz l1
lea dx,message
mov ah,09h
int 21h
;for display
mov si,offset data_arr
mov ch,00
mov cl,0ah
no_of_times: mov ah,09h ;one space separation for each digit
mov dx,offset tablike
int 21h
11 | P a g e
mov ah,00
mov bx,00
mov al,[si]
push cx
mov cx,10
no_of_digits:
mov dx,0
div cx ;(dx:ax/cx= ax dx/cx)
add dx,30h
push dx
inc bx
cmp ax,0
ja no_of_digits
mov ah,02h
mov cx,bx
popping:
pop dx
int 21h
loop popping
inc si
pop cx
loop no_of_times
mov ax,4c00h
int 21h
main endp
end main
WAP to find largest number among 10 numbers stored in memory & display it
TITLE TO DISPLAY THE LARGEST AMONG 10 NUMBERS
.MODEL SMALL
.STACK 64
.DATA
ARR_NUM DB 34,67,150,55,23,98,35,43,88,23
.CODE
12 | P a g e
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
MOV BX,00
MOV CX,09 ;COMPARISION DONE FOR 9 TIMES
MOV AL,ARR_NUM[BX]
L1: INC BX
CMP AL,ARR_NUM[BX]
JAE L2
MOV AL,ARR_NUM[BX]
L2: LOOP L1
MOV AH,00
MOV BX,00
MOV CX,10
MOV DX,00
A1: DIV CX
ADD DX,30H
PUSH DX
INC BX
MOV DX,00
CMP AX,00
JA A1
MOV AH,02H
MOV CX,BX
A2: POP DX
INT 21H
LOOP A2
MOV AX,4C00H
INT 21H
MAIN ENDP
END MAIN
WAP to generate multiplication table for user given single digit, and display them.
TITLE GENERATE MULTIPLICATION TABLE OF USER GIVEN SINGLE DIGIT
.MODEL SMALL
.STACK 64H
.DATA
13 | P a g e
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DX,AX
MOV AH,07H ;CONSOLE INPUT WITHOUT ECHO
INT 21H
SUB AL,30H
MOV BL,01
MOV BH,AL
MOV CX,10
BACK: PUSH CX
PUSH BX
MUL BL
MOV CX,10
MOV BX,00
L1: MOV DX,00
DIV CX
ADD DX,30H
PUSH DX
INC BX
CMP AX,00
JA L1
MOV CX,BX
MOV AH,02H
L2: POP DX
INT 21H
LOOP L2
MOV AH,02H ;A BLANK CHARACTER IN PRINTED TO
MOV DL," " ;SEPARATE TWO NUMBERS
INT 21H
POP BX
MOV AL,BH
INC BL
POP CX
LOOP BACK
14 | P a g e
MOV AX,4C00H
INT 21H
MAIN ENDP
END MAIN
WAP to display string stored in memory in reverse order
title program to display the stored string in reverse order
.model small
.stack 100h
.data
str1 db "Programming is fun.","$"
str2 db 50 dup ("$")
.code
main proc far
mov ax,@data
mov ds,ax ;data segment initialization
mov si,offset str1
mov di,offset str2
mov cx,00 ;cx register initialized to 0 to count charaters
l1: mov dl,[si]
cmp dl,"$"
je l2
inc cx ;increment counter
inc si ;increment offset address of source string
jmp l1
l2: dec si ;decrement si offset before copying character
mov bl,[si]
mov [di],bl
inc di
loop l2
mov ah,09h
mov dx,offset str2
int 21h ;display reverse ordered string
mov ax,4c00h
int 21h
main endp
end main
15 | P a g e
WAP to add sequence 1+2+3+ up to 100 and display result in HEX format.
title program to add sequence 1+3+5+... up to 100 terms and display result in hex format
.model small
.stack 64
.data
sum dw 00
.code
main proc far
mov ax,@data
mov ds,ax
mov cx,100
mov ax,01
l1: add sum,ax ;add sequence
add ax,02
loop l1
mov ax,sum ;copy sum to ax register
mov cx,16 ;copy base value of hex format for dividing data
mov bx,00 ;bx is used to count number of hex data
l3: mov dx,00
div cx
cmp dx,09 ;compare each single data with 9 digit
jbe l2
add dx,07h
l2: add dx,30h
push dx
inc bx
cmp ax,00
ja l3
mov cx,bx
mov ah,02h
popping: pop dx
int 21h
loop popping
mov ax,4c00h
int 21h
main endp
end main
16 | P a g e