You are on page 1of 54

Dawood University of Engineering and Technology Assembly Language

Lab-01

Objective:
Introduction to 8086/88 Assembler, the Central Processing Unit (CPU), and refresh the
hexadecimal number systems.

What is Assembly Language?


Assembly language is a low level programming language. Students need to get some knowledge
about computer structure in order to understand anything about the assembler. The simple
computer model is shown in the following figure.

The system bus connects the various components of a computer. The Central Processing


Unit (CPU) is the heart of the computer, and most of the computations take place inside the CPU.
Random Access Memory (RAM) is a temporary storage place where the programs are loaded in
order to be executed.

Inside the Central Processing Unit (CPU) 


The central processing unit is shown as the combination of registers in the following . The
registers are discussed in the following sub-sections.

General Purpose Registers

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.

Central Processing Unit

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.

Special Purpose Registers


The 8086/88 CPU has ten special purpose registers categorized in three different groups, namely,
Index and pointer registers, Segment registers, Flag registers. Each is discussed below 

Index and Pointer Registers

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.

Decimal Number Systems:

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.

Binary number systems:

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

Octal Number Systems:

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.

Hexadecimal Number Systems:

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.

Conversion among Bases

The possibilities to convert among bases are shown in the following figure.

Conversion from decimal to binary


– Divide the given number by two, keeping track of the remainder
– First remainder is bit 0 with the weight 1 (LSB, least-Significant Bit)
– Second remainder is bit 1 with the weight 2
– Third remainder is bit 2 with the weight 4
– nth remainder is bit n-1 with the weight 2(n-1) (MSB, Most-Significant Bit)

Page 4 of 54
Dawood University of Engineering and Technology Assembly Language

Conversion from decimal to octal


– Divide the given number by eight, keeping track of the remainder
– First remainder is bit 0 with the weight 1 (LSB, least-Significant Bit)
– Second remainder is bit 1
– Do the same till the last digit

Conversion from decimal to hexadecimal


– Divide the given number by sixteen, keeping track of the remainder
– First remainder is bit 0 with the weight 1 (LSB, least-Significant Bit)
– Second remainder is bit 1
– Do the same till the last digit

Conversion from binary to decimal
– Multiply each bit by 2n, the “weight” of the bit
– n is the sequence number of bits from LSB which is the right most bit
– Add the results

Conversion from binary to octal


– Group bits in threes, from right to left
– Convert to octal digits

Conversion from binary to hexadecimal


– Group bits in four, from right to left
– Convert to hexadecimal digits

Conversion from octal hexadecimal


– Use binary as an intermediary

Conversion from hexadecimal to octal


– Use binary as an intermediary

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.

No. Decimal Binary Octal Hexadecimal

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.

Instruction Set Architecture (ISA)


The ISA of a machine is the set of its attributes a system programmer needs to know in order to
develop system software or a complier requires for translation of a High-Level Language (HLL)
code into machine language. Examples of such attributes are (but not limited to):
 Instruction Set- An instruction set is a group of commands for a central processing unit
(CPU) in machine language. The term can refer to all possible instructions for a CPU or
a subset of instructions to enhance its performance in certain situations.
 Programmer Accessible Registers - these are the general-purpose registers (GPR) within
a processor in contrast to some special purpose registers only accessible to the system
hardware and Operating System (OS)
 Memory-Processor Interaction- The connections between the CPU and Memory
 Addressing Modes - means of specifying operands in an instruction (e.g. immediate
mode, direct mode, indirect mode, etc)
 Instruction Formats – breakup of an instruction into various fields (e.g. opcode,
specification of source and destination operands, etc)
ISA is also known as the programmer’s view or software model of the machine.

ISA of x86 Machines


From its onset in 1978, x86 ISA has been the most dominant in desktops and laptops. This
represents a family of machines beginning with 16-bit 8086/8088 microprocessors. (An n-bit
microprocessor is capable of performing n-bit operations). As an evolutionary process, Intel
continued to add capabilities and features to this basic ISA. The 80386 was the first 32-bit
processor of the family. The ISA of 32-bit processor is regarded as IA-32 (IA for Intel
Architecture) or x86-32 by Intel. IA-64 was introduced in Pentium-4F and later processors.
Operating Systems are now also categorized on the basis of the architecture they can run on. A
64-bit OS can execute both 64-bit and 32-bit applications. We will limit scope of our discussion
to IA-32.

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

EAX (32 Bits)

RAX (64 Bits)

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

Bit Name of Flag Type Description


OF (Overflow
11 Status Indicates overflow resulting from some arithmetic operation
Flag)
DF (Direction Determines left or right direction for moving or comparing
10 Control
Flag) string (character) data.
IF (Interrupt Indicates that all external interrupts, such as keyboard entry,
9 Control
Flag) are to be processed or ignored.
8 TF (Trap Flag) Control Permits operation of the processor in single-step mode.
Contains the resulting sign of an arithmetic operation (0 =
7 SF (Sign Flag) Status
positive and 1 = negative).
Indicates the result of an arithmetic or comparison operation
6 ZF (Zero Flag) Status
(0 = nonzero and 1 = zero result)
AF (Auxiliary Contains a carry out of bit 3 on 8–bit data, for specialized
4 Status
Flag) arithmetic.
Indicates even or odd parity of a low-order (rightmost) 8-bits
2 Parity Flag (PF) Status
of data.
Contains carry from a high-order (leftmost) bit following an
arithmetic operation; also, contains the contents of the last bit
0 CF (Carry Flag) Status
of a shift or rotate operation.

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:

Segmented Memory Model – memory appears to a program as a group of independent memory


segments, where code, data, and stack are contained in separate memory segments. To address
memory in this model, the processor must use segment registers and an offset to derive the linear
address. The primary reason for having segmented memory is to increase the system's reliability
by means of protecting one segment from other.

Real-Address Memory Model – is the original 8086 model and its existence ensures backward
compatibility.

Other commonly used memory models are:

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.

Little Endian Big Endian


Addresses Content Addresses Content
1996 1996
1997 1997
1998 1998
1999 1999

Lab Task-2

What is the significance of learning ISA of a processor?

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

_____________________________________________________________________________

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.

 How to enter and exit DEBUG


 How to assemble and unassembled a code
 How to observe machine code
 How to examine and alter the contents of registers and memory

Installing DOSBox and Mounting the Working Drive 


DOSBox is an emulator program which emulates an IBM PC compatible computer running a
DOS operating system. It is a free software written in C++ programming language and
distributed under the GNU General Public License. 

Download and install the DOSBox.

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:

C:\ > DEBUG and press Enter key


-

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

To quit the DEBUG, type Q at the DEBUG prompt and enter

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

Each command begins with a command letter followed by 1 to 3 operand parameters.


This lab will discuss memory management commands and assembler commands. The program
execution commands will be discussed later.
Setting up the Environment
Type the following command.
C:\>debug (press enter)

Memory Management Commands


 C Compare the contents of two blocks of memory.
 D Display the contents of memory.
 E Edit (Write) data into memory.
 F Fill the memory with a value.
 M Move the contents of memory.

Command C: Compare the contents of two blocks of memory

Command syntax C<Range>, <Address>

 <Range> indicates the source area


 <Address> is the starting address of the target memory block.
 If any mismatch is found, the system displays both the source and target addressees
and their contents.

Page 16 of 54
Dawood University of Engineering and Technology Assembly Language

Command D: Display the contents of memory

Command syntax D<Range>

 It will display the contents of a specified memory location.


 The ASCII codes for this value will also displayed.
 <Range> tells the D command what range of memory to display.
 If range is not specified, then the default starting address is set to the location
following the last address used by a previous D command.
 It there is no previous D commands, then the default starting address is 0100:0000.
 The default length of display is 80H.

Command F: Fill the memory with a value

Command syntax F<Range> <Series>

• It is used to change the contents of a block of memory to a certain sequence of values.


• The memory locations specified in <Range> are filled with the values specified in
<series>.

Command E: Edit (Write) data into memory

Command syntax E<Address> <Series>

• It is used to write data into memory.


• When E command is used without a <series> parameter; the system displays the
<address> and its contents, a byte of data.
• The user then types in a new byte of data, (two hexadecimal digits), to replace it.
• If <CR> is pressed, the next address can be edited.
• Memory can be edited until <Q> is pressed to exit the E command.

Command M: Move the contents of memory

Command syntax M<Range>, <Address>

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

Command H: Calculate the sum and difference

Command syntax H<Value 1>,<Value 2>

 If the sum is greater than FFFF the carry is discarded.


 Only 16 bits (4 hexadecimal digits) of the result are displayed.
 If <Value 1> is smaller than <Value 2>, borrow is used to execute subtraction.

Page 17 of 54
Dawood University of Engineering and Technology Assembly Language

Assembler Commands

The following letters stands for,


 A Assemble a program.
 U Disassemble a program

Command A: Assemble a program

Command syntax A [<Address>]


It is used to program with the 8088 assembly language.

Command U: Disassemble a program

Command syntax U [<Range>]

 It is used to disassemble assembly language instructions into machine code.


 <Range> is used to select a block of memory to disassemble.
 If <Range> is not specified, then the system will start immediately after the final
address
of the last U command.
 If no U command have been executed, the default starting address is CS: IP, which is
set
to 0100:0000 after power-on.
 The default length is 20H.

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 G: “Go” or Execute program

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

Command R: Check / Change the register contents

Command syntax R [=<Register>]


 The R command is used to check or change the contents of the 14 registers in the BGC-
8088.
 It can be used to dump the register contents, or to display and modify the register
contents.
 These functions can be described in the following ways.

R: (Display the register contents)


 If there is no <Register> specified, the R command is used to dump all the register
contents to the screen, as shown in Figure 3.2 (a).
 The registers are displayed in the following order:
AX, CX, DX, BX, SP, BP, SI, DI, ES, CS, SS, DS, IP and FG.
R: <Register>: (Display and allow to modify the register contents)
 After executing this command, the name and contents of the specified register are
displayed, as mentioned in Figure 3.2 (b) where the content of AX are modified.
 The user can then modify the register contents, or press the Return key to display the
next register in the order mentioned in Figure 3.2 (b).
 To exit use command Q or continue to press <CR> until all the registers have been
displayed.

RF: (Display and allow to modify the contents of the FG register)


 The RF command displays the current status of each flag.
 The definition of the flag register is as follows.

Page 21 of 54
Dawood University of Engineering and Technology Assembly Language

Page 22 of 54
Dawood University of Engineering and Technology Assembly Language

FLAGS NAMES BIT=0 BIT=1


OF Overflow Flag OV NV
DF Direction Flag DN UP
IF Interrupt Flag EI DI
TF Trap Flag
SF Sign Flag NG PL
ZF Zero Flag ZR NZ
AF Auxiliary Carry Flag AC NA
PF Parity Flag PE PO
CY Carry Flag CY NC

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.

Command T: Trace program execution

Command syntax T [=<Start address>], [<Value>]


 The T command provides single step program execution.
 It executes one instruction at a time and displays the register contents after execution of
each instruction.

Page 23 of 54
Dawood University of Engineering and Technology Assembly Language

 <Start address> is the beginning address for program to be executed.


 If <Start address> is not specified, the program starts at the current address of CS:IP.
 <Value> defines the number of instructions to be executed.
 The default value is 1.
 The P command (Procedure command) is used, instead of T command, for LOOP
instructions to pass through the looping as well as INT instructions.
The following figure shows a program execution with Trace command in which three (3)
numbers of instructions are executed.

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)

Memory Models Code segment size Data segment size

Small 64kb 64kb

Medium Can exceed 64kb 64kb

Compact 64kb Can exceed 64kb

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

C:\> LINK myfile.obj; < return >


If no errors occur, this will generate an executable file ready to be run stand-alone.

;Program to input a character and also prints it.

.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

Write a program which prints your name.


Lab Task-2

Write a program which prints your two-digit roll number.

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.

Examples of Legal Names


COUNTER1
@character
SUM_OF_DIGITS
$1000
DONE?
.Test

Examples of illegal Names


Two Words Contains a blank
2abc Begins with a digit
A45.28 . not first character
YOU$ME Contains an illegal character

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,

NOP NO operands, Does nothing


INC AX one operand, add 1 to the contents of AX
ADD WORD1, 2 two operand, add 2 to the contents of memory word Word1

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:

MOV CX, 0 ; move 0 to CX

Instead, use comments to put the instruction into the context of the program.

MOV CX, 0 ; CX counts terms, initially 0

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

1,234 illegal-contains a non-digit character


1B4DH hex
1B4D illegal hex number-does not end with “H”
FFFFH illegal hex number-does not begin with a decimal digit

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.

Pseudo-ops Stands for


DB define byte
DW define word
DD define double word (two consecutive words)
DQ define quad word (four consecutive words)
DT define ten bytes (ten consecutive bytes)

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

The pseudo-op DW means “Define Word”.


WRD DW -2

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,

B_ARRAY DB 10H, 20H, 30H

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:

Symbol Address Contents


B_ARRAY 200h 10h
B_ARRAY+l 201h 20h
B_ARRAY+2 202h 30h

In the same way, an array of words may be defined. For example,


W_ARRAY DW 1000, 40, 29887, 329

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:

Symbol Address Contents


W_ARRAY 0300h l000d
W_ARRAY+2 0302h 40d
W_ARRAY+4 0304h 29887d
W_ARRAY+6 0306h 329d

High and Low Bytes of a Word


Sometimes we need to refer to the high and low bytes of a word variable. Suppose we define

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.

It is possible to combine characters and numbers in one definition. For example,

MSG DB 'HELLO', 0AH, 0DH, ‘$’

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

name EQU constant

For example, the statement,

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

MOV DL, 0AH

and

MOV DL, LF

into same machine instruction.


The symbol on the right of an EQU can also be a string. For example

PROMPT EQU ‘TYPE YOUR NAME’

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

Note: no memory is allocated for EQU names.

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.

A few Basic Instructions of Assembly Language


There are over a hundred instructions in the instruction set for the 8086 CPU; there are also
instructions designed especially for the more advanced processors. In this lab six of the most
useful instructions for transferring data and doing arithmetic are discussed. The instructions can
be used with either byte or word operands.

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.

The MOV and XCHG Instruction


The MOV Instruction is used to transfer data between registers, between a register and a memory
location, or to move a number directly into a register or memory location. The syntax is

MOV destination, source

Here are some examples:

MOV AX, WQRD1

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

AX gets what was previously in BX; BX is unchanged.

MOV AH, ‘A’

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

XCHG destination, source

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

XCHG AX, WORDl

which swaps the contents of AX and memory location WORDl.

Restrictions on MOV and XCHG


For technical reasons, there are a few restrictions on the use of MOV and XCHG. Tables below
show the allowable combinations.

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,

ILLEGAL: MOV WORD1, WORD2

but we can get around this restriction by using a register:

MOV AX, WORD2


MOV WORD1, AX

ADD, SUB, INC, and DEC


The ADD and SUB instructions are used to add or subtract the contents of two registers, a
register and a memory location, or to add (subtract) a number to (from) a register or memory
location.

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,

ILLEGAL: ADD BYTE1, BYTE2


A solution is to move BYTE2 to a register before adding, thus

MOV AL, BYTE2 ; AX gets BYTE2

ADD BYTEl, AL ;add it to BYTEl

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

adds 1 to the content or WORDl

DEC BYTEl

subtracts 1 from variable 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

The destination may be a register or memory location. For example,


Page 38 of 54
Dawood University of Engineering and Technology Assembly Language

NEG BX

negates the content of BX.

Type Agreement of Operands

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

MOV AX, Byte1 ; illegal


is not allowed. However, the assembler will accept both of the following instructions.

MOV AH, ‘A’

and

MOV AX, ‘A’

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.

Translation of High-Level language to Assembly Language


High-level language instruction may be translated into Assembly language by using basic
instruction set of Assembly language. In the following a few examples only MOV, ADD, SUB,
INC, DEC, and NEG are used for ease.

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.

String Display Technique


INT 21h, Function 9 can be used to display a string in assembly language.

INT 21h, Function 9:


Display a string
Input: DX= offset address of string.
The string must end with a ‘$’ character

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

; Display first message


LEA DX, MSG1
MOV AH, 9
INT 21H

MOV DX,10
MOV AH, 2
INT 21H

MOV DX,13
MOV AH, 2
INT 21H

; Display second message


LEA DX, MSG2
MOV AH, 9
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.

How Instructions affect the Flags Register


In general, each time the processor executes an instruction, the flags are altered to reflect the
result. However, some instructions do not affect any of the flags, affect only some of them, or
may leave them undefined. Because the jump instructions depend on the flag settings, it is
important to know what each instruction does to the flags. The seven basic instructions MOV,
XCHG, ADD, SUB, INC, DEC, and NEG affect the flags as follows:

Instruction Affects Flags


MOV/XCHG None
ADD, SUB All
INC, DEC All accept CF
All (CF = 1 unless result is 0. OF = 1 if word operand is 8000h or byte
NEG
operand is 80h)

Example-1
ADD AX, BX, where AX contains FFFH, BX contains FFFFh.
Solution:
FFFFh
+ FFFFh
1 FFFEh

The result stored in AX is FFFEh = 1111 111 l 1111 1110.


SF = 1 because the msb is 1.
PF = 0 because there are 7 (odd number) of 1 bits in the low byte of the result.
ZF = 0 because the result is nonzero.
CF = 1 because there is a carry out of the msb on addition.
OF = 0 because the sign of the stored result is the same as that of the number being added (as a
binary addition, there is a carry into the msb and also a carry out).

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

The result stored in AL is 00h= 0000 0000.


SF =0 because the msb is 0.
PF =1 because all the bits in the result are 0.
ZF = 1 because the result is 0.
CF = 1 because there is a carry out of the msb on addition.
OF = 1 because the numbers being added are both negative, but the result is 0 (as a binary
addition, there is no carry into the msb but there is a carry out).
Example-3
SUB AX, BX, where AX contains 8000h and BX contains 0001h.
Solution:
8000h
- 0001h
1 7FFFh

The result stored in AX is 7FFFh= 0111 1111 1111 1111.


SF = 0 because the msb is 0.
PF = 1 because there are 8 (even number) one bits in the low byte of the result.
ZF = 0 because the result is nonzero.
CF = 0 because a smaller unsigned number is being subtracted from a larger one.
Now for OF. In a signed sense, we are subtracting a positive number from a negative one, which
is like adding two negatives. Because the result is positive (the wrong sign), OF = 1.

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

The result stored in AL is 00h.


SF = 0, PF = 1, ZF = 1. Even though there is a carry out, CF is unaffected by INC. This means
that if CF =0 before the execution of the instruction, CF will still be 0 afterward.
OF = 0 because numbers of unlike sign are being. added (there is a carry into the msb and also ;i
carry out)
Example-5
MOV AX, 5.
Solution:
The result stored in AX is -5 = FFFB h= 1111 11111 1111 10111h.
None of the flags are affected by MOV.
Example-6
NEG AX, where AX contains 8000h.
Solution:
8000h= 1000 0000 0000 0000
Ones Complement 0111 1111 1111 1111
+1
1000 0000 0000 0000 = 8000h

The result stored in AX is 8000h.


SF = l, PF = l, ZF = 0.
CF =1, because for NEG CF is always 1 unless the result is 0.
OF = 1, because the result is 8000h; when a number is negated, we would expect a sign change,
but because 8000h is its own two's complement, there is no sign change.

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.

Procedure declaration: Before using a procedure, it needs to be declared or defined. This


typically involves specifying the procedure's name, any parameters it accepts, and possibly its
return value.
Procedure call: To invoke a procedure, a call instruction is used. The call instruction transfers
control from the current location to the beginning of the called procedure. The instruction may
also pass any required arguments to the procedure.
Procedure prologue: When a procedure is called, it often needs to perform some initialization
tasks, such as saving the state of registers or allocating space for local variables. This code is
typically placed at the beginning of the procedure and is called the prologue.
Procedure body: The body of the procedure contains the actual code that performs the desired
task. This code can include any valid assembly language instructions, such as arithmetic
operations, memory accesses, conditional branching, etc.
Procedure epilogue: Once the procedure has completed its task, it may need to clean up any
resources it used, restore the saved register values, and return control back to the calling location.
This code is typically placed at the end of the procedure and is called the epilogue.
Return from procedure: To return from a procedure and resume execution at the calling
location, a return instruction is used. This instruction may also pass back a return value, if
applicable.
Parameter passing: Procedures often accept input parameters that are passed to them by the
calling code. The specific mechanism for passing parameters (e.g., through registers or on the
stack) depends on the architecture and calling convention being used.
Local variables: Procedures may have their own local variables that are used for temporary
storage during their execution. These variables are typically allocated on the stack or in registers.
Nested procedures: Procedures can be nested within each other, allowing for hierarchical code
organization. This means that a procedure can call another procedure, which can further call
additional procedures, creating a call stack.

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

; program to understand the use of Procedures in 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

mov dx, offset string1


mov ah,9
int 21h

call myenterproc

mov dx, offset string2


mov ah,9
int 21h

call myenterproc

mov dx, offset string3


mov ah,9
int 21h

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

; program to understand the use of Macros in 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

mov ah, 4ch


int 21h

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

You might also like