0% found this document useful (0 votes)
6 views29 pages

Asm

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 29

LABORATORY NOTES

Laboratory Notes: Computer Architecture Course

Chapter One: Introduction to the Microprocessor Based


PC
I Objectives of the Chapter
This chapter's objective is to introduce the general architecture of a Microprocessor based computer system.

II Mainframe, Mini, Microprocessor


The first computers were large and expensive computers. They were generally called mainframe computers.
They were all multi-user and run complex operating systems. The advent of Integrated Circuit Chip has enabled
smaller computer systems that were still multi-user systems. This computers are know as mini-computers.
At the end of the 1970s and the beginning of the 1980s, single chip processor based, cheap computers made
their appearance. They were not very powerful and they were intended for a single user. Those computers are
known as micro-processor bases computers or micro-computers.
During the last two decades the micro-computer technology has advanced dramatically and today
microcomputers are very powerful and they also run sophisticated operating systems as well as applications.

III What is a Personal Computer?


A personal computer is a computer that is used by a single user. However, with the advent of networks, this
definition has to be modified since most of the computers that were classified as PCs are used simultaneously
by several users. Very often in Ethiopia, people use the name PC for an 80X86 and Pentium microprocessor
based computer systems.

IV What is an IBM-Compatible Personal Computer?


IBM designed in 1981 an Intel 8086 processor based personal computer. Unlike its previous culture, IBM used
a processor that already exists but also made available all the diagram and the specification of the PC. Several
other companies started to produce personal computers based on those specifications. All those computers can
run the same programs (operating system and applications). This has enabled an amazing development of the
IBM PC compatible Personal Computers and software for those computers.

V Introduction to the Architecture of the IBM Compatible PC


The figure below shows the block diagram of microprocessor based computer systems. It has three blocks
interconnected by buses. A bus is a set of common connections that carry the same information. In general there
are three buses on a microprocessor based computer system: the data bus, the address bus and the control bus.

Memory system Buses MicroProcessor Buses Input/Output


Devices
DRAM, Static RAM, SRAM 8086, 8088, 80286, . . . Printer, Serial communications,
Pentium, Pentium Pro, . . . Floppy disk drive, hard disk
Cache, ROM, EEPROM, ... drive, Mouse, CD-ROM drive,
keyboard, monitor, Scanner, . .

Page - 2 -
Laboratory Notes: Computer Architecture Course

Figure: Block diagram of a microprocessor based computer system


The memory is used to store data and instruction need during the execution of the program. There are several
types of memories (DRAM, SRAM, cache, ROM, EEPROM, etc.)
The microprocessor is the heart of the microprocessor based computer system. It is sometimes referred as CPU.
It controls the memory and I/O through the buses. The microprocessor performs three tasks:
1- data transfer between itself and memory
2- simple arithmetic and logic operation
3- program flow via simple decisions
The power of the microprocessor is that it can execute millions of instructions a second. There are various types
of more or less expensive (compatible or not) microprocessors in the market.
The number of Input/Output devices connected to the microprocessor-based computer is increasing regularly.
Some of the I/O devices connected to the PC are:
- Printer,
- Serial communications,
- Floppy disk drive,
- hard disk drive,
- Mouse,
- CD-ROM drive,
- keyboard,
- monitor,
- Scanner,
- Modem
- Network Interface Cards
Some of those devices are connected through standard serial or parallel communication ports, others require the
addition of specialized cards.

Page - 3 -
Laboratory Notes: Computer Architecture Course

Chapter Two: Components of an IBM compatible PC


I Objective of this chapter
The objective of this chapter is to introduce the students to the various types of specific components of the PC.

II Introduction
A personal computer is a general purpose system designed to accommodate different operating systems. The
link between the hardware and the OS is a set of software routines known as BIOS (Basic Input and Output
System). This program routines are permanently stored in ROM or PROM memory chip. Because of their
permanent nature, BIOS routines are referred to as firmware (Figure).

Application programs

Operating System

BIOS

Hardware

Figure: PC Software/Hardware hierarchy


The hardware of the PC is composed of four essential components: the power supply, the disk drives, the
motherboard and the expansion slots.

III The power supply


The power supply is a self contained sealed unit in a metal container. It possesses a fan that is used to cool the
power supply as well as the other components of the computer.
A PC requires a number of stabilized DC voltages to supply the motherboard and the other devices. These are
typically as follows:

Voltage Color Current Power


+5V Red 20A 100W
-5V White 0.5A 2.5W
+12V Yellow 8A 96W
-12V Blue 0.5A 6W
All voltage has to be regulated to +/- 5%.
The +5V supply the TTL circuitry while the +12V supply is used to drive the disk drive motors.

Page - 4 -
Laboratory Notes: Computer Architecture Course

CMOS and NMOS processors require 3.3 V and 3.45 V circuitry. Additional circuitry should be included in the
motherboard to transform the 5V into 3.3V or 3.45 V.
The power supply has also some protection features to minimize damage that could be caused by a permanent
or intermittent fault. For example, good quality supply shuts the power supply down if the current is exceeds a
safe level. However, it is always important to have a power protection system such as a UPS and a stabilizer
outside the computer in order to protect the computer’s components from damage.
There are two types of connectors from the output of the power supply. The first is a

IV Motherboards and support chips


Different types of motherboards can be identified by the processor, the size and type of system and cache
memories, the type and number of expansion slots, etc. Some motherboards have maths or numerical processor
unit included. Nowadays, the coprocessor is generally incorporated inside the processor. This enables faster
floating point operations.
There are a number of support chips installed on the motherboard and required for the operation of the
computer. Examples are the clock generator, interrupt and DMA (Direct Memory Access) controllers.
The computer system relies on a number of configuration information and software routines which are stored in
TOM and RAMM devices. These are as follows.
1) The BIOS routines are stored in ROM or EPROM.
2) The keyboard controller also known as the keyboard BIOS converts serial codes from keyboard into
ASCII codes.
3) The CMOS setup chip is a small battery-backed RAM that stores the set-up and configuration
information required for the BIOS routine when the computer is initialized following power up or reset.
It also contains time and date information.
4) A timing clock generator chip, e.g. an 82284 and crystals on which the system clock is based
5) Expansion slots provide access to the motherboard via double-sided edge connectors. More than one
type of expansion slots may be included on the motherboard. There are three main types of expansion
slots (ISA, PCI and VESA). There are 8 Bit and 16 Bit ISA expansion slots. The communication speed
is 8.33 MHz. It is extensively used even though it is now being replaced by the PCI that uses 32 or 64
bit and that communicate at 33 MHz. The VESA slot is also 32/64 bit and is mainly used for fast video
graphics.

V Memory
It is very important the computer has enough memory that it can access at a speed comparable to that of the
processor. The PC has a hierarchy of memory devices that serve various purposes.
Two main properties determine the use of particular types of memory: these are the storage capacity (size) and
access time (speed). There are chips of various sizes. In practice a number of chips are usually connected in
parallel to form what is known as memory bank. Access time is the speed at which a location within a memory
chip can be made available to the data bus. Access time is specified in nanoseconds and varies between 20 ns to
200ns.
Memory chip marking indicates its size and speed. A typical configuration is XXXYZZZ-S where XXX is the
manufacturer's identity, Y is the data width in bits, ZZZ is the number of locations in K and S is the speed in 10
ns. For example, TMS841128-7 indicates a chip with 128 K 1 bit locations and an access time of 70 ns.

Page - 5 -
Laboratory Notes: Computer Architecture Course

1 RAM
There are two types of RAM: dynamic (DRAM) and static (SRAM). DRAM stores information in the form of a
charge capacitor which value should be restored (refreshed) at frequent interval (2-4ms) due to leakage. DRAM
has a high storage capacity and low power consumption but they are relative slower (access time of around 60
ns). SRAM use flip-flops. They are very fast (access time 20 ns or less) but they are more expensive.

2 ROMs
ROM devices are considerably slower than RAM (access time of around 200 ns). There are several types of
ROM. Mask ROM is a non-volatile memory that can only be read. PROM fulfils the same basic functions as a
ROM but can also be programmed once by the user. EPROM fulfils the same basic functions as a ROM but can
also be programmed any number of times by erasing the previous content by exposing the memory cells to
ultra-violet light. The EEPROM can be programmed and erased while still in circuitry by application of suitable
electrical signals.
The Flash RAM is an advance on EEPROM. It can be erased and reprogrammed using normal voltages that are
available in the PC. However, flash RAM suffers from a short life span, and has a long access time of 60 to 150
ns.

3 Cache memory
When the speed of the processor is faster than the memory, it will have to wait after until it receives data from
memory. The ideal would be to use SRAM as main memory unit so that the speed of the memory is similar to
the speed of the processor. However, this is costly. Cache memory technique involves the use of a block of a
few KB (8-512 KB) of fast SRAM to store the contents of the most frequently accessed RAM locations. The
processor will first attempt to obtain instructions or data from the fast cache memory. If they are not available it
will turn to the slower DRAM.

4 Memory packaging
earlier computers used discrete memory chips. Nowadays memory chips are mounted in on a PCB in 16 or 32
bit format. The two commonly used versions are the SIMM (single line memory module) and the DIMM (dual-
line memory module).

VI Video Cards
Video cards create video images that the computer wants to send to the monitor. There are six main standards
of video cards. Technological advances have contributed considerably in the improvement of the video
standards. Today the video cards have complex processors that perform complex image processing tasks that
were formerly performed by the main processor of the computer. The following standards are the in use.

1 MDA (Monochrome Display Adapter)


This monochrome standard (adopted in 1981) accepts only one mode: 25 by 80 character mode. The MDA
video cards have very limited Video RAM and can generally store only a single page at a time. This cards are
almost not used today.

Page - 6 -
Laboratory Notes: Computer Architecture Course

2 CGA (Color Graphic Adapter)


CGA also adopted in 1981 enabled graphic displays during the first days of the PC. One interesting thing about
CGA cards is that they CGA cards have a special output for normal television sets. The quality of the image is
lower than that of the MDA because of lower resolution used. In graphic mode the resolution is 320 * 200 for
four colors and 640*200 for 2 colors.

3 HGC (Hercules Graphic cards)


The HGC cards are produced by the Hercules company and are totally compatible with MDA cards. In addition
to the text mode it can handle graphic pages at the resolution of 720*348.

4 EGA (Enhanced Graphic Adapter)


IBM produced in 1985, EGA cards compatible with CGA and MDA cards. The resolution is only 640*350 but
16 colors can be displayed simultaneously among a palette of 256 colors. The video RAM on EGA cards could
be as high as 256 K byte.

5 VGA
VGA standard introduced in 1987 have become highly predominant for the decade that has followed. It is
compatible to EGA, CGA and MDA. The maximum resolution is 640*480 for 2, 4 or 16 colors. It can
display 256 colors for a resolution of 320*200. Since many hardware producers started to produce this card,
the users could buy high standard video cards for a reasonable price. Because of the competition, the producers
started to design new non standard enhanced VGA cards.

6 Super VGA
Electronically Super VGA cards resemble a lot VGA cards; however they are faster. They are also compatible
all the VGA modes and can produce more colors. They can display 256 colors in all the modes, not only in the
320*200 resolution mode.

Page - 7 -
Laboratory Notes: Computer Architecture Course

Chapter Three: Introduction to ELASS


I Assembling an assembly language program
ELASS is an Assembler program for the 8086/8088 processor that run on DOS operating system. To assemble
the assembly language program, enter the following command at the DOS prompt:
A> elass progname
where progname is the name of the source file in which the program to be assembled is stored. Generally the
file name should have the ASM extension even though the extension may be omitted when Elass is called.
If Elass does not find errors it will report ERRORS: NONE and create an object file named progname.obj. If
there is any error, no object file is produced and the error message is displayed.

II Linking an assembly language program


Once a program has been assembled it should be linked using either LINK or ELINK programs. To link using
LINK.EXE enter the following command
A> link progname;
The semicolon following progname directs LINK to apply its standard default and should not be omitted. When
it has finished linking, the linker produces the executable program progname.exe on the default device.
To link using ELINK.EXE enter the following command
A> elink progname;
Under ELINK you can omit the trailing semicolon without any effect. When ELINK finishes executing it will
display a brief index of the program it has just linked that indicates the size of the memory required for each of
the segments (code, data and stack).

III Executing an assembly language program


Once the executable has been obtained, it can be loaded and executed by invoking the name of the executable
file, like any other executable program. To load and execute an executable file just enter the following
command:
A> progname
The loader will load the program from the default device into memory, make some modification in order to
enable the program to run at the specific location where it is loaded and start executing it.
It is very common to encounter execution errors. There are two types of errors: processing errors and system
errors. Processing errors are due to programming errors and they are similar to programming errors made using
high level languages. The same methods used for high level language program is used to diagnostic and correct
those errors.
System errors on the other hand are unique to languages that permit low-level operations. A system error
corrupts or destroys the system on which it is running. It is generally not possible to do such errors when high
level languages are used because the compiler or the interpreter do the necessary supervision and also because
the programmer does not have direct access to the resources of the system. There is no supervision in assembly
language and system errors are very frequent. There are two signs that tell you that you have run into a system

Page - 8 -
Laboratory Notes: Computer Architecture Course

error. If the screen fills with an indescribable mess of random characters or if the keyboard seems to become
inoperative, you have probably a system error. To recover from a system error try to return to DOS, find the
error and assemble, link and execute the program again. Very often, it is not easy to return to the DOS. The
following are some ways that will enable you to return to DOS:
1- Pressing Enter a few times
2- Pressing CTRL-Break
3- Pressing ALT-CTRL-Break
4- Resetting the computer
Of course, it is always better to start with the first option and execute the second only if the first doesn't work
and execute the third if the second doesn't work, etc. Resetting the computer should be the last option.

IV Exercise
1) Find the prog0201.asm program delivered with the elass environment, look at its content in an editor,
assemble it, link it and execute it.

Page - 9 -
Laboratory Notes: Computer Architecture Course

Chapter Four: The Hardware of the 8086/8088 processor


based computer
Every digital computer is designed around the same five basic circuits: a power supply, a clock, a central
memory, a central processing unit and a number of peripheral devices. In this chapter we are going to learn how
each of the clock, the memory and the CPU work.

I The clock
A clock is a device inside the computer that send a steady, regular drumbeat of pulses. First, it sends out a high,
then a low, then a high etc. A clock high followed by a clock low is called a clock cycle.

Clock cycle

Figure: Clock pulse train


It takes a specific number of clock cycles to execute each machine instruction. In 8086/8088, the fastest
instruction requires two cycles while the slowest require more than 200 cycles.
The speed of the clock determine the speed of the execution. The clock speed is generally given by its
frequency. For a clock of 8 MHz, each clock cycle takes 125 nanoseconds.
There is nothing a programmer can do about the clock. Nonetheless it is important to know how the clock
works in order to improve the efficiency of your application.

II Memory
The memory stores bits. Bits are organized into sets of eight bits to form a byte. The eight bits are number from
0 through 7.

Bit number 7 6 5 4 3 2 1 0

Bits' values 0 0 0 0 0 0 0 0

The memory is homogeneous and ordered. Bytes can be accessed sequentially. When a computer is designed to
process two or more bytes as a unit, that unit of memory is known as a words. For the 8086/8088 a word is two
bytes large. However it is also possible to address a single byte. For example, the byte at address 0 of the
memory is composed of the 8 bits at the address 0 while the word at the address 0 is composed of the 16 bits
found at the address 0 and 1.

III The CPU


The CPU is the brain of the computer. It performs all arithmetic operations and also controls the execution
process. When the CPU wants to read a data from memory it puts the address of that data on the address bus

Page - 10 -
Laboratory Notes: Computer Architecture Course

and puts a control signal on the control bus. Memory responds by putting the content at the given address in
memory onto the data bus. However, this takes half a dozen or more cycles.
The 8086 and the 8088 are two processor of Intel. They are identical from the programmer's perspective. The
only technical difference is that the 8088 has 8 lines data bus while the 8086 has 16 line data bus. This makes
the 8086 faster since it can get more data from memory in the same time.

1 CPU Registers
Registers serve two purposes. First, operations on registers are very fast compared to memory. Second, some
operations require the use of a register. The 8086/8088 contains eight general-purpose registers, four segment
registers, an instruction pointer, and a flag register. Each register is 16 bits wide.

AX AL AX

BH BL BX

CH CL CX

DH DL BX General Purpose
Registers

SI
Index Registers
DI

BP
Pointer Registers
SP

CS

SS

DS

ES

IP

AX

Page - 11 -
Laboratory Notes: Computer Architecture Course

Register map of the Intel 8086/8088

The general purpose registers can all be used for arithmetic operations. The first four general purpose registers
(AX, BX, CX and DX) are separable and each of them can be treated as two 8 bit registers. For example, AX
can be separate into AH and AL. The programmer can use them for arithmetic operation.
The other four registers are index registers and pointer registers. They can also be used for arithmetic
operations. They can also be used to address data in memory. The SP is used specifically for the stack.
The other registers are used for purposes that will be indicated later.

2 Memory address space


The 8086/8088 has 1 megabyte of memory address space (MAS) since its address bus is 20 bits wide
(1 MBytes = 220 Bytes). Do not confuse MAS with real memory. Not all locations in the MAS is populated
with real memory chip; some locations are empty, some are use for the RAM, some for the Rom and some for
peripheral devices.
When the address of a location in MAS is referred in terms of its position in MAS, that address ios called an
absolute address. Typically it is a five digit hexadecimal number (Ex: $00001, $FFFF0, $ABEF4).
doesn't have a register that can hold 20 bits address. Therefore, it cannot manipulate directly complete
addresses.
Paragraph
The 8086/8088 divides the MAS into rows of 216 (65536) paragraphs of 16 Bytes size. Each row is called a
paragraph and is identified by a sequential number. The paragraphs do not intersect; for example, paragraph
$0002 starts where paragraph $0001 stops. There are 65,536 paragraphs in the MAS.
Exercise:
At what absolute address does each of the following paragraph starts and end.

Paragraph # Beginning @ End @


$0
$1
$2
$3
$128
$456

Physical segment
A block of segment that begins on a paragraph boundary and extends for 65,536 Bytes ($10000) is called a
physical segment. There are 65,536 physical segments in the MAS. The physical segments overlap.

Page - 12 -
Laboratory Notes: Computer Architecture Course

Every Physical segment has a segment number, a base and a range. The segment number is the number of the
paragraph at which it begins. The base is the absolute address of the first byte in that paragraph. The range
extends from the base to the highest absolute address within it.
Exercise:
Complete the following table.

Segment # Base Range


$0
$1
$2
$3
$128
$456

All physical segments have the same size. They logically wrap around at the top of the address space and
continue at the bottom.
Segment relative address
There is no 20 bit address in an 8086/8088 processor. Therefore there is no register that can hold the whole
address to be transmitted through the 20-lines address bus. In fact, segment relative addressing is used to
accessed the whole 1 MB address bus even though the registers are only 16 bit large.
Segment relative addressing is similar to the technique used by word-processor software to access a big file
using a small screen. With word-processors, since the screen is generally not large enough to show the whole
content of the file only a portion of that file will be shown at one given time. When the user needs to see or
modify a content in that file at a given position, he or she should scroll the file so that the screen shows that
position of the file. With segment relative addresses, at one given time, the programmer specifies the segment
of memory he/she wants to access and only that portion of the memory of size 216 Bytes can be accessed. If the
programmer needs to access a memory location that does not belong to that segment, he/she should first choose
another segment that contains the memory location he/she wishes.
To access a given memory location the programmer should provide a segment relative address composed of a
physical segment number and an offset. The physical segment number should identify a segment of the memory
that contains the specific location you want to access and the offset is the address of the specific location
relative to the beginning of the segment. Note that since the physical segments overlap, any given byte belong
to 4,096 segments. Also note that both the segment number and the offset can be stored in the 16 bit registers
available in the processor.
The 8086/8088 has four segment registers (CS, SS, DS and ES). The first three registers are respectively used
to store the segment number of the Code Segment, the Stack Segment and the Data Segment. ES is an extra
segment register that can be used by the programmer for its own purposes. These segment registers are used
independently from one another.
By convention segment relative addresses are expressed as follows:
<segment_number> : <offset>

Page - 13 -
Laboratory Notes: Computer Architecture Course

generally the segment_number is either a number in hexadecimal or a segment register. The offset can also be
either an hexadecimal number or a general purpose register. The CPU would find the absolute address by
summing the base of the segment and the offset. For example, the address 1234:5678 refers to the byte at the
offset $5678 in the segment $1234. The CPU will calculate the absolute address by adding $5678 (offset) to
$12340 base of the segment. The resultant absolute address would be $179B8.
Exercise:
Complete the following tables.

Absolute address Relative address Absolute address Relative address


0000:0010 $0
0001:0000 $1
0123:4567 $FFFF
1280:00FF $10000
4567:1230 $12800
4568:1220 $45600

Logical segments
A logical segment is a part of a program designed to be loaded into memory beginning on a paragraph
boundary. An 8086/8088 program generally three segments: it always has a code segment that store instructions
of the program, and very often, it has a data segment that stores the data of the program and a stack segment
that is used for the purpose of the CPU. When the program is loaded, the paragraph number of the paragraph
starting from where it is loaded will be stored in the corresponding segment register. For example, if the code
segment is loaded at the paragraph that starts at the absolute address $1230, the segment register CS will be
assigned the value $123.
Physical and logical segments are related but they are not identical. Logical segments, like physical segments,
start at a paragraph boundary, but logical segments can have any length between 1 and 216 bytes unlike physical
segments that have exactly 216 bytes size. Logical segments are defined only in the context of a program, not by
the CPU and there can be any number of segments; there are always 216 physical segments. Logical segments
do not overlap while physical segments do.
The same address specification conventions can be used to reference a location in logical segment as a location
in physical segment.

IV Summary
This chapter has introduced the main components of the 8086/8088 processor. First, it explains how the clock
that drives the processor works. It then introduces how the physical memory is organizes into a sequence of
bytes. The CPU registers are introduced and their functions are briefly described. Finally, the memory address
space's organization has been explained in length by defining the paragraph, the physical segment and the
logical segment. This chapter also explains how segment relative addresses are used to access the whole
memory address space of 1 Megabyte.

Page - 14 -
Laboratory Notes: Computer Architecture Course

Chapter Five: Analysis of an assembly language


program
I Program listing
The ELASS assembler can produce a listing of the program it assembles. This listing is interesting to
understand how the assembler program and the processor work.
If you wish to request a listing you should write the following directive at the top of your assembly program:
LIST parameter {{,parameter}}*
where parameter is SCR, LPT, TXT, TOF or EXPAND. The SCR parameter should be used to display the
listing on the screen and LPT should be used to print the listing.
It is possible to request the end of the listing at a given point of the assembly language program by inserting the
directive NOLIST. Otherwise, the assembler will generate a listing for the whole program.
The annotated listing produced by ELASS consists of four columns. The leftmost column is a line number
assigned for each line of the source program. This number is specially important for error messages since they
make reference to this number. The next column is the offset of the specific instruction or data in its
corresponding segment. The third column display the machine code produced for each assembler instruction
shown in the fourth column.
It is interesting to see how the assembler instruction is translated. In 8086/8088 the opcode is the first byte of
the instruction. The instructions also have one or more operands. It is also possible for the instruction not to
have an operand. Some machine code instructions are not complete. Part of the instructions are filled with
XXXX which means that the information at this position is not available and that it should be updated later on
by the linker or loader. This is because, some decisions such as exact location of the logical segments in the real
memory are not yet made when the program is assembled.
The 8086/8088 stores all word-sized values in memory the Less significant Word first and the most significant
word next. This is called the LSB/MSB order. Thus an hexadecimal value $EF05 will be displayed in the listing
as 05EF since the least significant byte (05) is stored ahead of the most significant byte (EF).

II Structure of the 8086/8088 program


An 8086/8088 program should specify the code segment's instructions, and the data that should be stored in the
data segment. It should also specify the amount of memory space that should be reserved for the stack even
though it is the responsibility of the CPU to use the stack segment and the programmer doesn't use the stack
directly.

1 Assembly language statement


An assembly language program is composed of a sequence of statements having the following basic format:
{identifier} keyword {{parameter}} {;comment}
The assembler is indifferent to case and column, but each statement must appear in the same line.
A keyword defines the nature of that statement. If the statement is an assembly language instruction it defines
the instruction mnemonic.

Page - 15 -
Laboratory Notes: Computer Architecture Course

An identifier is much like a variable name in a higher level language; it can be composed of alphabetical letters,
digits and special characters _, @, ?, ! and $ and the first character shouldn't be a digit.
A parameter extends and refines the meaning that the assembler attributes to the keyword in a statement. The
number of parameters that can be included in a given statement depends on the keyword.
There are three basic type of statements:
assembly language instruction: that specifies a instruction of the program that should be translated
into an machine language instruction
Ex:
ADD AX, BX
; Is an instruction that will add the content of AX and BX and will put the result in AX at
; execution time
data allocation statements that reserve memory area in the data segment for variable and assign their
initial values
Ex:
MESSAGE DB "Have a nice day!$"
; Is a data allocation statement that will reserve 17 bytes in the data segment and copy the value
; "Have a nice day!$" at loading time.
directives: that tell the assembler to work in a certain manner.
Ex:
LIST SCR
; Requests the assembler to display an annotated listing on the screen while assembling

2 Comments
It is equally or even more important to put comments in assembly language programs as in high level programs.
For ELASS, anything that follows semicolon in a given line is considered as a comment and will not be
processed by the assembler.

3 Directives
Assembly language directives are statements that describe the context in which the instructions in a program are
to be assembled into machine language and in which order the data allocation statements are to be processed
into data space. ELASS supports 28 different directives. Below are described some of the most important ones.
HEX directive
The HEX directive specifies the character that is used to indicate that a number is in hexadecimal notation. For
example, in the below program
HEX $
...
ADD AX, 10
ADD BX, $10

The first addition takes the value 10 in base 10 while the second takes it in base 16 (Hexadecimal). If the HEX
directive was not added, the assembler would take $10 as an identifier.

Page - 16 -
Laboratory Notes: Computer Architecture Course

SEGMENT directive
The SEGMENT directive defines the logical segment in which the subsequent instructions and data allocation
statements belong. It also gives a name for the base of that segment. This is very important since the address of
any element in an 8086/8088 program must be represented in a segment relative format. A segment ends where
another segment ends or where the ENDS directive is found.
Example:
ALPHA SEGMENT
.
.
.
ALPHA ENDS
BETA SEGMENT
.
.
.
BETA ENDS
An 8086/8088 program should of course specify a code segment and put all the instructions there. By default,
the linker takes the first segment as the CODE segment. The loader will put the paragraph number of the
paragraph where this segment is loaded into CS. Therefore, the programmer need not assign the value of CS.
When the program starts executing, IP will have the value 0. Thus the first instruction that will be executed will
be the one at the location CS:0000.
Hence, in the above example, ALPHA is the code segment and only assembly language instructions and
directives should be put there. There should be no data allocation statement in that segment.
As indicated earlier the programmer does not directly manipulate the stack. Therefore there is no statement
defined in the stack segment. However, the programmer should define the size of stack segment. For example:
TETA SEGMENT STACK $0400
defines a stack segment named TETA of size $400. The STACK that follows SEGMENT keyword indicates to
the linker that this directive defines the stack statement. The name of the segment is found at the beginning of
the statement and is arbitrary; it can even be STACK. The last value given at the end of the statement indicates
the size that the programmer wished to allocate for the stack. The author of DOS recommends that every
program should define at least $200 bytes of stack area. However, some applications that make heavy use of the
stack might need much more stack area. The loader automatically updates the SS segment register. Thus, there
is no need
While there is a way of specifying that a segment is code segment or a stack segment, there is no means for
specifying that a segment is a data segment. In fact there is no way for telling the linker where the data segment
is. Thus, when the program starts executing, the DS register is not assigned with the appropriate value. The
programmer himself/herself should write code that copies the paragraph number of the data segment into DS.
END Directive
This directive simply indicates that the end of the program is reached.

Page - 17 -
Laboratory Notes: Computer Architecture Course

4 Data allocation statements


Data allocation statements set aside and initialize one or more bytes of memory for use as data space. the
general format for a data allocation statement is:
{varname} data_definition_type {init {{,init}}}
where varname is an assembler identifier, data_definition_type is DB (Define Byte), DW (Define Word), DD
(Define Double Word), DQ (Define Quad Word) or DT (Define Ten Bytes) and init represents the value to
which the allocated memory region should be initialized.

5 Instruction statements
Instruction statements have the form:
{label:} mnemonic {operand {, operand}} {; comment}
An instruction can have one or more operands depending on the type of operation. When a mnemonic takes two
operands, the first is called the destination operand and the second is called the source operand. Below are
given some of the most common instructions:
The MOV instruction
Syntax:
MOV destination, source
MOV copies the content of the source into the destination. The source can be an immediate value (a value that
remains constant once the program is loaded), a register or a memory location. The destination can be a register
or a memory location.
There are some restrictions that apply on MOV and the programmer should see the detailed semantics of MOV
in a reference manual of the Assembly Language, in order to use it appropriately. For example, while it is
possible to copy an immediate value to a general purpose register using the MOV instruction, it is not possible
to copy an immediate value directly to the segment register DS; i.e. MOV AX, 0 is legal but MOV DS, 0 is
illegal.
Note:
There are a number of operators that can be used to request some tasks that can be performed by the assembler
or the linker. They have the following syntax:
OPERATOR OPERAND
This expression can replace an immediate value operand in an instruction. For example, in the following
instruction:
MOV AX, OFFSET MESSAGE
The expression OFFSET MESSAGE calculates the offset of the variable MESSAGE within the data segment
and puts the result as an operand. This is done before the program is loaded to be executed.
The INT instruction
Syntax:
INT interrupt_number

Page - 18 -
Laboratory Notes: Computer Architecture Course

The INT instruction calls software interrupts. These are a number of subroutines defined for the 8086/8088.
These subroutines are used by the operating system and other assembly level applications.
Some of the interrupts are used very frequently while others are rarely used. Interrupt number $21 is
particularly important since it has more than 100 functions supplied by DOS for I/O and other functions.
Interupt $21 should be used as follows:
MOV AH, function_number ; This puts the function number in AH
INT $21 ; This calls the specific function of the Interrupt
For example, to call function $09 of interrupt $21, we should write:
MOV AH, $09 ; This puts the function number ($9) in AH
INT $21 ; This calls the function $09 of Interrupt $21
In addition to the above, whenever a function of an interrupt is called, the parameters should be put in the
appropriate registers prior to calling the interrupt (INT $21 in the above case). For example, the function $09 of
INT $21 is a function that displays on the screen a string of characters stored in memory. Therefore, the
location of the string should be given as a parameter to the function prior to calling the interrupt. In this
particular case, the function expects the address of string to be displayed in DS:DX. For example, if the string is
found in the variable MESSAGE declared in the DATA segment, we should call the interrupt as follows
(assuming that DS already contains the paragraph number of the data segment):
MOV DX, OFFSET MESSAGE ; This puts the offset of MESSAGE in DX
MOV AH, $09 ; This puts the function number ($9) in AH
INT $21 ; This calls the function $09 of Interrupt $21

Another important function of INT $21 is function $4C. This function enables an assembler program that
terminates to transfer the control back to DOS operating system. Unless this function is called at the end of the
code segment the program will stop without transferring the control to the operating system, which will require
the user of the program to restart the computer at the end of each execution of the program. Function $4C
should be called as follows:
MOV AX, $4C00 ; This puts the function number ($4C) in AH
; and argument 00 in AL (which means no error)
INT $21 ; This calls the function $4C of Interrupt $21

The ADD instruction


Syntax:
ADD destination, source
This is equivalent to:
destination := destination + source
If the result of the addition is too large to be stored in the destination location, the result will be truncated and
the overflow flag (of the flags register) will be set.
For example, if AX=F000 and BX=2000 and

Page - 19 -
Laboratory Notes: Computer Architecture Course

ADD AX, BX is executed,


the sum is $11000 which is too large to be stored in AX. Thus, AX will hold $1000 and the overflow flag of the
flags register will be set.
The SUB instruction
Syntax:
SUB destination, source
This is equivalent to:
destination := destination - source
If the result of the subtraction is negative the Carry flag will be set.
The MULT instruction
Syntax:
MULT operand
Where operand is a general-purpose register or a memory location. MULT works differently whether the
operand is a 16-bit or 8-bit location.

16-bit multiplication

AX

X
operand

DX AX

The multiplier is found in AX (default operand) and the multiplicand is the explicit operand found in the
instruction. The result of the operation will be found in DX (for the most significant word) and AX (for the
least significant word).

Page - 20 -
Laboratory Notes: Computer Architecture Course

8-bit multiplication

AL

X
operand

AH AL

The multiplier is found in AL (default operand) and the multiplicand is the explicit operand found in the
instruction. The result of the operation will be found in AH (for the most significant byte) and AL (for the least
significant byte).
In either 16-bit and 8-bit multiplication, the carry and Overflow flags will be set if the higher order register of
the product is non zero and cleared if it is zero.
The DIV instruction
Syntax:
DIV operand
Where operand is a general-purpose register or a memory location. DIV works differently whether the operand
is a 16-bit or 8-bit location.
16-bit division

DX AX

÷ operand

DX AX

The dividend is found in DX:AX and the divisor is the explicit operand found in the instruction. The result of
the operation will be found in AX (for the quotient) and DX (for the remainder).

Page - 21 -
Laboratory Notes: Computer Architecture Course

8-bit division

AH AL

÷ operand

AH AL

The dividend is found in AX and the divisor is the explicit operand found in the instruction. The result of the
operation will be found in AL (for the quotient) and AH (for the remainder).
Example of a program
; This program displays “HAVE A NICE DAY”
LIST SCR
HEX $
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV DX, OFFSET MESSAGE
MOV AH, $09
INT $21
MOV AX, $4C00
INT $21
STACK SEGMENT STACK $0400
DATA SEGMENT
MESSAGE DB "HAVE A NICE DAY.$"
END

Page - 22 -
Laboratory Notes: Computer Architecture Course

Chapter Six: Debugging an assembly language program


Debugging an assembly language program consists into executing the program in small sections and examining
the contents of memory and the content of the CPU registers following the execution of a section of the code to
see if the result conforms to the expectation of the programmer.
The debugger is a utility program that allows the programmer to try out a program in an environment in which
the program’s execution can be suspended at any given point. While the program is halted, the debugger
enables to examine the contents of the registers and the memory.
The debugger can be called at the DOS prompt as follows:
DEBUG progname.exe
When called, the debugger will display its own prompt and will wait for commands from the user. At this point
no instruction of the program is executed.
The below table gives some commands of the debugger as well as the associated actions.

Command Action
R Displays the content of all registers
R register_name Displays the content of register register_name
U {address {l length}} Disassembles the code found at the address address and having the
length of length bytes. If length is not specified the default length is
120.
D {address {l length}} Dumps (displays) the content of the memory at the address address
and having the length of length bytes. If length is not specified the
default length is 180.
G {=address}{{breakpoint}} Go command: executes the program starting at the address address
and specifies breakpoints where the program stops.
Note: it is dangerous to execute G after the whole program has
executed.
T Trace command: displays the value of the registers after the
execution of the current instruction.
Note: Don’t use T command when the next instruction is an INT
P Process command: similar to T but also works with an INT
Q Quit the debugger and go to DOS

The following tips are useful to detect run time errors using your debugger:
- If your program may behave abnormally when executed, you should first run your program in the
debugger with a breakpoint set about halfway through it
- There are two kinds of breakpoints:

Page - 23 -
Laboratory Notes: Computer Architecture Course

- Soft breakpoints (specified using G command)


Note: The address of the breakpoint should be the address of the opcode. If you give another
address, there is unpredictable error
- Hard breakpoint (explicitly installed in your program using INT $03).
Note: Never run a program with a hard breakpoint in DOS, but only in DEBUG

Page - 24 -
Laboratory Notes: Computer Architecture Course

Chapter Seven: Control Transfer Instructions


Normally, the architecture of the CPU provides for program flow to proceed sequentially from one instruction
to the next as in the below figure:

Fetch the Instruction


at CS:IP

Update IP to
address at next instruction

Execute this
instruction

The control transfer instructions are special class of instructions that, when executed adjust the content of the IP
register and sometimes the CS register.

I Subroutines
A subroutine is a sequence of code designed to be invoked at one or several points in a program. It is invoked
with a CALL instruction and it returns to the point from which it was called with a RET instruction.
Subroutines are usually organized into procedures. A procedure starts with a PROC directive:
Syntax of PROC
procname PROC
Syntax of CALL
CALL procname
When CALL is executed, the CPU puts in IP the address of the instruction after the CALL without executing it.
Then it copies IP into the stack. Finally it adjusts IP so that it points to the first instruction of the called
procedure. After that the instructions will be executed one after the other until RET is executed.
When RET is executed the address in the stack is copied into IP. This will enable the control to return to the
calling program, just after the CALL statement.

Page - 25 -
Laboratory Notes: Computer Architecture Course

The order of procedures in the program is irrelevant for the program flow. However, the first procedure will be
the first to be executed.

II Jumps
Jumps enable to the programmer to unconditionally move the control to a designated instruction. This is
equivalent to GOTO command in higher level languages.
Syntax:
JMP label
Note:
Labels can be given to any line of a program (instruction, comment, blank, etc.)

III Branches
The branch instructions of the 8086/8088 assembly language are equivalent to the “if then else” instruction of
high level instructions. In the 8086/8088 assembly language there are a number of instruction that perform
branch operations.
A program branch is implemented as a two-step process:
- First, a compare instruction should be used to verify the condition
- Second, a conditional jump instruction should be used to jump or not to a given code according to
the result of the first instruction
The information obtained by the first instruction is passed to the second instruction using the flag register.

1 The flag register


The flag register is a 16 bit register. Six of its bits are devoted to the status flags (C, P, A, Z, S, O) and 3 of its
bits are devoted to control flags (T, I, P). The remaining 7 bits are undefined. Each flag can be set to a value of
1 or cleared to a value of 0. Generally, the CPU adjusts the flags in the course of executing arithmetic
operations. They reflect the outcome of an operation.

F E D C B A 9 8 7 6 5 4 3 2 1 0
O D I T S Z A P C

The following table gives the letter representing each flag, its name, the two-letter code used to indicate
whether the flag is set (1) or cleared (0) and the purpose of the flag.

Flag Name Clear Set Purpose


C Carry No Carry (NC) Carry (CY) This flag is adjusted by several arithmetic instructions
P Parity Parity Odd (PO) Parity Even This flag is adjusted by several arithmetic instructions
(PE)

Page - 26 -
Laboratory Notes: Computer Architecture Course

A Auxiliary No Aux. Carry Aux. Carry Used Internally by CPU


Carry (NA) (AC)
Z Zero Not Zero (NZ)_ Zero (ZR) This flag is adjusted by several arithmetic instructions
S Sign Positive (PL) Negative (NG) This flag is adjusted by several arithmetic instructions
to reflect the algebraic result of a computation
T Trap When the flag is set, the CPU executes a single
instruction and then executes INT $01
I Interrupt Disabled (DI) Enabled (EI) Disable or enable interrupt
D Direction Up (UP) Down (DN) Adjusted by the programmer to direct string handling
instructions
O Overflow No overflow Overflow (OV) This flag is adjusted by several arithmetic instructions
(NV) to reflect a computational result outside of permissible
range

Note: You should see the reference manual of the language for the details as to which instructions affect which
flags.

2 Comparison
The CMP (CoMPare) instruction is used to compare two values and to adjust the status flags accordingly.

Syntax:
CMP destination, source
CMP subtracts the value of source from the value in destination. It does not store the result of the subtraction. It
sets the zero flad if the result is zero (the operands are equal). Otherwise it will clear the flag.
If the contents of the destination is below the content of the source, the carry flag is set; otherwise it is cleared.
The below table summarizes the actions of the CMP instruction

Carry Flag Zero Flag


Destination above Source NC NZ
Destination = Source NC ZR
Destination below Source CY NZ

Page - 27 -
Laboratory Notes: Computer Architecture Course

3 Conditional Jumps
The conditional jump instructions enable the programmer to specify jumps that are conditional to the value of
one or several flags.
Syntax
JXXX label
XXX describes the condition for which you are testing. There are 31 conditional jumps that test a flag or a
combination of flags and jump to the label if the flag is set. Among the 31 jumps 15 of them are the converse of
the other 15. The 31st does not use a flag but tests the CX register.
Note: A number of jumps are equivalent.

Instruction Action Equivalent Instruction


JNZ Jumps to label if Zero is set JE (Equal)
JNZ Jumps to label if Zero is clear JNE (Not equal)
JB Jumps to label if below
JBE Jumps to label if below or equal
JAE Jumps to label if above or equal JNB (Not below)
JA Jumps to label if above
JNBE Jumps to label if not below or equal
JNAE Jumps to label if not above or equal
JNA Jumps to label if not above
JCXZ Jumps to label if CX is zero

There is one major caveat with conditional jumps: they have limited range. The opcode to which a conditional
jump directs a program flow must be at an offset in the range (XXXX-$007E) through (XXXX + $0081) where
XXXX is the offset of the jump instruction itself.
This restriction does not apply on the JMP command. We can therefore use a trick such as the following to
overcome this problem:
JNZ NEAR_AT_HAND
JMP LONG_WAY_OFF
NEAR_AT_HAND:
•••••

LONG_WAY_OFF

Page - 28 -
Laboratory Notes: Computer Architecture Course

4 Loops
A program loop can be implemented either with an explicit counter or an implicit counter. Explicit counter
usually use the fact that the DEC instruction not only subtracts 1 from the content of its operand but also sets
the Zero flag if the result of the subtraction is Zero.
LOOP_LABEL:
•••
DEC CX
JNZ LOOP_LABEL
The LOOP instruction can also be used to implicitly count the iterations of a loop. The below code is equivalent
to the above code, but uses an implicit count rather than an explicit.
LOOP_LABEL:
•••
LOOP LABEL
There are some variations on the LOOP instruction:
- LOOPZ Label will continue the loop while the zero flag is set and CX does not contain a value of
zero
- LOOPNZ will continue the loop while the zero flag is cleared and CX does not contain a
value of zero

Page - 29 -

You might also like