You are on page 1of 63

Computer Architecture and Assembly Language

Lab manual.

INTRODUCTION
A course on Computer Architecture and Assembly Language is meant to provide insight into
working of computer systems. There are several reasons for its inclusion in various disciplines. The
obvious objective of studying computer architecture is to learn how to design one. Writing machine
dependent software such as compilers, operating systems, and device drivers, need knowledge of
possible structural and functional organization of computer architectures. A software engineer or
scientific programmer interested in high performance studies computer architecture to learn how to
design programs to gain maximum performance from a given architecture. Working with systems
that involve a variety of interfaces, equipment and communication facilities require knowledge of
computer organization. Last, but not least, understanding cost/performance trade-offs in a computer
system which result from design and implementation decisions can be achieved through
understanding of computer architecture.

This laboratory workbook is developed to strengthen topics covered in theory classes. There are two
major parts in this workbook: Part – I contains assembly language programming for x86 processors,
used in desktops and laptops. This will enable the students to grasp low-level programming details
of commonly used machines. Visual Studio has been used as programming environment. Part – II
explores, in depth, assembly language of MIPS processor, an essential component of many
embedded systems. SPIM, a freely available MIPS simulator has been used to this end. Thus,
students get an opportunity of learning assembly language of both CISC (x86) and RISC (MIPS)
machines. Two labs are devoted to description of cache and virtual memory operations.

The lab sessions are intended to be thought provoking so that students can think out-of-the- box and
have their own way of solving a problem rather than following the traditional footsteps. This is what
makes the most exciting area of Computer Architecture & Assembly language!
Lab Session 01
1. OBJECT
Exploring Instruction Set Architecture (ISA) of x86 Machines.

2. THEORY
2.1 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
 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
 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.
2.2 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.
2.2.1 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:
2.2.1.1 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.
31 30 29 --- 3 2 1 0

Apart from accessing the register as a whole, these registers can be accessed in pieces as illustrated in Fig 1-1.
8 8

AH AL 8 bits + 8 bits

AX 16 bits

EAX 32 bits

Fig. 1-1
It should be carefully noted that high-order 16 bits of these registers cannot be referenced independently.
2.2.1.2 Index Registers
ESI(Extended Source Index) and EDI(Extended Destination Index) registers are respectively used as source and
destination addresses in string operations. They can also be used to implement array indices.
2.2.1.3 Pointer Registers
The EIP (Extended Instruction Pointer) register contains the offset in the current code segment for the next
instruction to be executed. (Segments will be explained shortly).
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.
2.2.1.4 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). Table 1-1 gives some of the
commonly used control and status flags.
Table 1-1
Bit Name of Flag Type Description
11 OF (Overflow Flag) Status Indicates overflow resulting from some arithmetic operation
Determines left or right direction for moving or comparing string
10 DF (Direction Flag) Control
(character) data.
Indicates that all external interrupts, such as keyboard entry, are to be
9 IF (Interrupt Flag) Control
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 = positive and 1
7 SF (Sign Flag) Status
= negative).
Indicates the result of an arithmetic or comparison operation (0 = nonzero
6 ZF (Zero Flag) Status
and 1 = zero result)
4 AF (Auxiliary Flag) Status Contains a carry out of bit 3 on 8–bit data, for specialized arithmetic.
2 Parity Flag (PF) Status Indicates even or odd parity of a low-order (rightmost) 8-bits of data
Contains carry from a high-order (leftmost) bit following an arithmetic
0 CF (Carry Flag) Status operation; also, contains the contents of the last bit of a shift or rotate
operation.

2.2.2 Memory Addressing


A 32-bit processor uses 32-bit addresses and thus can access 2 32B = 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.
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 0xA2B1C3D4 in main memory. The two byte orderings are illustrated in Fig. 1-2.
Addresses Contents Addresses Contents
2032 A2 2032 D4
2033 B1 2033 C3
2034 C3 2034 B1
2035 D4 2035 A2

BIG Endian LITTLE Endian


Fig. 1-2
2.2.3 Memory Models
IA-32 can use one of the three basic memory models:
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
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.
2.2.4 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 Fig. 1-3. When using the segmented memory model, each segment is loaded with a different memory
address (Fig. 1-4).
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 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.
Fig. 1-3

Fig. 1-4

EXERCISES
a) 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.
Addresses Contents Addresses Contents
1996 1996
1997 1997
1998 1998
1998 1998

LITTLE Endian BIG Endian

b) What is the significance of learning ISA of a processor?


LAB # 06 IQRA University

Ans: The
Instruction Set
Architecture
(ISA) is the part
of
the processor th
at is visible to
the

programmer or
compiler writer.
The ISA serves
as the boundary
between
software and

hardware. .
The ISA of a
processor can
be described
using 5
catagories:
Operand
Storage in
the CPU.

c) Show
the
ECX
registe
r and
the
size
and
positio
n of
the
CH,
CL,
and
CX
within
it.

Spring 2020 Page 7


LAB # 06 IQRA University

The microprocessor performs three main tasks for the


Lab computer system
Session  Data transfer between itself and the memory or IO
02  Simple arithmetic and logic operations.
1. OBJ  Program flow via simple decision.
ECT
More about
Micro 3.BUS:
Processor
Computer Bus is an electrical pathway through
2.Micro which the processor communicates with the internal
Processor:
or external devices attached to the computer.
The
The BUS selects:
microprocess
i. An I/O or Memory device.
or,
sometimes ii. Transfer data between IO or Memory and the

referred as microprocessor.

C.P.U, is the iii. Controls the IO or Memory system.

controlling
element in a
computer There are three main types of busses, listed as

system. The follows:

microprocess 1. Data Bus: It transfers the data between different

or controls components of computer system. Data bus of

memory & microprocessor 8086 consist of 16 parallel lines,

IO each line can carry only one bit at a time whereas

operations microprocessor 8088 has 8-bit data bus i.e. transfers

through a only 8-bit data at a time.

series of 2. Address Bus: It carries address information of


wires called different components or memory locations of the
“BUSES”. computer, both the 8088 and 8086 have 20 bits
address bus.

Spring 2020 Page 8


LAB # 06 IQRA University

3. Control iii. Byte Enable (E): A group of lines that indicates


Bus: It sends the size of the data.
control
signals to Registers:
different Temporary memory inside the processor called
unites of registers. Mainly there are two types of registers in
computer, 8088/8086 architecture
three  General Purpose Registers
common
 Special Purpose Registers
control
signals are:
i. Read (R):
A signal line
that active
when the General Purpose Registers:
device is 1. AX (Accumulator): For 16 bits operations, AX

being read registers stores operands for arithmetic operations

by the CPU. 2. BX (Base Registers): Used to hold starting


ii. Write location of a memory region within data segment.
(W): A 3. CX (Count Register): It defined as a counter,
signal line primarily used in loop instructions.
that active
4. DX (Data Registers): It is used to hold the part of
only when
result from multiplication or a part of dividend
the device is
before division.
being written
5. BP (Base Pointer): Points to a memory location
by CPU.
for memory data transfer.

6. DI (Data Index): It addresses string destination


data for the string instructions.

Spring 2020 Page 9


LAB # 06 IQRA University

7. SI
(Source
Index): It
addresses
source string
data for the
string
instructions.

Spring 2020 Page 10


LAB # 06 IQRA University

Spring 2020 Page 11


LAB # 06 IQRA University

Spring 2020 Page 12


LAB # 06 IQRA University

Spring 2020 Page 13


LAB # 06 IQRA University

Spring 2020 Page 14


LAB # 06 IQRA University

Spring 2020 Page 15


LAB # 06 IQRA University

Special Conditional Flags: It represents the result of last


Purpose arithmetic and logical instructions.
Registers: a) Carry Flag (CF): It has value 1if the arithmetic
1. IP operation produces a carry in MSB position, 0
(Instruction
otherwise.
Pointer): It
holds the 16 b) Auxiliary Flag (AF): If an operation performed in
bits address ALU generates a carry/borrow from lower nibble
of the next (D0-D3) to upper nibble (D4-D7), the AF is set to1(i.e.
code byte carry given by D3 bit to D4), 0 otherwise.
within the c) Parity Flag (PF): It indicates the parity of result i.e.
code if lower order 8 bits of the result contains eve
segment. number of 1’s the parity flag is set to 1.0 otherwise.
2. SP (Stack d) Zero Flag (ZF): If the result of arithmetic or
Pointer): It logical operations is zero it is set to be 1, 0 otherwise.
addresses an e) Sign Flag (SF): The sign of number is indicated by
area of MSB bit. If the result of operation is negative (If
memory MSB is 1) then sign flag is set be 1 ,0 otherwise.
called stack.
f) Overflow Flag (OF): It occurs when signed
3. FLAGS: It numbers are added or subtracted. An OF is set to be
indicates the 1 if the result has exceeded capacity of machine.
condition of
microprocess Control Flags: These flags are internally set or
or and reset to control certain operations of the processor
controls its with specific instructions put in the program from the
operations. user.
They are
modified a) Trap Flag (TF): It is used for single step control. It
automaticall allows user to execute one instruction of a program at
y by CPU a time for debugging. When TF is set to 1 , the
after program can be run in single step mode.
mathematica
b) Interrupt Flag (IF): It is an interrupt enable/disable
l operations,
flag i.e. used to allow/prohibit the interruption of a
it has 9 flags
program.
and divided
into two c) Direction Flag (DF): It selects either the increment
categories: or decrement mode for SI or DI registers for strings
instructions. It is used in string operations .it is set to

Conditional 1 if string bytes are access from the higher memory
Flags address to lower memory address, 0 otherwise.
 Control
Flags

Spring 2020 Page 16


LAB # 06 IQRA University

Types of bus:
. system bus
. data bus
.control bus
. host bus

Q2. What effect on Conditional Flags will


happens after the addition of 10110001 and
10101011

Q3. What effect on Conditional Flags will happens


Exercise: after the addition of 11110011 and 11101111

Q1. What
do
understand Q4. What effect on Conditional Flags will happens
by the term after the addition of 01111100 and 11111001
“BUS”,
name the
types of bus
in a
computer
system.

ANS: A bus Lab Session 03


is a 3. OBJECT
communicatio
n system that Exploring Assembly Language Programming
transfers data
between
components 3. Assembly Language:
inside a 3.1 Definition
computer, or
between Assembly language is a low-level
computers. programming language for computers,
This microprocessors, microcontrollers, and other
expression programmable devices in which each statement
covers all
related
corresponds to a single machine language instruction.
hardware An assembly language is specific to a certain computer
components architecture, in contrast to most high-level
and software, programming languages, which generally are portable
including to multiple systems.
communicatio
n protocols. Assembly language programs are converted into
executable machine code by a utility program referred

Spring 2020 Page 17


LAB # 06 IQRA University

to as an coded instructions which are easier to


assembler, remember
the  Programming is simplified as a programmer
conversion
does not need to know the exact storage
process being
referred to as location of data and instructions.
assembly or  It compiles directly into machine code and thus
assembling you have very direct control over memory.
the program.
3.3 Disadvantages of Assembly Language:
3.2  Assembler languages are unique to specific
Advantages types of computers.
of Assembly  Programs are not portable to other computers.
Language:  assembly language is specific to a particular
 Asse
machine architecture. assembly languages are
mbly
designed for specific make and model of a
langu
microprocessor.
age is
 It takes lot of time to code or write the program,
more
as it is more complex in nature.
huma
 A program written in assembly language takes
n-
more execution time compared to machine
reada
language.
ble
than 3.4 Introduction to Debug:
machi MS-DOS Debug runs at a 16-bit process level
ne and therefore it is limited to 16-bit computer programs
Free DOS Debug has a "DEBUGX" version supporting
langu
32-bit DPMI programs as well. Well, nowadays,
age. processors are 64 bit so a separate 80x86 zip file and a
 It can platform name DOSBOX is to be downloaded and
directl install in computer to make debug.exe workable
y perfectly!!
comm Then install DOSBOX in following location
unicat C:\Program Files (x86)\DOSBox-0.74
Then start DOSBOX and
e with
Change the Z (default drive) to C drive by
hardw typing
are. mount c: c:\SUBFOLDER
 Uses LIKE AS mount c:
symb c:\Debug64bit\DOSBox\debug
olic The result that will be shown that C drive has
been mounted
then enter c:
It will appear C:\

Spring 2020 Page 18


LAB # 06 IQRA University

Type  PC Program Counter


debug and  Points the next instruction to be
write “a” to
executed.
start
assembly  Shows the address of next instruction.
language
3.5 Command Parameters
code
Debug's command prompt is a hyphen (–). Commands
Type may be typed in either uppercase or lowercase letters,
extra enter in any column. A command may be followed by one or
when done more parameters. A comma or space may be used to
typing separate any two parameters. The standard command
assembly parameters are explained here.
language  <Address> consists of two portions. Segment
code and type address: Offset address. Segment registers, CS, DS, ES
“r”, “t”, “q” and SS are often used to hold a segment address.
for checking Segment address can be omitted, but the offset value
register, must be indicated.
trapping or
quit.  <Number> a 4 digit hexadecimal number. 
<Byte> a 2 digit hexadecimal number.  <String> a
set of characters included in single or double quotation
marks

3.6 DEBUG COMMANDS


The system commands can be divided into the
following groups according to their functions.
 Memory Management Commands.  Assembler
Commands.  Program Execution

Commands.
3.6.1. Memory Management Commands
 IP  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
Instru value.  M Move the contents of memory.
ction  Command C: Compare the contents of two blocks of
Pointe memory
r Command syntax C<Range>,<Address>
For example, the bytes between DS:0100 and DS:0105
are compared to the bytes at DS:0200:
C 100 105 200
The following is displayed by Debug:

Spring 2020 Page 19


LAB # 06 IQRA University

A====== Assemble a program.


U ======Disassemble a program
 Command A: Assemble a program into machine
language.
Command syntax A [<Address>]
 Command
D: Display
the contents
of memory
Command
syntax
D<Range>
It will display
the contents
of a specified
memory
location.
<Range> tells
the D  Command U: Disassemble a program
command Command syntax U [<Range>]
what range of It is used to disassemble assembly language
memory to instructions into machine code. <Range> is used to
display. If select a block of memory to disassemble. If <Range>
range is not is not specified, then the system will start immediately
specified, after the final address of the last U command.
then the
default
starting
address is set
to the
location
following the
last address
used by a
previous D

3.6.2
Assembler
Commands

Spring 2020 Page 20


LAB # 06 IQRA University

Write different code and observe


changes in flags

Mov ax, 0045


Add ax 45

For each add instruction in this exercise, assume


that EAX contains the given contents before the
instruction is executed. Give the contents of EAX 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 eax and stores the result back in eax)

Contents of EAX Contents of EAX


Instruction
(Before) (After)
0045 add ax, 45
FF45 add ax, 45
0045 add ax, -45
FF45 add ax, -45
FFFF add ax, 1

Q2. Write down the code to display the contents of 300


to 400 memory locations in DS.

Exer
cise

Spring 2020 Page 21


LAB # 06 IQRA University

the step by step changes in AX,BX,CX,DX and IP


registers
MOV ax,1234 MOV
bx,1232 add ax,bx NOP

Lab Session 4
1. OBJECT

Exploring Assembly Language Syntax: Print a word


Disk operating system (DOS) routines
INT 21 H is used to invoke a large number of DOS
function. The type of called function is specified by
pulling a number in AH register for text.
Int 20h invoke Interrupt for graphics

For example
Q3 Write
AH=1 input with echo
down the
following AH=8 single-key input without echo
code @ offset
address 100 AH=2 single-character output
of Code
Single-Key Input
segment,
Write down Input: AH=1
the command
to display Output: AL= ASCII code if character key is pressed,
initial values otherwise 0.
of all
registers, To input character with echo:
execute by MOV AH,1
tracing
Command, INT 21H read character will be in AL regis
write down
To input a character without echo:
MOV AH, 8
INT 21H read character will be in AL regis
To display a character

Spring 2020 Page 22


LAB # 06 IQRA University

MOV AH, 2
MOV DL, ‘?’
INT 21H

Write two alphabet in next line:


dosseg
.model small
.stack 100h
.data
.code
main proc
mov dl, 4bh
mov AH, 2
int 21H
mov Dl, 40h
mov Ah, 2
int 21H read a character and display
mov Dl,4ch it
mov Ah,2 ;terminate program normally
int 21h
mov ah, 4ch
int 2IH

main endp
end main

Exercise:
ASCII Q1 Write down the code to display your name
character through ASCII codes.
charts
Q2 Write down the code to generate following
output through alphabets
C
A
A
L

Q3 Prepare the code to print I q r a.


through ASCII codes.

Q4. Prepare the code to print through ASCII


codes.
I
Q
R
Spring 2020 Page 23
LAB # 06 IQRA University

A We have already seen immediate (operand is part of


instruction) and register direct (operand is in specified
Q5 Write register) addressing modes in first program. Let's discuss
down the direct and register indirect addressing modes.
code to
Print “Iqra
University” In direct addressing mode, operand's address is part of
through instruction. For example, the instruction mov sum, eax
alphabets from program uses register-direct mode for eax and direct
addressing mode for sum. In assembly language, any
memory reference coded as just a name will be direct.

In register-indirect addressing mode, the specified register


(surrounded by square brackets [ ]) contains operand's
address. For example, the instruction add eax, [edx]adds an
operand pointed to by edx to the contents of eax and puts
the result in eax. However, when size of memory operand is
ambiguous, the PTR operator must be used to give its size to
assembler. For example, mov [ebx], 0 will generate an error
because it cannot be ascertained whether the destination is a
byte, word, double-word, or quad-word. If it is a byte, you
should use the instruction as mov BYTE PTR [ebx], 0. This
is valid for WORD, DWORD and QWORD directives as
Lab well.
Session
5 In an instruction like add eax, [edx], it is not necessary to
use DWORD PTR [edx] because the assembler assumes
Initializing that the source will be double-word as the destination eax is
data and double-word.
more
Arithmetic
Following is the program to add two numbers and save the
operations result in a variable named sum instead of a variable sum:
1.
THEORY dosseg

.model small

1.1 .stack 100h


Arithmetic .data
Addressing
.code
formats
main proc

mov eax,5d

Spring 2020 Page 24


LAB # 06 IQRA University

add In the first program only, there is a variable for storing result
eax,6d but in next program all the data is initialized by some
mov variable and also the result is stored in a variable sum.
ah, 4ch

int 2IH
Arithmetic operations also involved increment and
main endp decrement operation that are used to increase 1 or reduce 1
end main
to any value

For example
Now observe
another mov ax,1000h
program and
inc ax ; 1001h
observe the
output dec ax ; 1000h

Write a neg eax ;-100h


program that
perform Another command is used in assembly to take additive
subtraction to inverse of the value that is change the sign of the value that
two values. is

Write two alphabet in next line:


dosseg dosseg
.model flat .model small
.stack 100h
.stack 100h
.data
.data .code
.code main proc
mov ah, 1 ; every thing input is saved into “al” register
main proc
int 21h
mov mov dl,value; value would be output
mov AH, 2
eax,00FFh
int 21H

main endp
sub
ebx,eax
end main

mov
ah, 4ch

int 2IH Exercise


main endp Write a program to implement following algebraic
end main expressions where X, Y,
X=12D, Y= 14D

Spring 2020 Page 25


LAB # 06 IQRA University

i) Evalu iv) (x+y+z)


at Home Task
e
X Write a program to implement following algebraic
+ expressions where X, Y,
Y X=40D, Y= 14D , Z=56D
ii) X-Y
an i) Evaluate ((X+Y)-Z)+X and print output of
d its character equivalent
pr ii)
int
ou
tp
ut
of
its
ch
ar
ac
te
r
eq
ui
va
le
nt

Write a
program to
implement
following
algebraic
expressions
where X, Y,
Z should be
entered by
user As
X=4, Y=1,
Z=3 and
result should
be shown all
intermediate
output where
applied.
iii) (X-
Y)
-z

Spring 2020 Page 26


LAB # 06 IQRA University

Objective

LAB#6
To understand and familiarize with the initializing of
variables along with the revising addition of two numbers
with 8086 emulator environment.

//Review
.model small // memory size
.stack 100h // stack size
.data // data segment directive
(It directs the data i.e variables are
initialize here)
.code // instructions are written
here
main proc

mov dl, ‘A’ // data is inserted in


data register
mov ah, 2 // single character
output
int 21h //
Spring 2020 Page 27
LAB # 06 IQRA University

help the assembler to correctly understand


mov the assembly language programs to prepare
ah,4ch the codes.
// exit
comman
d
int 21h
//
generate
interrupt
main
endp
end
main

Assem
bler
directiv
es:
An
assembler
is a DB: Define Byte The DB directive is used
program to reserve byte of memory locations in the
used to available memory. While preparing the EXE
convert an file, this directive directs the assembler to
assembly allocate the specified number of memory
language bytes to the said data
program type may be a constant, variable, string, etc.
into the Example
equivalent
num1 db '1'
machine
code
modules
which may
further be DW: Define Word The DW directive serves
converted the same purposes as the DB directive, but
to it now makes the assembler reserve the
executable number of memory word (16-bit) instead of
codes. bytes. Some example is given to explain this
Assembler directive.
directives

Spring 2020 Page 28


LAB # 06 IQRA University

Example:- specified variable requiring 10-bytes its


storage and initialise the 10-bytes with the
WORDS specified values. The directive may be used
in case of variable facing heavy numerical
DW calculations, generally processed by
1234H numerical processors.

var1 dw
“Words$”

DQ:
Define
Quadword
These
directors is
used to
direct, the
assembler
to reserve
4 words ASSUME: Assume Logical Segment Name
(8bytes) of The ASSUME directive is used to inform the
memory assemble, the name of the logical segments
for the to be assumed for
specified
different segments used in the program. The
variable
statement ASSUME CS: CODE
and may
initialise it directs the assembler that the machine
with the code is available in a segment named
specified CODE, and hence the CS register to be
values. loaded with the address (segment)
allotted by the operating system for the label
CODE while loading. Similar,
DT: Define
Ten Bytes ASSUME DS: DATA indicates to the
The DT assembler that the data items related to
directive the
directs the
assembler program, are available in a logical segment
to define named DATA, and the DS register
the
Spring 2020 Page 29
LAB # 06 IQRA University

is to be In assembly language programming, the


initialised subroutines are called procedures. The
by the ENDP directive is used to indicate the end of
segment a procedure. A procedure is usually
address assigned a name, i.e. label. To mark the end
value of a particular procedure, the name of the
decided by procedure, i.e. label may appear as a prefix
the with the directive ENDP.
operating
PROCEDURE START
system for
:
the data
segment, : STAR ENDP
while
loading.
ENDS: END of Segment This directive
marks the end of a logical segment. The
END: END logical segments are assigned with the
of Program names using the ASSUME directive. The
The END names appear with the ENDS directive as
directed prefixes to mark the end of those particular
marks the segments.
end of an
DATA SEGMENT
assembly
language :
program. DATA ENDS
When the
assembler ASSUME CS : CODE, DS :
comes DATA
across this CODE SEGMENT
END
directive, it :
ignores the CODE ENDS END
source
lines
available EQU: Equate The directive EQU is used to
later on. assign a label with a value or a symbol. The
use of this directive is just to reduce the
recurrence of the numerical values or
ENDP: constants in a program code. The recurring
END of value is assigned with a label, and that label
Procedure is used in place of that numerical value,
Spring 2020 Page 30
LAB # 06 IQRA University

throughout of a block of statements or to describe an


the algorithm. A comment line contains a
program. semicolon followed by text. A comment can
also be placed at the end of a source line.
Example:-
Comments are included in the assembly
LABEL listing, but are not significant to the
assembler.
EQU

0500H Examples:
ADDITION

EQU

ADD
The first ; this is a comment line
statement * Labels - A label is a symbol followed by
assigns a colon. Labels are required on assembler
the directives that define the value of a symbol
constant (for instance, EQU). For these directives,
500H with labels are assigned the value corresponding
the label to the expression in the operand field. Note
LABEL, the labels MUST begin in column zero; there
while the can't be any white space before a label (but
second note that some directives like ORG noted
statement below do require whitespace in the leading
assigns column).
another
label
ADDITION Examples:
with
mnemonic length:EQU$18; length = $18
ADD.
* * String Constants - A string constant is a
Comments series of printable characters enclosed in
-A single (') or double quote ("). Double quotes
comment are only allowed within strings delimited by
is used to single quotes. Single quotes are only
explain the allowed within strings delimited by double
purpose quotes.
and usage
Spring 2020 Page 31
LAB # 06 IQRA University

example, the statement RESULT PROC


NEAR marks the start of a routine RESULT,
Example: while is to be called by a program located in
the same segment of memory. The FAR
directive is used for the procedures to be
'ABCD',
called by the programs located in a different
"ABCD",
segment of memory. The example statement
'A',
is as follows:
are all
Example:-
valid string
constants RESULT PROC NEAR ROUTINE
PROC FAR

PROC:
Procedure PTR: Pointer The pointer operator is used to
The PROC declare the type of a label, variable or the
directive memory operand. The operator PTR is
marks the prefixed by either BYTE or WORD. If the
start of a prefix is BYTE than the particular label,
named variable or memory operand is treated as an
procedure 8-bit quantity, while if WORD is the prefix,
in the then it is treated as a 16-bit quantity.
statement. Example:-
Also, the
types MOV AL, BYTE PTR [SI] - Moves content of
NEAR or memory location addressed by SI (8-bit) to
FAR AL
specify the INC BYTE PTR [BX] - Increments byte
type of the contents of memory location addressed by
procedure, BX
i.e.
whether it
is to be SEG: Segment of a Label The SEG operator
called by is used to decide the segment address of
the main the label, variable, or procedure and
program substitutes the segment base address in
located place of ‘SEG label”. The example given
within 64K below explains the use of SEG operator.
of physical
memory or Example:-
not. For

Spring 2020 Page 32


LAB # 06 IQRA University

MOV AX, sum db 0


SEG .code
ARRAY;
main proc
This
statement mov ax, @data
moves the mov ds, ax
segment mov dl, var1
address of mov bl, var2
ARRAY in add bl,dl
mov sum, bl
MOV DS, sub sum,48
AX; mov dl,sum
which it is mov ah,2
int 21h
appearing, mov ah,4ch
to register int 21h
AX and main endp
then to end main
DS.

Ad
dition
of two
number
s using
assemb
ler
directiv
es

.model
small
.stack
100h
.data
var1 db
‘1’
var2 db
‘4’
Spring 2020 Page 33
LAB # 06 IQRA University

Spring 2020 Page 34


LAB # 06 IQRA University

Use single step option for line by line execution

Lab Objective
Task#1: Write a program that takes a single character input and displays it in a new line and
observe the contents of registers by using single stepping and record them.
New line hex code is 0A
And single character input ah, 1

Spring 2020 Page 35


LAB # 06 IQRA University

Observe the contents of registers by using single stepping and record them.

st
Registers After 1 After After After After
Instruction 2nd 3rd 4th 5th
AX 7200 07201 F400 7204 7208
BX 00 00 00 00 0068
CX 011A 011A 011A 011A 011A
DX 0000 00 00 00 00

Registers After After After After After


6th 7th 8th 9th 10th
AX 0720a F4200 F4204 07420C 0720E
BX 0068 0068 0068 0068 00
CX 011A 011A 011A 011A 011A
DX 000D 000D 000D 000D 000A

Spring 2020 Page 36


LAB # 06 IQRA University

Spring 2020 Page 37


LAB # 06 IQRA University

Lab Session 7

Objective
To understand and familiarize with writing strings in Assembly language with 8086 emulator environment

Review of Assembly Language Directives:


PROC: Procedure The PROC directive marks the start of a named procedure
in the statement. Also, the types NEAR or FAR specify the type of the
procedure, i.e. whether it is to be called by the main program located within
64K of physical memory or not. For example, the statement RESULT PROC
NEAR marks the start of a routine RESULT, while is to be called by a program
located in the same segment of memory. The FAR directive is used for the
procedures to be called by the programs located in a different segment of
memory. The example statement is as follows:
Example:-
RESULT PROC NEAR
ROUTINE PROC FAR

ENDP:
END of Procedure In assembly language programming, the subroutines are
called procedures. The ENDP directive is used to indicate the end of a
procedure. A procedure is usually assigned a name, i.e. label. To mark the
end of a particular procedure, the name of the procedure, i.e. label may
appear as a prefix with the directive ENDP.
PROCEDURE START
:
: START ENDP
END: END of Program --The END directed marks the end of an assembly
language program. When the assembler comes across this END directive, it
ignores the source lines available later on.

Spring 2020 Page 38


LAB # 06 IQRA University

OFFSET: It’s an assembly language directives. It can be used to indicate that you are
referencing the address of a value and not the value itself.

@data holds the address of data segment address

Spring 2020 Page 39


LAB # 06 IQRA University

WRITING A STRING IN ASSEMBLY

What is String: String is a series of character that is written in “ ” and should


be ended upon a $ symbol that indicates an end of the string.

Where to write string:

String that is terminated by $ symbol is written in the Data segment under the
heading of .Data

What is function designated to Display string in


Assembly
Function 09H (Display string)
Instruction:
mov ah, 09h ; command to display string
int 21h ; invoke interrupt
How to retrieve the String in main Proc:
To retrieve string there are four steps:
Step 1 : Get the starting address of Data segment in to AX
register i.e.
mov ax, @data
Step 2: copy the data segment address in to Data
Segment register (DS)
mov ds, ax

Step 3: GET the starting address of string as Offset


string_name and copy it in any 16 bit register BX, DX
Spring 2020 Page 40
LAB # 06 IQRA University

MOV DX, OFFSET MESSAGE1

Step 4: Call string display function by moving 09h in to AH


register followed by an interrupt call
MOV AH, 09H

Int 21h

;Example 1: Code to Display String

;TITLE LAB 07
.MODEL SMALL
.STACK 100H
.DATA
MESSAGE1 DB "IQRA UNIVERSITY$"
.CODE
MAIN proc

Spring 2020 Page 41


LAB # 06 IQRA University

MOV AX,@DATA ; Get address of DATA segment

MOV DS, AX ; Move address of DATA segment into DS register

MOV DX, OFFSET MESSAGE1 ;Get offset address of MESSAGE1

MOV AH, 09H ;Move display string function into AH register

INT 21H ; Invoke DOS interrupt

MOV AH, 4CH


INT 21H
Main endp
END

Lab Session 7

Objective
To understand and familiarize with writing strings in Assembly language with 8086 emulator
environment

Review of Assembly Language Directives:


PROC: Procedure The PROC directive marks the start of a named procedure
in the statement. Also, the types NEAR or FAR specify the type of the
procedure, i.e. whether it is to be called by the main program located within
64K of physical memory or not. For example, the statement RESULT PROC
NEAR marks the start of a routine RESULT, while is to be called by a program
located in the same segment of memory. The FAR directive is used for the
procedures to be called by the programs located in a different segment of
memory. The example statement is as follows:
Example:-
RESULT PROC NEAR
ROUTINE PROC FAR

ENDP:

Spring 2020 Page 42


LAB # 06 IQRA University

END of Procedure In assembly language programming, the subroutines are


called procedures. The ENDP directive is used to indicate the end of a
procedure. A procedure is usually assigned a name, i.e. label. To mark the
end of a particular procedure, the name of the procedure, i.e. label may
appear as a prefix with the directive ENDP.
PROCEDURE START
:
: START ENDP
END: END of Program --The END directed marks the end of an assembly
language program. When the assembler comes across this END directive, it
ignores the source lines available later on.

OFFSET: It’s an assembly language directives. It can be used to indicate that you are
referencing the address of a value and not the value itself.

@data holds the address of data segment address

Spring 2020 Page 43


LAB # 06 IQRA University

WRITING A STRING IN ASSEMBLY

What is String: String is a series of character that is written in “ ” and should


be ended upon a $ symbol that indicates an end of the string.

Where to write string:

String that is terminated by $ symbol is written in the Data segment under the
heading of .Data

What is function designated to Display string in


Assembly
Function 09H (Display string)
Instruction:
mov ah, 09h ; command to display string
int 21h ; invoke interrupt
How to retrieve the String in main Proc:
To retrieve string there are four steps:
Step 1 : Get the starting address of Data segment in to AX
register i.e.
mov ax, @data
Step 2: copy the data segment address in to Data
Segment register (DS)
mov ds, ax

Step 3: GET the starting address of string as Offset


string_name and copy it in any 16 bit register BX, DX
Spring 2020 Page 44
LAB # 06 IQRA University

MOV DX, OFFSET MESSAGE1

Step 4: Call string display function by moving 09h in to AH


register followed by an interrupt call
MOV AH, 09H

Int 21h

;Example 1: Code to Display String

;TITLE LAB 07
.MODEL SMALL
.STACK 100H
.DATA
MESSAGE1 DB "IQRA UNIVERSITY$"
.CODE
MAIN proc

Spring 2020 Page 45


LAB # 06 IQRA University

MOV AX,@DATA ; Get address of DATA segment

MOV DS, AX ; Move address of DATA segment into DS register

MOV DX, OFFSET MESSAGE1 ;Get offset address of MESSAGE1

MOV AH, 09H ;Move display string function into AH register

INT 21H ; Invoke DOS interrupt

MOV AH, 4CH


INT 21H
Main endp
END

;Example 2: Code to Display String


;In inform 0AH is for new line and 0DH is for carriage return
;TITLE LAB 07
.MODEL SMALL
.STACK 100H

Spring 2020 Page 46


LAB # 06 IQRA University

.DATA
inform DB 0AH, 0DH, "I am a Coder 7898$"
.CODE
MAIN proc
MOV AX,@DATA ; Get address of DATA segment
MOV DS, AX ; Move address of DATA segment into DS register
MOV DX, OFFSET inform ;Get offset address of inform
MOV AH, 09H ;Move display string function into AH register
INT 21H ; Invoke DOS interrupt
MOV AH, 4CH
INT 21H
Main endp
END

Spring 2020 Page 47


LAB # 06 IQRA University

Spring 2020 Page 48


LAB # 06 IQRA University

Spring 2020 Page 49


LAB # 06 IQRA University

;Example 2: Code to Display String


;In inform 0AH is for new line and 0DH is for carriage return
;TITLE LAB 07
.MODEL SMALL
.STACK 100H
.DATA
inform DB 0AH, 0DH, "I am a Coder 7898$"
.CODE
MAIN proc
MOV AX,@DATA ; Get address of DATA segment
MOV DS, AX ; Move address of DATA segment into DS register
MOV DX, OFFSET inform ;Get offset address of inform
MOV AH, 09H ;Move display string function into AH register
INT 21H ; Invoke DOS interrupt
Spring 2020 Page 50
LAB # 06 IQRA University

MOV AH, 4CH


INT 21H
Main endp
END

Spring 2020 Page 51


LAB # 06 IQRA University

Spring 2020 Page 52


LAB # 06 IQRA University

Spring 2020 Page 53


LAB # 06 IQRA University

Lab Session 8
Objective
To understand and familiarize with some more addition instructions with
8086 emulator environment
Review of Addition:
ADD (Addition) Instruction
The ADD instruction adds the value of destination to the
source and the result store in the destination register, where
source register remains unchanged.

ADD Instruction format


ADD Destination, Source
; Destination = Destination + Source

Spring 2020 Page 54


LAB # 06 IQRA University

Example1 : ADD two decimal values and than two hexa


decimal values and observe the values of variables from
“vars” window.
.MODEL SMALL
.STACK 100H
.data
var1 db ‘1’
var2 db ‘4’
sum db 0
.code
main proc
mov ax, @data
mov ds, ax

mov dl, var1


mov bl, var2
add bl,dl
mov sum, bl
mov ah,4ch
Spring 2020 Page 55
LAB # 06 IQRA University

int 21h
main endp
END

Some more addition instructions:


INC (Increment) Instruction
The INC (Increment) instruction increases the content of
register by 1.
Increment instruction format:

INC DESTINATION; Destination = Destination + 1

E.g. MOV AH, 0FD H ; AH = FD H


INC AH ; AH = FE H
INC AH ; AH = FF H
INC AH ; AH = 00 H
INC AH ; AH = 01 H
INT interrupt number
DEC (Decrement) instruction
The DEC (decrement) instruction decreases the content of
register by 1.

Decrement instruction format:

DEC DESTINATIION; Destination = Destination - 1

E.g. MOV AH, 02H ; AH = 02 H


DEC AH ; AH = 01 H
DEC AH ; AH = 00 H
Spring 2020 Page 56
LAB # 06 IQRA University

DEC AH ; AH = FF H
DEC AH ; AH = FE H

ADC (Add with carry) Instruction


ADC instruction adds the two numbers with carry. When two
number are added carry may generated this carry store by
making the carry flag 1, if no carry is generated then carry flag
becomes 0.

ADC Instruction format


ADC Destination, Source; Destination = Destination
+ Source + Carry
E.g. 27 92
+ 32 83
--------------------
5A 15
--------------------

Spring 2020 Page 57


LAB # 06 IQRA University

MOV AL, 92H ; AL = 92 H


MOV BL, 83H ; BL = 83 H
ADD AL, BL ; AL = 92 H + 83 H Result is 15 H
and C = 1
MOV CL, AL ; Saving result of lower byte
MOV AL, 27H ; AL = 27 H
MOV BL, 32H ; BL = 32 H
ADC AL, BL ; AL = 27 H + 32 H +C (C = 1)
Result is 5A and C = 0
MOV CH, AL ; Saving result of higher byte

The CX Register contains 5A15 (Result)

Practice Task:
Design a proper assembly program to add
2793+3383 and save the result in DX register.

second27 mov ah, 27 first 93 mov al, 93


33 83 mov bl, 83
Add al,bl

Spring 2020 Page 58


LAB # 06 IQRA University

Mov dl,al
6

111
0 011
0 111
1 0 10

1
1
0

Spring 2020 Page 59


LAB # 06 IQRA University

Spring 2020 Page 60


LAB # 06 IQRA University

Spring 2020 Page 61


LAB # 06 IQRA University

Spring 2020 Page 62


LAB # 06 IQRA University

Spring 2020 Page 63

You might also like