Lab Manual of Assembly Language (Amna)
Lab Manual of Assembly Language (Amna)
Lab Manual of Assembly Language (Amna)
-22S-028
1
Assembly Language Sir Mustafa
Student Information:
Name : Amna
ID : BCB-22F-021
Instructor : Sir Mustafa
Course : Computer Organization & Assembly Languages
Semester : 4th
Acknowledgement
I would like to express my deepest gratitude to Sir Mustafa for their unwavering dedication and exceptional
guidance throughout Assembly Language. Their passion for teaching and commitment to fostering a
positive learning environment have been instrumental in my academic journey.
I am truly grateful for the patience and encouragement that Sir Mustafa has provided, creating a space
where curiosity is celebrated, and knowledge is cultivated. Their insightful feedback and constructive
criticism have been invaluable in shaping my understanding and skills in programming.
I also appreciate Her's commitment to going above and beyond in ensuring that each student reaches their
full potential. The enthusiasm and creativity they bring to the classroom have not only made learning
enjoyable but have also inspired me to strive for excellence.
I extend my heartfelt thanks to her for being an inspiring mentor and for imparting not only knowledge but
also the importance of curiosity, critical thinking, and lifelong learning. I am truly fortunate to have had the
privilege of being their student.
Thank you, Sir Mustafa, for your tireless efforts, guidance, and the positive impact you have made on my
educational journey.
1.1.2 Memory
Memory is also a very mandatory part of the computer to store data. The data stored in memory is
called data element. It can only perform two operations (Read and write). The read operation refers to
fetching data from memory to process, basically it copies the duplicate of data. The write operation refers to
overwrite or new write data into the memory.
1.1.3 Microprocessor
A microprocessor is a central processing unit (CPU) that is contained on a single integrated circuit (IC)
or chip. It functions as the brain of a computer or other electronic devices, carrying out instructions and
performing arithmetic and logic operations. Microprocessors are crucial components in various devices,
including personal computers, smartphones, embedded systems, and many other electronic gadgets.
The key characteristics of a microprocessor include its ability to fetch, decode, and execute
instructions stored in the computer's memory. It performs tasks such as data manipulation, arithmetic
calculations, and control operations to execute programs and run applications.
Microprocessors come in different architectures, speeds, and capabilities, and they have played a
pivotal role in the advancement of computing technology by enabling the development of increasingly
powerful and compact electronic devices.
The ALU performs arithmetic and logic operations, such as addition, subtraction, AND, OR, and NOT
operations. It is a key component for executing mathematical and logical instructions.
Control Unit
The control unit manages the flow of data between the processor's different components. It
interprets and decodes instructions, coordinates the activities of the ALU and other units, and
ensures that instructions are executed in the correct sequence.
Registers
Registers are small, fast storage locations within the CPU used for temporarily holding data and
instructions. The registers are part of the execution unit and facilitate quick access to data needed
for processing.
Instruction Decoder
This unit is responsible for decoding machine instructions fetched from memory, breaking them
down into operations that the ALU and other units can understand and execute.
Address Bus:
This is a set of wires used to carry information about the memory address from the microprocessor
to the memory or other peripheral devices.
Data Bus:
The data bus is a set of wires used to carry data between the microprocessor and memory or
peripheral devices.
Control Bus:
The control bus consists of various control signals that coordinate and control the activities of the
other components in the microprocessor and the connected devices.
Mnemonic Codes
Assembly language uses mnemonic codes or symbols that represent specific machine code
instructions. These mnemonics are more human-readable than raw binary code.
A program is written in assembly language using these mnemonics along with operand values, which
are specific data or memory addresses.
An assembler is used to translate the assembly code into machine code. The assembler converts
each assembly language instruction into its corresponding binary representation.
The resulting machine code is a set of instructions that the CPU can directly execute. Each instruction
corresponds to a low-level operation, such as moving data between registers, performing arithmetic
operations, or branching based on conditions.
Assembly language provides direct access to the computer's hardware resources, including registers
and memory. Programmers can control these resources at a very detailed level.
Processor-Specific
Assembly language is closely tied to the architecture of a specific CPU. Programs written in assembly
language are not portable between different architectures without modification.
Low-Level Operations
Efficiency
Assembly language is often used for tasks that require a high level of efficiency and control, such as
writing operating systems, device drivers, or performance-critical code.
84 54 01010100 T T
85 55 01010101 U U
86 56 01010110 V V
87 57 01010111 W W
88 58 01011000 X X
89 59 01011001 Y Y
90 5A 01011010 Z Z
91 5B 01011011 [ [ left square bracket
92 5C 01011100 \ \ backslash
93 5D 01011101 ] ] right square bracket
94 5E 01011110 ^ ^ caret / circumflex
95 5F 01011111 _ _ underscore
96 60 01100000 ` ` grave / accent
97 61 01100001 a a
98 62 01100010 b b
99 63 01100011 c c
100 64 01100100 d d
101 65 01100101 e e
102 66 01100110 f f
103 67 01100111 g g
104 68 01101000 h h
105 69 01101001 i i
106 6A 01101010 j j
107 6B 01101011 k k
108 6C 01101100 l l
109 6D 01101101 m m
110 6E 01101110 n n
111 6F 01101111 o o
112 70 01110000 p p
113 71 01110001 q q
114 72 01110010 r r
115 73 01110011 s s
116 74 01110100 t t
151 97 10010111 - —
152 98 10011000 - ˜
153 99 10011001 - ™
154 9A 10011010 - š
155 9B 10011011 - ›
156 9C 10011100 - œ
157 9D 10011101 - •
158 9E 10011110 - ž
159 9F 10011111 - Ÿ
2.3.2 Create a program which prints 0 on screen with the help of ASCII code.
.model small
.stack 100h
.data
.code
Start:
Mov ah, 2h
Mov dl,48 ; 48 is ASCII code of 0
Int 21h
Mov ah, 4ch
Int 21h
End start
2.3.3 Create a program which prints A character with the help of ASCII code.
.model small
.stack 100h
.data
.code
Start:
Mov ah, 2h
Mov dl,65 ; 65 is ASCII code of capital A
Int 21h
Mov ah, 4ch
Int 21h
End start
2.3.4 Create a program which prints your name with the help of function 02.
.model small
.stack 100h
.data
.code Start:
Mov ah, 2h
Mov dl,'S'
Int 21h
Mov ah, 2h
Mov dl,'H'
Int 21h
Mov ah, 2h
Mov dl,'A'
Int 21h
Mov ah, 2h
Mov dl,'I'
Int 21h
Mov ah, 2h
Mov dl,'R'
Int 21h
Mov ah, 4ch
Int 21h
End start
2.3.5 Write all errors which you know about in today’s class and write how you correct them.
3.3.2 Create a program which take input and print it on output screen.
.model small
.stack 100h
.data .code
start:
mov ah,01; 01h used for take char input from user
int 21h ; inturrpt
mov ah,02 ; 02 used for print single character on output
screen mov dl,al int 21h
mov ah,4ch ; 4ch used to terminate the
program int 21h end start
3.3.3 Create a program which takes input a character from user and print it on new line.
.model small
.stack 100h
.data .code
start:
mov ah,01; 01h used for take char input from user
int 21h ; inturrpt
mov bl,al ; input value store in bl register
mov ah,02
3.4 Task
3.4.1 create a program which take 7 input and store using registers and print on new line
Ascending/descending order.
.model small
.stack 100h
.data .code
start:
MOV AH,01h ; Input 1
INT 21H
MOV BL,AL
MOV AH,01h ; Input 2
INT 21H
MOV BH,AL
MOV AH,01h ; Input 3
INT 21H
MOV CL,AL
MOV AH,01h ; Input 4
INT 21H
MOV CH,AL
MOV AH,01h ; Input 5
INT 21H
MOV DH,AL
; Line Feed
MOV AH,02
MOV DL,10
INT 21H
;ASSENDING ORDER
MOV AH,02 ; Output 1
MOV DL,BL
INT 21H
MOV AH,02 ; Output 2
MOV DL,BH
INT 21H
MOV AH,02 ; Output 3
MOV DL,CL
INT 21H
MOV AH,02 ; Output 4
MOV DL,CH
INT 21H
MOV AH,02 ; Output 5
MOV DL,DH
INT 21H
; Line Feed
MOV AH,02
MOV DL,10
INT 21H
;DECENDING ORDER
MOV AH,02 ; Output 1
MOV DL,DH
INT 21H
MOV AH,02 ; Output 2
MOV DL,CH
INT 21H
MOV AH,02 ; Output 3
MOV DL,CL
INT 21H
MOV AH,02 ; Output 4
MOV DL,BH
INT 21H
MOV AH,02 ; Output 5
MOV DL,BL
INT 21H
mov ah,4ch ; 4ch used to terminate the
program int 21h end start
21h mov
ah,02 mov
dl,"*" int
21h ;line
feed mov
ah,02 mov
dl,0Ah int
21h ;Take
Input mov
ah,01 int
21h mov
bl,al ;li
ne feed mov
ah,02 mov
dl,0Ah int
21h mov
ah,02 mov
dl,bl int
21h ;line
feed mov
ah,02 mov
dl,0Ah int
21h ;
border mov
ah,02 mov
dl,"*" int
21h mov
ah,02 mov
dl,"*" int
21h mov
ah,02 mov
dl,"*" int
21h mov
ah,02 mov
dl,"*" int
21h mov
ah,02 mov
dl,"*" int
21h mov
ah,02 mov
dl,"*" int
21h mov
ah,02 mov
dl,"*" int
21h mov
ah,02 mov
dl,"*" int
21h mov
ah,02 mov
dl,"*" int
21h
mov ah,4ch ; 4ch used to terminate the
program int 21h end start
CH#4 Variable
4.1 Introduction:
In 8086 assembly language, a variable is a memory location. The following directives define variables of different
sizes.
A directive (i.e. a command to the assembler) is used to define variables. In 8086 assembly language, the directive db
defines a byte sized variable; dw defines a word sized variable (16 bits) and dd defines a double word (long word, 32
bits) variable.
A Java variable of type int may be implemented using a size of 16 or 32 bits, i.e. dw or dd is used. A Java variable of
type char, which is used to store a single character, is implemented using the db directive.
The definition of the variable colour demonstrates how to declare an array of characters of size 80, which contains
undefined values. Introduction to 8086 Assembly Language Programming( alp5) 2 The purpose of dup is to tell the
assembler to duplicate or repeat the data definition directive a specific number of times, in this case 80 dup specifies
that 80 bytes of storage are to be set aside since dup is used with the db directive.
The (?) with the dup means that storage allocated by the directive is unitialised or undefined.
i and k are byte sized variables, where i is initialised to 20 and k is left undefined.
num is a 16-bit variable, initialised to 4000 and the variable large is a 32-bit variable, initialised to 15000.
LEA is more powerful because it also allows you to get the address of an indexed variables. Getting the address of the
variable can be very useful in some situations, for example when you need to pass parameters to a procedure.
4.3 Examples
4.3.1 Function 9 : is use to display string on screen
Object: To display a string on the screen
.model small
.stack
100h .data
str1 db 'Sindh Madressatul Islam University$' ;define byte
.code main
proc ;initial
ize ds
Mov ax,@data ;name of data segment defined
by .data Mov ds,ax ;initialize ds;display
string lea dx,str1 ;get string mov ah,09h
;display string function int 21h
;display string
;return to DOS
mov ah,4ch ;exit program with return code int
21h ;DOS exit
end main
4.4 Task
4.4.1 . Assembly code displaying bio data (multiple strings) on new line.
.model small
.stack
100h .data
str1 db 'Name: Shair Muhammad.$' ;define
byte str2 db 'Roll no.: BCB-22S-028.$' str3 db
'Batch: A.$'
str4 db 'University: Sindh Madressatul Islam University.$'
.code main
proc ;initial
ize ds
mov ax,@data ;name of data segment defined by .data mov
ds,ax ;initialize ds
;display string
lea dx,str1 ;get string mov
ah,09h ;display string function int
21h ;display string mov
dl,10 ;line feed mov ah,02h int
21h ;display string
The small letters of the alphabet start from 97 to z as like the capital letter start from 65 to 91. When you
press our input 65 decimal the computer it will consider as ‘A’.
21h mov
ah,4ch int
21h end
start
6.3 Task
6.3.1 Create a program which convert capital to small
;lab5 Create a program which convert capital to small
.model small
.stack
100h .data
str1 db 'Enter upper case letter: - $'
str2 db 0dh,0ah,'Lower case: - $'
.code start:
;initialie data segment
Mov ax,@data
Mov ds,ax
;display string
mov ah,09 lea
dx,str1 int 21h
;input character
mov ah, 01h int
21h
;lower to upper case
conversion add al,32 mov
bl,al ;display string mov
ah,09 lea dx,str2 int 21h
;display string mov ah,02
mov dl,bl int 21h mov
ah,4ch int 21h end start
text db
10,"---------------------------------------------",10,"AMAZING
FACT",10,"Enter Any number from range (1 to 9): $"
output_label db 10,"Your entered number is: $" output_label2
db 10,"Symbol of entered number is: $"
.code start:
mov ax,@data mov
ds,ax mov ah,09
lea dx,text int 21h
mov ah,01h int 21h
cmp al,13 je exit
mov ah,09 lea
dx,output_label int
21h mov ah,02
mov dl,al int 21h
cmp al,50 je addrat
sub al,16 jmp next
addrat: add al,14
next: mov ah,09
lea dx,output_label2
int 21h mov ah,02
mov dl,al int 21h
jmp start exit:
mov ah,4ch int 21h
end start
A FOR loop is implemented using the LOOP instruction. The counter for the loop is the CX register, which is
initialized to loop count, which is the number of times the loop is executed. Execution of the LOOP
instruction causes CX to be decremented automatically. If CX becomes 0, the next instruction after loop is
done.
7.2 Examples
7.2.1 Write a program that prints a character 100 times using loop.
.model small
.stack 100h .code main proc mov
ah,02h ;Display a character
mov cx,100 ;Number of times loop will
execute mov dl,'*' ;Character to be printed
print: ;loop starts from here
int 21h ;Loop body
Loop print ;executes the For
loop mov ah,4ch ;DOS exit int
21h
main endp
end main
7.2.3 Write a program that prints a character 10 times using loop on next line.
.model small
.stack 100h
.code
start: mov
cx,10
print:
mov ah,02h
mov dl,'*'
int 21h
mov ah,02h
mov dl,10
int 21h loop
print mov
ah,4ch int
21h end start
7.2.4 Write a program that prints STRINGS in new line using loop.
.model Small
.Stack 100h
.data
MSG1 db "Shair Muhammad$"
MSG2 db "Cyber Security$"
MSG3 db "SMI University $"
.code
start:
mov ax, @data
mov ds, ax mov
cx, 4h
Print:
mov ah, 09h
mov dx, offset MSG1
int 21h
mov ah, 4ch
mov
ah, 02h
mov dl, 0dh
Int 21h
mov ah, 02h
mov dl, 0ah
Int 21h
Loop Print
mov ah, 4ch
Int 21h end start
7.3 Task
7.3.1 Write a Program to Display ASCII characters from A to Z through Looping.
.model Small
.Stack 100h
.code
start:
mov cx, 26
mov al,'A'
upper_letter:
mov ah,02 mov
dl,al int 21h
inc al loop
upper_letter
mov ah,4ch
int 21h end start
mov ah,02
mov dl,al
int 21h
dec al
loop capital_reverse_alphabat
mov ah,4ch int 21h end
start
7.3.7 Write a Program to Display ASCII characters from A-Z & z-a through looping.
.model Small
.Stack 100h
.code
start:
mov cx, 26
mov al,'A'
CapitalLetter:
mov ah,02 mov
dl,al int 21h
inc al loop
CapitalLetter
mov ah,02 mov
dl,0Ah int 21h
mov cx, 26 mov
al,'z'
ReverseSmallLetter:
mov ah,02
mov dl,al int 21h
dec al loop
ReverseSmallLetter
mov ah,4ch int 21h
end start
7.3.8 Write a Program to Display ASCII characters from Z-A & a-z through looping.
.model Small
.Stack 100h
.code
start:
mov cx, 26
mov al,'Z'
ReverseCapitalLetter:
mov ah,02
mov dl,al
int 21h
dec al
loop ReverseCapitalLetter
mov ah,02 mov dl,0Ah
int 21h mov cx, 26
mov al,'a'
SmallLetter:
mov ah,02
mov dl,al
int 21h inc
al loop
SmallLetter
mov ah,4ch
int 21h end start
0iiiiii0iiiiii0iiiiii0iiiiii0
.model Small
.Stack 100h
.code
start:
mov cx, 4
outer_loop:
mov ah,02
mov dl,48
int 21h
push cx mov
cx,6
inner_loop:
mov ah,02
mov dl,'i'
int 21h loop
inner_loop pop
cx loop outer_loop
mov
ah,4ch int
21h end start
Following are the conditional jump instructions used on signed data used for arithmetic operations.
Instruction Description Flags Tested
1 JE/JZ Jump Equal or Jump Zero ZF
2 JNE/JNZ Jump not Equal or Jump Not Zero ZF
3 JG/JNLE Jump Greater or Jump Not Less/Equal OF, SF, ZF
4 JGE/JNL Jump Greater/Equal or Jump Not Less OF, SF
5 JL/JNGE Jump Less or Jump Not Greater/Equal OF, SF
6 JLE/JNG Jump Less/Equal or Jump Not Greater OF, SF, ZF
Following are the conditional jump instructions used on unsigned data used for logical operations –
Instruction Description Flags Tested
1 JE/JZ Jump Equal or Jump Zero ZF
2 JNE/JNZ Jump not Equal or Jump Not Zero ZF
3 JA/JNBE Jump Above or Jump Not Below/Equal CF, ZF
4 JAE/JNB Jump Above/Equal or Jump Not Below CF
5 JB/JNAE Jump Below or Jump Not Above/Equal CF
6 JBE/JNA Jump Below/Equal or Jump Not Above AF, CF
The following conditional jump instructions have special uses and check the value of flags
Instruction Description Flags Tested
1 JXCZ Jump if CX is Zero none
2 JC Jump If Carry CF
3 JNC Jump If No Carry CF
4 JO Jump If Overflow OF
5 JNO Jump If No Overflow OF
6 JP/JPE Jump Parity or Jump Parity Even PF
7 JNP/JPO Jump No Parity or Jump Parity Odd PF
It should be noted that the TEST instruction doesn't make any changes to the operands used with the
instruction. The following is an example of a TEST instruction.
It should be noted that the CMP instruction also does not affect the operands. When the destination operand
and source operand are equal, ZF will be set to 1. If the destination operand is less than the source operand,
CF will be set to 1. In all the remaining conditions, the respective flags will be set to 0.
8.2 Examples
8.2.1 Create a program using JZ condition.
.model small
.stack
100h .code
start:
mov ax,6
cmp ax, 6 ; ax-6 and store value in
ax jz true Mov ah, 02 mov dl, 'F' int
21h
jmp exit
true:
mov ah, 02 mov
dl , 'T'
int 21h
exit:
mov ah, 4ch
int 21h end
start
.stack
100h .code
start:
mov ax,5
cmp ax, 4
je true Mov
ah, 02 mov
dl, 'F' int
21h jmp
exit true:
mov ah, 02
mov dl , 'T'
int 21h exit:
mov ah, 4ch
int 21h end
start
.stack
100h .code
start:
mov ax,4
cmp ax, 5
jg true Mov
ah, 02 mov
dl, 'F' int
21h jmp
exit true:
mov ah, 02
mov dl , 'T'
int 21h exit:
mov ah, 4ch
int 21h end
start
8.3 Task
8.3.1 Create program using different jump category given in theory lecture.
8.3.2 Create any five programs using different jumps category.
• JMP (Jump): This is an unconditional jump instruction. It transfers the control of the program to a
specified address without any conditions.
• JE (Jump if Equal): This instruction jumps to a specified address if the Zero Flag (ZF) is set, indicating
that the previous operation resulted in an equal condition.
• JNE (Jump if Not Equal): This instruction jumps to a specified address if the Zero Flag (ZF) is clear,
indicating that the previous operation did not result in an equal condition.
• JG (Jump if Greater), JGE (Jump if Greater or Equal), JL (Jump if Less), JLE (Jump if Less or Equal):
These are conditional jump instructions based on the sign, zero, carry, and overflow flags. They allow
for branching based on the comparison results of two values.
• JO (Jump if Overflow): Jumps to the specified address if the overflow flag (OF) is set.
• JNO (Jump if Not Overflow): Jumps to the specified address if the overflow flag (OF) is clear.
• JS (Jump if Sign), JNS (Jump if Not Sign): These instructions jump based on the state of the sign flag
(SF), which indicates if the result of the previous operation was positive or negative.
• By using stack: both loops start with cx register. Push the outer loop cx into stack and then again
initialize cx for inner loop in loop after completed inner loop, pop stack value to cx register from the
stack.
• By using bx register. Create outer loop with cx register and inner of that loop move cx value into bx
register then create inner loop with cx register as like after competed inner loop, again move bx
value into cx register.
These are two common methods to create nested loop. Maybe more than method for nested loop etc.
9.2.2 Create a program to create while loop until press enter key
.model small
.stack 100h
.data .code
start:
mov dx,0
mov ah,01
int 21h
while_:
end_case: mov
ah, 4ch int
21h end start
9.2.5 OR (Create a program using OR condition) OR = when both condition are true.
.model small
.stack 100h
.code start:
mov ah,01
int 21h cmp
al,'Y' je
then cmp
al, 'y' je
then then :
mov dl, al
mov ah ,02
int 21h jmp
endif_ endif_:
mov ah,4ch
int 21h
end start
9.2.6 AND (create a program using And logic means when both conditions are true than
program will execute otherwise it terminates).
.model small
.stack 100h
.data .code
start:
mov ah,01
int 21h cmp
al,'A' je
endif_ cmp
al, 'Z' je
endif_ mov
dl, al mov
ah ,02 int
21h jmp
endif_
endif_:
l1 mov
ah,4ch int
21h end start
9.4 Task
9.4.1 Create program Using AND or OR logic.
.model small
.stack
100h .code
start:
mov al,00000001b
or al,00000101b ; use or gate
;result00000101b ; print 5 add
al,48 ; to print decimal number
mov ah,02 mov dl,al int 21h
mov ah,4ch int 21h end start
exit jmp
start exit:
mov ah,4ch
int 21h end
start
21h mov
dl,10 mov
ah,02 int
21h mov al,
msg1 mov
bl,msg2 cmp
al,bl je
equal cmp
al,bl jne
not_equal
not_equal:
mov ah,09
lea dx,cmp2
int 21h equal:
mov ah,09
lea dx,cmp1
int 21h mov
ah,4ch int 21h
end start
10.3.2 Take input a string and stored variable than print it on screen.
.model small
.stack
100h .data
var1 db 100 dup('$')
.code
begin:
mov ax,@data
mov ds,ax mov
si,offset var1
l1: mov ah,01
int 21h
cmp al,13 ; Ascii of enter
key je pend mov [si],al
inc si jmp l1 pend:
mov dx, offset var1
mov ah,09 int 21h
mov ah, 4ch int
21h end begin
10.3.5 Create a program using string instruction stosb to store content in variable from al
register.
.model small
.stack 100h
.data
text1 db 'hello$' text2
db 5 dup('$')
.code begin:
Mov ax ,@data
mov ds,ax
Mov es,ax
lea di, text1
Cld Mov
al,'A' stosb
mov dx, offset text1
mov ah,09 int 21h
mov ah, 4ch int 21h
end begin
10.3.6 Create code which store A 5 times in variable which already contain any string using
stosb string instruction.
.model small
.stack 100h .data
str1 db "hello$"
str2 db 5 dup('0$')
.code start:
mov ax,@data
mov es,ax
mov ds,ax
cld mov
cx,5 mov
al,'A' lea
di, str1
repne stosb
mov ah,09
lea dx, str1
int 21h
mov ah,4ch
int 21h end
start
cld lodsb
mov ah,02
mov dl, al
int 21h mov
ah,4ch int
21h end start
• AND, refers to logical operation that true when all conditions are true else false.
• OR also refers to logical addition operation that true when anyone of them is true.
• XOR is a logical operation that true when two different operands if both operands are same then, it
will false.
• NOT is a also logical instructions that change the bit states. • The results of the operation-is
stored in the destination,
• which must be a register or memory location.
• The source may be a constant, register, or memory location.
• However, memory-to-memory operations are not allowed.
• The highest bit is copied into both the Carry flag and into the lowest bit.
• No Bits are lost.
• ROR (rotate right) shifts each bit to the right.
• The lowest bit is copied into both carry flag and into the highest bit.
• No bits are lost.
.stack
100h .code
start:
mov ax,2
mov bx,4
and ax,bx
add ax,48
mov ah,02
mov dx,ax
int 21h
mov ah,4ch
int 21h end
start