Lab Manual Assembly Lab 1 To 14 (04-07-2023)
Lab Manual Assembly Lab 1 To 14 (04-07-2023)
Lab-01
Objective:
Introduction to 8086/88 Assembler, the Central Processing Unit (CPU), and refresh the
hexadecimal number systems.
The 8086/88 CPU has 8 general purpose registers, each register is discussed below:
AX - the accumulator register (divided into AH / AL)
BX - the base address register (divided into BH / BL)
CX - the count register (divided into CH / CL)
DX - the data register (divided into DH / DL)
Since registers are located inside the CPU, they manipulate data much faster than memory.
Accessing data in a register usually takes no time. Accessing a memory location requires the use
of a system bus, so it takes much longer. Therefore, it is recommended to keep data variables in
registers.
Page 1 of 54
Dawood University of Engineering and Technology Assembly Language
Introduction to 8086/88 Assembler, the Central Processing Unit (CPU), and refresh the
hexadecimal number systems.
The main purpose of a register is to temporary store a number, similar to a variable. The size of
each register (listed above) is 16 bit. Four general purpose registers (AX, BX, CX, DX) can be
split into two separate 8 bits (or one byte) registers, for example if
AX= 00110000001110012,thenAH=001100002 and AL=001110012. The same is applicable for
other 3 registers (i.e., BX, CX, DX), where "H" is for higher and "L" is for lower byte.
The 8086/88 CPU has two index and two pointer registers, as listed below:
SI - source index register.
DI - destination index register.
BP - base pointer.
SP - stack pointer.
IP - instruction pointer
Page 2 of 54
Dawood University of Engineering and Technology Assembly Language
These registers are used for indexing an array of memory in association with the segment
registers. For example, IP register always works together with CS segment register and it points
to currently executing instruction in the program memory.
Segment Registers
The 8086/88 CPU has 4 segment registers, each register is discussed below
CS - points at the segment containing the current program.
DS - generally points at segment where variables are defined.
ES - extra segment register, it's up to a coder to define its usage.
SS - points at the segment containing the stack.
Although it is possible to store any data in the segment registers, however, this is not
recommended and is not considered as good practice. The segment registers have a very special
purpose –to point at accessible blocks of memory.
Segment registers work together with general purpose registers, index registers and pointer
registers to access any memory value. For example if we would like to access memory at the
physical address 12345h (hexadecimal), we should set the DS = 1230h and SI = 0045h. This is
called the logical address and this way much more memory can be accessed than using a single
register that is limited to only 16 bit values. However, 8086/88 has accessible internal memory of
20bits (i.e., 220 memory cells). For this, CPU makes a calculation of physical address by
multiplying the segment register by 10h (or 1610) and adding general purpose register to the
resulting value (1230h * 10h + 45h = 12345h):
The address formed with 2 registers, in such a way, is called an effective address.
By default BX, SI and DI registers work with DS segment register; BP and SP work
with SS segment register. Other general-purpose registers cannot be used to form an effective
address. Moreover, only BX can form an effective address, BH and BL are not allowed for such
purpose.
Flags Register
Flags register is considered as special purpose register. It indicates the current status of the
microprocessor. Flags register is modified automatically by CPU after arithmetic and logical
operations, this allows to determine the type of the result, and to determine conditions to transfer
control to other parts of the program. Generally, these registers cannot be accessed directly,
similar to AX and other general registers. It is possible to change values of system registers using
some recommended methods that will be discussed later in the course.
Page 3 of 54
Dawood University of Engineering and Technology Assembly Language
Number Systems
To understand the internal structure of any microprocessor and to learn the respective assembly
language, the knowledge of number systems (specially, the hexadecimal number system) is
necessary.
This lab provides some description and activities to refresh the concept of number systems.
Two number systems are considered to be used for the purpose of communication between
human and machine. Theses number systems are binary, and decimal.
However, hexadecimal number systems is considered to be understandable by both human and
machines.
In the following sub-sections, the decimal, binary, octal, and hexadecimal number systems are
discussed briefly.
It is also called Base-10 number system. Since it is base-10, therefore only ten items (i.e., 0, to 9)
are used in this number system. These decimal (ten) items are termed as digits.
It is also called Base-2 number system. Since it is base-2, therefore only two digits (i.e., 0, and 1)
are used in this number system. These binary digits are termed as bits (binary digits).
It is also called Base-8 number system. Since it is base-8, therefore only eight digits (i.e., 0, to 7)
are used in this number system. These octal digits can also be represented as bits.
It is also called Base-16 number system. Since it is base-16, therefore only eight digits (i.e., 0, to
9, and A to B) are used in this number system. These hexadecimal digits can also be represented
as bits.
The possibilities to convert among bases are shown in the following figure.
Page 4 of 54
Dawood University of Engineering and Technology Assembly Language
A Quick Example
Following is a quick example of such conversions.
Page 5 of 54
Dawood University of Engineering and Technology Assembly Language
Lab Task-1
Convert and fill the table give below. Use your roll number to fill the last (blank) row.
1. 45
2. 76
3. 1101101
4. 623
5. 8DF
6.
Lab Task-2
Write the full names of the following registers and mention their size.
AX ________________________________________________________________
BX ________________________________________________________________
CX ________________________________________________________________
DX ________________________________________________________________
EAX________________________________________________________________
EBX________________________________________________________________
ECX________________________________________________________________
EDX________________________________________________________________
RAX________________________________________________________________
RBX________________________________________________________________
RCX________________________________________________________________
RDX________________________________________________________________
Page 6 of 54
Dawood University of Engineering and Technology Assembly Language
Lab-02
Objective:
Exploring Instruction Set Architecture (ISA) of x86 Machines and Register Organization.
Registers
Registers are storage locations inside the processor. A register can be accessed more quickly than
a memory location. Different registers serve different purposes. Some of them are described
below:
General-Purpose Registers
EAX, EBX, ECX and EDX are called data or general-purpose registers. (E is for extended as
they are 32-bit extensions of their 16-bit counter parts AX, BX, CX and DX in 16-bit ISA). The
register EAX is also known as accumulator because it is used as destination in many arithmetic
operations. Some instructions generate more efficient code if they reference the EAX register
rather than other registers.
Bits in a register are conventionally numbered from right to left, beginning with 0 as shown
below.
Page 7 of 54
Dawood University of Engineering and Technology Assembly Language
Apart from accessing the register as a whole, these registers can be accessed in pieces as
illustrated in the following figure
AX(16 Bits)
AH (8 Bits) AL (8 Bits)
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
It should be carefully noted that high-order 16 bits of these registers cannot be referenced
independently.
Index Registers
ESI (Extended Source Index) and ED I(Extended Destination Index) registers are respectively
used as source and destination addresses in string operations. They can also be used to
implement array indices.
Pointer Registers
The EIP (Extended Instruction Pointer) register contains the offset in the current code segment
for the next instruction to be executed.
ESP (Extended Stack Pointer) and EBP (Extended Base Pointer) are used to manipulate stack - a
memory area reserved for holding parameters and return address for procedure calls. ESP holds
address of top of stack, location where the last data item was pushed. EBP is used in procedure
calls to hold address of a reference point in the stack.
Flags Register
EFLAGS register is never accessed as a whole. Rather, individual bits of this register either
control the CPU operation (control flags) or reflect the outcome of a CPU operation (status flag).
Following shows some of the commonly used control and status flags.
Page 8 of 54
Dawood University of Engineering and Technology Assembly Language
Abbreviation =1 =0
CF CY(Carry) NC (No Carry)
PF PE (Parity Even) PO (Parity Odd)
AF AC (Auxiliary Carry) NA (No Auxiliary Carry)
ZF ZR (Zero) NZ (Not Zero)
SF NG (Negative) PL (Positive)
IF EI (Enable Interrupt) DI (Disable Interrupt)
DF DN (Down) UP (Up)
OF OV (Overflow) NV (Not Overflow)
Memory Addressing
A 32-bit processor uses 32-bit addresses and thus can access 232B = 4GB physical memory.
Depending on the machine, a processor can access one or more bytes from memory at a time.
The number of bytes accessed simultaneously from main memory is called word length of
machine.
Generally, all machines are byte-addressable i.e.; every byte stored in memory has a unique
address. However, word length of a machine is typically some integral multiple of a byte.
Therefore, the address of a word must be the address of one of its constituting bytes. In this
regard, one of the following methods of addressing (also known as byte ordering) may be used.
Page 9 of 54
Dawood University of Engineering and Technology Assembly Language
Big Endian – the higher byte is stored at lower memory address (i.e. Big Byte first). MIPS,
Apple, Sun SPARC are some of the machines in this class.
Little Endian - the lower byte is stored at lower memory address (i.e. Little Byte first). Intel’s
machines use little endian.
Consider for example, storing 0xA1B2C3D4 in main memory. The two-byte orderings are
illustrated below.
Little Endian Big Endian
Addresses Content Addresses Content
2032 D4 2032 A1
2033 C3 2033 B2
2034 B2 2034 C3
2035 A1 2035 D4
Memory model
The memory model specifies the memory size assigned to each of the different parts or segments
of a program. There exist different memory models for the 8086 proceessor IA-32 can use one of
the three basic memory models:
Real-Address Memory Model – is the original 8086 model and its existence ensures backward
compatibility.
Flat Memory Model – memory appears to a program as a single, contiguous address space of
4GB. Code, data, and stack are all contained in this address space, also called the linear address
space
Page 10 of 54
Dawood University of Engineering and Technology Assembly Language
Tiny Memory Model: In the TINY model both code and data occupy one physical segment.
Therefore, all procedures and variables are by default addressed as NEAR, by pointing at their
offsets in the segment. On assembling and linking a source file, the tiny model automatically
generates a com file, which is smaller in size than an exe file.
Small Memory Model: In the SMALL model all code is placed in one physical segment and all
data in another physical segment. In this model, all procedures and variables are addressed as
NEAR by pointing to their offsets only.
Compact Memory Model In the COMPACT model, all elements of code (e.g. procedures) are
placed into one physical segment. However, each element of data can be placed by default into
its own physical segment. Consequently, data elements are addressed by pointing both at the
segment and offset addresses. In this model, all code elements (procedures) are addressed as
NEAR and data elements (variables) are addressed as FAR.
Medium Memory Model: The MEDIUM model is the opposite of the compact model. In this
model data elements are treated as NEAR and code elements are addressed as FAR.
Large Memory Model: In the LARGE model both code elements (procedures) and data
elements (variables) are put in different physical segments. Procedures and variables are
addressed as FAR by pointing at both the segment and offset addresses that contain those
elements. However, no data array can have a size that exceeds one physical segment (i.e. 64
KB).
Huge Memory Model: The HUGE memory is similar to the LARGE model with the exception
that a data array may have a size that exceeds one physical segment (i.e. 64 KB).
In MASM, segments are declared using the .MODEL directive. This directive is placed at the
very beginning of the program, or after the optional title directive
Segment Registers
The segment registers hold the segment selectors which are special pointers that point to start of
individual segments in memory. The use of segment registers is dependent on the memory
management model in use.
In a flat memory model, segment registers point to overlapping segments, each of which begins
at address 0 as illustrated in the following figure. When using the segmented memory model,
each segment is loaded with a different memory address.
Page 11 of 54
Dawood University of Engineering and Technology Assembly Language
The segment registers (CS, DS, SS, ES, FS, and GS) hold 16-bit segment selectors. To access a
particular segment in memory, the segment selector for that segment must be present in the
appropriate segment register. Each of the segment registers is associated with one of three types
of storage: code, data, or stack. For example, the CS register contains the segment selector for
the code segment, where the instructions being executed are stored. The processor fetches
instructions from the code segment, using a logical address that consists of the segment selector
in the CS register and the contents of the EIP register. The EIP register contains the offset within
the code segment of the next instruction to be fetched.
The DS, ES, FS, and GS registers point to four data segments. The availability of four data
segments permits efficient and secure access to different types of data structures. With the flat
Page 12 of 54
Dawood University of Engineering and Technology Assembly Language
memory model, we use, the segment registers become essentially irrelevant to the programmer
because operating system gives each of CS, DS, ES and SS values.
Lab Task-1
Fill in the following tables to show storage of 0xABDADDBA at address 1996 in the memory of
a machine using (i) little endian (ii) big endian byte ordering.
Lab Task-2
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
_____________________________________________________________________________
Page 13 of 54
Dawood University of Engineering and Technology Assembly Language
Lab Task-3
For each add instruction in this exercise, assume that AX contains the given contents before the
instruction is executed. Give the contents of AX as well as the values of the CF, OF, SF, PF, AF
and ZF after the instruction is executed. All numbers are in hex. (Hint: add eax, 45 adds 45 to the
contents of register ax and stores the result back in ax)
Contents of AX Contents of AX
Instruction CF OF SF PF AF ZF
(Before) (After)
0045 add ax, 45
FF45 add ax, 45
0045 add ax, -45
FF45 add ax, -45
FFFF add ax, 1
Lab Task-4
Show the ECX register and the size and position of the CH, CL, and CX within it.
Page 14 of 54
Dawood University of Engineering and Technology Assembly Language
Lab-03
Objective:
To understand the system commands using debug utility and to introduce System commands
using DEBUG programming utility (at command prompt using PC).
DEBUG
DEBUG is a program included in the MS-DOS and PC-DOS operating systems that allows the
programmer to monitor a program’s execution closely for debugging purposes. Specifically, it
can be used to examine and alter the contents of memory, to enter and run programs, and to
stop programs at certain points in order to check or even change data. This lab will help to
learn the following.
Click the DOSBox icon, it will open a command window with a prompt Z:\>. Type the
following commands.
Z:\>Mount c c:\MP
This command emulates drive C as your working drive.
Next, write the following command
Z:\> C: and press Enter key
This command will result in the following prompt
C:\ >
a) Entering and Exiting DEBUG
To enter and exit the DEBUG, simply type its name at the DOS level:
DEBUG prompt
NOTE:
• DEBUG is not case-sensitive.
• It is assumed in the above instruction that DEBUG program is on the drive C
• After enter key pressed, the DEBUG prompt “-“ will appear on the following line.
Page 15 of 54
Dawood University of Engineering and Technology Assembly Language
-Q <return>
C:\>
System Commands
The system commands can be divided into the following groups according to their
functions.
• Memory Management Commands.
• Assembler Commands.
• Program Execution Commands.
Note:
• Hexadecimal number system is used throughout the experiments (unless otherwise
specified).
• The suffix ‘H’ can be omitted.
Page 16 of 54
Dawood University of Engineering and Technology Assembly Language
It moves the contents of a block of memory specified in <Range> from one place to another.
The starting address of the destination is specified in <Address>.
Page 17 of 54
Dawood University of Engineering and Technology Assembly Language
Assembler Commands
Lab Task-1
Show the ECX register and the size and position of the CH, CL, and CX within it.
Lab Task-2
Do the following using Memory management commands and capture each screen using print
screen option.
a) Display the memory contents starting from memory location 0100h
b) Display the memory contents of first 10 bytes starting from memory location 0100h
c) Fill the memory location starting from 0200h with your name and 2-digit Roll #
d) Note the last offset (memory) value of the last digit of your roll #
e) Move the block of memory where you filled your name and Roll # to the memory
location starting from 0400h
f) Compare the two memory blocks (one starting from 0100h & the other from 0400h)
g) Edit 2018-CS- to the memory block starting from 0400h before your Roll #
h) Compare the two memory blocks (one starting from 0100h & the other from 0400h)
i) Calculate the sum and difference of 75 and 34 using H command
Page 18 of 54
Dawood University of Engineering and Technology Assembly Language
Page 19 of 54
Dawood University of Engineering and Technology Assembly Language
Lab-04
Objective:
To understand control commands using debug programming utility (at command prompt using PC).
Program Control
The program control commands are considered as the part of System commands. The three
basic commands are as under:
G Execute program
R Display or modify the contents of registers.
T Trace program execution.
Command syntax G [=<Start address>],[<Break point 1>, <Break point 2>... <Break point
n>]
The ‘=’ sign in the above syntax indicates that a <Start address> parameter will be
given.
If “=” is not used, then the system assumes any parameters as break points.
<Start address> specifies the beginning address of the program to be executed.
If <Start address> not given, execution will start at the default value, which is the
current CS: IP location.
<Break point> shows the program results (at the specified instruction locations given by
programmer) as to determine whether the program runs as expected.
Up to 10 break points are allowed.
Multiple break points make debugging easier when the program contains flow control
instructions, such as conditional jumps (e.g., JNZ, JC, JNE).
If no break point is set, control will not return to the user, until the program execution is
completed.
Figure below shows the execution of code using G command.
Page 20 of 54
Dawood University of Engineering and Technology Assembly Language
Page 21 of 54
Dawood University of Engineering and Technology Assembly Language
Page 22 of 54
Dawood University of Engineering and Technology Assembly Language
MSB LSB
bit # bit #
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
XX XX XX XX OF DF IF TF SF ZF XX AF XX PF XX CY
Flags register is a bit-addressable register and holds 16 different bits. These bits are used
individually to indicate different status as well as control. The 16 flag bits are shown above with
their bit locations. Among the 16 bits, only nine bits are used and the rest are reserved for future.
There are six status flags and three control flags. Overflow, Sign, Zero, Auxiliary, Parity, and
Carry flags are considered as status flags whereas Direction, Interrupt, and Trap flags are the
control flags. The Flag registers are described in Table 3.1 with their names and possible values.
Page 23 of 54
Dawood University of Engineering and Technology Assembly Language
Lab Task-1
Assemble a program using DEBUG programming utility to move the decimal values in registers
as given below
AX = 543110
BX = 932110
CX = 4503210
DX = 2310210
(Hint: First convert the above numbers in Hexadecimal system)
Lab Task-2
Apply the program control command T to execute the code. Capture the screen.
Write down and analyze the values of each registers including IP and Flag registers.
Page 24 of 54
Dawood University of Engineering and Technology Assembly Language
Lab Task-3
Apply the program control command G to execute the code. Capture the screen.
Write down and analyze the values of each registers including IP and Flag registers.
Page 25 of 54
Dawood University of Engineering and Technology Assembly Language
Lab-05
Objective:
To create and assemble an executable Assembly Language program using Assembler and Linker
utilities.
Steps Involved
The three steps to be followed to create an executable assembly language program are listed as
under
Steps Input Program Output
1. Edit the program Keyboard Editor myfile.asm
2. Assemble the program myfile.asm TASM or MASM myfile.obj
3. Link the program myfile.obj LINK or TLINK myfile.exe
Where, .asm, .obj, and .exe are the extension meaning assembler, object and executable file
respectively.
Directives:
.model (memory models)
Large Both can exceed 64kb (single data array can’t exceed 64kb)
Huge Both can exceed 64kb (single data array can exceed 64kb)
.stack 100h
Stack memory reserved 100 bytes
.data
Data segment starts
.code
Code segment starts
Programming Formats:
Page 26 of 54
Dawood University of Engineering and Technology Assembly Language
As it is obvious from the above description that a program is to be typed in by using an editor,
like Notepad (in windows) or Edit (in DOS) and stored with the extension “.asm”. It is worth
mentioning that there are two segment definitions used to write an assembly code, Full segment
and Simplified segment, as shown in following figure. The stored “.asm” program is then
presented to an assembler program such as TASM (Turbo Assembler) or MASM (Microsoft’s
Macro Assembler) in the following manner, (assuming that you are working on the COMMAND
PROMPT)
C:\> MASM myfile.asm; < return >
If no errors found in the program, MASM will generate an object file. This object file is to be
presented to the TLINK (Turbo Linker) or LINK as
.model small
.stack 100h
.code
main proc
mov ah,1
int 21h
mov dl, al
mov ah,2
int 21h
mov ah,4ch
int 21h
main endp
end main
The executable file just created can be analyzed or debugged using DEBUG utility, the method
will be
C:\> myfile.exe < return >
or
C:\> DEBUG myfile.exe < return >
The desired DEBUG commands can then be used. The program file “myfile.exe” can now
be analyzed by using U command.
Page 27 of 54
Dawood University of Engineering and Technology Assembly Language
Lab Task-1
Page 28 of 54
Dawood University of Engineering and Technology Assembly Language
Lab-06
Objective:
To learn different constructs of assembly language program.
Name Field
The name field is used for instruction labels, procedure names and variable names. The
assembler translates names into memory addresses. Name can be from 1 to 31 characters, and
may contain letters, digits, and other logical characters ?,@, $, and %. Embedded blanks are not
allowed and may not begin with a digit. The assembler does not differentiate between upper and
lower case in a name.
Operation Field
For an instruction, the operation field contains a symbolic operation code (opcode). The
assembler translates a symbolic opcode into a machine language opcode. Opcode symbols often
describe the operation function; for example, MOV, ADD, SUB.
In an assembler directive, the operation field contains a pseudo operation code (pseudo-op).
Pseudo-ops are not translated into machine code; rather, they simply tell the assembler to do
something. For example, the PROC pseudo-op is used to create a procedure.
Operand Field
For an instruction, the operation field specifies the data that are to be acted on by the operation.
An instruction may have zero one or two operands. For example,
In a two operating instruction the first operand is the destination operand. It is the register or
memory location where the result is stored (Note: Some instruction does not store the result).
The second operand is the source operand. The source is usually not modified by the instruction.
Page 29 of 54
Dawood University of Engineering and Technology Assembly Language
For an assembler directive, the operand field usually contains more information about the
directive.
Comment Field
The comment field of a statement is used by the programmer to say something about what the
statement does. A semi colon marks the beginning of the field; and assembler ignores anything
type after the semi colon.
Comments are optional, but because assembly language is so low-level, it is almost impossible to
understand an assembly language program without comment.
In fact, a good programming practice dictates a comment on almost every line. The art of good
comment is developed through practice. Do not say something obvious like this:
Instead, use comments to put the instruction into the context of the program.
It is also permissible to make an entire line a comment and to use them to create space in our
program:
;
; initialize registers
;
MOV AX, 0
MOV BX, 0
Program Data
The processor operates only on binary data. Thus, the assembler must translate all data
representation into binary numbers. However, in an Assembly language program we may express
data as binary, decimal, or hex number, and even as characters.
Numbers
A binary number is written as a bit string followed by the letter “B” or ‘b’; for example 11001B.
A decimal number is a string of decimal digits, ending with an optional “D” of “d”.
A hex number must begin with a decimal digit and end with the letter “H” or “h”; for example
0ABCH (the reason for this is that the assembler would be unable to tell whether a system such
as “ABCH” represent the name “ABCH” or the hex number ABC).
Any of the preceding number may have an optional sign. Here are examples of legal and illegal
numbers for MASM.
Number Type
11011 decimal
11011B binary
64223 decimal
-21843D decimal
Page 30 of 54
Dawood University of Engineering and Technology Assembly Language
Characters
Characters and character string must be enclosed in single or double quotes; for example, “A” or
‘Hello’. Characters are translated into their ASCII codes by the assembler, so there is no
difference between using “A” and 41h (the ASCII for “A” is 41h) in a program.
Variables
Variables play the same role in assembly language that they do in high-level language. Each
variable has a data type and is assigned a memory address by the program. The data defining
pseudo-ops and their meanings are listed in the table below. Each pseudo-ops can be used to set
aside one or more data items of the given type.
Byte Variable
The assembler directive that defines a byte variable takes the following form.
name DB initial value
where the pseudo-op DB stands for “defined byte”.
For example,
ALPHA DB 4
This directive causes the assembler to associate a memory byte with the name ALPHA, and
initializes it to 4. A question mark (“?”) used in place of an initial value sets aside an
uninitialized byte, for example,
BYTE DB ?
The decimal range of initial value that can be specified is -128 to 127 if a signed interpretation is
being given, or 0 to 255 for an unsigned interpretation. These are the ranges of values that fit in a
byte.
Word Variable
The assembler directive for defining a word variable has the following form:
name DW initial value
Page 31 of 54
Dawood University of Engineering and Technology Assembly Language
As with byte variable, a question mark in place of initial value means an uninitialized word. The
decimal range of initial value can be specified is -32,768 to 32,767 for a signed interpretation, or
0 to 65,535 for an unsigned interpretation.
Lab Task-1
Which of the following names are legal in IBM PC assembly language?
a) TWO_WORDS
b) ?1
c) Two words
d) .@?
e) $145
f) LET’S GO
Lab Task-2
Which of the following are legal numbers? If they are legal, tell whether they are binary, decimal
or hex numbers.
a) 246
b) 246h
c) 1001
d) 1,101
e) 2A3h
f) FFFEh
g) 0Ah
h) Bh
i) 1110b
Lab Task-3
If it is legal, give data definition pseudo-ops to define each of the following.
a) A word variable A initialized to 52
b) A word variable WORDl, uninitialized
c) A byte variable B, initialized to 25h
d) A byte variable Cl, uninitialized
e) A word variable WORD2, initialized to 65536
Page 32 of 54
Dawood University of Engineering and Technology Assembly Language
Lab-07
Objective:
To understand arrays and constants in Assembly language.
Arrays
In assembly language, an array is just a sequence of memory bytes or words. For example, to
define a three-byte array called B_ARRAY, whose initial values are l0h, 20h, and 30h, we can
write,
The name B_ARRAY is associated with the first of these bytes, B_ARRAY+l with the second,
and B_ARRAY+2 with the third. If the assembler assigns the offset address 0200h to
B_ARRAY, then memory would look like this:
Sets up an array of four words; with initial values 1000, 40, 29887, and 329. The initial word is
associated with the name W_ARRAY, the next one with W_ARRAY+2, the next with
W_ARRAY+ 4, and so on. If the array starts at 0300h, it will look like this:
WORD1 DW 1234h
The low byte of WORD1 contains 34h, and the high byte contains 12h. The low byte has
symbolic address WORD1, and the high byte has symbolic address WORD1+1.
Character Strings
An array of ASCII codes can be initialized with a string of character. For example,
LETTERS DB ‘ABC$’
is equivalent to
Page 33 of 54
Dawood University of Engineering and Technology Assembly Language
LETTERS DB 41h,42h,43h
Inside a string, the assembler differentiates between upper and lower case. Thus, the string "abc"
is translated into three bytes with values 61h, 62h, and 63h.
is equivalent to
MSG DB 48H, 45H, 4CH, 4CH, 4FH, 0AH, 0DH, 24H
Named Constants
To make assembly language code easier to understand, it is often desirable to use a symbolic
names for a constant quantity.
EQU (Equates)
To assign a name to a constant, we can use the EQU (equates) pseudo-op. The syntax is
LF EQU 0AH
assigns the name LF to 0AH, the ASCII code of the line feed character. The name LF may now
be used in place of 0AH anywhere in the program. Thus, the assembler translates the instruction
and
MOV DL, LF
Then instead of
MSG DB ‘TYPE YOUR NAME’
we could say
MSG DB PROMPT
Page 34 of 54
Dawood University of Engineering and Technology Assembly Language
Lab Task-1
If it is legal, give data definition pseudo-ops to define each of the following.
a) A word array ARRAYI, initialized to the first five positive integers (i.e. 1 to 5)
b) A constant BELL equal to 07h
c) A constant MSG equal to ‘THIS IS A MESSAGE$’
Lab Task-2
Suppose that the following data are loaded starting at offset 0000h:
A DB 7
B DW 1ABCh
C DB 'HELLO'
a) Give the offset address assigned to variable A, B, and C.
b) Give the content of byte at offset 0002h in hex.
c) Give the content of byte at offset 0004h in hex.
d) Give the offset address of the character "O" in "HELLO"
Page 35 of 54
Dawood University of Engineering and Technology Assembly Language
Lab-08
Objective:
Learn about a few instructions of Assembly Language.
In the following, WORDI and WORD2 are word variables, and BYTEI and BYTE2 are byte
variables. AH is the high byte of register AX, and BL is the low byte of BX.
This reads "Move WORDl to AX". The content of register AX are replaced by the contents of
memory location WORD1. The content of WORD1 are unchanged. In other words a copy of
WORD1 is sent to AX.
MOV AX, BX
This is a move of the number 041h (the ASCII code of "A'') into register AH. The previous value
of AH is overwritten (replaced by new value).
The XCHG (exchange) operation is used to exchange the contents of two registers, or a register
and a memory location. The syntax is
An example is
XCHG AH, BL
Page 36 of 54
Dawood University of Engineering and Technology Assembly Language
This instruction swaps the contents of AH and BL, so that AH contains what was previously in
BL and BL contains what was originally in AH. Another example is
Destination Operand
General Segment Memory
Source Operand Constant
Register Register Location
General Register Yes yes yes no
Segment Register Yes no yes no
Memory Location Yes yes no no
Constant Yes no yes no
Destination Operand
General Memory
Source Operand
Register Location
General Register Yes yes
Memory Location Yes no
Note in particular that a MOV or XCHG between memory locations is not allowed. For example,
The syntax is
ADD destination, source
SUB destination, source
Page 37 of 54
Dawood University of Engineering and Technology Assembly Language
For example,
ADD WORDl, AX
This instruction, "Add AX to WORD1" causes the contents of AX and memory word WORD1 to
be added, and the sum is stored in WORD1. AX is unchanged.
SUB AX, DX
In this example, "Subtract DX from AX" the value of DX is subtracted from the value of AX,
with the difference being stored in AX. DX is unchanged.
ADD BL, 5
This is an addition of the number 5 to the contents of register BL. As was the case with MOV
and XCHG, there are some restrictions on the combinations of operands allowable with ADD
and SUB. Direct addition or subtraction between memory locations is illegal; for example,
INC (increment) is used to add 1 to the contents of a register or memory location and DEC
(decrement) subtracts 1 from a register or memory location. The syntax is
INC destination
DEC destination
For example.
INC WORD1
DEC BYTEl
NEG is used to negate the contents of the destination. NEG does this by replacing the contents
by its two’s. complement. The syntax is
NEG destination
NEG BX
The operands of the preceding two operands instruction must be of the same type; that is, both
bytes or words thus an instruction such as
and
In the former case, the assembler reasons that since the destination AH is a byte, the source must
be a byte, and it moves 41h into AH. In the latter case, it assumes that because the destination is
a word, so is the source, and it moves 0041h into AX.
Statement
B=A
Translation
MOV AX, A ; move A into AX
MOV B, AX ; and then into B
Remember that directly memory-memory move is illegal.so we must move the contant of A into
register before moving it into B.
Statement
A=5-A
Translation
MOV AX, 5 ; put 5 in AX
SUB AX, A ; AX contain 5-A
MOV A, AX ; put it in A
Page 39 of 54
Dawood University of Engineering and Technology Assembly Language
This example illustrates one approach to translating assignment statement: do the arithmetic in a
register-for example in AX-then move the result into the destination variable. In this case, there
is another shorter way
NEG A ; A=-A
ADD A, 5 ; A=5-A
The next example shows how to do multiplication by a constant.
Statement
A=B-2A
Translation
MOV AX, B ; AX has B
SUB AX, A ; AX has B-A
SUB AX, A ; AX has B-2A
MOV A, AX ; MOV result to A
Lab Task-1
Tell whether each of the following instructions is legal or illegal. Wl and W2 are word variables,
and B1 and B2 are byte variables.
a) MOV DS, AX
b) MOV DS, l000h
c) MOV CS, ES
d) MOV Wl, DS
e) XCHG W1, W2
f) SUB 5, Bl
g) ADD Bl, B2
h) ADD AL,256
i) MOV Wl, Bl
Lab Task-2
Using only MOV, ADD, SUB, INC, DEC, and NEG, translate the following high level language
assignment statements Into assembly language. A, B, and C are word variables.
a) A = B - A
b) A = - (A+ 1)
c) C = A + B
d) B = 3B + 7
e) A=B-A-1
Page 40 of 54
Dawood University of Engineering and Technology Assembly Language
Lab-09
Objective:
To understand string handling in Assembly language.
The ‘$’ marks the end of the string and is not displayed. If the string contains the ASCII code of
a control character, the control function is performed.
The following program will print “Dawood!” on the screen. This message is defined in the data
segment as
MSG DB ‘Dawood!$’
The LEA Instruction:
INT 21h, function, expects the offset of the character string to be in DX. To get it there, we use
the following instruction:
LEA destination, source
Where destination is a general register and source is a memory location. LEA stands for load
effective address. It puts a copy of the source offset address into the destination. For example:
LEA DX, MSG
puts the offset address of the variable MSG into DX. Because this program contains some data
so it holds a data segment which will begin with an instruction that initializes DS.
Program Segment Prefix
When a program is loaded in memory, DOS prefaces it with a 256- byte program segment prefix
(PSP). The PSP contains information about the prog bram. So that program may access this area,
DOS places its segment number in both DS and ES before executing the program. The result is
that DS does not contain the segment number of the data segment. To correct this, a program
containing data segment with these two instructions:
MOV AX, @DATA
MOV DS, AX
Page 41 of 54
Dawood University of Engineering and Technology Assembly Language
@DATA is the name of the data segment defined by .DATA. The assembler translates the name
@DATA into segment number. Two instructions are needed because a number (the data segment
number) may not be moved directly into a segment register.
With DS initialized, “Dawood” can be printed by placing its address in DX and executing INT
21H.
LEA DX, MSG ;get message
Mov Ah, 9 ;display string function
INT 21h; ;display string
.MODEL SMALL
.STACK 100H
.DATA
MSG DB ‘Dawood $’
.CODE
MAIN PROC
; initialize DS
MOV AX, @DATA
MOV DS, AX
; Display message
LEA DX, MSG
MOV AH, 9
INT 21H
; return to DOS
MOV AH, 4CH
INT 21H ;DOS Exit Command
MAIN ENDP
END MAIN
If you want to display two strings with separate line you may use the following program.
Page 42 of 54
Dawood University of Engineering and Technology Assembly Language
.MODEL SAMLL
.STACK 100H
.DATA
MSG1 DB ‘Dawood $’
MSG2 DB ‘University $’
.CODE
MAIN PROC
; initialize DS
MOV AX, @DATA
MOV DS, AX
MOV DX,10
MOV AH, 2
INT 21H
MOV DX,13
MOV AH, 2
INT 21H
; return to DOS
MOV AH, 4CH
INT 21H ;DOS Exit Command
MAIN ENDP
END MAIN
Page 43 of 54
Dawood University of Engineering and Technology Assembly Language
Lab Task-1
Write a program in Assembly language which prints the following messages and takes two one
digit number as input.
Message-1: Please enter the first number.
Message-2: Please enter the second number.
Page 44 of 54
Dawood University of Engineering and Technology Assembly Language
Lab-10
Objective:
To understand how instructions affect the flags register.
Example-1
ADD AX, BX, where AX contains FFFH, BX contains FFFFh.
Solution:
FFFFh
+ FFFFh
1 FFFEh
Page 45 of 54
Dawood University of Engineering and Technology Assembly Language
Example-2
ADD AL, BL; Where AL contains 80h, BL contains 80h.
Solution:
80h
+ 80h
1 00h
Page 46 of 54
Dawood University of Engineering and Technology Assembly Language
Example-4
INC AL, where AL contains FFh.
Solution:
FFh
+ 01h
1 00h
Page 47 of 54
Dawood University of Engineering and Technology Assembly Language
Lab Task-1
For each of the following instructions, give the new destination contents and the new settings of
CF, SF, ZF, PF, and OF. Suppose that the flags are initially 0 in each part al this question.
a) ADD AX, BX where AX contains 7FFFh and BX contains 000lh
b) SUB AL, BL where AL contains 01h and BL contains FFh
c) DEC AL where AL contains 00h
d) NEG AL where AL contains 7Fh
e) XCHG AX, BX where AX contains 1ABCh and BX contains 712Ah
f) ADD AL, BL where AL contains 80h and BL contains FFh
g) SUB AX, BX where AX contains 000h and BX contains 8000h
h) NEG AX where AX contains 0001h
Page 48 of 54
Dawood University of Engineering and Technology Assembly Language
Lab-11
Objective:
To understand the use of procedures and macro in Assembly language.
Procedure
In Assembly language, procedures (also known as subroutines or functions) are a fundamental
concept that allows for the organization and reuse of code. A procedure is a self-contained block
of code that performs a specific task and can be called from multiple locations within a program.
Page 49 of 54
Dawood University of Engineering and Technology Assembly Language
Code reuse: Procedures enable code reuse, as they can be called from multiple locations within
a program. This promotes modularity and reduces code duplication, making programs more
maintainable.
Procedures are a powerful mechanism in assembly language programming as they allow for the
creation of reusable and structured code. They enable the programmer to break down complex
tasks into smaller, more manageable pieces, making the code easier to write, understand, and
maintain.
Macro
In assembly language, a macro is a mechanism that allows to define reusable code templates.
Macros are similar to procedures, but they are expanded during assembly-time rather than being
called at runtime like procedures. The concept of macros is a way to automate repetitive or
complex code sequences and improve code readability and maintainability.
Macro definition: To define a macro, you specify its name and the code sequence it represents.
This code sequence can include any valid assembly language instructions, directives, or even
other macros.
Macro invocation: To use a macro, you invoke it by its name, possibly providing arguments or
parameters that the macro can use. The macro invocation is typically done by using a macro-
specific directive, such as "MACRO" or "%MACRO".
Macro expansion: During the assembly process, when a macro is invoked, the assembler
replaces the macro invocation with the code sequence defined by the macro. This expansion
occurs before the actual assembly of the program.
Parameter passing: Macros can accept parameters or arguments, allowing for customization
and flexibility. Parameters can be used within the macro definition to create code that can adapt
to different situations. The specific syntax for passing parameters to macros depends on the
assembler and the macro definition.
Code generation: Macros can generate code based on their parameters or other factors. This
allows for the creation of repetitive code sequences, loops, conditional statements, and other
code constructs without the need to manually write them every time.
Code reuse and modularity: Macros enable code reuse by allowing the same code sequence to
be used at multiple locations in the program. This promotes modularity, reduces code
duplication, and improves maintainability.
Macro libraries: Macros can be organized into libraries for easy management and sharing.
Macro libraries contain a collection of pre-defined macros that can be included in assembly
programs.
Preprocessor directives: Some assemblers provide preprocessor directives specifically for
working with macros. These directives allow for conditional macro expansion, macro nesting,
macro parameter manipulation, and other advanced macro-related features.
Page 50 of 54
Dawood University of Engineering and Technology Assembly Language
Symbolic constants: Macros can define symbolic constants, which are values that are replaced
by their corresponding values during macro expansion. This allows for the creation of
meaningful and easily modifiable constants in the code.
Code readability and maintainability: Macros can improve code readability by providing
meaningful names to complex or repetitive code sequences. They also enhance code
maintainability by centralizing changes within the macro definition, which automatically affects
all instances of the macro in the program.
It's important to note that the specific syntax and features related to macros can vary depending
on the assembler and the assembly language being used. Therefore, it's essential to consult the
documentation or resources specific to the assembler the programmers are working with to
understand its macro capabilities and usage.
Difference between Macro and Procedure
Macro Procedure
Macro definition contains a set of Procedure contains a set of instructions
1. instruction to support modular which can be called repetitively which
programming. can perform a specific task.
It is used for small set of instructions It is used for large set of instructions
2.
(mostly less than ten instructions). (mostly more than ten instructions).
In case of macro memory requirement is In case of procedure memory
3.
high. requirement is less.
CALL and RET instruction/statements CALL and RET instruction/statements
4.
are not required in macro. are required in procedure.
Assembler directive MACRO is used to Assembler directive PROC is used to
define macro and assembler directive define procedure and assembler directive
5.
ENDM is used to indicate the body is ENDP is used to indicate the body is
over. over.
Execution time of macro is less as it Execution time of procedures is high as
6.
executes faster than procedure. it executes slower than macro.
Here machine code is created multiple Here machine code is created only once,
7. times as each time machine code is it is generated only once when the
generated when macro is called. procedure is defined.
In a macro, parameter is passed as part In a procedure, parameters are passed in
8.
of statement that calls macro. registers and memory locations of stack.
Overhead time takes place during calling
Overhead time does not take place as
9. procedure and returning control to
there is no calling and returning.
calling program.
Page 51 of 54
Dawood University of Engineering and Technology Assembly Language
.model small
.stack 100h
.data
string1 db 'Dawood$'
string2 db 'University$'
string3 db 'Karachi$'
.code
main proc
mov ax, @data
mov ds, ax
call myenterproc
call myenterproc
mov ah,4ch
int 21h
main endp
myenterproc proc
mov dx,10
mov ah,2
int 21h
mov dx,13
mov ah,2
int 21h
ret
myenterproc endp
end main
Page 52 of 54
Dawood University of Engineering and Technology Assembly Language
myprintmacro macro p1
mov dx, offset p1
mov ah, 9
int 21h
mov ah,10
int 21h
mov dx, 13
mov ah, 2
int 21h
mov dx, 10
mov ah, 2
int 21h
endm
.model small
.stack 100h
.data
string1 db 'Dawood$'
string2 db 'University$'
string3 db 'Karachi$'
.code
main proc
mov ax, @data
mov ds, ax
myprintmacro string1
myprintmacro string2
myprintmacro string3
main endp
end main
Lab Task-1
Define a macro called ‘CalculateSum’ that calculates the sum of two numbers and stores the
result in a register. The macro should take two parameters, the two numbers to be added, and
should use appropriate registers for computation. The result should be stored in a specified
register.
Page 53 of 54
Dawood University of Engineering and Technology Assembly Language
Lab-12
Page 54 of 54