0% found this document useful (0 votes)
270 views60 pages

Microprocessor Programming (22415) Unit 4: Assembly Language Hours: 16 Marks: 20

This document provides information about assembly language programming including: - The structure of an assembly language program with segments for data and code. - How to write programs for operations like addition, subtraction, multiplication and division of 8-bit, 16-bit, and 32-bit numbers. - How to write programs to compare strings, find the smallest/largest number in an array, and other tasks using assembly language instructions. Detailed code examples are given for each type of program to demonstrate how to write assembly language code to perform various numeric and string operations. Learning outcomes focus on writing programs to solve problems using appropriate control structures and instructions.

Uploaded by

Shahnawaz Khan
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)
270 views60 pages

Microprocessor Programming (22415) Unit 4: Assembly Language Hours: 16 Marks: 20

This document provides information about assembly language programming including: - The structure of an assembly language program with segments for data and code. - How to write programs for operations like addition, subtraction, multiplication and division of 8-bit, 16-bit, and 32-bit numbers. - How to write programs to compare strings, find the smallest/largest number in an array, and other tasks using assembly language instructions. Detailed code examples are given for each type of program to demonstrate how to write assembly language code to perform various numeric and string operations. Learning outcomes focus on writing programs to solve problems using appropriate control structures and instructions.

Uploaded by

Shahnawaz Khan
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
You are on page 1/ 60

Microprocessor programming(22415)

Unit 4 : Assembly Language


Programming
Hours: 16 Marks: 20

Prepared by:
Mrs. Kousar Ayub A.
Lecturer(Selection Grade)
Computer Engg. Dept
M. H. Saboo Siddik Polytechnic
Mrs. Kousar Ayub Akumalla
1
Learning Outcomes
The learners will be able to:
• Writing Assembly language program for
given problem.
• Develop t he relevant program for given
problem.
• Apply the relevant control loop in the
program for given problem.
• Use of string instruction for strings/block
manipulate its elements.
Mrs. Kousar Ayub Akumalla 2
Structure of assembly language
program of 8086
Using segment directives
My_data segment
…… ;data declaration (data segment of program)

My_data ends
My_code segment
Assume cs:my_code, ds:my_data
…….. ; program code
(code segment of program)

(code segment of program)


My_code ends
end start
Structure of assembly language
program of 8086
Using .data and .code directives
.model small
.data
…….;data declaration(data segment of program)
.code
……… ; programe code
Ends
end
Symbol , variables and constants
• Variables are the symbols whose value can be
dynamically varied during the run time.
• Assembler assign a memory location to a
variable which is not visible for the user
directly
• The assemble symbol consist of letters A-Z(a-
z),0-9 digit, special characters (_,@,$,?)
• Numeric constants can be represented as
binary or decimal or hexadecimal integer.
Valid constant
• Binary constant must have digit 0 or 1 and end
with letter ‘B’

• decimal constant must have digit 0 to 9 and


end with letter ‘D’
• hexadecimal constant must have digit 0 to
9,Ato F and end with letter ‘H’
Programming using assembler
• Addition of two numbers
• 8-bit
• 16-bit
• 32-bit
1)WAP for performing addition of two
8 bit Hexadecimal numbers
.model small
.code
Mov al,50h
Mov bl,20h
Add al,bl
Ends
end
.1)WAP for performing addition of two 8 bit Hexadecimal numbers .
.model small
data segment .data
No1 db 01h ; x db 25h
no2 db 02h y db 26h
Res db ? result db 00h
data ends .code
code segment Mov ax,@data
Assume cs: code, ds:data mov ds,ax
start: mov ax, data mov al,x
mov ds,ax mov bl,y
mov al,no1 add al,bl
add al,no2 mov result,al
mov res, al mov ah,4ch
int 03h int 21h
ends ends
end start end
.1)WAP for performing addition /subtraction of two 8 bit BCD numbers .
.model small
data segment .data
No1 db 01h ;
x db 25h
no2 db 02h
Res db ? y db 26h
data ends result db 00h
code segment .code
Assume cs: code, ds:data Mov ax,@data
start: mov ax,@ data mov ds,ax
mov ds,ax mov al,x
mov al,no1
mov bl,y
add al,no2
daa
Sub al,bl
mov res, al das
int 03h mov result,al
ends mov ah,4ch
end start int 21h
ends
output
Write
.model small
a program to add two 32 bit nos.
.data
x dd 00000025h
y dd 00000026h
result dd 00000000h
.code
mov ax,@data
mov ds,ax
mov ax,word ptr x
mov bx,word ptr y
add ax,bx
mov word ptr result,ax
mov ax,word ptr x+1
mov bx,word ptr y+1
add ax,bx
mov word ptr result+1,ax
mov ah,4ch
int 21h
ends
WAP for performing addition of two 16
bit Hexadecimal numbers.
.model small
.data
No1 dw 0001h
no2 dw 0010h
Res dw ?
.code
mov ax,@data
mov ds,ax
mov ax,no1
add ax,no2
mov res, ax
int 03h
ends
end
WAP for performing addition of two 16 bit numbers
.model smallwhere result greater than 16-bit
.data
No1 dw 0001h
no2 dw 0010h
Resm dw ?
Resl dw ?
.code
mov ax,@data
mov ds,ax
mov ax,no1
add ax,no2
Jnc next
Inc resm
next : mov resl, ax
int 03h
ends
end start
WAP for performing subtraction of two
16 bit Hexadecimal
Program Code:- Output:- numbers.
.model small
.data
no1 dw 0001h
no2 dw 0010h
Res dw ?
data ends
.code
mov ax,@data
mov ds,ax
mov ax,no1
sub ax,no2
mov res,ax
int 03h
ends
end
WAP to add series of number of 8 bit numbers
.model small
.data
Array db 2H,4H,6H,7H,5H
Rlsb db 00H
Rmsb db 00H
.code
mov ax,@data
Mov ds,ax
mov cx,0005H
Mov si,offset array
up: mov al,[si]
Add rlsb,al
jnc next
add rmsb,01H
next: inc si
dec cl
jnz up
int 03H
code ends
end
WAP to add series of BCD number of 8 bit
.model small
.data numbers
Array db 2H,4H,6H,7H,5H
Rlsb db 00H
Rmsb db 00H
.code
mov ax,@data
Mov ds,ax
mov cx,0005H
Mov si,offset array
up: mov al,[si]
Add AL, rlsb
daa
jnc next
add rmsb,01H
next: mov rlsb, al
inc si
dec cl
jnz up
int 03H
ends
Q. Program to multiply two 8-bit signed nos.
.model small
.data
x db -5h
y db -4h
res dw 0000h
.code
Mov ax,@data
mov ds, ax
Mov ax, 0000h
mov al, x
Mov bl, y
Imul bl
mov res, ax
ends
end
Program to multiply two 16-bit unsigned nos.
.model small
.data
x dw 0020H
y dw 0010H
res_l dw 0000H
res_h dw 0000H
.code
Mov ax, @data
mov ds, ax
Mov ax, x
Mov bx, y
Mul bl
Mov res_l, ax
Mov res_h, dx
ends
end
Program to multiply two 16-bit signed nos.
.model small
.data
x dw -12H
y dw -10H
res_l dw 0000H
res_h dw 0000H
.code
Mov ax, @data
mov ds, ax
Mov ax, x
Mov bx, y
Imul bl
Mov res_l, ax
Mov res_h, dx
ends
end
Write a program to divide two 16 bit nos.
.model small
.data
x dw 0008h
y dw 0002h
q dw 0000h
r dw 0000h
.code
mov ax,@data
mov ds,ax
mov ax,x
mov bx,y
div bx
mov q,ax
mov r,dx
ends
end
Write a program to divide 32/ 16 bit nos.
.model small
.data
x dd 22222222h
y dw 0002h
q dw 0000h
r dw 0000h
.code
mov ax,@data
mov ds,ax
mov ax, word ptr x
mov dx, word ptr x+1
div bx
Mov q,ax
mov r,dx
ends
end
Q. Program to multiply two 8-bit BCD nos.
.model small
.data
.model small
x db 5h
.data y db 4h
x db 5h res db 00h
y db 4h Res_m db 00h ;result more than 8-bit
res db 00h .code
.code Mov ax,@data
Mov ax,@data mov ds, ax
mov ds, ax Mov ax, 0000h
Mov ax, 0000h Mov ch ,00h
Mov ch ,00h mov cl, y
mov cl, y up : add al, x
up : add al, x daa
daa Jnc next
Loop up Inc res_m
mov res, al Next : Loop up
ends mov res, al
end ends
Q. Program to divide two 8-bit BCD nos.
model small .
.data
x db 5h
y db 4h
rem db 00h
q db 00h
Res_m db ooh ;result more than 8-bit
.code
Mov ax,@data
mov ds, ax
Mov al,x
Next : sub al,y
Das
Inc q
Cmp al,y
Jnc next
Inc res_m
mov rem, al
ends
end
WAP
.modelto find smaller no from array of 8 bit numbers
small
.data
Array db 2H,4H,6H,7H,5H
smaller db 00H
.code
mov ax,data
Mov ds,ax
mov cx,0005H
Mov si,offset array
mov al,[si]
Dec cx
Up : Inc si
Cmp AL, [si]
jc next
Mov al,[si]
next: loop up
Mov smaller,al
int 03H
ends
end
WAP to find lager no from array of 8 bit numbers
.model small
.data
Array db 2H,4H,6H,7H,5H
large db 00H
.code
mov ax,data
Mov ds,ax
mov cx,0005H
Mov si,offset array
mov al,[si]
Dec cx
Up : Inc si
Cmp AL, [si]
jnc next
Mov al,[si]
next: loop up
Mov large,al
int 03H
ends
end
Program to compare two strings without
model small using string instructions.
.data
s1 db 'COMPUTER$'
s2 db 'COMPUTER$‘
s1_len db 00H
s2_len db 00H
msg1 db 'Strings are equal$'
msg2 db 'Strings are not equal$'
.code
movax, @data
mov ds, ax
lea si, s1
next:
mov al, [si]
cmp al, '$'
je exit
incsi
inc s1_len
jmp next
exit:
compare two strings
movsi, offset s2
next1:
mov al, [si]
cmp al, '$'
je exit1
incsi
inc s2_len
jmp next1
exit1:
mov al, s1_len
cmp al, s2_len
jne exit2
mov ah, 09H
lea dx, msg1
int 21H
jmp exit3
compare two strings
exit2:
mov ah, 09H
lea dx, msg2
int 21H
exit3:
mov ah, 4CH
int 21H
ends
end
Program
model small
to compare two strings using
.data string instructions
s1 db 'COMPUTER$'
s2 db 'COMPUTER$'
s1_len db 00H
s2_len db 00H
msg1 db 'Strings are equal$'
msg2 db 'Strings are not equal$'
.code
Mov ax, @data; Initialise data segment
mov ds, ax
Mov es, ax ; Initialise extra segment
Mov si, offset s1 ; Initialise memory pointer for source string
Next: mov al, [si]
cmp al, '$ ‘ ; compare with '$'
compare two strings
je exit
incsi
inc s1_len
jmp next
exit:
movsi, offset s2
next1:
mov al, [si]
cmp al, '$'
je exit1
incsi
inc s2_len
Compare two strings
jmp next1
exit1:
mov al, s1_len
cmp al, s2_len
jne exit2
cld; clear direction flag
Mov ch, 0
mov cl, s1_len
Mov si, offset s1
mov di, offset s2
up: cmpsb ; compare character of source string with
character of destination string
jnz exit2
loop up
Compare two strings
Inc si
mov ah, 09H
lea dx, msg1
int 21H
jmp exit3
exit2:
mov ah, 09H
lea dx, msg2
int 21H
exit3:
mov ah, 4CH
int 21H
ends
end
Program to arrange string in reverse order.
.model small
.data
s1 db 'COMPUTER DEPARTMENT$'
s2 db 50 DUP('$')
msg1 db 'The source string is: $'
msg2 db 'The reverse string is: $'
count db 00H
.code
movax, @data
mov ds, ax
movsi, offset s1
next: mov al, [si]
cmp al, '$'
je exit
String in reverse order
Inc si
inc count
jmp next
exit: mov di, offset s2
up: dec si
mov al, [si]
mov [di], al
inc di
dec count
jnz up
mov ah, 09H
lea dx, msg1
int 21H
String in reverse order
mov ah, 09H
lea dx, s1
int 21H
mov ah, 09H
lea dx, msg2
int 21H
mov ah, 09H
lea dx, s2
int 21H
ends
end
.model small
Program to find length of string.
.data
s1 db 'COMPUTER$'
Len db 00H
.code
Mov ax, @data
Mov ds, ax
Mov si, offset s1
next: mov al, [si]
cmp al, '$'
je exit
Inc si
Inc len
jmp next
exit: mov ah, 4CH
int 21H
ends
End
Length=8
Program to concatenate two strings
.model small
.data
s1 db 'COMPUTER$'
s2 db 'DEPARTMENT$'
msgdb 'Concatenated string: $'
.code
movax, @data
mov ds, ax
movsi, offset s1
next: mov al, [si]
cmp al, '$'
je exit
incsi
jmp next
exit:
mov di, offset s2
up: mov al, [di]
cmp al, '$'
je exit1
concatenate two strings
mov [si], al
Inc si
inc di
jmp up
exit1: mov al, '$'
mov [si], al
mov ah, 09H
lea dx, msg
int 21H
mov ah, 09H
lea dx, s1
int 21H
Mov ah, 4CH
int 21H
ends
end
Program to count odd numbers in an array
.model small
.data
array dw 134H, 65H, 876H, 976H, 23H
odd_no db 00H
.code
movax, @data
mov ds, ax
mov cx, 0005H
movsi, offset array
next: movax, [si]
rorax, 1
jncdn
incodd_no
dn:add si, 2
loop next
mov ah, 4CH
int 21H
ends
end
Program to count even numbers in an array
.model small
.data
array dw 134H, 65H, 876H, 976H, 23H
even_nodw 00H
.code
movax, @data
mov ds, ax
mov cx, 0005H
movsi, offset array
next: movax, [si]
rorax, 1
jcdn
inceven_no
dn:add si, 2
loop next
mov ah, 4CH
int 21H
ends
end
Program to find number is even/odd.
.model small
.data
Num db 87H
odd1 db 00H
even1 db 00H
.code
Mov ax, @data
mov ds, ax
mov al, num
ror al, 1
Jnc dn
rol al, 1
mov odd1, al
jmp exit
dn: rol al, 1
mov even1, al
exit: ends
end
Program to add all odd numbers in an array
model small of 10 numbers
.data
array db 6, 5, 21, 3, 8, 9, 11, 13, 1, 2
arrdb 10 DUP(0)
count dw 00H
sum db 00H
.code
movax, @data
mov ds, ax
mov cx, 000AH
movsi, offset array
mov di, offset arr
up: mov al, [si]
ror al, 1
jncdn
rol al, 1
odd numbers in an array
inc count
mov [di], al
inc di
dn: inc si
loop up
Mov cx, count
Mov si, offset arr
next: mov al, [si]
add sum, al
Inc si
loop next
mov ah, 4CH
int 21H
ends
end
Program
.model small
to check number is positive or negative
.data
Num db -9H
Pos db 00H
neg1 db 00H
.code
Mov ax, @data
mov ds, ax
mov al, num
rol al, 1
Jnc dn
ror al, 1
mov neg1, al
jmp exit
dn: ror al, 1
Mov pos, al
exit: ends
end
Assending order
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
Ascending order
DOWN: INC SI
DEC CL
JNZ UP1
DEC CH
JNZ UP2

INT 3
CODE ENDS
END START
Descending
DATA SEGMENT
order
x DW 42H,34H,26H,17H,09H
LEN EQU 05
ASCD DB 10 DUP(0)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV BX,LEN-1
MOV CX,BX
UP1: MOV BX,CX
LEA SI,X
UP: MOV AX,[SI]
Descending order
MOV DX,[SI+2]
CMP AX,DX
JA DOWN
MOV [SI],DX
MOV [SI+2],AX
DOWN: INC SI
INC SI
DEC BX
JNZ UP
DEC CX
JNZ UP1
MOV AH,4CH
INT 21H
CODE ENDS
END START
Program to count even and/or odd nos in an array
.model small
.data
array dw 13H, 65H, 87H, 97H, 23H, 20H, 2H, 8H, 10H, 18H
even_nodw 00H
odd_nodw 00H
.code
Mov ax, @data
mov ds, ax
mov cx, 000AH
Mov si, offset array
next: movax, [si]
Ror a x, 1
Jc dn
Inc even_no
....to count even and/or odd nos in an
array
jmp down
dn: inc odd_no
down: add si, 2
loop next
Mov ax, even_no
Mov bx, odd_no
ends
end
Program to count number of 1’s in a
.model small given number.
.data
Num db 0FFH
ones db 00H
.code
Mov ax, @data
mov ds, ax
mov cx, 0008H
mov al, num
up: ror al, 1
Jnc dn
inc ones
dn: loop up
mov al, ones
mov ah, 4CH
int 21H
ends
end
Program to count number of 0’s in a
.model small
.data given number
numdb 0AAH
zeros db 00H
.code
movax, @data
mov ds, ax
mov cx, 0008H
mov al, num
up: ror al, 1
jcdn
inc zeros
dn: loop up
mov al, zeros
mov ah, 4CH
int 21H
ends
end
Program to count positive as well as
negative numbers in array
.model small
.data
array dw -0009H, 0010H, -0008H, 0001H, 0002H
posdw 00H
neg1 dw 00H
.code
movax, @data
mov ds, ax
mov cx, 0005H
lea si, array
up: mov al, [si]
rol al, 1
.......count positive as well as negative
numbers in array
Jc nev
Inc pos
Jmp dn
nev: inc neg1
dn: add si, 2
loop up
exit: movax, pos
Mov bx, neg1
mov ah, 4CH
int 21H
ends
end
. Program to add all positive numbers
.model small
.data
in array of 10 numbers
array dw -0009H, 0010H, -0008H, 0001H, 0002H, 0006H, 0008H,
0005H, -0001H, -0044H
posdw 0000H
sum dw 0000H
.code
movax, @data
mov ds, ax
mov cx, 000AH
lea si, array
up: mov al, [si]
rol al, 1
jcdn
incpos
movbx, [si]
add sum, bx
........add all positive numbers in array
of 10 numbers
movbx, sum
dn: add si, 2
loop up
exit:
movax, pos
movbx, sum
mov ah, 4CH
int 21H
ends
end
Convert
.model small
lowercase to upper case
.data
s1 db 'computer$'
s2 db 20 dup(‘$’)
.code
movax, @data
mov ds, ax
Mov si, offset s1
Mov di, offset s2
next: mov al, [si]
cmp al, '$'
je exit
Sub al,20h ; convert to upper case
Mov [di],al
Inc si
Inc di
Jmp next
Exit : ends
end
Convert upper case to lowercase
.model small
.data
s1 db 'COMPUTER$'
s2 db 20 dup(‘$’)
.code
movax, @data
mov ds, ax
Mov si, offset s1
Mov di, offset s2
next: mov al, [si]
cmp al, '$'
je exit
add al,20h ; convert to lower case
Mov [di],al
Inc si
Inc di
Jmp next
Exit : ends
end
Thank you…..

You can mail your Queries to :


[email protected]

60

You might also like