0% found this document useful (0 votes)
24 views17 pages

8086 Assembly Language Programs

The document contains a series of assembly language programs written for the 8086 microprocessor. Each program demonstrates various functionalities such as input validation, string manipulation, data transfer, arithmetic operations, and the use of procedures and macros. The programs are structured with a clear model, data, and code sections, showcasing practical applications of assembly language programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views17 pages

8086 Assembly Language Programs

The document contains a series of assembly language programs written for the 8086 microprocessor. Each program demonstrates various functionalities such as input validation, string manipulation, data transfer, arithmetic operations, and the use of procedures and macros. The programs are structured with a clear model, data, and code sections, showcasing practical applications of assembly language programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAMS IN 8086

1. Write an 8086 ALP which will input the user name from the keyboard. If the user is ‘POKHARA’ it
will output ‘The username is valid’ else it will output ‘Invalid user name’.

TITLE input name and compare


.MODEL SMALL
.STACK 100H

.DATA
INPUT DB 7 DUP(?)
USERNAME DB ‘POKHARA’,’$’
MESSAGE1 DB ‘The username is valid’,’$’
MESSAGE2 DB ‘Invalid user name’,’$’

.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV ES, AX

MOV SI, OFFSET INPUT


MOV CX, 0AH
L1:
MOV AH, 01H
INT 21H
CMP AL, 13
JE L2
MOV [SI], AL
INC SI
LOOP L1

L2:
MOV SI, OFFSET INPUT
MOV DI, OFFSET USERNAME
CLD
REPE CMPSB
JE L3
MOV DX, OFFSET MESSAGE2
JMP L4
L3: MOV DX, OFFSET MESSAGE1
L4: MOV AH, 09H
INT 21H
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

2. Write a program for 8086 microprocessor to count the number of data bytes that are less than
20H from the block of 10 data bytes of memory location ‘ARRAY_DATA’. Store the sorted result
in memory location ‘RESULT’.

.MODEL SMALL
.STACK 100H
.DATA
ARRAY_DATA DB 53, 12, 75, 32, 10, 06, 89, 32, 08, 49
RESULT DW 7500H

.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV SI, OFFSET ARRAY_DATA


MOV AX, 20H
MOV DX, 00H
MOV CX, 0AH

L1:
MOV BX, [SI]
CMP AX, BX
JG L2
INC SI
DEC CX
JNZ L1
JMP L3

L2:
INC DX
INC SI
DEC CX
JNZ L1
L3:
MOV SI, RESULT
MOV [SI], DX

MOV AH, 4CH


INT 21H
MAIN ENDP
END MAIN
3. Write an 8086 ALP using MASM to accept a string from keyboard and display it in console in
reverse order.

TITLE reverse string inputted from keyboard


.MODEL SMALL
.STACK 100H
.DATA
MSG DB 20 DUP(‘$’)

.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV CX, 0000H

MOV BL, 00H


MOV SI, OFFSET MSG
L1:
MOV AH, 01H
INT 21H
CMP AL, 13
JE L2
MOV [SI], AL
INC SI
INC CX
JMP L1
L2:
DEC SI
MOV DX, [SI]
MOV AH, 02H
INT 21H
LOOP L3
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
4. Write an assembly language in 8086 which transfer 10 bytes of data stored in “array 1” to the
new location “array 2”.

.MODEL SMALL
.STACK 100H
.DATA
array1 db 01, 02, 03, 04, 05, 06, 07, 08, 09, 10
array2 db 10 dup(?)

.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV SI, OFFSET array1


MOV DI, OFFSET array2
MOV CX, 0AH
L1:
MOV AH, [SI]
MOV [DI], AH
INC SI
INC DI
LOOP L1

MOV AX, 4CH


INT 21H
MAIN ENDP
END MAIN

5. Write a program in 8086 which displays “Hello World” on the screen.

.MODEL SMALL
.DATA
Message DB “Hello World$”
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV AH, 09H


MOV DX, OFFSET Message
INT 21H
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

6. Write an assembly language programming for 8086 to find the square root of a given number.
Assume that a number is of two digits and is perfect square.
.MODEL SMALL
.STACK 100H
.DATA
VAR DW 0051H
ROOT DB 2 DUP(?)

.CODE

MAIN PROC
MOV AX, DATA
MOV DS, AX

MOV CX, 0000H


LEA SI, VAR
MOV BX, [SI]

L1:
MOV AX,CX
MUL CX
CMP AX, BX
JE L2
INC CX
JMP L1

L2:
LEA SI, ROOT
MOV [SI], CX

MOV AH, 4CH


INT 21H

MAIN ENDP
END MAIN

7. Write an 8086 ALP for MASM to display the text “POKHARAUNIVERSITY’ in reverse order.

Title reverse the POKHARAUNIVERSITY


.MODEL SMALL
.STACK 100H
.DATA
STRING DB “POKHARAUNIVERSITY$”
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV CX, 0000H
MOV SI, OFFSET STRING

L1:
MOV AL, [SI]
CMP AL, 36
JE L2
INC SI
INC CX
JMP L1

L2:
DEC SI
MOV DX, [SI]
MOV AH, 02H
INT 21H
LOOP L2
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

8. Write an assembly level program in 8086 to find the largest number among 10 blocks of data
and store the largest value in location “largest”.

.MODEL SMALL
.DATA
ARRAY1 DB 10,12,58,05,95,45,36,17,56,89
LARGEST DB 00H
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV CX, 0AH


MOV SI, OFFSET ARRAY1
MOV AL, 00H

L1:
CMP AL, [SI]
JG L2
MOV AL, [SI]
L2:
INC SI
LOOP L1

MOV SI, OFFSET LARGEST


MOV [SI], AL

MOV AH, 4CH


INT 21H
MAIN ENDP
END MAIN

9. Write an 8086 program to enter a string from the keyboard. Count the number of repetitions of
letter ‘a’ and ‘A’. If the count is even, display “POKHARA” else display “UNIVERSITY”.
; Program to count repetitions of 'a' and 'A' in an input string and display messages

.MODEL SMALL
.STACK 100H

.DATA
MSG1 DB 'Enter a string: $'
MSG2 DB 'POKHARA$', 0
MSG3 DB 'UNIVERSITY$', 0
INPUT DB 20 DUP('$'), '$'
PROMPT_NEWLINE DB 13, 10, '$'
COUNT DW 0

.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

; Display the prompt message


LEA DX, MSG1
MOV AH, 09H
INT 21H

; Read the string from the keyboard


LEA DX, INPUT
MOV AH, 0AH
INT 21H
; Initialize registers for counting
MOV SI, OFFSET INPUT + 2 ; Skip the length byte and enter key
MOV CX, 20 ; Assuming 20 is the maximum length of input
MOV COUNT, 0 ; Clear the COUNT register

COUNT_LOOP:
CMP CX, 0
JE DISPLAY_RESULT ; Exit loop if all characters are checked

LODSB ; Load next character into AL


CMP AL, 'a'
JE INCREMENT_COUNT
CMP AL, 'A'
JE INCREMENT_COUNT
JMP NEXT_CHAR

INCREMENT_COUNT:
INC COUNT

NEXT_CHAR:
DEC CX
JMP COUNT_LOOP

DISPLAY_RESULT:
TEST COUNT, 1 ; Check if COUNT is even
JZ PRINT_POKHARA ; If zero, COUNT is even
JMP PRINT_UNIVERSITY ; Else, odd

PRINT_POKHARA:
LEA DX, PROMPT_NEWLINE
MOV AH, 09H
INT 21H
LEA DX, MSG2
MOV AH, 09H
INT 21H
JMP END_PROGRAM

PRINT_UNIVERSITY:
LEA DX, PROMPT_NEWLINE
MOV AH, 09H
INT 21H
LEA DX, MSG3
MOV AH, 09H
INT 21H

END_PROGRAM:
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
10. Write an 8086 program to display string using macro.
.MODEL SMALL

.DATA

MESSAGE DB 'HELLO WORLD$'

MESSAGE1 DB ' I AM GOD$'

.CODE

PRINTSTRING MACRO STR

MOV AH,09

MOV DX,OFFSET STR

INT 21H

ENDM

TERMINATE MACRO

MOV AH,4CH

INT 21H

ENDM

MAIN PROC NEAR

MOV AX,@DATA

MOV DS,AX

PRINTSTRING MESSAGE

PRINTSTRING MESSAGE1

TERMINATE

ENDP MAIN

END MAIN

11. Write an 8086 ALP to demonstrate simple stack operation.


Title Simple program of stack operation
.MODEL SMALL
.STACK 100H

.DATA
NUM1 DW 1234H
NUM2 DW 5678H
RESULT1 DW ?
RESULT2 DW ?

.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV AX, STACK


MOV SS, AX

MOV AX, NUM1


PUSH AX

MOV AX, NUM2


PUSH AX

POP AX
MOV RESULT1, AX

POP AX
MOV RESULT2, AX

MOV AX, 4C00H


INT 21H
MAIN ENDP
END MAIN
12. Write an 8086 program to demonstrate use of Procedure.
.MODEL SMALL
.STACK 100H

.DATA
NUM1 DW 5
NUM2 DW 3
RESULT DW ?

.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV AX, NUM1


MOV BX, NUM2
CALL ADD_NUMBERS

MOV RESULT, AX

MOV AX, 4C00H


INT 21H
MAIN ENDP

ADD_NUMBERS PROC
ADD AX, BX
RET
ADD_NUMBERS ENDP

END MAIN

13. You have given string data "Microprocessor and Assembly language programming". Write an
ALP to print "Microprocessor Programming" from the above given data.
.MODEL SMALL
.STACK 100H
.DATA
inputString DB 'Microprocessor and Assembly language programming$', 0
outputString DB 'Microprocessor Programming$', 0

.CODE
MAIN PROC

MOV AX, @DATA


MOV DS, AX

MOV DX, OFFSET outputString


MOV AH, 09H
INT 21H
MOV AH, 4CH
INT 21H

MAIN ENDP
END MAIN

14. Write a procedure program for 8086 for newline and use it to display three different strings in
different lines.
.MODEL SMALL
.STACK 100H
.DATA
string1 DB 'First Line$', 0
string2 DB 'Second Line$', 0
string3 DB 'Third Line$', 0
new DB 0DH, 0AH, '$' ; New line characters

.CODE
MAIN PROC
; Initialize DS
MOV AX, @DATA
MOV DS, AX

; Print first string


MOV DX, OFFSET string1
MOV AH, 09H
INT 21H
CALL NEWLINE

; Print second string


MOV DX, OFFSET string2
MOV AH, 09H
INT 21H
CALL NEWLINE

; Print third string


MOV DX, OFFSET string3
MOV AH, 09H
INT 21H
CALL NEWLINE

; Exit program
MOV AH, 4CH
INT 21H
MAIN ENDP

NEWLINE PROC
MOV DX, OFFSET new
MOV AH, 09H
INT 21H
RET
NEWLINE ENDP

END MAIN

15. Write an assembly language program for 8086 to find the sum of two numbers which is input by
the user through the keyboard and display the sum on the screen.
Note: It only works for single digit sum
.MODEL SMALL
.STACK 100H
.DATA
msg1 DB 'Enter first number: $'
msg2 DB 0DH, 0AH, 'Enter second number: $'
msg3 DB 0DH, 0AH, 'Sum: $'
num1 DB ?
num2 DB ?
sum DB ?
newlin DB 0DH, 0AH, '$'

.CODE
MAIN PROC
; Initialize DS
MOV AX, @DATA
MOV DS, AX

; Prompt for first number


MOV DX, OFFSET msg1
MOV AH, 09H
INT 21H

; Read first number


MOV AH, 01H
INT 21H
SUB AL, 30H ; Convert ASCII to number
MOV BL, AL
CALL NEWLINE

; Prompt for second number


MOV DX, OFFSET msg2
MOV AH, 09H
INT 21H

; Read second number


MOV AH, 01H
INT 21H
SUB AL, 30H ; Convert ASCII to number
MOV BH, AL
CALL NEWLINE

; Compute sum
MOV AL, BL
ADD AL, BH
MOV sum, AL
ADD sum, 30H ; Convert number to ASCII

; Display sum
MOV DX, OFFSET msg3
MOV AH, 09H
INT 21H

MOV DL, sum


MOV AH, 02H
INT 21H
CALL NEWLINE

; Exit program
MOV AH, 4CH
INT 21H

MAIN ENDP

NEWLINE PROC
MOV DX, OFFSET newlin
MOV AH, 09H
INT 21H
RET
NEWLINE ENDP

END MAIN

16. Write an assembly language program to display a string 'POKHARA UNIVERSITY' characterwise
using macro to insert space between characters.
.MODEL SMALL
.STACK 100H
.DATA
message DB 'POKHARA UNIVERSITY$', 0
space DB ' $', 0

.CODE
PRINT_SPACE MACRO
MOV DX, OFFSET space
MOV AH, 09H
INT 21H
ENDM

MAIN PROC
; Initialize DS
MOV AX, @DATA
MOV DS, AX

; Print characters one by one with space


MOV SI, OFFSET message
PRINT_LOOP:
MOV DL, [SI]
CMP DL, '$'
JE DONE
MOV AH, 02H
INT 21H
PRINT_SPACE
INC SI
JMP PRINT_LOOP

DONE:
CALL NEWLINE

; Exit program
MOV AH, 4CH
INT 21H

MAIN ENDP

END MAIN

17. Write an assembly language program for 8086 microprocessors to check how many times the
last digit of your symbol number is repeated in your symbol number and display the result (For
example if your symbol number is 321123, the last digit '3' is repeated 2 times, so your program
should display the result 2).
.MODEL SMALL
.STACK 100H
.DATA
symbolNumber DB '321123$', 0 ; Example symbol number
lastDigit DB ?
count DB 0
resultMsg DB 'Repeated Count: $'

.CODE
MAIN PROC
; Initialize DS
MOV AX, @DATA
MOV DS, AX

; Find the last digit


MOV SI, OFFSET symbolNumber
CHECK_LAST:
MOV AL, [SI]
CMP AL, '$'
JE STORE_LAST
INC SI
JMP CHECK_LAST

STORE_LAST:
DEC SI
MOV AL, [SI]
MOV lastDigit, AL

; Reset SI to start checking


MOV SI, OFFSET symbolNumber
MOV CX, 0

COUNT_LOOP:
MOV AL, [SI]
CMP AL, '$'
JE DISPLAY_RESULT
CMP AL, lastDigit
JNE SKIP_INCREMENT
INC CX
SKIP_INCREMENT:
INC SI
JMP COUNT_LOOP
DISPLAY_RESULT:
MOV count, CL

; Print message
MOV DX, OFFSET resultMsg
MOV AH, 09H
INT 21H

; Convert count to ASCII and print


ADD CL, 30H
MOV DL, CL
MOV AH, 02H
INT 21H
CALL NEWLINE

; Exit program
MOV AH, 4CH
INT 21H

MAIN ENDP
END MAIN

You might also like