Assembly Language Programming 16 31
Assembly Language Programming 16 31
Assembly Language Programming 16 31
Microcomputer
Systems
1.1 . .
The Components Figure 1.1 shows a typical microcomputer system, consisting of a
of a Microcomputer system unit, a keyboard, a dispiay screen, and disk drives. The system unit
is often referred to as "the computer," because It houses the circuit boards
System of the computer. The keyboard, display screen, and disk drives are called 1/0
devices because they perform input/output operations for the computer.
They arc also called peripheral devices or peripherals. -
Integrated-circuit (IC) chips are used in the construction of computer
circuits. · c c I ma con ai h dreds or even thousands of transistors.
These IC circuits arc .kriown !IS di~tial c --~ s ause t ey operate on .
discrete voltage slBnal levels, typiCaly, a high voltage and a low voltage. We
use the symbols 0 ailcf' T'to represent the low- and high-voltage signals,
respectively. These symbols are called l_!!..°.!!'"Y digit~, or,.bits. All information
processed by the computer ls.represented by strings of O's and l's; that is,
by bit strings. . · ·
4 7. 1 The Components of a Microcomputer System
,---
Figure 1. 1 A Mlaoc""1pUtlW ;
System
'~, .
.··>r-.:~~,
·.·• ... \
·.
1.1.1
Memory Bites and Words
Infonnatlon"·processed by the computer Is stored In Its memory. A
niemorys1i_cu}t_elem~nt_ can store one bit of data. However, the mtmory
circuits are usually organized Into groups that can store eight bits of daf,J,
and a string of eight bits ls called a byte. Each memory byte clrcult_:or
memory byte, for short-is identified by a· number that Is called its IMI-
·~. like the street address of a house. The first memory byte has address
Chapter 7 Microcomputer Systems s.
, . . _ 1.2 A Motherboard
--·--·--------· ·-::- ···-· -··1
i!m~ a&m· 1
• 1
'V'
0. The data "!lltOred in a memory byte are called Its i;pgtcgts. When the
contents of a ml'mory hytl' are treated as a single numhcr, we often i1w the
term v.atue to denote them ..
It is important °t; understand the difference b<'twecn address and
contents. The address of a memory byte i~ fixed and is different from the
address t>f ahy otlll'r memory byte in the l''o111j1uter. Yet the contcnt5 of a
memory byte arc not unique and arc subject to change, because they denote
the data curmilly l~ing siored. Hgure 1.3 shows the organization of memory
bytes; tl'n: contents ille arbitracy. ~
Another distinction between address and contents.ls that while the
contents of .a memory byte are always eight bits, the numtrer of bits ln an
7 0 0 1 0 1 1 0
6 1 1 0 0 1 1 1 0
5
4
0 0 0 0
1 1 1 0
,,
1 1
1
0 1
0 1
3 0 0 0 0 0 0 0 0
2 1 1 1 1 1 1 1
1 • 0 0 1 1 1 1 0
0 0 1 0 0 0 0 I
6 · 1. t The Components of a Microcomputer System
address depends on the processor. For example, the Intel 8086 microprocessor
assigns a 20-bit address, and the Intel 80286 microprocessor uses a 24~bit
address. The number of bits used In the address determines the number of
bytes that can be accessed by the processor. '
Bit Position\/"'
Figure 1.4 shows the bit positions in a microcomputer word and a
byte. The positions are numbered from right to left, starting with O. In a
word, the bits 0 to 7 form the low byte and the bits 8 lo 15 form the ltigll
byte. For a word ~tored In memory, its low hyte comes from the memory
byte with the lower address and Its high byte Is from the memory byte with
the higher address.
Memory Operationi. •
The processor can perform two operations on memory: r~ad (fetch)
the contents of a location and write (store) dilla at a locallon. In a read
operation, the processor only gets a copy of the data; the original contents
Word bit
position
15 14 13 12 11 10 9 8 7 6 5 4 3 2 0
~1
I I I I
High byte Low byte
Chapter 7 Microcomputer Systems 7
:r
Figure 1.5 Bus Connections
of a "Microcomputer Address bus
of the location are unchanged. In a wri~e operatiqn, the _data written become
the new c~ntents of the location; ·t_he original contents are thus lost.
Buses
A processor communicates with memory and l/Q.,clrcults by using
signals that travel along a set of wires or connections called .Juua that
connect the different comp0nents. There are ~hrec kinds of slgnals:.address,
data, and control. And there are three buses:.address bus; data bus, and
control bus. For example, to read the contents of a mc.mory location, th~
CPU places the address oC the memory location on the address bus, and It
re<;elves the data, sent by tlie memory circuits, on the data bus, A control
signal l~ required to inform the memory to perform a read operation. The
CPU sends the control signal on the control bus. Figure 1.5 Is a diagram of
the bus connections for.a microcomputer. · · ·
1.1.2
The CPU . As stated, ttie CPU Is the brain of the computer. It controls the i.:om-
pute(by executing programs stored In memory. A program might be a system
program. ·or an application program. written by a user. In any ease, each
lnstiuctlon that the CPU executes Is a bit string (for the Intel 8086, instruc-
tions are from one to six bytes long). This language of O's and 1's is called
machine. language.
1, l ~~ts <?fa. MicrQcomputer System
AX
BX
ex
ox
General registers
BP
cs
External bus
Temporary registers
B~afe, <fnJ!.J!U!:!!t~
The bus_ Interface unit (BIU) facilitates communication betw,cc1· 'he
EU and the memory or J/0 circuits. It is responsible for transmittir"' ad-
dresses, data,· and control signals on the buses. Its registers are named CS, •
OS, ES, SS,. and IP; they hold addresses of memory locations. The IP
(qistruCtion pointer) contains the address of the next instruction to be
executed 'by the EU. · ' · ·
The fill and the filY. are connected by an ~nternal b~; and they work
together. While the EU is executing an instruction, the BJU fetches up to six
byte% of the next instruction and places them in the instruction queue. This
operation is called i1rsrruc«qr1 qrefetch. The purpose is to speed up the pro-
Cj!.ll_Or. If the EU needs to communicate with memory or the peripherals, the
BIU suspends instruction prefetch and performs the needed oneratians • ....-
1.1.3
110 Ports 1/0 devjces are connected to the computer through 1/0 circuits. Each
of these circuits contains several registers called 1/0 ports. Some are used
for data while others are used for control commands. Like memory locations,
the I/O ports have addresses and arc connected to the bus system. However,
these addresses are known as 110 addresses and can only be used in input or
output instructions. This allows the Cl'U to distinguish between an 1/0 port
and a memory location.
1/0 ports function as transfer points between the Cl'U and 1/0 de-
vices. Data to be Input from :m 1/0 devise arc st·nt to ii port where they can
. be read by the CPU. On output, the CPU writes data to an 1/0 port. The 1/0
circuit then transmits the data to the 1/0 device.
1.2
Instruction To understand how the CPU operates, let's look at how an Instruction
Execution Is executed. First of all, a machine instruction has two parts: an opcode and
operands. The opcode specifies the type of operation, and the operands are
often given as memory addresses to the data to be operated on. The CPU
goes through the following steps to execute a machine instruction (the
fctch,..exccutc cycle):
Fetch
1. Fetch an lmtruction from memory.
2. Decode the Instruction to determine the operation.
3. · Fetch data from memory if necessary.
7.2 Instruction Execution
Execute
4. Perform the operation on the data.
5.. Store the result in memory if needed.
To see what this entails, let's trace through the execution of a typical machine
language instruction for the 8086. Suppose we look at the iustructlon that
adds the contents of register AX to the contents of the memory word at
address 0. The CPU actually adds the two numbers in the ALU and then
stores the result back to memory word O. The machine code is
00000001 00000110 00000000 00000000
Before execution, we assume that the first byte of the Instruction is stored
at the location indicated by the IP.
1. Fetch the instruction. To start the cycle, the BIU places a mem-
ory read request on the control bus and the address of the in-
struction on the address bus. Memory responds by sending the
contents of the location specified-namely, the instruction
.code just given-over the data bus, Because the instruction
code Is four bytes and the 8086 c:an only read a word at a
time, this involves two read operations. The CPU accepts the
data and adds four to the JP so that the IP will contain the ad-
dress of the next instruction.
2. Decode the lnstmction. On receiving the Instruction, a decoder
circuit in the EU decodes the instruction and determines that it is
an ADD operation involving the word at address 0.
3. Fetch data from memory. The EU informs the BIU to get the con-
tents of memory word 0. The BIU sends address 0 over the ad-
dress bus and a memory read request is again sent over the
control bus. The contents of memory word 0 arc sent back over
the data bus to the EU and arc placed In a holding register.
4. Perform the oper<1tiun. The contents of the holding register and
the AX register arc sent to the ALU circuit, which performs the re-
quired addition and holds the sum.
5. Store the result. The EU directs the BIU to store the sum at ad-
dress 0. To do so, the llIU sends out a memory write request over
the control bus, the ;iddress 0 over the address bus, and the sum
10 be stored over the data bus. The previous contents of memory r
word 0 arc ov1:rwritl1.•n by the sum.
The cycle is now repeated for the instruction whose address is con-
t;iined in the II'.
Timing
The prl'Cl.'ding example shows that even though machine instructions
are very simple, their execution is actually quite complex. To ensure that the
steps arc carried out in an orderly fashion, a clock circuit controls the processor
hy generating a train of clock pulses as shown in Figure 1.7. The time inter\'al
!Jetwcen two pulses· is known as·a clock period, and the nwnbcr of pulses per
second is calk'CI the clock rate or clock speed, measured In megahertz
(MHz)." One megahertz Is 1 million cycles (pulses) per second. The original
lnM PC had a clock rate of.4.77 MHz, but the latest PS/2 model has .1 cluck
rate of 33 MHz.
. · Th~ _fi>mputer circuits.are activated by the clock pulses; that is, the
circuits perform an operation only. when a clock pulse is present. E.Jch step
in the instru<.:tiun fct<.:h and execution <.:ydc requires one or more do<.:k pe-
riods. For example, the 8086 takes four clock periods to do a memory read
and a multiplication operation may take more than seventy clock periods.
If we s·peed up the clock circuit, a processor can be made to operate faster.
. However,_ each processor has a rated maximum clock speed beyond which
it may not function properly..
1.3 ...
110 Devices 1/0 devices are needed to get information into and out of the com-
puter. The primary 1/0 devices are magnetic disks, the keyboard, the display
monitor, and the printer.
· Magnetic Disks
We've seen that the contl'nts of RAM arc lost when the computer
is turned off, so magnetic disks an: used for permanent storage of programs
and data. There are two kinds of disks: tlOJJJlY disks (also calll'd diskettes)
and hard disks. The device that reads and writes data ori a disk is called
· a disk drive.
Floppy lfoks come in s i/4-inch or :n~-in<.:h diameter siZl!S. They arc
lightweight and portable; it is easy to put a diskette away for safekeeping or
use it on different computers. The amount of data a floppy disk can hold
depends on the ly~c:; it ,1nges from 360 kilobytes to 1.44 megab.ytcs. A
°
kilobyte (KU) is 2 bytes. · ·•
. A hard disk and its disk drive are cndoscd in a hermetically scaled
container that is not removable: from the computer; thus, it is also called a
fixed disk. It can hold a Jot more data than a floppy disk-typically 20,
·HI. to over 100 megabytes. A program can also access information on a hard
disk much foster than a floppy disk.
. Disk operations arc covered in Chapter 19.
Keyboard
The keyboard allows the user to enter information into the cornputt:r.
It has the keys usual!)' fo_und on a typewriter, plus a number of <.:ontml and
function keys. It has its own microprocessor that sends a coded signal to the
computer whenever a key is pressed or released.
When a key is prc!>sed, the corresponding key character normally
appears on the screen. Uut Interestingly enough, there is no direct <.:onnc:ction
between the keyboard and the screen. The data from the keyboard are re-
ceived by the current running program. The program must send the data to
the screen before a character is displayed. In Chapter 12 you will learn how
to control the keyl>Oard.
12 1.4 Programming Languages
Display Monitor
1be display monitor is the standard output device of the computer.
The information displayed on the screen Is generated by a circuit In the com-
puter called a video adapter. Most adapters can generate both text characters
and graphics images. Some monitors are capable of displaying In color.
We discuss text mode operations in Chapter 12, and cover graphics
mode in Chapter 16.
Printers
Although monitors give fast visual feedback, the information is not
permanent. Printers, however, arc slow but provide more permanent output.
Printer outputs arc known as hardcopies.
111e three common kinds of printers are daisy wllL'f!I, dot matrix, and
/nscr printers. The output of a daisy wheel printer is similar to that of a typewriter.
A dot matrix printer prints characters composed of dots; depending on the
number of dots useu per character, some dot matrix printers can generate
near-letter-quality printing. The advantage of dot matrix printers is that they
can print characters with different fonts as well as graphics.
The laser printer also prints characters composed of dots; however,
the resolution is so hi~h (JOO dots per Inch) that it has typewriter quality.
The laser printer is expensive, but in the field of desktop publishing it is
indispensable. It is also quiet compared to the other printers.
1.4
Programming The operations of the computer's hardware are controlled by its
Languages software. When the computer is on, itls always in the process of executing
instructions. To fully understand the computer's operations, we must also
study its instructions.
Machine Language
A CPU can only execute machine language instructions. As we've
seen, they arc bit strings. The following Is a short machine language program
for the I BM PC:
Machine instruction Operation
10100001 00000000 00000000 Fetch the contents of memory word 0
and put it in register AX. ·
00000101 00000100 00000000 Add 4 to AX.. "
10100011 00000000 00000000 Store the contents of AX in memory
word 0.
A~ you can wl'll imagine, writing programs in machine language is
tt>dious and subject to error!
Assembly Language.
A more convenient language to use is asftmbly language. In as-
sembly language, we use symbolic names to represent operations, registers,
and memory locations. If location 0 is symbolized by A, the preceding pro-
gram expressed in lllM J>C assembly language would look like this:
Chapter 7 Microco/T1puter Systems .13
High-Level Lang~ages
Even though it's easier to write programs in asscrn6Jly language than
machine language, it's still difficult because the instruction set is so primitive.
That is why high-level languages such as F.QJ..U:JiAN, l~I. ~ml others
were devel0ped. Different high-level languages arc designed for different ap-
plications, but they generally allow programmers to write programs that look
.more like natural language text than is possible in assembly la11guage.
A program called a C0-1tpill"r is needed to·translate a high-level lan-
ghage program into machine code. \. .,11.:_Jilation is milleiiWOlvcd than assem-
hling lx.'(;ause it entails the trnnslation or comp;1•x mathematical (:xpressions and
natural language comniands i·nto simple'macl1i.nc operations. A high-level lan-
guage statement ty1>kally translates into many machine lanb'Uage instructions.
.1
14 1.5 An Assembly language Program
crucial parts of a program can be written in assembly language, with the rest
written in a high-level language.
In addition to these considerations, there is another reason for
learning assembly language. Only by studying assembly language ls it
possible to gain a feeling for the way the computer "thinksn and why
certain things happen the way they do inside the computer. High-level
languages tend to obscure the details of the compiled machine language
program that the computer actually executes. Sometimes a slight change
in a program produces a major increase in th~ run time of that program,
or arithmetic overflow unexpectedly occurs. Such things can be under-
stood on the assembly language level.
Even though here you will study assembly language specifically for.
the IBM PC, the techniques you wlll learn are typical of those used in any
assembly language. Learning other assembly languages should be relatwely
easy after you have read this boOk. ~
;
r'Assembly To give an idea of what an assembly language program looks like, here
nguage Program is a simple example. The following program adds the contents of two memory
locations, symbolized by A and n. The sum is stored in location SUM.
MOV AX,A copies the contents of word A into register AX. AOD AX,B.adds
the c'?ntents o~ B _to it, _so that AX now, hol~s the total!~-~OV SUM,ft,.X
stores the answer in vanable SUM.
• Before this program could be run on the computer, it would have
to be assembled into a.machine language program. The steps are explaim:d
in Chapter 4. Because there were no output lnstructi9ns, we could not see
the answer on the screen, but we·co.uld trace the program's exeeutlon ain
debugger such as the DEUUG program,
Glossary
add-In board or card Circuit board that connects to the
'motherboard, usually containsJ/O cir-
• cuits or additional memory
address A number that identifies a memory location
address bus The set of electrical pathways for address
signals
arithmetic and logic unit, • ~.PU. circ·~it where arithmetic and logic
ALU operations arc done
i:uscn1blcr , ~ program that translates an assembly lan-
guage program into machine language
assembly language ~y~~onc representation of machine lan-
guage
.binary digit A symbol,that can have value 0 or 1
bit , ~in,ary ~igit
bus A set of wires or connections connecting
the CPU, mcmol)', ilml J/O .PQIU
16 Gtossar'y
Exercises
b. Wlut is
• l:it 7 of ~te 2°!
• bit 0 of word 3?
o bit 4 of byte 2?
• bit 11 of word 2?
18 Exercises