Lab Manual of Assembly Language (Amna)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 68

Shair Muhammad / BCB

-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

Program : Cyber Security


Department : Computer Science
Year : Spring 2023

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.

CH#1. INTRODUCTION TO COMPUTER ORGANIZATIONS & ASSEMBLY LANGUAGE................................................................5

Saqlain hassan / BCB


-22F-027
2
Assembly Language Sir Mustafa

1.1 COMPUTER ORGANIZATION..........................................................................................................................................................5


1.2 ASSEMBLY LANGUAGE................................................................................................................................................................. 6
CH#2 INSTRUCTION TO RUN THE ASSEMBLY PROGRAM........................................................................................................... 8
2.1 REQUIREMENT............................................................................................................................................................................ 8
2.2 DOC FUNCTION/SUB ROUTINE....................................................................................................................................................... 8
2.3 ASCII TABLE OF 8086 MICROPROCESSOR.......................................................................................................................................8
2.4 LAB TIME................................................................................................................................................................................. 16
CH#3 BASIC DOS FUNCTION.............................................................................................................................................. 18
3.1 INTRODUCTION:....................................................................................................................................................................... 18
3.2 SUB ROUTINE/ DOS FUNCTIONS................................................................................................................................................. 18
3.3 LAB TIME................................................................................................................................................................................ 18
3.4 TASK...................................................................................................................................................................................... 20
CH#4 VARIABLE................................................................................................................................................................ 24
4.1 INTRODUCTION:....................................................................................................................................................................... 24
4.2 GETTING THE ADDRESS OF A VARIABLE.......................................................................................................................................... 24
4.3 EXAMPLES............................................................................................................................................................................... 24
4.4 TASK...................................................................................................................................................................................... 26
CH#5 EXCHANGE KEYWORD.............................................................................................................................................. 29
5.1 INTRODUCTION......................................................................................................................................................................... 29
5.2 LEGAL COMBINATION OF OPERANDS FOR XCHG...............................................................................................................................29
5.3 PRACTICAL FOR EXCHANGE KEY WORD...........................................................................................................................................29
CH#6 CASE CONVERSION.................................................................................................................................................. 32
6.1 INTRODUCTION......................................................................................................................................................................... 32
6.2 PRACTICAL EXAMPLES................................................................................................................................................................. 32
6.3 TASK...................................................................................................................................................................................... 34
CH#7 UNCONDITIONAL LOOP............................................................................................................................................ 37
7.1 INTRODUCTION......................................................................................................................................................................... 37
7.2 EXAMPLES................................................................................................................................................................................37
7.3 TASK....................................................................................................................................................................................... 39
CH#8 CONDITIONAL JUMPS............................................................................................................................................... 45
8.1 INTRODUCTION......................................................................................................................................................................... 45
8.2 EXAMPLES................................................................................................................................................................................46
8.3 TASK...................................................................................................................................................................................... 48
CH#9 BRANCHING AND NESTED LOOP............................................................................................................................... 49
9.1 INTRODUCTION........................................................................................................................................................................ 49
9.2 PROGRAM FOR BRANCHING.........................................................................................................................................................50

Saqlain hassan / BCB


-22F-027
3
Assembly Language Sir Mustafa

9.3 PROGRAM FOR NESTED LOOP......................................................................................................................................................53


9.4 TASK...................................................................................................................................................................................... 55
CH#10 STRING INSTRUCTIONS.......................................................................................................................................... 57
10.1 INTRODUCTION...................................................................................................................................................................... 57
10.2 TWO FUNCTION USE TO SET THE STATUS OF DIRECTION FLAG............................................................................................................57
10.3 LAB FOR PRACTICES.................................................................................................................................................................58
CH#11 BIT MANIPULATIONS............................................................................................................................................. 65
11.1 INTRODUCTION...................................................................................................................................................................... 65
11.2 SHIFTING AND ROTATING INSTRUCTIONS.................................................................................................................................. 65
11.3 PROGRAM FOR BIT MANIPULATION......................................................................................................................................... 67
11.4 PROGRAM FOR LOGICAL INSTRUCTION..................................................................................................................................... 68
11.5 LAB TASK........................................................................................................................................................................ 69

Saqlain hassan / BCB


-22F-027
4
Assembly Language Sir Mustafa

Ch#1. Introduction to Computer Organizations & Assembly


Language
1.1 Computer Organization
1.1.1 Introduction
The computer is an electronic device, consisting of input, output, and CPU etc. The main part of the
computer is CPU that is considered brain of computer.

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.

1.1.4 Execution Unit


In a microprocessor, the execution unit is a fundamental part responsible for carrying out the actual
processing of instructions. It typically consists of various functional units, including:

Saqlain hassan / BCB


-22F-027
5
Assembly Language Sir Mustafa

Arithmetic Logic Unit (ALU)

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.

1.1.5 Bus Interface Unit (BIU)


In a microprocessor, the bus interface unit manages communication between the processor and the
system's buses. Here are key components that might be involved in the bus interface:

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.

1.2 Assembly Language


1.2.1 Introduction
Assembly language is a low-level programming language that is closely tied to the architecture and
instruction set of a computer's central processing unit (CPU). It is considered a second-generation
programming language, sitting just above machine code in terms of abstraction. Assembly language is specific
to a particular computer architecture, and programs written in it are generally not portable across different
architectures.

1.2.2 How does it work?


Assembly language works by providing a human-readable representation of the machine code
instructions that a computer's central processing unit (CPU) can execute. Let's break down how it works.

Mnemonic Codes

Saqlain hassan / BCB


-22F-027
6
Assembly Language Sir Mustafa

Assembly language uses mnemonic codes or symbols that represent specific machine code
instructions. These mnemonics are more human-readable than raw binary code.

Assembly Language Program

A program is written in assembly language using these mnemonics along with operand values, which
are specific data or memory addresses.

Assembly Code Translation

An assembler is used to translate the assembly code into machine code. The assembler converts
each assembly language instruction into its corresponding binary representation.

Machine Code Execution

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.

Direct Access to Hardware

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

Assembly language allows programmers to perform low-level operations and optimizations. It


provides a level of control over system resources that is not achievable in higher-level languages.

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.

Ch#2 Instruction to run the assembly program.


2.1 Requirement
1. We require environments to perform the assembly code, so we use one’s famous IDE that is TASM,
Nodepad++ for our assembly practices.
2. How to run the program?

You need to follow the following step.

i. Open Tasm CLI from your PC


Save file with .asm in C drive tasm 1.4 folder.
ii. Tasm filename.asm
Helps you to find errors in the program.
iii. Tlink filename.obj
Create object file for machine with bind some possible library.

Saqlain hassan / BCB


-22F-027
7
Assembly Language Sir Mustafa

iv. Filename or filename.exe


Create an executable file or show the result on screen.

2.2 Doc Function/Sub routine


01 = take input of 1byte it can be int,char,symbol
3fh = read string from user
02 = print a 1byte on output screen
4ch = used to get exit from program
0ah = used for line feed
09 = print string on output screen.
0dh =used for carriage return
21h = is used to execute your instruction at priority level from processor

2.3 ASCII TABLE of 8086 Microprocessor


Dec Hex Binary HTML Char Description
0 00 00000000 � NUL Null
1 01 00000001  SOH Start of Heading
2 02 00000010  STX Start of Text
3 03 00000011  ETX End of Text
4 04 00000100  EOT End of Transmission
5 05 00000101  ENQ Enquiry
6 06 00000110  ACK Acknowledge
7 07 00000111  BEL Bell
8 08 00001000  BS Backspace
9 09 00001001 	 HT Horizontal Tab
10 0A 00001010 
 LF Line Feed
11 0B 00001011  VT Vertical Tab
12 0C 00001100  FF Form Feed
13 0D 00001101 
 CR Carriage Return
14 0E 00001110  SO Shift Out

Dec Hex Binary HTML Char Description


15 0F 00001111  SI Shift In
16 10 00010000  DLE Data Link Escape
17 11 00010001  DC1 Device Control 1
18 12 00010010  DC2 Device Control 2
19 13 00010011  DC3 Device Control 3
20 14 00010100  DC4 Device Control 4

Saqlain hassan / BCB


-22F-027
8
Assembly Language Sir Mustafa

21 15 00010101  NAK Negative Acknowledge


22 16 00010110  SYN Synchronize
23 17 00010111  ETB End of Transmission Block
24 18 00011000  CAN Cancel
25 19 00011001  EM End of Medium
26 1A 00011010  SUB Substitute
27 1B 00011011  ESC Escape
28 1C 00011100  FS File Separator
29 1D 00011101  GS Group Separator
30 1E 00011110  RS Record Separator
31 1F 00011111  US Unit Separator
32 20 00100000   space Space
33 21 00100001 ! ! exclamation mark
34 22 00100010 " " double quote
35 23 00100011 # # number
36 24 00100100 $ $ dollar
37 25 00100101 % % percent
38 26 00100110 & & ampersand
39 27 00100111 ' ' single quote
40 28 00101000 ( ( left parenthesis
41 29 00101001 ) ) right parenthesis
42 2A 00101010 * * asterisk
43 2B 00101011 + + plus
44 2C 00101100 , , comma
45 2D 00101101 - - minus
46 2E 00101110 . . period
47 2F 00101111 / / slash
48 30 00110000 0 0 zero

Dec Hex Binary HTML Char Description


49 31 00110001 1 1 one
50 32 00110010 2 2 two
51 33 00110011 3 3 three
52 34 00110100 4 4 four

Saqlain hassan / BCB


-22F-027
9
Assembly Language Sir Mustafa

53 35 00110101 5 5 five


54 36 00110110 6 6 six
55 37 00110111 7 7 seven
56 38 00111000 8 8 eight
57 39 00111001 9 9 nine
58 3A 00111010 : : colon
59 3B 00111011 &#59; ; semicolon
60 3C 00111100 &#60; < less than
61 3D 00111101 &#61; = equality
sign
62 3E 00111110 &#62; > greater
than
63 3F 00111111 &#63; ? question mark
64 40 01000000 &#64; @ at sign
65 41 01000001 &#65; A
66 42 01000010 &#66; B
67 43 01000011 &#67; C
68 44 01000100 &#68; D
69 45 01000101 &#69; E
70 46 01000110 &#70; F
71 47 01000111 &#71; G
72 48 01001000 &#72; H
73 49 01001001 &#73; I
74 4A 01001010 &#74; J
75 4B 01001011 &#75; K
76 4C 01001100 &#76; L
77 4D 01001101 &#77; M
78 4E 01001110 &#78; N
79 4F 01001111 &#79; O
80 50 01010000 &#80; P
81 51 01010001 &#81; Q
82 52 01010010 &#82; R

Dec Hex Binary HTML Char Description


83 53 01010011 &#83; S

Saqlain hassan / BCB


-22F-027
1
0
Assembly Language Sir Mustafa

84 54 01010100 &#84; T
85 55 01010101 &#85; U
86 56 01010110 &#86; V
87 57 01010111 &#87; W
88 58 01011000 &#88; X
89 59 01011001 &#89; Y
90 5A 01011010 &#90; Z
91 5B 01011011 &#91; [ left square bracket
92 5C 01011100 &#92; \ backslash
93 5D 01011101 &#93; ] right square bracket
94 5E 01011110 &#94; ^ caret / circumflex
95 5F 01011111 &#95; _ underscore
96 60 01100000 &#96; ` grave / accent
97 61 01100001 &#97; a
98 62 01100010 &#98; b
99 63 01100011 &#99; c
100 64 01100100 &#100; d
101 65 01100101 &#101; e
102 66 01100110 &#102; f
103 67 01100111 &#103; g
104 68 01101000 &#104; h
105 69 01101001 &#105; i
106 6A 01101010 &#106; j
107 6B 01101011 &#107; k
108 6C 01101100 &#108; l
109 6D 01101101 &#109; m
110 6E 01101110 &#110; n
111 6F 01101111 &#111; o
112 70 01110000 &#112 p
113 71 01110001 &#113; q
114 72 01110010 &#114; r
115 73 01110011 &#115; s
116 74 01110100 &#116; t

Saqlain hassan / BCB


-22F-027
1
1
Assembly Language Sir Mustafa

Dec Hex Binary HTML Char Description

Saqlain hassan / BCB


-22F-027
1
2
Assembly Language Sir Mustafa

117 75 01110101 &#117; u


118 76 01110110 &#118; v
119 77 01110111 &#119; w
120 78 01111000 &#120; x
121 79 01111001 &#121; y
122 7A 01111010 &#122; z
123 7B 01111011 &#123; { left curly bracket
124 7C 01111100 &#124; | vertical bar
125 7D 01111101 &#125; } right curly bracket
126 7E 01111110 &#126; ~ tilde
127 7F 01111111 &#127; DEL delete
128 80 10000000 - €
129 81 10000001 - •
130 82 10000010 - ‚
131 83 10000011 - ƒ
132 84 10000100 - „
133 85 10000101 - …
134 86 10000110 - †
135 87 10000111 - ‡
136 88 10001000 - ˆ
137 89 10001001 - ‰
138 8A 10001010 - Š
139 8B 10001011 - ‹
140 8C 10001100 - Œ
141 8D 10001101 - •
142 8E 10001110 - Ž
143 8F 10001111 - •
144 90 10010000 - •
145 91 10010001 - ‘
146 92 10010010 - ’
147 93 10010011 - “
148 94 10010100 - ”
149 95 10010101 - •
150 96 10010110 - –

Saqlain hassan / BCB


-22F-027
1
3
Assembly Language Sir Mustafa

Dec Hex Binary HTML Char Description

Saqlain hassan / BCB


-22F-027
1
4
Assembly Language Sir Mustafa

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 - Ÿ

160 A0 10100000 &#160;


161 A1 10100001 &#161; ¡
162 A2 10100010 &#162; ¢
163 A3 10100011 &#163; £
164 A4 10100100 &#164; ¤
165 A5 10100101 &#165; ¥
166 A6 10100110 &#166; ¦
167 A7 10100111 &#167; §
168 A8 10101000 &#168; ¨
169 A9 10101001 &#169; ©
170 AA 10101010 &#170; ª
171 AB 10101011 &#171; «
172 AC 10101100 &#172; ¬
173 AD 10101101 &#173;
174 AE 10101110 &#174; ®
175 AF 10101111 &#175; ¯
176 B0 10110000 &#176; °
177 B1 10110001 &#177; ±
178 B2 10110010 &#178; ²
179 B3 10110011 &#179; ³
180 B4 10110100 &#180; ´
181 B5 10110101 &#181; µ
182 B6 10110110 &#182; ¶
183 B7 10110111 &#183; ·

Saqlain hassan / BCB


-22F-027
1
5
Assembly Language Sir Mustafa

184 B8 10111000 &#184; ¸

Dec Hex Binary HTML Char Description


185 B9 10111001 &#185; ¹
186 BA 10111010 &#186; º
187 BB 10111011 &#187; »
188 BC 10111100 &#188; ¼
189 BD 10111101 &#189; ½
190 BE 10111110 &#190; ¾
191 BF 10111111 &#191; ¿
192 C0 11000000 &#192; À
193 C1 11000001 &#193; Á
194 C2 11000010 &#194; Â
195 C3 11000011 &#195; Ã
196 C4 11000100 &#196; Ä
197 C5 11000101 &#197; Å
198 C6 11000110 &#198; Æ
199 C7 11000111 &#199; Ç
200 C8 11001000 &#200; È
201 C9 11001001 &#201; É
202 CA 11001010 &#202; Ê
203 CB 11001011 &#203; Ë
204 CC 11001100 &#204; Ì
205 CD 11001101 &#205; Í
206 CE 11001110 &#206; Î
207 CF 11001111 &#207; Ï
208 D0 11010000 &#208; Ð
209 D1 11010001 &#209; Ñ
210 D2 11010010 &#210; Ò
211 D3 11010011 &#211; Ó
212 D4 11010100 &#212; Ô
213 D5 11010101 &#213; Õ
214 D6 11010110 &#214; Ö
215 D7 11010111 &#215; ×
216 D8 11011000 &#216; Ø

Saqlain hassan / BCB


-22F-027
1
6
Assembly Language Sir Mustafa

217 D9 11011001 &#217; Ù


218 DA 11011010 &#218; Ú

Dec Hex Binary HTML Char Description


219 DB 11011011 &#219; Û
220 DC 11011100 &#220; Ü
221 DD 11011101 &#221; Ý
222 DE 11011110 &#222; Þ
223 DF 11011111 &#223; ß
224 E0 11100000 &#224; à
225 E1 11100001 &#225; á
226 E2 11100010 &#226; â
227 E3 11100011 &#227; ã
228 E4 11100100 &#228; ä
229 E5 11100101 &#229; å
230 E6 11100110 &#230; æ
231 E7 11100111 &#231; ç
232 E8 11101000 &#232; è
233 E9 11101001 &#233; é
234 EA 11101010 &#234; ê
235 EB 11101011 &#235; ë
236 EC 11101100 &#236; ì
237 ED 11101101 &#237; í
238 EE 11101110 &#238; î
239 EF 11101111 &#239; ï
240 F0 11110000 &#240; ð
241 F1 11110001 &#241; ñ
242 F2 11110010 &#242; ò
243 F3 11110011 &#243; ó
244 F4 11110100 &#244; ô
245 F5 11110101 &#245; õ
246 F6 11110110 &#246; ö
247 F7 11110111 &#247; ÷
248 F8 11111000 &#248; ø

Saqlain hassan / BCB


-22F-027
1
7
Assembly Language Sir Mustafa

249 F9 11111001 &#249; ù


250 FA 11111010 &#250; ú
251 FB 11111011 &#251; û
252 FC 11111100 &#252; ü

Dec Hex Binary HTML Char Description


253 FD 11111101 &#253; ý
254 FE 11111110 &#254; þ
255 FF 11111111 &#255; ÿ

2.4 Lab Time


2.3.1 Create a program to print a character.
.model small
.stack 100h
.data
.code
Start:
Mov ah, 2h
Mov dl,"S"
Int 21h
Mov ah, 4ch
Int 21h
End start

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

Saqlain hassan / BCB


-22F-027
1
8
Assembly Language Sir Mustafa

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.

Saqlain hassan / BCB


-22F-027
1
9
Assembly Language Sir Mustafa

Ch#3 Basic DOS Function


3.1 Introduction:
In assembly language DOS functions and interrupts are used for input/output services.
An interrupt occurs when any currently executing program is disturbed (interrupted).
Interrupts are generated for a variety of reasons, usually related to peripheral devices such as keyboard, disk
drive or printer.
The Intel 8086 microprocessor recognizes two types of interrupts; hardware and software.
Hardware interrupt is generated when a peripheral device needs attention from microprocessor.
A software interrupt is a call to subroutine located in the operating system, usually an input-output routine.
i.e. a software interrupt calls built-in subroutine from the operating system usually DOS for input and output
operations.
Different DOS functions

3.2 Sub Routine/ DOS functions


3.2.1 INT 4Ch: Terminate Process
• Ends the current process (program), returns an optional 8-bit return code to the calling process.
• A return code of 0 usually indicates successful completion.
• Statement 4CH

3.2.2 ASCII Control Characters


• 08h  Backspace (Moves one column to the left)
• 09h  Horizontal tab (skips forward n columns)
• 0Ah  Line feed (moves to next output line)
• 0Ch  Form feed (moves to next printer page)
• 0Dh  Carriage return (moves to leftmost output column)
• 1Bh  Escape character.

3.2.3 Selected Output


• 02h, 06h  Write a single character to standard output.
• 05h  Write a single character to default printer.
• 09h  Write string (terminated by a $ character) to standard output.
• 40h  Write an array of bytes (block of data) to a file or device.

3.2.4 Selected Input Functions


• 01h, 06h  Read a single character from standard input.
• 0Ah  Read array of buffered characters from standard input.
• 0Bh  Get status of the standard input buffer.
• 3Fh  Read from file or device.

3.2.5 Date/Time Functions


• 2Ah  Get system date.
• 2Bh  Set system date.
• 2Ch  Get system time.
• 2Dh  Set system time.

Saqlain hassan / BCB


-22F-027
2
0
Assembly Language Sir Mustafa

3.3 Lab Time


3.3.1 Create a program which take input from user.
.model small
.stack 100h
.data
.code
start:
mov ah,01; 01h used for take char input from user
int 21h ; inturrpt
mov ah,4ch ; 4ch used to terminate the program
int 21h
end start

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

Saqlain hassan / BCB


-22F-027
2
1
Assembly Language Sir Mustafa

mov dl,0Ah; 0Ah or 10 used to line


break int 21h mov ah,02 mov
dl,0Dh
mov ah,02 ; 02 used for print single character on output
screen mov dl,bl int 21h
mov ah,4ch ; 4ch used to terminate the
program int 21h end start

3.3.4 Take string as an input from user.


.model small
.stack 100h
.data .code
start:
MOV AH,3FH ; 3FH used to take string value but couldn't be stored
INT 21H
mov ah,4ch ; 4ch used to terminate the
program int 21h end start

3.3.5 Take input with 08h and print it.


.model small
.stack 100h
.data .code
start:
MOV AH,08h ; 08h used to take single value with echo (Hidden Input)
INT 21H
MOV AH,02
MOV DL,AL
INT 21H
mov ah,4ch ; 4ch used to terminate the
program int 21h end start

Saqlain hassan / BCB


-22F-027
2
2
Assembly Language Sir Mustafa

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

Saqlain hassan / BCB


-22F-027
2
3
Assembly Language Sir Mustafa

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

3.4.2 Create a code for given output **********


A
A
**********
.model small
.stack 100h
.data .code
start: ;bo
rder 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

Saqlain hassan / BCB


-22F-027
2
4
Assembly Language Sir Mustafa

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

Saqlain hassan / BCB


-22F-027
2
5
Assembly Language Sir Mustafa

CH#4 Variable
4.1 Introduction:
In 8086 assembly language, a variable is a memory location. The following directives define variables of different
sizes.

• db: Defines a byte-sized variable


• dw: Defines a word-sized variable (16 bits)
• dd: Defines a double word (long word, 32 bits) variable

4.1.1: Declaring Variables in Assembly Language


As in Java, variables must be declared before they can be used. Unlike Java, we do not specify a variable type in the
declaration in assembly language. Instead, we declare the name and size of the variable, i.e. the number of bytes the
variable will occupy. We may also specify an initial value.

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.

Saqlain hassan / BCB


-22F-027
2
6
Assembly Language Sir Mustafa

4.2 Getting the Address of a Variable


There is LEA (Load Effective Address) instruction and alternative OFFSET operator. Both OFFSET and LEA can be used
to get the offset address of the variable.

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.3.2 To display string on screen. (offset)


.model small
.stack
100h .data
msg db " Hello world$"
.code start: mov
ax,@data Mov ds,ax
mov ah,09 mov dx,
offset msg int 21h
mov ah,4ch int 21h
end start

Saqlain hassan / BCB


-22F-027
2
7
Assembly Language Sir Mustafa

4.3.3 Display String on new line


.MODEL SMALL
.STACK 100H
.DATA
STRING_1 DB 'SMI$'
STRING_2 DB 'University $'
.CODE
MAIN PROC
MOV AX, @DATA ; initialize DS
MOV DS, AX
LEA DX, STRING_1 ; load & display the STRING_1
MOV AH, 9
INT 21H
MOV AH, 2 ; carriage return
MOV DL, 0DH
INT 21H MOV DL, 0AH
; line feed
INT 21H LEA DX, STRING_2 ; load &
display the STRING_2
MOV AH, 9
INT 21H
MOV AH, 4CH ; return control to DOS
INT 21H
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

Saqlain hassan / BCB


-22F-027
2
8
Assembly Language Sir Mustafa

lea dx,str2 ;get string mov


ah,09h ;display string function int
21h ;display string mov
dl,10 ;line feed mov ah,02h int
21h ;display string
lea dx,str3 ;get string mov
ah,09h ;display string function int
21h ;display string mov
dl,10 ;line feed mov ah,02h int
21h ;display string
lea dx,str4 ;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.2 Create this format using Assembly Program.


*******************************
Name: Input Name from User
Father Name: Input father Name
Roll No: Input roll No:
Institute Name: Input Institute Name
*******************************
.model small
.stack
100h .data
stars db '***********************$'
_name db 10,"Name: $"
fname db "Father Name: $"
roll_no db "Roll No: $"
institute_name db "Institute Name: $"
.code start:
mov ax,@data mov ds,ax
mov ah,09 lea dx,stars
int 21h mov ah,09 ; name
lea dx,_name int 21h
mov ah,3fh ;input int 21h
mov ah,09 ; fname lea
dx,fname int 21h mov
ah,3fh ;input int 21h
mov ah,09 ; Roll NO lea

Saqlain hassan / BCB


-22F-027
2
9
Assembly Language Sir Mustafa

dx,roll_no int 21h


mov ah,3fh ;input int 21h
mov ah,09 ; Institute Name
lea dx,institute_name int
21h mov ah,3fh ;input
int 21h mov ah,09
lea dx,stars
int 21h mov
ah,4ch int
21h end start

CH#5 Exchange Keyword


5.1 Introduction
Now we will write another Assembly program which does some
Exchange Operation.

Before actually starting to write the program, Let’s understand


XCHG instruction.

As we know the programs work only with the instructions in the


instruction set. Instruction XCHG is used to EXCHANGE memory
variables in the following permutations above. REG stands for
Registers (Eg. AX, BX, CX, DX ). memory stands for Variable or
Address. immediate stands for Numbers or Values. Let us
understand the meanings of the above permutations.

Before actually starting to write the program, Let’s understand


XCHG instruction.

As we know the programs work only with the instructions in the


instruction set. Instruction XCHG is used to EXCHANGE memory
variables in the following permutations above. REG stands for
Registers (Eg. AX, BX, CX, DX ). memory stands for Variable or Address. immediate stands for Numbers or Values. Let
us understand the meanings of the above permutations.

Saqlain hassan / BCB


-22F-027
3
0
Assembly Language Sir Mustafa

5.2 Legal combination of operands for XCHG

5.3 Practical for exchange key word


5.3.1 To create a program for Exchange the value between different registers.
.model small
.stack
100h .code
start:
mov al, 49 ; ascii code of
1 mov bl, 50 ; ascii code
of 2 xchg al,bl ; swap al
and bl mov ah,02 mov dl,al
int 21h
mov ah,02 ; print
character mov dl,bl int 21h
Mov ah, 4ch ; terminate the
program Int 21h end start

5.3.2 Use variable in above program


;Use variable in above program
.model small
.stack 100h .data
msg1 db "Hello $"
msg2 db "World $"
.code start:
mov ax,@data mov ds,ax
lea bx, msg1 lea cx, msg2
xchg bx,cx ; swap al and bl
mov ah,09 mov dx,bx ;
Hello int 21h mov ah,09
; print string mov dx,cx ;
World int 21h
Mov ah, 4ch ; terminate the program
Int 21h
end start

Saqlain hassan / BCB


-22F-027
3
1
Assembly Language Sir Mustafa

5.3.3 Create a above program using input.


.model small
.stack 100h .code start:
mov ah,01 int 21h mov
bl,al mov ah,01 int 21h
mov bh,al xchg bl,bh ;
swap bl and bh mov ah,02
mov dl,bl int 21h mov
ah,02 ; print character mov
dl,bh int 21h
Mov ah, 4ch ; terminate the program
Int 21h
end start

5.3.4 Create a program which exchange characters.


.model small
.stack
100h .code
start:
mov al,"A"
mov bl,"B"
xchg al,bl ; swap bl and
bh mov ah,02 mov dl,al
int 21h
mov ah,02 ; print
character mov dl,bl int
21h
Mov ah, 4ch ; terminate the program
Int 21h
end start

Saqlain hassan / BCB


-22F-027
3
2
Assembly Language Sir Mustafa

CH#6 Case Conversion


6.1 Introduction
In the case conversions, we change the upper case to lower case and lower case to upper case. The problem
is challenging in assembly language because we use technical on ascii code.

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’.

6.2 Practical examples


6.2.1 To convert the small letter into capital
.model small
.stack
100h .data
str1 db 'Enter lower case letter: - $'
str2 db 0dh,0ah,'Upper 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 sub al,20h 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

6.2.2 write a program by using EQU mnemonics


.model small
.stack 100h
.data
p equ 41h ; A
.code start:
mov ax,@data
mov ds,ax
mov ah,02
mov dl,p int

Saqlain hassan / BCB


-22F-027
3
3
Assembly Language Sir Mustafa

21h mov
ah,4ch int
21h end
start

6.2.3 Also write a program by using equ


; PR3--> EQU
.model small
.stack 100h
.data
Msg1 equ 'type your name $'
Msg db msg1
p equ 41h .code
start:
mov ax,@data
mov ds,ax
Mov ah,09
lea dx, msg
int 21h
mov ah,02
mov dl ,p
int 21h
mov ah,4ch
int 21h end
start

6.2.4 Use EQU in program for new line.


; pr 5--> Use EQU in program for new line.
.model small
.stack 100h .data
line equ 0AH
carriage equ
0DH .code start:
mov ah,02 mov
dl, 'a' int 21h
mov ah, 02
mov dl,line
int 21h mov
ah, 02 mov
dl,carriage
int 21h mov

Saqlain hassan / BCB


-22F-027
3
4
Assembly Language Sir Mustafa

ah,02 mov dl,


'b' int 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

6.3.2 Create a program which print! on 1, @ on 2, # on 3 and so on up to 9.


; Create a program which print! on 1, @ on 2, # on 3 and so on up to 9.
.model small
.stack
100h .data

Saqlain hassan / BCB


-22F-027
3
5
Assembly Language Sir Mustafa

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

Saqlain hassan / BCB


-22F-027
3
6
Assembly Language Sir Mustafa

CH#7 Unconditional Loop


7.1 Introduction
7.1.1 Loop
A loop is a sequence of instructions that is repeated. The number of times to repeat may be known in
advance, or it may depend on conditions i.e. it is a count-controlled loop.

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.2 To print all ASCII Characters.


.model small
.stack 100h
.data .cod
e main
proc
; ACSII Characters ranges from 0-
255 mov cx,256 ; 256 times display
mov ah,02h; set output function mov
dl,0 ; initialize from 0 next:
int 21h ;print ASCII character
inc dl ;increment DL to next ASCII
character loop next ;return to DOS
mov ah,4ch ;exit program with return
code int 21h ;DOS exit main endp end main

Saqlain hassan / BCB


-22F-027
3
7
Assembly Language Sir Mustafa

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

Saqlain hassan / BCB


-22F-027
3
8
Assembly Language Sir Mustafa

mov ax, @data


mov ds, ax mov ah,
09h mov dx, offset
MSG2 int 21h
mov ah, 4ch

mov ah, 02h


mov dl, 0dh
Int 21h
mov ah,
02h
mov dl,
0ah
Int 21h
mov ax, @data
mov ds, ax mov ah,
09h mov dx, offset
MSG3 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

Saqlain hassan / BCB


-22F-027
3
9
Assembly Language Sir Mustafa

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

7.3.2 Write a Program to Display ASCII characters from a to z through looping.


.model Small
.Stack 100h
.code
start: mov
cx, 26
mov al,97 ; 97 is ascii of small letter
a small_letter: mov ah,02 mov
dl,al int 21h inc al loop
small_letter mov ah,4ch int 21h
end start

7.3.3 Write a Program to Display ASCII characters from Z to A through looping.


.model Small
.Stack 100h
.code
start:
mov cx, 26
mov al,'Z'
capital_reverse_alphabat:

Saqlain hassan / BCB


-22F-027
4
0
Assembly Language Sir Mustafa

mov ah,02
mov dl,al
int 21h
dec al
loop capital_reverse_alphabat
mov ah,4ch int 21h end
start

7.3.4 Write a Program to Display ASCII characters from 9 to 0 through looping.


.model Small
.Stack 100h
.code
start:
mov cx, 10
mov al,57
reverse_decimal:
mov ah,02 mov
dl,al int 21h
dec al loop
reverse_decimal
mov ah,4ch int
21h end start

7.3.5 Write a Program to Display ASCII characters from 0 to 9 through looping.


.model Small
.Stack 100h
.code
start:
mov cx, 10 mov
al,48 ; ascii of 0
forward_decimal: mov
ah,02 mov dl,al
int 21h inc al
loop forward_decimal
mov ah,4ch int 21h end
start

Saqlain hassan / BCB


-22F-027
4
1
Assembly Language Sir Mustafa

7.3.6 Write a Program to Display ASCII characters from z to a through looping.


.model Small
.Stack 100h
.code
start:
mov cx, 26
mov al,'z'
small_reverse_alphabat:
mov ah,02
mov dl,al
int 21h
dec al
loop small_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

Saqlain hassan / BCB


-22F-027
4
2
Assembly Language Sir Mustafa

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

7.3.9 Write a program to print your name 10 times.


.model Small
.Stack 100h
.data
MY_NAME db "Shair Muhammad $"
.code start:
mov ax, @data
mov ds, ax mov
cx, 10 mov
bl,48
Print:
mov ah, 09h

Saqlain hassan / BCB


-22F-027
4
3
Assembly Language Sir Mustafa

mov dx, offset MY_NAME


int 21h mov ah,02h
mov dl,bl int 21h
mov dl,0Ah ;linefeed
int 21h inc bl
Loop print mov
ah,4ch int 21h end
start

7.3.10 Write a program in Assembly Language to print following pattern

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

Saqlain hassan / BCB


-22F-027
4
4
Assembly Language Sir Mustafa

CH#8 Conditional Jumps


8.1 Introduction
If some specified condition is satisfied in conditional jump, the control flow is transferred to a target instruction.
There are numerous conditional jump instructions depending upon the condition and data.

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

Saqlain hassan / BCB


-22F-027
4
5
Assembly Language Sir Mustafa

8 JS Jump Sign (negative value) SF


9 JNS Jump No Sign (positive value) SF

8.1.2 TEST instruction


The TEST instruction computes the bitwise logical AND of first operand and the second operand.
According to the result, the status flags SF, ZF and PF will be set.

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.

Example: TEST EAX, EAX

8.1.3 CMP instruction


The CMP instruction is another example of conditionals. A CMP instruction performs a subtract
operation on both operands, and the status flags ZF and CF will be set according to the result.

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

8.2.2 Write a program by using JE Flag (Jump if equal).


.model small

Saqlain hassan / BCB


-22F-027
4
6
Assembly Language Sir Mustafa

.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

8.2.3 Write a program by using JB condition (jump if below)


.model small
.stack
100h .code
start:
mov ax,4
cmp ax, 5
jb true Mov
ah, 02 mov
dl, 'F' int
21h mov
ah,4ch int
21h jmp
exit true:
mov ah, 02
mov dl , 'T'
int 21h exit:
mov ah, 4ch
int 21h end
start

8.2.4 Create a program using JG condition (jump if greater)


.model small

Saqlain hassan / BCB


-22F-027
4
7
Assembly Language Sir Mustafa

.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.

Saqlain hassan / BCB


-22F-027
4
8
Assembly Language Sir Mustafa

CH#9 Branching and Nested Loop


9.1 Introduction
9.1.1 Branching.
The 8086 microprocessor, branching refers to altering the flow of execution based on certain conditions or
unconditional jumps to specific memory locations. In other words, it's about controlling which instructions
get executed next based on the results of previous operations or predefined conditions.

• 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.

9.1.2 Nested Loop


Implementing nested loops in assembly language, particularly for the 8086 microprocessors, requires careful
management of loop counters, conditions, and branch instructions. Nested loops involve one loop inside
another, and to achieve this in assembly language, you'll typically use multiple loop control variables and
branching instructions.

There are two types of loops working at a time that are:

• OUTER_LOOP is the outer loop that controls the number of rows.


• INNER_LOOP is the inner loop that prints the asterisks (*) based on the current row number.
It’s not easy to use nested loop in assembly as like high level language so we require to technic for implement
nested loop. According to my opinion, there are two common ways, you can use to create a nested loop
scenario that are following.

• 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.

Saqlain hassan / BCB


-22F-027
4
9
Assembly Language Sir Mustafa

9.2 Program for Branching


9.2.1 Program will take input until user press capital A
.model small
.stack
100h .code
start:
mov ah,01
repeat: int
21h
cmp al, 'A'
jne repeat
mov ah,4ch
int 21h
end start

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_:

Saqlain hassan / BCB


-22F-027
5
0
Assembly Language Sir Mustafa

cmp al, 0dh


je end_while
inc dx int
21h jmp
while_ end_while:
mov ah, 4ch int
21h end start
9.2.3 Create a program for if then (If condition is true it print e otherwise it terminate)
.model small
.stack 100h
.code start:
mov al,49
CMP al, 49
Je then ; no, exit NEG
AX ; yes, change sign jmp
exit then:
mov ah, 02
mov dl, 'E'
int 21h
jmp exit exit:
mov ah,4ch int 21h
end start

9.2.4 Write a program for If Then Else


.model small
.stack
100h .code
start:
mov ah,01
int 21h cmp
al,49 je odd
cmp al,51
je odd cmp
al,50 je
even1 cmp
al,52 je
even1 jmp
end_case
even1:
mov dl, 'e'
jmp display1 odd:
mov dl ,'o'
jmp display1
display1:
mov ah,02
int 21h

Saqlain hassan / BCB


-22F-027
5
1
Assembly Language Sir Mustafa

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_:

Saqlain hassan / BCB


-22F-027
5
2
Assembly Language Sir Mustafa

mov ah,4ch int


21h end start

9.3 Program for Nested Loop


9.3.1 Write a program to nested loop by using bx register
.model small
.stack 100h .code
start: mov
cx,4 l1:
;mov dx,4
mov dx,49
mov ah,2
int 21h
mov bx,cx
mov cx,5 l2:
mov dx,50
mov ah,2
int 21h
loop l2
mov cx,bx
loop l1 mov
ah,4ch int
21h end start

9.3.2 Create nested loop with the help of stack.


.model small
.stack
100h .code
start:
mov cx,4
l1:
mov dx,"S"
mov ah,2
int 21h
push cx mov
cx,5 l2:
mov dx,50
mov ah,2
int 21h
loop l2
pop cx loop

Saqlain hassan / BCB


-22F-027
5
3
Assembly Language Sir Mustafa

l1 mov
ah,4ch int
21h end start

9.3.3 Create an alternative way to produce above output.


.model small
.stack
100h .code
start:
mov dh, "1" ;
ASCII=49 mov cx, 3
outerLoop:
mov ah, 02h
mov dl, dh ; Successively "1", "2", "3"
inc dh int 21h mov bx, 3
innerLoop:
mov ah, 02h mov
dl, "2" ; ASCII=50 int
21h dec bx jnz
innerLoop dec cx jnz
outerLoop mov ah,4ch int
21h end start

9.3.4 Create pattern with help of nested loop.


.model small
.stack 100h
.code
start:
mov cx, 4
l1:
mov bx,cx
l2:
mov dl,'*'
mov ah,02
int 21h loop
l2 mov ah, 02
mov dl, 0ah
int 21h mov
ah, 02 mov dl,
0dh int 21h

Saqlain hassan / BCB


-22F-027
5
4
Assembly Language Sir Mustafa

mov cx,bx loop 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

9.4.2 Create a program using Case with your own logic.


9.4.3 Create a program Repeat until press n.
.model small
.stack 100h
.data
menu db
10,"================================================",10,09,09, "SHAIR
MUHAMMAD(BCB-
22S028)",10,"================================================",10,"Press
n for exit : $" .code start:
mov ax,@data
mov ds,ax
mov
ah,09 lea
dx,menu int
21h
mov ah,01
int 21h cmp
al,'n' je
exit cmp
al,'N' je

Saqlain hassan / BCB


-22F-027
5
5
Assembly Language Sir Mustafa

exit jmp
start exit:
mov ah,4ch
int 21h end
start

CH#10 String Instructions


10.1 Introduction
• String - a byte or word array.
• Operations that can be performed with string instructions:
• copy a string into another string
• search a string for a particular byte or word
• store characters in a string
• compare strings of characters alphanumerically
• String - a byte or word array.
• Operations that can be performed with string instructions:
• copy a string into another string
• search a string for a particular byte or word
• store characters in a string
• compare strings of characters alphanumerically

10.2 Two Function use to set the status of Direction Flag


• CLD: Clear direction flag
• This function make DF=0 • STD: set direction flag
• This function make DF=1

10.2.2 Moving String


• Movsb: move string byte (1 byte) given by DS:SI into ES:DI

Saqlain hassan / BCB


-22F-027
5
6
Assembly Language Sir Mustafa

• Movsw: move string word (2 byte)given by DS:SI into ES:D


• Movsd: move string double word (4byte)given by DS:SI into ES:DI

10.2.3 Store string


• STOSB - copies contents of AL to BYTE address given by ES:DI. DI is incremented/decremented by 1.
• STOSW - copies the contents of AX to the WORD address given by ES:DI. DI is
incremented/decremented by 2.
• STOSD - copies contents of EAX to the DOUBLE WORD address given by ES:DI. DI is
incremented/decremented by 4.
10.2.4 Load string
• LODSB - moves the BYTE at address DS:SI into AL. SI is incremented/decremented by 1 based on the state
of direction flag.
• LODSW - moves the WORD at address DS: SI into AX. SI is incremented/decremented by 2.
• LODSD - moves the DOUBLE WORD at address DS:SI into EAX. SI is incremented/decremented by 4.

10.2.5 Scan string


• SCASB - compares BYTE at ES:DI with AL and sets flags according to result.
• SCASW - compares WORD at ES:DI with AX and sets flags.
• SCASD - compares DOUBLE WORD at ES:DI with EAX and sets flags.

10.2.6 Compare string


• CMPSB - compares BYTE at ES:DI with BYTE at DS:SI and sets flags.
• CMPSW - compares WORD at ES:DI with WORD at DS:SI and sets flags.
• CMPSD - compares DOUBLE WORD at ES:DI with WORD at DS:SI and sets flags.

10.2.7 REPE and REPNE


• REPE repeats the following string instruction while the zero flag is set.
• RENE repeats the following string instruction while the zero flag is clear.
• They are used with the SCAS and CMPS instructions.

10.3 Lab for practices


10.3.1 : Compare two strings and display message accordingly in Assembly Language.
.model small
.stack
100h .data
msg1 db "This is 1st string$" msg2
db "This is 2nd string$" cmp1 db 'Both
the strings are equal$' cmp2 db 'both
the strings are not equal$'
.code start:
Mov ax,@data
Mov ds, ax
mov ah,09
lea dx,msg1
int 21h mov
dl,10 mov
ah,02 int
21h mov
ah,09 lea dx
,msg2 int

Saqlain hassan / BCB


-22F-027
5
7
Assembly Language Sir Mustafa

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

Saqlain hassan / BCB


-22F-027
5
8
Assembly Language Sir Mustafa

10.3.3 Reverse the string.


.model small
.stack 100h .data
text1 db 'hello$'
text2 db 5
dup('0$')
.code begin:
mov
ax,@data
mov ds,ax
mov es,ax
lea
si,text1+4
lea
di,text2
std mov
cx,5
move:
movsb add
di,2 loop move
mov dx, offset text2
mov ah,09 int 21h
mov ah, 4ch int 21h
end begin

10.3.4 Copy String using Movesb


.model small
.stack 100h .data
text1 db 'hello$'
text2 db 5
dup('0$')
.code begin:
mov ax,@data mov
ds,ax mov es,ax
lea si,text1 lea
di,text2 cld mov

Saqlain hassan / BCB


-22F-027
5
9
Assembly Language Sir Mustafa

cx,5 rep movsb


mov dx, offset text2
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

Saqlain hassan / BCB


-22F-027
6
0
Assembly Language Sir Mustafa

int 21h
mov ah,4ch
int 21h end
start

10.3.7 stosb using loop


.model small
.stack 100h
.data
string1 db 'hello$' string2
db 5 dup(' $')
.code start:
mov ax,@data
mov ds,ax mov
es,ax lea
di,string2
mov cx,5 l1:
cld mov ah,01
int 21h
stosb loop l1
mov ah,09 lea
dx,string2
int 21h mov
ah,4ch int
21h end start

10.3.8 Create program using load instruction.


.model small
.stack
100h .data
str1 db
"hello$" .code
start:
mov ax,@data
mov es,ax
mov ds,ax
lea si,str1

Saqlain hassan / BCB


-22F-027
6
1
Assembly Language Sir Mustafa

cld lodsb
mov ah,02
mov dl, al
int 21h mov
ah,4ch int
21h end start

10.3.9 lodsb with loop


.model small
.stack 100h .data
string1 db 'Sher$'
string2 db 5 dup(' $')
.code start:
mov ax,@data
mov ds,ax mov
es,ax lea
si,string1
mov al,'0'
mov cx,5
l1: cld
lodsb mov
ah,02 mov
dl,al int
21h
loop l1
mov ah,4ch
int 21h end
start

10.3.10 word game using stosb


.model small
.stack 100h
.data
string1 db 'meat$'
string2 db 5 dup(' $')
.code start:
mov ax,@data
mov ds,ax mov
es,ax lea
di,string1+2 cld
mov al,'e' stosb
mov ah,09 lea
dx,string1 int
21h mov ah,4ch
int 21h end start

Saqlain hassan / BCB


-22F-027
6
2
Assembly Language Sir Mustafa

10.3.11 Create program using Scan string operation.


.model small
.stack 100h
.data var
db
"A$" .code
start:
mov ax,@data
mov ds, ax
mov es, ax
lea di, var
cld mov
al,'A'
scasb jz
equal mov
dl,49 mov
ah,02 int
21h jmp
close
equal:
mov dl, 48
mov ah,02
int 21h jmp
close close:
mov ah, 4ch
int 21h end start

CH#11 Bit Manipulations


11.1 Introduction
Bit manipulation in assembly language, especially for the 8086 microprocessors, refers to the process of manipulating
individual bits within a byte, word, or double word (depending on the context and requirement). This manipulation
can involve setting, clearing, toggling, or testing specific bits within a data item.

11.1.1 Logical Instruction

Saqlain hassan / BCB


-22F-027
6
3
Assembly Language Sir Mustafa

• 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.

11.2 Shifting and Rotating Instructions


11.2.1 Shift Left (SHL)
• It is an example of logical shift.
• It performs left shift in the destination operand.
• Filling the lowest bit with 0.
• Highest bit is moved in carry flag.
• And bit in CF is discarded.

11.2.2 Shift Right (SHR)


• The Shift Right instruction performs a logical right shift on the destination operand. The highest bit
posit.

11.2.3 SAL & SAR Instruction


• SAL (Shift Arithmetic left) is identical to SHL.
• SAR (Shift arithmetic right) performs a right and stored in destination on operand.

11.2.4 ROL & ROR


• ROL (Rotate left) shifts each bit to the left.

Saqlain hassan / BCB


-22F-027
6
4
Assembly Language Sir Mustafa

• 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.

11.2.5 RCL & RCR


• RCL (rotate carry left) shifts each bit to the left.
• Copies the carry flag to the least significant bit.
• Copies the most significant bit to the carry flag.
• RCR (rotate carry right) shifts each bit to the right.
• Copies the carry flag to the most significant bit.
• Copies the least significant bit to the carry flag.

11.3 Program for bit manipulation


11.3.1 Create a program to illustrate SHL.
.model small
.stack
100h .code
main proc
mov dx,4
shl dx,1
add dx,48
mov ah,2
int 21h
mov ah,4ch

Saqlain hassan / BCB


-22F-027
6
5
Assembly Language Sir Mustafa

int 21h main


endp end main

11.3.2 Write a program by using SHL.


.model small
.stack
100h .code
main proc
mov dx,4
shr dx,1
add dx,48
mov ah,2
int 21h
mov ah,4ch
int 21h main
endp end main

11.3.3 Write a program with the help of RCL/RCR


.model small
.stack
100h .code
start: clc
mov bl,4
rcl bl,1
add bl,48
mov ah,02
mov dl,bl
int 21h
mov ah,4ch
int 21h end
start

11.4 Program for logical Instruction


11.4.1 program for AND logical instruction.
.model small

Saqlain hassan / BCB


-22F-027
6
6
Assembly Language Sir Mustafa

.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

11.4.2 program for OR logical instruction.


.model small
.stack
100h .code
start:
mov bl,101b
or bl,110B
add bl,48
mov ah,02
mov dl,bl
int 21h mov
ah,4ch int
21h end start

11.4.3 program for XOR logical instruction.


.model small
.stack
100h .code
start:
mov bl,101b
xor bl,110B
add bl,48
mov ah,02
mov dl,bl
int 21h mov
ah,4ch int
21h end start

Saqlain hassan / BCB


-22F-027
6
7
Assembly Language Sir Mustafa

11.5 Lab Task

Saqlain hassan / BCB


-22F-027
6
8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy