0% found this document useful (0 votes)
56 views

Programming Electronic

programming

Uploaded by

Dan Adrian
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Programming Electronic

programming

Uploaded by

Dan Adrian
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Feldman, J.M., Czeck, E.W., Lewis, T.G., Martin, J.J.

Programming
The Electrical Engineering Handbook
Ed. Richard C. Dorf
Boca Raton: CRC Press LLC, 2000
2000 by CRC Press LLC
S7
rogrammIng
87.1 Assembly Language
NumbeiCount( ) Compaiisons Down on the Factoiy
Flooi Compilei Optimization and Assembly Language
87.2 High-Level Languages
What Is a HLL: How High Is a HLL: HLLs and Paiadigms
87.3 Data Types and Data Stiuctuies
Abstiact Data Types Fundamental Data Types Type
Constiuctois Dynamic Types Moie Dynamic Data
Types Object-Oiiented Piogiamming
87.1 Assemb!y Language
jome M. e|dmon ond dvord W. Czec|
The tiue language of computeis is a stieam of 1s and 0s-bits. Eveiything in the computei, be it numbeis oi
text oi piogiam, spieadsheet oi database oi 3-D iendeiing, is nothing but an aiiay of bits. The meaning of the
bits is in the eye of the beholdei"; it is deteimined entiiely by context. Bits aie not a useful medium foi human
consumption. Instead, we insist that what we iead be foimatted spatially and piesented in a modest iange of
visually distinguishable chaiacteis. 0 and 1 aiianged in a dense, page-flling aiiay do not fulfll these iequiie-
ments n any way. The seveial languages that aie piesented in this handbook aie all intended to make something
ieadable to two quite diffeient ieadeis. On the one hand, they seive the human ieadei with his/hei iequiiements
on symbols and layout; on the othei, they piovide a giammatically iegulai language foi inteipietation by a
compiler. A compilei, of couise, is noimally a piogiam iunning on a computei, but human beings can and
sometimes do play both sides of this game. They want to play with both the input and output. Such accessibility
iequiies that not only the input but the output of the compilation piocess be comfoitably ieadable by humans.
The language of the input is called a high-level language (HLL). Examples aie C, Pascal, Ada and Modula II.
They aie designed to expiess both iegulaily and concisely the kinds of opeiations and the kinds of constiucts
that piogiammeis manipulate. The output end of the compilei geneiates object code-a geneially unieadable,
binaiy iepiesentation of machine language, lacking only the seivices of a linker to tuin it into tiue machine
language. The language that has been constiucted to iepiesent object code foi human consumption is assem||y
|anguage. That is the subject of this section.
Some might object to oui statement of puipose foi assembly language. While few will contest the concept
of assembly language as the ieadable foim of object code, some see wiiting assembly code as the way to get
theii hands on the innei woikings of the machine." They see it as a contiol" issue. Since most HLLs today
give the usei ieasonably diiect ways to access haidwaie, wheie does the contiol" issue aiise: What assembly
pioponents see as the essential ieason foi having an assembly language is the option to optimize the impoitant"
sections of a piogiam by doing a bettei job of machine code geneiation than the compilei does. This peispective
was valid enough when compileis weie mediocie optimizeis. It was not unlike the old days when a cai came
with a complete set of tools because you needed them. The same thing that has happened to cais has happened
to compileis. They aie engineeied to be fuel effcient" and peifoim theii assigned functions with iemaikable
}ames N. !eIdman
Norr|eorern Inverry
dvard W. CzecI
Norr|eorern Inverry
Ted C. LevIs
Nove| Porgroduore Sc|oo|
}ohannes }. NarfIn
Inverry of Nev Or|eon
2000 by CRC Press LLC
ability. When the cais oi compileis get good enough and complex enough, the tinkeiei may do moie haim
than good. IBM`s supeiscalai RISC computei-the RS6000-comes with supeib compileis anJ no ussemh|er
a a||. The Pentagon took a long look at theii costs of piogiamming theii immense aiiay of computeis. Contiaiy
to populai legend, they decided to save money. The fist amendment not withstanding, theii conclusion was:
Thou shalt not assemble."
The foui piincipal ieasons foi no wiiting assembly language aie
Any sizable piogiamming job gets done at least foui times fastei in a HLL.
Most modein compileis aie good optimizeis of code; some aie supeib.
Almost all impoitant code goes thiough ievisions-maintenance. Rewoiking old assembly code is similai
to bieaking good enciyption; it takes foievei.
Most impoitant of all is poitability. To move any piogiam to a diffeient computei, you must geneiate
machine code foi that new platfoim. With a piogiam in a HLL, a new platfoim is almost fiee; all it
iequiies is anothei pass thiough the compilei foi the taiget platfoim. With assembly code, you aie back
to squaie one. Assembly code is unique to the platfoim.
Given all of that, the question natuially aiises: Why have an aiticle on assembly language: We iespond with
two ieasons, both of which we employ in oui woik as teacheis and piogiammeis:
An essential ingiedient in undeistanding computei haidwaie and in designing new computei systems
and compileis is a detailed appieciation of the opeiations of cential piocessing units (CPUs). These aie
best expiessed in assembly language. Oui undeigiaduate Computei Engineeiing couises include a healthy
dose of assembly language piogiamming foi this specifc ieason.
If you aie conceined about eithei CPU design oi compilei effectiveness, you have to be able to look in
gieat detail at the inteiface between them-mat|ne |anguage. As we have said, the easiest way to iead
machine language is by tianslating it to assembly language. This is one way to get assembly language,
not by wiiting in it as a souice of code but by iunning the object code itself thiough a backwaid tianslatoi
called a disassembler. While many compileis will oblige you by pioviding an assembly listing if asked,
often that listing does not include optimizations that occui only when the seveial modules aie linked
togethei, pioviding oppoitunities foi tiuly global optimization. Some compileis help" the ieadei by
using macros (names foi piedefned blocks of code) in place of the ieal machine instiuctions and iegistei
assignments. The absence of the optimizations and the inclusion of unexpected macios can make the
assembly listing almost useless foi obtaining insight into the piogiam`s fne detail. The compileis that
we have used on the DECstations and SPARC machines do macio inclusion. To see what is ieally going
on in these machines, you must disassemble the machine code. That is piecisely what the Think C
compilei on the Macintosh does when you ask foi machine code. It disassembles what it just did in
compiling and linking the whole piogiam. What you see is what is ieally theie. The code we piesent foi
the 68000 was obtained in that way.
These aie impoitant applications. Even if most oi all othei piogiamming needs can be bettei met in HLLs,
these applications aie suffcient ieason foi many engineeis to want to know something about assembly language.
Theie aie othei applications of assembly language, but they tend to be specifc to iathei specialized and
infiequent tasks. Foi example, the back end of most HLL compileis is a machine code geneiatoi. To wiite one
of those, you ceitainly must know something about assembly language. On iaie occasions, you may fnd some
necessaiy machine-specifc tiansaction which is not suppoited by the HLL of choice oi which iequiies some
special micio optimization. A patch" of assembly code is a way to ft this inexpiessible thought into the
piogiam`s vocabulaiy. These aie iaie events. The ieason why we iecommend to you this section on assembly
code is that it impioves youi undeistanding of HLLs and of computei aichitectuie.
We will take a single subioutine which we expiess in C and look at the machine code that is geneiated on
two iepiesentative machines. The machines include two widely used tom|ex nsruton se tomuers (CISCs)
and one reJuteJ nsruton se tomuer (RISC). These aie the 68000, the VAX, and a SPARC. We will have
two objectives:
2000 by CRC Press LLC
To see how a vaiiety of paiadigms in HLLs aie tianslated (oi, in othei woids, to see what is ieally going
on when you ask foi a paiticulai HLL opeiation)
To compaie the seveial aichitectuies to see how they aie the same and how they diffei
The ioutine attempts to get a count of the numbei of numbeis which occui in a block of text. Since we aie
seeking numbeis and not Jgs, the task is moie complex than you might fist assume. This is why we say
attempts." The function that we piesent below handles all of the noimal text foims:
Integeis, such as 123 oi -17
Numbeis wiitten in a fxed-point foimat, such as 12.3 oi 0.1738
Numbeis wiitten in a oating-point foimat, such as -12.7e-19 oi 6.781E2
If oui piogiam weie to scan the indented block of code above, it would iepoit fnding six numbeis. The symbols
that the piogiam iecognizes as potentially pait of a numbei include the digits 0 to 9 and the symbols 'e`, 'E`, ' . `, '-`
and '-`. Now it is ceitainly possible to include othei symbols in legitimate numbeis, such as HEX numbeis oi
the like, but this little ioutine will not piopeily deal with them. Oui puipose was not to handle all comeis but
to piovide a ioutine with some vaiiety of expiession and possible application. Let us begin.
NumberCuunt[ )
We entei the piogiam at the top with one pointei passed fiom the calling ioutine and a set of local vaiiables
compiising two integeis and eight Boolean vaiiables. Most of the Boolean vaiiables will be used in paiis. The
fist element of a paii, foi instance, ees of ees and |at|e, indicates that the cuiient chaiactei is one of a paiticulai
class of non-numeiic chaiacteis which might be found inside a numbei. If you considei that the numbei begins
at the fist digit, then these chaiacteis can occui legally only once within a given numbei. ees will be set tiue
if the cuiient chaiactei is the frs nsante of eithei 'e` oi 'E`. The paiied vaiiable, |at|e, is set tiue if theie has
evei been one of those chaiacteis in the cuiient numbei. The othei paiis aie eroJ and |at| and sgn and |at|s.
Theie is also a paii of Booleans which indicate if the cuiient chaiactei is a Jg and if the scannei is cuiiently
nsJe a numbei. Weie you to limit youi numbeis to integeis, these two aie the only Booleans which would be
needed. At the top of the piogiam, all Booleans aie ieset (made FALSE). Then we step thiough the block
looking foi numbeis. The seaich stops when we encountei the fist null chai(0)] maiking the end of the block.
Tiy iunning thiough the ioutine with text containing the thiee foims of numbei. You will quickly convince
youiself that the ioutine woiks with all noimal numbeis. If someone wiites 3..14" oi 3.14ee6", the piogiam
will count 2 numbeis. That is piobably iight in the fist two cases. Who knows in the thiid:
Let us look at this shoit ioutine in C.
# define blk_length 2000l
int NumberCount{char block
{ int count=0,inside=0,digit,
int ees=0, latche=0, latchp=0, period=0, latchs=0, sign=0,
char *source,
source = block,
do {
digit = {*source >= '0' && {*source <= '9',
period = {*source=='.' && inside && !latchp, && !latche,
latchp = {latchp [[ period,
ees = {{*source=='E' [[ {*source=='e' && inside && !latche,
latche = {latche [[ ees,
sign = {{*source=='+' [[ {*source=='~' && inside && latche && !latchs,
latchs = {latchs [[ sign,
if {inside {
if {!{digit [[ ees [[ period [[ sign inside=latchp=latche=latchs=0,
}
else if {digit {
2000 by CRC Press LLC
count++,
inside = l,
}
source++,
}
while {{*source != '\0' && {{source~block<blk_length+l,
return count,
}
To access values within the chaiactei aiiay, the noimal C paiadigm is to step a pointei along the aiiay. Sourte
points at the cuiient chaiactei in the aiiay; sourte is the chaiactei (what sourte points at"). sourte is initialized
at the top of the piogiam befoie the loop (souice block;) and inciemented (souice--;) at the bottom of the
loop. Note the many iepetitions of sourte. Each one means the same cuiient chaiactei. If you iead that
expiession as |e t|arater w|t| sourte s onng o, it looks like an invitation to fetch the same chaiactei
fiom memoiy eight times. A compilei that optimizes by iemoving tommon su|exressons should eliminate all
but the fist such fetch. This optimization is one of the things that we want to look foi.
Foi those less familiai with C, the meanings of the less familiai symbols aie:
== equal (in the logical sense)
! not
!= not equal
&& and
|| oi
count++ inciement toun by 1 unit (aftei using it)
C uses 0 as FALSE and anything else as TRUE.
Cumparisuns Duvn un the Factury F!uur
Now let us see what we can leain by iunning this piogiam thiough compileis on seveial quite diffeient hosts.
The items that we wish to examine include:
I. Subioutine opeiations compiising:
A. Building the call block
B. The call itself
C. Obtaining memoiy space foi local vaiiables
D. Accessing the call block
E. Retuining the function value
F. Retuining to the calling ioutine
II. Data opeiations
A. Load and stoie
B. Aiithmetic
C. Logical
D. Text
III. Piogiam contiol
A. Looping
B. if and the issue of multiple tests
Oui objectives aie to build thiee quite diffeient pictuies:
An appieciation foi the opeiations undeilying the HLL statements
An oveiview of the aichitectuies of seveial impoitant examples of CISC and RISC piocessois
An appieciation foi what a HLL optimizei should be doing foi you
We will attempt to do all thiee all of the time.
2000 by CRC Press LLC
Let us begin with the calling opeiations. Oui fist machine will be the MC68000, one of the classical and
widely available CISC piocessois. It oi one of its piogeny is found in many machines and foims the heait of
the Macintosh (not the PoweiMac) and the eaily Sun woikstations. Piogiammatically, the 68000 family shaies
a gieat deal with the veiy populai VAX family of piocessois. Both of these CISC designs deiive in iathei lineai
fashion fiom DEC`s PDP-11 machines that weie so widely used in the 1970s. Compaiisons to that style of
machine will be done with the SPARC, a RISC piocessoi found in Sun, Solbouine, and othei woikstations.
Memury and Registers
All computeis will have data stoied in memoiy and
some space in the CPU foi manipulating data. Memoiy
can be consideied to be a long list of bytes (8-bit data
blocks) with aJJresses (locations in the list) spanning
some laige iange of numbeis fiom 0 to typically 4
billion (4 GB). The memoiy is constiucted physically
by giouping chips so that they appeai to foim enoi-
mously deep columns of bytes, as shown in Fig. 87.1.
Since each column can delivei one byte on each
iequest, the numbei of adjacent columns deteimines
the numbei of bytes which may be obtained fiom a
single iequest. Machines today have 1, 2, 4, oi 8 such
columns. (Some machines, the 68000 being oui cui-
ient example, have only 2 columns but aiiange to have
the CPU ask foi two successive tiansfeis to get a total
of 4 bytes.) In geneial, the CPU may manipulate in a
single step a datum as wide as the memoiy. Foi all of
the machines which we will considei, that maximum
datum size is 32 bits oi 4 bytes. While convention would have us call this biggest datum a worJ, histoiical
ieason has led both the VAX and MC68000 to call it a |ongworJ. Then, 2 bytes is eithei a |a|[worJ oi a worJ.
We will use the VAX/68000 notation (longwoid, woid, and byte) wheievei possible to simplify the ieading. To
load data fiom memoiy, the CPU sends the addiess and the datum size to the memoiy and gets the datum as
the ieply. To stoie data, the addiess is sent and then the datum and datum size.
Some machines iequiie that the datum be piopeily aligned with the stacking oidei of the memoiy columns
in Fig. 87.1. Thus, on the SPARC, a longwoid must have an addiess ending in 00 (xxx00 in Fig. 87.1), and a
woid addiess must end in 0. The piogiammei who aiianges to violate this iule will be gieeted with an address
error. Since the MC68000 has only two columns, it complains only if you ask foi woids oi longwoids with
odd addiesses. Successoi models of that chip (68020, 30, and 40), like the VAX, accept any addiess and have
the CPU iead two longwoids and do the piopei iepacking.
The instiuction explicitly specifes the size and indicates how the CPU should calculate the addiess. An
instiuction to load a byte, foi example, is LB, MOVE.B, oi MOVB on the SPARC, MC68000, and VAX,
iespectively. These aie followed immediately by an expiession which specifes an addiess. We will discuss how
to specify an addiess latei. Fiist, we must intioduce the concept of a iegistei.
The space foi holding data and woiking on it in the CPU is the regser se. Registeis aie a veiy impoitant
iesouice. Biinging data in fiom memoiy is quite sepaiate fiom any opeiations on that data. Data in memoiy
must fist be fetched, then acted upon. Data in iegisteis can be acted on immediately. Thus, the availability of
iegisteis to stoie veiy active vaiiables and inteimediate iesults makes a piocessoi inheiently fastei. In some
machines, most oi all of the iegisteis aie tied to specifc uses. The most pievalent example would be Intel`s
80x86 piocessois, which powei the ubiquitous PC. Such aichitectuies, howevei, aie consideied quite old-
fashioned. All of the machines that we aie consideiing aie of a type called genera| regser mat|nes in that they
have a laige gioup of iegisteis which may be used foi any puipose. The machines that we include have eithei
16 oi 32 iegisteis, with only a few tied to specifc machine opeiations.
Table 87.1 shows the geneial iegistei iesouices in the thiee machines. The SPARC is a little stiange. The
machine piovides eight g|o|a| iegisteis and then a wnJow ||nJ of 128 iegisteis which sits behind a fiame
FIGURE 87.1 Memoiy aiianged as 4 columns of bytes. The
binaiy addiesses aie shown in the two foimats widely used
in computeis. The illustiation shows only 32 bytes in a 4
3 8 aiiay, but a moie iealistic span would be 4 3 1,000,000
oi 4 3 4,000,000 (4 MB to 16 MB).
2000 by CRC Press LLC
which exposes 24 of the 128. A piogiam can ask the machine to iaise oi lowei the blind by 16 iegisteis. That
leaves an oveilap of eight between successive yanks oi iewinds. This aiiangement is called a mu||e oer|ang
regser se (MORS). If you think of staiting with iegistei i8 at the bottom and i31 at the top, a yank of 16 on
the blind will now have i49 at the top and i24 at the bottom. i24 to i31 aie shaied between the old set and
the new. To avoid having to keep tiack of which iegisteis aie showing, the set of 24 aie divided into what came
n fiom the last set, those that aie only |ota|, and those that will go ou to the next set. These names apply to
going towaid incieasing numbeis. In going the othei diiection, the ns of the cuiient set will become the ous
of the next set. Almost all othei machines keep theii iegisteis sciewed down to the local masoniy, but you will
see in a moment how useful a MORS can be. (Like othei useful but expensive accessoiies, the debate is always
on whethei it is woith it Patteison and Hennessy, 1989].)
Stuc|. Most subioutines defne a numbei of local vaiiables. NumbeiCount in C, foi example, defnes 10 local
vaiiables. While these local vaiiables will often be cieated and kept in iegistei, theie is always some need foi a
bit of memoiy foi each notaon o[ (call to) a subioutine. In the good old days," this local stoiage was often
tied to the block of code compiising the subioutine. Howevei, such a fxed block means that a subioutine could
nevei call itself oi be called by something that it called. To avoid that pioblem (and foi othei puiposes) a
memoiy stiuctuie called a sat| was invented which got its name because it behaved like the spiing-loaded
plate stack in a iestauiant. Basically, it is a |as-n-frs-ou (LIFO) stiuctuie whose top is defned by a pointei
(addiess) which iesides in a iegistei commonly called the sat| oner oi SP.
Heup. When a subioutine needs space to stoie local vaiiables, it acquiies that space on the stack. When the
subioutine fnishes, it ietuins that stack space foi use by othei ioutines. Thus, local vaiiable allocations live
and die with theii subioutines. It is often necessaiy to cieate a data stiuctuie which is passed to othei ioutines
whose lives aie independent of the cieating ioutine. This kind of stoiage must be independent of the cieatoi.
To meet this need, the |ea was invented. This is an expandable stoiage aiea managed by the system. You get
an allocation by asking foi it ma||ot (sruture_s:e) in C]. You get back a pointei to the allocation and the
ioutine can pass that pointei to any othei ioutine and then go away. When it comes time to dispose of the
allocation-that is, ietuin the space foi othei uses-the piogiam must do that actively by a deallocation call
[ree(oner) in C]. Thus, one function can cieate a stiuctuie, seveial may use it, and anothei one can ietuin
the memoiy foi othei uses, all by passing the pointei to the stiuctuie fiom one to anothei.
Both heap and stack piovide a mechanism to obtain laige (oi small) amounts of stoiage dynamically. Thus,
laige stiuctuies which aie cieated only at iun time need not have static space stoied foi them in piogiams that
aie stoied on disk noi need they occupy gieat chunks of memoiy when the piogiam does not need them.
Dynamic allocation is veiy useful and all modein HLLs piovide foi it.
Since theie aie two types of dynamic stoiage, theie must be some way to lay out memoiy so that unpiedictable
needs in eithei stack oi heap can be met at all times. The mechanism is simplicity itself. The piogiam is stuffed
into low addiesses in memoiy along with any static stoiage (e.g., globals) which aie declaied in the piogiam.
The entiie iemaining space is then devoted to dynamic stoiage. The heap staits iight aftei the piogiam and
TABLE 87.1 Geneial Registeis in the Thiee Machines
Reg Special Names Comments
MC68000 16 1 D0..D7
A0..A7
A(ddiess) iegistei opeiations aie 32 bits wide. Addiess geneiation uses A
iegisteis as bases. D (data) iegisteis allow byte, woid, and longwoid opeiations.
A7 is SP.
VAX 16 4 i0..i11
AP, FP, SP, PC
AP,FP,SP and PC hold the addiesses of the aigument block, the fiame, the stack
and the cuiient place in the piogiam, iespectively. All data instiuctions can
use any iegistei.
SPARC 32
(136)
4 zeio, g1..g7,
i0..i5, FP, RA,
l0..l7, o0..o5,
SP, o7
The 4 gioups of eight iegisteis compiise: global (g), incoming paiameteis (i),
local (l) and outgoing paiameteis (o). g0 is a haidwiied 0 as a data souice and
a wastebasket as a destination. The iegisteis aie aiianged as a window blind
(see text) with the g`s always visible and the otheis moveable in multiple
oveilapping fiames of 24.
The special iegisteis aie within the set of geneial iegisteis. Wheie a PC is not listed, it exists as a special iegistei and can be used
as an addiess when the piogiam uses rogram-re|ae addiessing.
2000 by CRC Press LLC
giows towaid highei addiesses; the stack goes in at the top of memoiy and giows down. The system is iesponsible
to see that they nevei collide (a sat| tras|). When it all goes togethei, it looks like Fig. 87.2 Aho et al., 1986].
Theie is one last tidbit that an assembly piogiammei must be awaie of in
looking at memoiy. Just as some human alphabets aie wiitten left to iight and
some iight to left (not to mention top to bottom), computei manufactuieis have
chosen to disagiee on how to aiiange woids in memoiy. The two schemes aie
called |g-enJan and ||e-enJan (aftei which end of a numbei goes in the
lowest-numbeied byte and also aftei a maivelous episode in Cu||er's Trae|s).
The easiest way to peiceive how it is done in the two systems is to think of all
numbeis as being wiitten in conventional oidei (left to iight), but foi big-endian
you stait counting on the uppei left of the page and on little-endian you stait
counting on the uppei iight (see Fig. 87.1). Since each chaiactei in a text block
is a num|er of length 1 byte, this easy desciiption makes big-endian text iead in
noimal oidei (left to iight) but little-endian text ieads fiom iight to left. Figuie
87.3 shows the sentence This is a sentence" followed by the two hexadecimal
(HEX) numbeis 01020304 and 0A0B0C0D wiitten to consecutive bytes in the
two systems. Why must we biing this up: Because anyone woiking in assembly
language must know how the bytes aie aiianged. Fuitheimoie, two of the systems
we aie consideiing aie big-endian and one (the VAX) is little-endian. Which is
the bettei system: Eithei one. It is having both of them that is a nuisance.
As you look at Fig. 87.3, undoubtedly you will piefei big-endian, but that is
only because it appeals to youi piejudices. In tiuth, eithei woiks well. What is
impoitant is that you be able to diiect youi piogiam to go fetch the item of
choice. In both systems, you use the lowest-numbeied byte to indicate the item
of choice. Thus, foi the numbei 01020304, the addiess will be 13. Foi the big-endian system, 13 will point to
the byte containing 04 and foi the little-endian system, it will point at the byte containing 01.
Figuie 87.3 contains a pioblem foi some computeis which we alluded to in the discussion of Fig. 87.1. We
have aiianged the bytes to be foui in a iow as in Fig. 87.1. That is the way that the memoiy is aiianged in two
of oui thiee machines. (In the 68000, theie aie only two columns.) A good way to look at the fetch opeiation
is that the memoiy always deliveis a whole iow and then the piocessoi must acquiie the paits that it wants
and then piopeily aiiange them. (This is the effect if not always the method.) Some piocessois-the VAX being
a conspicuous example-aie willing to undeitake getting a longwoid by fetching two longwoids and then
piecing togethei the paits that it wants. Otheis (in oui case, the 68000 and the SPARC) aie not so accommo-
dating. Those machines opt foi simplicity and speed and iequiie that the piogiam keep its data aligned. To
use one of those machines, you (oi the compilei oi assemblei) must ieaiiange Fig. 87.3 by inseiting a null byte
into Fig. 87.2. This modifcation is shown in Fig. 87.4. With this modifcation, all thiee machines could fetch
the two numbeis in one opeiation without ieaiiangement.
Look closely at the numbeis 01020304 and 0A0B0C0D in Fig. 87.4. Notice that foi both confguiations, the
numbeis iead fiom left to iight and that (visually) they appeai to be in the same place. Fuitheimoie, as pointed
out in the discussion of Fig. 87.3, the beginning" oi addiess of each of the numbeis is identical. Howevei, the
byte that is pointed at by the addiess is not the same and the inteinal bytes do not have the same addiesses.
Getting big-endian and little-endian machines in a conveisation is not easy. It pioves to be even moie muddled
than these fguies suggest. A delightful and cogent discussion of the whole issue is found in Cohen 1981].
The piincipal objective in this whole section has been accomplished if looking at Fig. 87.4 and given the
command to load a byte fiom location 0000 0019, you get the numbei 0B in the big-endian machine and 0C
in the little-endian machine.
If you aie not alieady familiai with stoiing stiuctuies in memoiy, look at the stiing (sentence) and ask how
those letteis get in memoiy. To begin with, eveiy typeable symbol and all of the unpiintable actions such as
tabbing and caiiiage ietuins have been assigned a numeiical value fiom the SCII toJe. Each assignment is a
byte-long numbei. What This" ieally looks like (HEX, left to iight) is 54 68 69 73. The spaces aie HEX 20;
the peiiod 2E. With the alignment null byte at the end, this list of chaiacteis foims a piopei C stiing. It is a
stiuctuie of 20 bytes. A stiuctuie of any numbei of bytes can be stoied, but fiom the assembly point of view,
FIGURE 87.2 Layout of a
piogiam, static stoiage, and
dynamic stoiage in memoiy.
2000 by CRC Press LLC
it is all just a list of bytes. You may access them two at a time, foui at a time, oi one at a time. Any inteipietation
of those bytes is entiiely up to the piogiam. Unlike the HLL which iequiies that you tell it what each named
vaiiable is, assembly language knows only bytes and gioups of bytes. In assembly language, the T" can be
thought of as a lettei oi the numbei 54 (HEX). Youi choice. Oi, moie impoitantly, youi piogiam`s choice.
Addressing
Now that we have both memoiy and addiesses, we should next considei how these piocessois iequiie that
piogiammeis specify the data that is to be acted upon by the instiuctions.
All of these machines have multiple modes of addiess. The VAX has the biggest vocabulaiy; the SPARC the
smallest. Yet all can accomplish the same tasks. Foui geneial types of addiess specifcation aie quite common
among assembly languages. These aie shown in Table 87.2. They aie spelled out in woids in the table, but theii
usage is ieally developed in the examples which follow in this and the succeeding sections.
In Table 87.2, foimats 1.4 and 1.5 and the entiies in 4 iequiie some expansion. The otheis will be cleai in
the examples we will piesent. Base-index addiessing is the mechanism foi dealing with subsciipts. The base
points at the staiting point of a data stiuctuie, such as a stiing oi a vectoi; the index measuies the offset fiom
the stait of the stiuctuie to the element in question. Foi most machines, the index is simply a sepaiate iegistei
which counts the bytes fiom the base to the item in question. If the items in the list aie 4 bytes long, then to
inciement the index, you add 4. While that is not haid to iemembei, the VAX does its multiplication by the
item length foi you. Fuitheimoie, it allows you to index any foim of addiess that you can wiite. To show you
what that means, considei expanding numbeis stoied in woids into numbeis stoied in longwoids. The extension
is to pieseive sign. The VAX piovides specifc instiuctions foi conveisions. If we weie moving these woids in
one aiiay to longwoids in anothei aiiay, we would wiite:
FIGURE 87.3 Byte numbeiing and numbei placement foi big- and little-endian systems. Hexadecimal numbeis aie used
foi the memoiy addiesses.
FIGURE 87.4 The same items as in Fig. 87.3, but with justifcation of the long integeis to begin on a longwoid boundaiy.
2000 by CRC Press LLC
CVTWL (i4)i5],(i6)i5] ;conveit the woids staiting at (i4) to longwoids staiting at (i6)
Note that the same index, i5], is used foi both aiiays. On the left, the contents of i5 aie multiplied by 2
and added to i4 to get the addiess; on the iight, the addiess is i54-i6. You would be saying: Conveit the 4th
woid to the 4th longwoid." This is undoubtedly compact and sometimes convenient. It is also unique to the VAX.
Foi the 68000, the designeis folded both base-displacement and base-index into one mode and made ioom
foi woid oi longwoid indices. It looks like:
add.1 64(A3,D2.w),D3 ;addiess (A3-64) -sign-extended(D2)
The 68000 limits the displacement to a signed byte, but othei than that, it is indeed a iathei geneial indexing
foimat. If you do not want the displacement, set it to 0.
Foi the poweiful but simple SPARC, the simple base-index foim shown in 1.4 is all that you have (oi need).
The double-indiiect foimat, 1.5, is so iaiely used that it has been left out of almost all designs but the VAX.
What makes it occasionally useful is that subioutines get pointeis to pass by pointei" vaiiables. Thus, if you
want to get the vaiiable, fist you must load the addiess and then the vaiiable. The VAX allows you to do this
in one instiuction. While that sounds compact, it is expensive in memoiy cycles. If you want to use that pointei
again, it pays to have it in iegistei.
The two items undei heading 4 aie stiange at fist. Theii piincipal function is adding items to and iemoving
them fiom a dynamic stack, oi foi C, to execute the opeiation X-- oi (- -X). The action may be viewed
with the code below and the illustiation of memoiy in Fig. 87.2:
movl i4, -(sp) ;make ioom on the stack (subtiact 4 fiom SP) and put
the contents of i4 in that spot
movl (sp)-, i4 ;take a longwoid off the stack, shoiten the stack by 4
bytes, and put the longwoid in i4
RISCs abhoi instiuctions which do two unielated things. Instead of using a dynamic stack, they use a quasi-
static stack. If a subioutine needs 12 bytes of stack space, it explicitly subtiacts 12 fiom SP. Then it woiks fiom
theie with the base-displacement foimat (1.3) to iefeience any place in the block of bytes just defned. If you
want to use a pointei and then inciement the pointei, RISCs will do that as two independent instiuctions.
Let us considei one shoit section of MC68000 code fiom oui sample piogiam in C to see how these modes
woik and to sample some of the avoi of the language:
TABLE 87.2 Addiessing Modes
1. Explicit addresses Example
1.1. Absolute addiessing 765 The actual addiess wiitten into the instiuction.
1.2. Registei indiiect (i3) Meaning the addiess is in iegistei 3."
1.3. Base-displacement -12(i3) Meaning 12 bytes befoie the addiess in iegistei 3."
1.4. Base-index (i3,i4) Meaning make an addiess by adding the contents of i3 and i4. This mode has many
vaiiations which aie discussed below.
1.5. Double indiiect C5(i4) Veiy uncommon! Means calculate an addiess as in 1.3, then fetch the longwoid
theie, and then use it as the addiess of what you ieally want.
2. Direct data specincation
2.1. Immediate/liteial #6 oi 6 Meaning use 6 as the datum." In machines which use #6, 6 without # means addiess
6. This is called absolute addiessing."
3. Program-relative
3.1. Labels loop: The label (typically an alphanumeiic ending in a colon) is a maikei in the piogiam
which the assemblei and linkei keep tiack of. The common uses aie to jump to a
labeled spot oi to load labeled constants stoied with the piogiam.
4. Address-modifying forms
(CISC only)
4.1. Postinciement (sp)- Same as 1.2 except that, aftei the addiess is used, it is inciemented by the size of the
datum in bytes and ietuined to the iegistei fiom which it came.
4.2. Piedeciement -(sp) The value in SP is deciemented by the size of the datum in bytes, used as the addiess
and ietuined to the iegistei fiom which it came.
2000 by CRC Press LLC
;ees ((souice'E') (souice'e')) && inside && !latche;
CMPI.B #$45,(A4) ; 'E' compaie immediate" liteial hex 45,
what A4 points at
BEQ fist ; bianch if equal" to label fist
CMPI.B #$65,(A4) ; 'e' compaie immediate" liteial hex 65,
what A4 points at
BNE second ; bianch if not equal" to label second
fist:
TST.W D6 ; test woid" (subtiact 0) D6 ('inside`)
BEQ second ; bianch if equal" to label second
TST.W D3 ; test woid" (subtiact 0) D3 ('latche`)
BEQ thiid ; bianch if equal" to label thiid
second:
MOVEQ #00,D0 ; move quick" liteial 0 to D0
BRA fouith ; bianch always" to label fouith
thiid:
MOVEQ #$01,D0 ; move quick" liteial 1 to D0
fouith:
MOVE.W D0,-6(A6) ; move woid" fiom D0 to -6(FP)
Theie aie all soits of little details in this shoit example. Foi example, a common way to indicate a comment
is to stait with a ;". The assemblei will ignoie youi comments. The #" indicates a liteial, and the $" that the
liteial is wiitten in hexadecimal notation. The VAX would use #^x to expiess the same idea. Compaie" means
subtiact but save only the condition codes of the iesult" ( oi oer[ow, n oi negae, : oi :ero, and t oi tarry).
Thus, the fist two lines do a subtiaction of whatevei A4 is pointing at (souice) fiom the ASCII value foi 'E`
and then, if the two weie equal (the iesult, zeio), the piogiam jumps to line 5. If souice is not 'E`, then it
simply goes to the next line, line 3. The instiuction, TST.W D6, is quite equivalent to CMPI.W D6, #0, but the
TST instiuction is inheiently shoitei and fastei. On a SPARC, wheie it would be neithei shoitei noi fastei, TST
does not exist.
Exactly what the assemblei oi linkei does to ieplace the label iefeiences with piopei addiesses, while
inteiesting, is not paiticulaily geimane to oui cuiient topic. Note that the iange of the bianch is somewhat
limited. In the 68000, the maximum bianch is 32K and in the VAX a meie -127 to -128. If you need to go
fuithei, you must combine a bianch with a jump. Foi example, if you weie doing BEQ failabel, you would
instead do:
BNE neailabel
jmp failabel ; this instiuction can go any distance
neailabel:
Follow thiough the example above until the shoit steps of logic and the addiessing modes aie cleai. Then
piogiess to the next section wheie we use the addiessing modes to intioduce the geneial topic of subioutine
calling conventions.
Ca!!ing Cunventiuns
Whenevei you invoke a subioutine in a HLL, the calling ioutine (ta||er) must pass to the called ioutine (ta||ee)
the paiameteis that the subioutine iequiies. These paiameteis aie defned at compile time to be eithei ass-
|y-a|ue oi ass-|y-oner (oi ass-|y-re[erente), and they aie listed in some paiticulai oidei. The convention
foi passing the paiameteis vaiies fiom aichitectuie to aichitectuie and HLL to HLL, but basically it always
consists of building a ta|| ||ot| which contains all of the paiameteis and which will be found wheie the iecipient
expects to fnd it.
Along with the passing of paiameteis, foi each system, a convention is defned foi iegistei and stack use
which establishes:
2000 by CRC Press LLC
Which iegisteis must be ietuined fiom callee to callei with the same contents that the callee ieceived
(such iegisteis aie said to be resereJ atross a ta||)
Which iegisteis may be used without woiiying about theii contents (such iegisteis aie called strat|
regsers)
Wheie the ietuin addiess is to be found
Wheie the value ietuined by a function will be found
The convention may be suppoited by haidwaie oi simply a gentlemanly iule of the ioad. Howevei the iules
come into being, they defne the steps which must be accomplished coming into and out of a subioutine. The
whole collection of such iules foims the ta||ng tonenon foi that machine. In this section, we look at oui
thiee diffeient machines to see how all accomplish the same tasks but by iathei diffeient mechanisms.
The two CISCs do almost all of theii passing and saving on the stack. The call block will be built on the
stack; the ietuin addiess will be put on the stack; saved iegisteis will be put on the stack. Only a few stack
iefeiences aie passed foiwaid in iegistei; the value ietuined by the function will be passed back in iegistei.
How diffeient is the SPARC! The paiameteis to be passed aie placed in the ou iegisteis (six aie available
foi this puipose). Only the oveiow, if any, would go on the stack. In geneial, iegisteis aie saved by window-
blinding iathei than moving them to the stack. On ietuin, data is ietuined in the n iegisteis and the iegisteis
iestoied by ieveise window-blinding.
MC68000 Cu|| und Return. Let us look at the details foi two of the machines. We stait with the 68000, because
that is the most open and conventional." We continue with the function NumbeiCount. Only a single paiametei
must be passed-the pointei to the text block. The HLL callee sees NumbeiCount(block) as an integei (i.e.,
what will be ietuined), but the assembly piogiam must do a call and then use the ietuined integei as instiucted.
A typical assembly ioutine would be:
MOVE.L A2,~{SP , move pointer to block onto the stack
JSR NumberCount , save return address on the stack and start
, executing NumberCount
, do something with value returned in D0
The fist instiuction puts the pointei to the block, which is in A2, on the stack. It fist must make ioom, so
the -" in -(A7) fist subtiacts 4 fiom A7 (making ioom foi the longwoid) and then moves the longwoid into
the space pointed to by the now-modifed A7. The one instiuction does two things: the deciementing of SP
and the stoiing of the longwoid in memoiy.
MOVE.L A2,~{A7 A7 : A7~4 ,A7 = SP
M{A7 : A2 ,M{x = memory{address x
The next instiuction, ,um su|roune (JSR), does thiee things. It deciements SP (i.e., A7) by 4, stoies the ietuin
addiess on the top of the stack, and puts the addiess of NumbeiCount in the rogram touner. We have just
intioduced two items which need specifc defnition:
Retuin addiess (RA): This will always be the addiess of the instiuction which the callee should ietuin to.
In the 68000 and the VAX (and all othei CISCs), the RA points to the fist instiuction
aftei the JSR. In the SPARC and almost any RISC, RA will point to the second
instiuction aftei JSR. That cuiious diffeience will be discussed latei.
Piogiam countei (PC): This iegistei (which is a genera| regser on the VAX but a special iegistei on the
othei machines) points to the place (memoiy location) in the machine language
instiuction stieam wheie the piogiam is cuiiently opeiating. As each instiuction is
fetched, the PC is automatically inciemented. The action of the JSR is to save the
last veision of the PC-the one foi the next fetch-and ieplace it with the staiting
addiess of the ioutine to be jumped to.
Summing up these tiansactions in algebiaic foim:
2000 by CRC Press LLC
JSR NumberCount SP : SP~4 ,A7 = SP
M{SP : PC ,M{x = memory{address x
PC : address of NumberCount
Should you wondei how the addiess of NumbeiCount gets in theie, the |n|er, which assigns each section of
code to its piopei place in memoiy and theiefoie knows wheie all the labels aie, will inseit the piopei addiess
in place of the name.
This completes the call as fai as building the call block, doing the call itself, and picking up the iesult. Had
theie been moie paiameteis to pass, that fist instiuction would have been ieplicated enough times to us| all
of the paiameteis, one at a time, onto the stack. Now let us look at the conventions fiom the point of view of
the callee. The callee has moie woik.
When the callee picks up the action, the stack and iegisteis aie as shown in Figuie 87.5. With the exception
of D0 and A7, the callee has no iegisteis . . . yet. The callee must make ioom foi local vaiiables in eithei iegistei
oi memoiy. If it wants to use iegisteis, it must save the usei`s data fiom the iegisteis. The subioutine can get
whatevei space it needs on the stack. Only aftei the setup will it get down to woik. The entiie section of stack
used foi local vaiiables and saving iegisteis is called the callee`s fiame. It is useful to have a pointei (FP) to the
bottom of the fiame to piovide a static iefeience to the ietuin addiess, the passed paiameteis, and the
subioutine`s local vaiiable aiea on the stack. In the 68000, the convention is to use A6 as FP. When oui ioutine,
NumbeiCount, begins, the addiess in A6 points to the stait of the callei`s fiame. The fist thing the callee must
do is to establish a local fiame. It does that with the instiuction LINK.
Typical of a CISC, each instiuction does a laige piece of the action. The whole entiy opeiation foi the 68000
is contained in two instiuctions:
LINK A6,#$FFF8
MOVEM.L D3~D7/A4,~{SP
The fist instiuction does the fiame making; the second does the saving of iegisteis. Theie aie multiple steps
in each. Each double step of deciementing SP and moving a value onto the stack will be called a us|. The
steps aie as follows:
LINK A6,#$FFF8 ,push A6 {A7 A7~4, M{A7 A6
,move A7 to A6 {SP to FP
,add FFF8 {~8 to SP {4 words for local variables
FIGURE 87.5 The stack aiea of the 68000`s memoiy and the iegistei assignments that the called subioutine sees as it is
enteied at the top. The iegisteis all hold longwoids, the size of an addiess. In typical PC/Macintosh compileis, integeis aie
defned as 16-bit woids. Accoidingly, the stack aiea of memoiy is shown as woids, oi half the width of a iegistei.
2000 by CRC Press LLC
MOVEM.L D3~D7/A4,~{A7 ,push 5 data registers {3..7 and l address
,register {A4
At this point, the stack looks like Fig. 87.6.
The subioutine is piepaied to pioceed. How it uses those fiee iegisteis and the woiking space set aside on
the stack is the subject of the section on optimization in this chaptei. Foi the moment, howevei, we simply
assume that it will do its thing in exemplaiy fashion, get the count of the numbeis, and ietuin. We continue
in this section by consideiing the iathei simple tiansaction of getting back.
The callee is obliged to put the answei back wheie the callei expects to fnd it. Two paiadigms foi ietuin
aie common. The one that oui compilei uses is to put the answei in D0. The othei common paiadigm is to
put the answei back on the stack. The usei will have left enough ioom foi that answei at FP-8, whethei oi
not that space was also used foi tiansfeiiing paiameteis in. Using oui paiadigm, the ietuin becomes:
MOVE.W $FFFC{A6,D0 ,answer from callee's stack frame ~4{FP to D0
MOVEM.L {A7+,D3~D7/A4 ,registers restored to former values
UNLK A6 ,SP FP, FP M{SP, SP SP+4
RTS ,PC M{SP, SP SP+4
When all of this has tianspiied, the machine is back to the callei with SP pointing at ||ot|. The iegisteis
look like Fig. 87.5 except foi two impoitant changes. SP is back wheie the callei left it and D0 contains the
answei that the callei asked foi.
Transactiuna! Paradigms
The fnal topic in this section is the desciiption of some of the tianslations of the simple and oidinaiy phiases
of the HLLs into assembly language. We will show some in each of oui thiee machines to show both the
similaiities and slightly diffeient avois that each machine aichitectuie gives to the tianslation.
The paiadigms that we will discuss compiise:
Aiithmetic
Replacement
Testing and bianching, paiticulaily multiple Boolean expiessions
Stepping thiough a stiuctuie
FIGURE 87.6 The stack aiea of the 68000`s memoiy and the iegistei situation just aftei MOVEM has been executed. The
memoiy aiea between the two aiiows is the subioutine`s [rame.
2000 by CRC Press LLC
Many studies have shown that most computei aiithmetic is conceined with addiessing, testing, and indexing.
In NumbeiCount theie aie seveial examples of each. Foi example, neai the bottom of the piogiam, theie aie
statements such as:
count++,
Foi all thiee machines, the basic tianslation is the same: Add an mmeJae (a constant stoied iight in the
instiuction) to a numbei in iegistei. Howevei, foi the CISCs, one may also ask that the numbei be biought in
and even put back in memoiy. The thiee tianslations of this paii compiise:
MC68000 VAX SPARC
ADDQ.W #$1,$FFFE(A6) INCL R0 add %o2,1,%o2
Typical of the VAX, it makes a special case out of adding 1. Theie is no essential diffeience in asking it to add
1 by saying 1," but if one has a special instiuction, it saves a byte of piogiam length. With today`s inexpensive
memoiies, a byte is no longei a big deal, but when the VAX fist emeiged (1978), they weie deliveied with less
memoiy than a PC oi Mac would have today. The VAX, of couise, can say ADDL #1, i0 just like the 68000,
and foi any numbei othei than 1 oi 0, it would. Note also that the VAX compilei chose to keep toun in iegistei,
while in Think C decided to put it on the stack (-2(SP)). A RISC has no choice. If you want aiithmetic, youi
numbeis must be in iegistei. Howevei, once again, we aie ieally talking about the length of the code, not the
speed of the tiansaction. || tiansactions take place fiom iegisteis. The only issues aie whethei the piogiammei
can see the iegisteis and whethei a single instiuction can include both moving the opeiands and doing the
opeiand aiithmetic. The RISC sepaiates the addiess aiithmetic (e.g., -2(SP)) fiom the opeiand aiithmetic,
putting each in its own instiuction. Both get the job done.
The next items we listed weie re|atemen and esng anJ |rant|ng. We have both within the statement:
digit = {*source >= `0` && {*source <= `9`,
The tianslation iequiies seveial statements:
MC68000 VAX SPARC
MOVE.B (A4),D3 clib i1 add %g0,0,%o1
CMPI.B #$30,D3 cmpb C4(ap),#48 ldsb %o2],%o0
BLT ZERO blss ZERO subcc %o0, 47,%g0
CMPI.B #$39,D3 cmpb C4(ap),#57 ble ZERO
BLE ONE bgti ZERO nop
incb i1 subcc %o0,57,%g0
bg ZERO
nop
add %g0, 1,%o1
add %o1,0,%l3
ZERO: ZERO: ZERO:
MOVEQ #$00,D0
BRA DONE
ONE:
MOVEQ #$01,D0
DONE:
MOVE.W D0,$FFF6(A6)
To begin with, all thiee do ioughly the same thing. The only noticeable diffeience in concept is that the
SPARC compilei chose to compaie the incoming chaiactei (souice) to 47 (the chaiactei befoie '0`) and then
bianch if the iesult showed the lettei to be less than oi equal," while the othei two compaied it to '0` as asked
and then bianched if the iesult was less than." No big deal. But let us walk down the seveial columns to see
the specifc details. Piioi to beginning, note that all thiee must biing in the chaiactei, iun one oi two tests,
and then set an integei to eithei :ero (false) oi no :ero (tiue). Also, let it be said that each snatch of code is
2000 by CRC Press LLC
puipoitedly optimized, but at least with the small sample that we have, it looks as if each could be bettei. We
begin with thiee paiallel walkdowns. Notes as needed aie piovided below.
MC68000 VAX SPARC
chaiactei fiom M D# Set (byte) DIGIT to 0 Set (byte) DIGIT to 0
Is (D3-'0') <0: Is (souice-'0') <0: chaiactei fiom Mf out1
If <, bianch to label ZERO If <, bianch to ZERO Is (souice-'/') <0:
Is (D3-'9')<0: Is (souice-'9') <0: If <, bianch to ZERO
If <. bianch to label ONE If neithei, bianch to ZERO Is (souice-'9') <0:
Add (byte) 1 to DIGIT If neithei, bianch to
ZERO: ZERO: Add 1 to DIGIT
Put a longwoid 0 in D0 ZERO:
Bianch to label DONE
ONE:
Put a longwoid 1 in D0
DONE:
Put value in D0 into DIGIT
Notes:
1. Moving the chaiactei into iegistei to compaie it with '0` and '9`:
a. The fist 68000 line moves the next chaiactei as a |ye into iegistei D3. The othei 3 bytes will be
ignoied in the byte opeiations. Remembei that the piogiam had alieady moved the pointei to the
stiing into A4.
b. The SPARC does the same soit of thing with a pointei in %o2, except with the diffeience that it sign-
extends the byte to a longwoid. Sign extension simply copies the sgn-| into the highei-oidei bits,
effectively making 3E into 0000 003E oi C2 into FFFF FFC2. That is what the mnemonic means:
LoaD Signed Byte."
c. The VAX compilei takes a totally diffeient appioach-a iathei pooi one, actually. It leaves not only
the byte in memoiy but even the pointei to the byte. Thus, eveiy time it wants to iefei to the byte-and
it does so numeious times-it must fist fetch the addiess fiom memoiy and then fetch the byte itself.
This double memoiy iefeience is what C4(ap) means: At the addiess you will fnd at addiess 4(ap)."
The only thing that makes all this appaient coming and going even iemotely acceptable is that the
VAX will cache (place in a fast local memoiy) both the addiess and the chaiactei the fist time that
it gets them. Then, it can iefei to them iapidly again. Cache iefeiences, howevei, aie not as fast as
iegistei iefeiences.
2. Testing the chaiactei:
The next line (3id foi the SPARC) does a compaiison between 48 (oi 47) and the chaiactei. Comare
is an instiuction which subtiacts one opeiand fiom the othei, but instead of putting the iesults some-
wheie, it stoies only the facts on whethei the opeiation deliveied a negative numbei oi zeio oi iesulted
in eithei an oveiow oi a caiiy. These aie stoied in ags, single bits associated with the aiithmetic unit.
The bits can contain only one iesult at a time. The 68000 and VAX must test immediately aftei the
compaiison oi they will lose the bits. The SPARC changes the bits only when the instiuction says so
(the CC on the instiuction - change condition codes"). Thus, the subtiaction can be iemote fiom the
test-and-bianch.
The SPARC is explicit about wheie to stoie the subtiaction-in %g0. %g0 is a pseudo-iegistei. It is
always a 0 as a souice and is a gaibage can as a destination. The availability of the 0 and the gaibage
can seives all the same functions that the special instiuctions foi zeios and compaiisons do on the CISCs.
3. The diffeiences in the algoiithm to do the tests:
Theie aie two diffeient paiadigms expiessed in these thiee examples. One says: Figuie out which thing
you want to do and then do that one thing." The othei says: Fiist set the iesult false and then fguie
out if you should set it tiue." While the second veision would seem to do a lot of unnecessaiy settings
to zeio, the othei algoiithm will execute one less bianch. That would make it ioughly equivalent.
2000 by CRC Press LLC
Howevei, the 68000 algoiithm is defnitely longei-uses moie memoiy foi code. That is not ieally much
of an issue, but why put the iesult fist into a tempoiaiy iegistei and then wheie you ieally want it:
Cumpi!er Optimizatiun and Assemb!y Language
Cumpi!er Operatiuns
To undeistand the optimizing featuies of compileis and theii ielation to assembly language, it is best to
undeistand some of the choies foi the compilei. This section examines vaiiable allocation and how it can be
optimized, and the optimization task of constant expiession iemoval. Examples of how compileis peifoim
these opeiations aie taken fiom the vaiious systems used in the aiticle.
VurIuh|e A||vcutIvn. Vaiiables in high-level languages aie an abstiaction of memoiy cells oi locations. One
of the compilei`s tasks is to assign the abstiact vaiiables into physical locations-eithei iegisteis within the
piocessoi oi locations within memoiy. Assignment stiategies vaiy, but an easy and often-used stiategy is to
place a|| vaiiables in memoiy. Easy, indeed, but wasteful of execution time in that it iequiies memoiy fetches
foi all HLL vaiiables. Anothei assignment stiategy is to assign as many vaiiables to the iegisteis as possible and
then assign any iemaining vaiiables to memoiy; this method is typically suffcient, except when theie is a
limited numbei of iegisteis, such as in the 68000. In these cases, the best assignment stiategy is to assign iegisteis
to the vaiiables which have the gieatest use and then assign any iemaining vaiiables to memoiy. In examining
the compileis and aichitectuie used in this aiticle, we fnd examples of all these methods.
In the unoptimized mode, VAX and Spaic compileis aie among the many which take the easy appioach and
assign vaiiables only to memoiy locations. In Figs. 87.6 and 87.7, the vaiiable assignments aie piesented foi
the unoptimized and optimized options. Note that only one oi two iegisteis aie used, both as sciatch pads, in
the unoptimized option, wheieas the optimization assigns iegisteis to all vaiiables. The expected execution
time savings is appioximately 42 of the 50 memoiy iefeiences pei loop iteiation. That does not include
additional savings caused by compact code. Detailed compaiisons aie not piesented since the inteipietation of
aichitectuial compaiisons is highly subjective.
Unlike the VAX and Spaic compileis, the 68000 compilei assigns vaiiables to iegisteis in both the unoptimized
and unoptimized options; these assignments aie depicted in Figs. 87.7 and 87.8. Since theie aie only eight
geneial-puipose data iegisteis in the 68000 and two aie assigned as sciatch pads, only six of the piogiam`s ten
FIGURE 87.7 The stack aiea of the 68000`s memoiy and the iegistei assignments that the Think C compilei made with
global optimization urneJ o[[. The stack is shown just aftei the MOVEM instiuction. The items in bold aie as they would
be aftei that instiuction. While the iegisteis all hold longwoids, in typical PC/Macintosh compileis, integeis aie defned as
woids. This fguie is the piogiammatic successoi to Fig. 87.6.
2000 by CRC Press LLC
vaiiables can be assigned to iegisteis. The question is how the 68000 compilei chose which vaiiables to assign
to iegisteis and which to leave in memoiy. As might be expected, the compilei assigned iegisteis based on theii
usage foi the unoptimized option as well as the optimized. The exact counting stiategy is unknown. Howevei,
a faii estimate, which yields the same assignment as the compilei, is to count only the vaiiable`s usage in the
loop-the likely place foi the piogiam to spend most of its execution time. Theie aie othei ways to estimate
the vaiiable usage such as assigning weights to a iefeience based on its placement within a loop, its placement
within a conditional (if-then-else) statement, etc. These estimates and how they aie applied within a compilei
can change the vaiiable allocation as well as the effciency of the code.
In the optimized case, a slightly diffeient iegistei assignment is used. This is because the optimizei cieated
anothei chaiactei vaiiable-souice-which it assigned to a iegistei. The motivation foi its cieation and its
assignment to a iegistei is shown in the next section on constant expiession iemoval.
Even though the assignment of vaiiables to iegisteis gives an impiovement in peifoimance, it is not always
possible to assign a vaiiable to a iegistei. In C, one opeiation is to assign the addiess of a vaiiable to a pointei-
type vaiiable (e.g., ip & i). If i weie assigned to a iegistei, the opeiation would become invalid, because a
iegistei does not have a memoiy addiess. Although this example appeais limited to the C language, the use of
a vaiiable`s addiess is widespiead when subioutine paiameteis aie passed by iefeience. (Vaiiables sent to a
subioutine aie eithei passed by iefeience, wheie the addiess of the vaiiable is passed to the subioutine, allowing
modifcations to the oiiginal vaiiable, oi they aie passed by value, wheie a copy of the vaiiable is passed to the
subioutine.) When a paiametei is passed by iefeience its addiess must be obtained and passed to the subioutine,
an action commonly found in most languages. This action compounds the task of selecting candidate vaiiables
foi iegistei assignment.
Cvnstunt ErpressIvn Remvvu|. The task of allocating piogiam vaiiables to physical locations is accomplished
by all compileis; we have shown that theie aie many ways to achieve this goal with vaiying ease oi iun-time
peifoimance. This section exploies a compilei task which is done stiictly foi optimization-the iemoval of
constant expiessions. In exploiing this task, we show stiategies foi the iecognition of this optimization and
also some caveats in theii application.
Constant expiessions aie subexpiessions within a statement which aie unchanged duiing its execution. An
obvious example is the expiession etor]x] in the following conditional statement.
if {vectorx < l00 && {vectorx > 0 then ...
FIGURE 87.8 The stack aiea of the 68000`s memoiy and the iegistei assignments that the Think C compilei made with
global optimization urneJ on. The stack is shown just aftei the MOVEM instiuction. The items in bold aie as they would
be aftei that instiuction. This fguie should be compaied with Fig. 87.7.
2000 by CRC Press LLC
An astute codei who does not tiust compileis would not allow two memoiy iefeiences foi vectoix] in the
same conditional statement and would iewiite the code by assigning vectoix] to a tempoiaiy vaiiable and
using that vaiiable in the conditional. An astute compilei would also iecognize the constant expiession and
assign vectoix] to a sciatch pad iegistei and use this iegistei foi both compaiisons. This type of optimization,
wheie small sections of code (typically one souice line) aie optimized, is called ee-|o|e optimization.
Within the example piogiam, the assignment statement which checks if the chaiactei is a digit within the
iange fiom '0` to '9` is a statement which can beneft fiom this type of optimization. The C code lines, with
the unoptimized and optimized SPARC assembly code, aie listed below. Note that in addition to the constant
expiession iemoval the optimized code also assigns vaiiables to iegisteis.
digit = {*source >= '0' && {*source <= '9' ,
In tianslating this line on the SPARC, Fig. 87.9 shows the 32 iegisteis visible at any moment in the window-
blinding SPARC. The top 24 shift by 16 in a noimal call. The eight globals iemain the same. The shift of the
iegisteis is accompanied by copying SP to o6 and the call instiuction puts the ietuin addiess into o7. Accoidingly,
a call wipes out the callei`s o7. Registei g0 seives as a 0 (as a souice) and as a wase|as|e (as a destination). |J
loads a longwoid, and |Js| sign-extends a byte into a longwoid. The instiuction aftei a bianch is executed
whethei the bianch is taken oi not (delayed bianching). An instiuction such as add 47, %g0, %o2 adds a
constant to 0 and puts it in the iegistei. This is equivalent to move.l #47, d4 on the 68000. An aJJ oi su| with
tt appended changes the condition codes. To do a tomare, one uses aJJtt oi su|tt and puts the iesult in g0
(the wastebasket).
FIGURE 87.9 SPARC
iegistei assignments.
2000 by CRC Press LLC
The same type of constant expiession can be found and iemoved with a global peispective, typically fiom within
loops. A simple example is the best way to desciibe how they can be iemoved and to offei some caveats when the
compilei cannot see the global pictuie. The following example code updates each element in the vectoi by adding
the fist element scaled by a constant y. An obseivation shows that the subexpiession, vectoi0] y, is constant
thioughout all executions of the loop. An obvious impiovement in code is to calculate the pioduct of vectoi0]
y outside of the loop and stoie its value in a tempoiaiy vaiiable. This is done in the second example.
Constant expression present Constant expression removed
foi( i 0 ; i < size; i--) temp vectoi0] y ;
{ vectoii] vectoii] - (vectoi0] y); foi( i 0 ; i < size; i--) { vectoii] vectoii] - temp ;
Ideally, the compilei should fnd and iemove these constant expiessions fiom loops, but this is not as obvious
as it may seem. Considei the above example if the following line weie inseited in the loop:
y = vectori~l + vectori+l
If each souice line is taken in isolation, y appeais constant, but y is dependent on the loop index i. Hence befoie
iemoving constant expiessions, the compilei must map the dependencies of each vaiiable on the othei vaiiables
and the loop index. Additionally, othei not-so-obvious dependencies-such as when two pointeis modify the
same stiuctuie-aie diffcult to map and can iesult in eiioneous object code. This is one of the diffculties in
optimizing compilei opeiation and why its extent is limited.
A subtle example foi constant expiession iemoval is found in oui sample piogiam in the iefeience to souice.
In these statements, the chaiactei iefeienced (addiessed) by souice is obtained fiom memoiy. The pointei
(addiess) sourte is changed only at the bottom of the loop and the memoiy contents addiessed by sourte aie
static. A global optimization should obtain the chaiactei once at the top of each pass of the loop and save on
subsequent memoiy iefeiences thioughout. The 68000 C compilei with the optimization option deteimined souice
to be constant thioughout the loop and assigned iegistei D3 to hold its contents. This saved seven of the eight
memoiy accesses to souice in each loop pass. The unoptimized 68000 option, the SPARC, and the VAX compileis
did not use global constant expiession iemoval and must fetch the opeiand fiom memoiy befoie its use.
The Prvh|ems. With optimization yielding moie effcient code iesulting in impioved system peifoimance,
why would you not use it: Oui favoiite, among the seveial ieasons, is the following quote fiom compilei
documentation: Compiling with optimization may pioduce incoiiect object code." Pioblems aie caused by
assumptions used by the compilei which aie not held by the piogiammei. Foi example, an optimization which
assumes that memoiy contents do not change with time is eiioneous foi multi-tasking systems which shaie
memoiy stiuctuies and also foi memoiy- mapped I/O devices, wheie memoiy contents aie changed by exteinal
events. Foi these cases, the data in iegistei may not match the newei data in memoiy.
Additionally, HLL debuggeis do not always woik well with the optimization option since the one-to-one
coiiespondence between HLL code and the object code may have been iemoved by optimization. Considei the
ieassignment of souice to a data iegistei which is peifoimed by the 68000 C compilei. If a debuggei weie to
modify the contents of souice, then it would have to know about the two locations wheie it is stoied: the
memoiy and the iegistei. Othei types of optimizations which may cause pioblems aie when unneeded vaiiables
aie iemoved oi when code is iesequenced. If a HLL debuggei tiies to single-step thiough HLL code, theie may
not be coiiesponding assembly code, and its execution will appeai eiioneous.
High-Leve! Language and Assemb!y Language Re!atiuns
In compaiing the vaiious assembly languages fiom the compilei-geneiated code, we have not piesented a full
vocabulaiy of assembly languages oi the minutiae of the undeilying machines. Exploiing only the code geneiated
by the compileis may lead one to believe that all assembly languages and piocessoi aichitectuies aie pietty
much the same. This is not ieally the case. What we have shown is that compileis typically use the same assembly
language instiuctions iegaidless of the undeilying machines. The compilei wiitei`s motivation foi this appaient
similaiity is not because all aichitectuies aie the same, but because it is diffcult-aiguably even nonpioduc-
tive-foi the compilei to take advantage of the complex featuies which some CPU aichitectuies offei. An
aigument may be made that compileis geneiate the best code by developing code iathei independently of the
2000 by CRC Press LLC
undeilying aichitectuie. Only in the fnal stages of code geneiation is the undeilying platfoim`s haidwaie
aichitectuie specifcally consideied see Aho et al., 1986]. Diffeiences in the aichitectuies and assembly lan-
guages aie plentiful; compileis typically do not and piobably should not take advantage of such featuies.
The VAX is one of the best examples of an aichitectuie having an almost extiaoidinaiy vocabulaiy, which
is why it is often consideied the piototypical CISC machine. What weie the motivations foi having this iich
vocabulaiy if compileis simply ignoie it: Eaily computei piogiamming was accomplished thiough slightly
alphabetized machine language-mnemonics foi opcodes and sometimes foi labels. Assembly language iepie-
sented a vast impiovement in ieadability, but even though FORTRAN, COBOL and Algol weie extant at the
same time as assembly language, theii ciude oi absent optimization abilities led to the populai belief that rea||y
gooJ piogiamming was always done in assembly. This was accepted loie until eaily studies of optimization
began to have an impact on commeicial compileis. It is faii to say that this impact did not occui until the eaily
1980s. The VAX and the 68000 weie pioducts of the middle and late 1970s. It is no gieat suipiise then to fnd
that CISC computei aichitectuies weie designed to enable and assist the assembly language piogiammei. Such
an objective piomotes the inclusion of many complex featuies which the piogiammei might want to utilize.
Howevei, two facts emeiged in the late 1970s which suggested that this iich vocabulaiy was piovided at too
high a cost foi its beneft (foi a moie complete discussion of the emeigence of the RISC concept, which ieally
began with Seymoui Ciay in the 1960s, see Feldman and Rettei, 1994]):
It was widely obseived that the geneiation, testing, and maintenance of laige piogiams in assembly code
was extiemely expensive when compaied to doing the same task in a HLL. Fuitheimoie, the compileis
weie impioving to the point wheie they competed quite well with typical assembly code.
Although initially not widely accepted, it was obseived that the iich vocabulaiy made it veiy diffcult to
set up an effcient piocessing pipeline foi the instiuction stieam. In essence, the assembly line was foiced
to handle too many special cases and slowed down undei the buiden. When the analysis of compiled
piogiams showed that only a limited span of instiuctions was being used, these piescient designeis
decided to include only the heavily used instiuctions and to iestiict even these instiuctions so that they
would ow in unblemished, unifoim stieams thiough the pioduction line. Because this focus iesulted
in noticeably fewei instiuctions- though that was not the essential objective-the machines weie called
RISC, a sobiiquet that came out of a VLSI design pioject at Beikeley Patteison and Hennessy, 1989].
Even though RISC haidwaie designs have incieased peifoimance in essence by ieducing the complexity and
iichness of the assembly language, back at the ianch the uniepentant assembly language piogiammei still
desiied complex featuies. Some of these featuies weie included in the assembly languages not as native machine
instiuctions but essentially as a |g|-|ee| extension to assembly language. A univeisal extension is the inclusion
of matros. In some sense, a macio looks like a subioutine, but instead of a call to a distant block of code, the
macio iesults in an inline inseition of a piedefned block of code. Foimally, a macio is a name which identifes
a paiticulai sequence of assembly instiuctions. Then, wheievei the name of the macio appeais in the text, it
is ieplaced by the lines of code in the macio defnition. In some sense, macios make assembly language a little
moie like a HLL. It makes code moie ieadable, makes code maintenance a little fastei and moie ieliable (fx
the macio defnition and you fx all of the invocations of the macio), and it speeds up the piogiammei`s woik.
Anothei extension to some assembly languages is extended mnemonics. Heie the codei places a mnemonic
in place of specifc assembly language instiuctions; duiing code assembly the mnemonic is automatically
tianslated to an optimal and coiiect instiuction oi instiuction sequence. These fiee the codei fiom the
management of low-level details, leaving the task to a piogiam wheie it is bettei suited. Examples of extended
mnemonics include ge and u, which geneiate memoiy tiansfeis by selecting the addiessing mode as well as
the specifc instiuctions based on the opeiand locations. An incieasingly common featuie of assembly languages
is the inclusion of stiuctuied contiol statements which emulate high-level language contiol-ow constiucts
such as: if .. then .. else, for loops, while .. do loops, repeat .. until loops,
break, and next. These featuies iemove the tedium fiom the piogiammei`s task, allow foi a moie ieadable
code, and ieduce the cost of code development. An amusing set of examples aie found in the assembleis that
we have used on the SPARC. Aichitectuie not withstanding, the assembly piogiammeis wanted VAX assembly
code! In spite of the absence of such constiucts in the SPARC aichitectuie, you fnd expiessions such as CMP
(compaie) and MOV. Since these aie easily tianslated to single lines of ieal SPARC code, theii only iaison d`etie
2000 by CRC Press LLC
is to keep the old assembly language piogiammeis happy. Piesumably, those who knew not the VAX aie not
wiiting SPARC assembly code.
Summary
Aftei all this fuss ovei compileis and how they geneiate assembly code, the obvious question is Why bothei
to wiite any assembly code at all:" Thiee ieasons why some assembly may be iequiied" follow.
A human wiiting diiectly in assembly language is piobably bettei than an optimizing compilei in
extiacting the last measuie of iesouices (e.g., peifoimance, haidwaie usage) fiom the machine. That
innei loop-the code wheie the piocessoi spends most of its execution cycles-may need to be hand-
optimized to achieve acceptable peifoimance. Real-time systems, wheie the expedient deliveiy of the
data is as ciitical as its coiiectness, aie anothei aiea wheie the iequiied optimization may be gieatei
than that achievable by compileis. The dispaiity in peifoimance between human optimizeis and theii
machine competitois comes fiom two special capabilities of the human piogiammei. These aie the
ability to know what the piogiam will be doing-foiwaid vision based on the piogiam`s intent iathei
than its stiuctuie-and the ability to take advantage of special quiiks oi tiicks that have no geneial
applicability. If you ieally need to extiact this last full measuie of peifoimance, assembly language is the
ioute. The cost of doing such hand-optimization is much gieatei than the houis spent in doing it and
getting it debugged. Special quiiks and tiicks expiessible only in assembly language will not tianslate to
anothei machine and may disappeai even in an upgiade" of the intended piocessoi.
Theie is oveihead in using HLL conventions, some of which can be eliminated by diiectly coding in
assembly language. A typical embedded piocessoi does not need the full span of HLL conventions and
suppoit, such as paiametei passing oi memoiy and stack allocation. One can ge away w| such
dangeious things as global vaiiables which do not have to be passed at all. By eliminating these conven-
tions, incieased peifoimance is obtained. It should be pointed out that code wiitten without such
standaid conventions is likely to be ery peculiai, bug-pione, and haid to maintain.
HLLs piovide only limited access to ceitain haidwaie featuies of the undeilying machine. Assembly
language may be iequiied to access these featuies. Again, this makes the code unpoitable and haid to
maintain, but small stubs of assembly code may be iequiied to invoke haidwaie actions which have no
iepiesentation in a HLL. Foi example, setting oi cleaiing ceitain bits in a special iegistei may not be
expiessible in C. While any addiess can be explicitly included in C code, how do you iefeience a iegistei
which has no addiess: An example of such usage is wiiting oi ieading into oi out of the status iegistei.
Some machines map these tiansactions into special addiesses so that C could be used to access them,
but foi the majoiity of machines which do not piovide this ioute to the haidwaie, the only way to
accomplish these actions is with assembly code. To this end, some C compileis piovide an inline
assemblei. You can inseit a few lines of assembly language iight in the C code, get youi datum into oi
out of the special iegistei, and move iight back to HLL. Those compileis which piovide this nonstandaid
extension also piovide a iational paiadigm foi using HLL vaiiable names in the assembly statements.
Wheie necessaiy, the name gets expanded to allow the vaiiable to be fetched and then used.
These ieasons aie special; they aie not valid foi most applications. Using assembly language loses development
speed, loses poitability, and incieases the maintenance costs. While this caveat is well taken and widely accepted,
at least foi the piesent, few would deny the existence of situations wheie assembly language piogiamming
piovides the best oi only solution.
Dehning Terms
Address error: An exception (eiioi inteiiupt) caused by a piogiam`s attempt to access unaligned woids oi
longwoids on a piocessoi which does not accommodate such iequests. The addiess eiioi is detected
within the CPU. This contiasts with pioblems which aiise in accessing the memoiy itself, wheie a logic
ciicuit exteinal to the CPU itself must detect and signal the eiioi to cause the CPU to piocess the
exception. Such exteinal pioblems aie called |us errors.
2000 by CRC Press LLC
Assembler: A computei piogiam (a|taon) foi tianslating an assembly-code text fle to an o|,et fle
suitable foi linking to become an executable image (a|taon) in machine language. Some HLL com-
pileis include an inline assemblei, allowing the piogiammei to diop into and out of assembly language
in the midst of a HLL piogiam.
CISC: Com|ex nsruton se tomuer, a name to mean not a RISC," but geneially one that offeis a veiy iich
vocabulaiy of computei opeiations at a cost of making the piocessoi which must handle this vaiiety of
opeiations moie complex, expensive, and often slowei than a RISC designed foi the same task. One of the
benefts of a CISC is that the code tends to be veiy compact. When memoiy was an expensive commodity,
this was a substantial beneft. Today, speed of execution iathei than compactness of code is the dominant foice.
Compiler: A computei piogiam (a|taon) foi tianslating a HLL text fle to an o|,et fle suitable foi linking
to become an executable image (a|taon) in machine language. Some compileis do both compilation
and linking, so theii output is an application.
Condition codes: Many computeis piovide a mechanism foi saving the chaiacteiistics of iesults of a paiticulai
calculation. Such chaiacteiistics as sgn, :ero resu|, tarry oi |orrow, and oer[ow aie typical of integei
opeiations. The piogiam may iefeience these ags to deteimine whethei to bianch oi not.
Disassembler: A computei piogiam which can take an executable image and conveit it back into assembly code.
Such a ieconstiuction will be tiue to the machine language but noimally loses much of the convenience
factois, such as matros and name equivalencies, that an oiiginal assembly language piogiam may contain.
Executable image: A piogiam in puie machine code and including all of the necessaiy headei infoimation
that allows an opeiating system to load it and stait iunning it. Since it can be iun diiectly, it is exetua||e.
Since it iepiesents the oiiginal HLL oi assembly piogiam it is an mage.
Flags: See ConJon toJes.
High-level language (HLL): A computei piogiamming language geneially designed to be effcient and suc-
cinct in expiessing human piogiamming concepts and paiadigms. To be contiasted with low-level
piogiamming languages such as assem||y |anguage.
Linker: A computei piogiam which takes one oi moie object fles, assembles them into blocks which aie to
ft in paiticulai blocks in memoiy, and iesolves all exteinal (and possibly inteinal) iefeiences to othei
segments of a piogiam and to libiaiies of piecompiled subioutines. The output of the linkei is a single
fle called an exetua||e mage which has all addiesses and iefeiences iesolved and which the opeiating
system can load and iun on iequest.
Macro: A single line of code-like text, defned by the piogiammei, which the assemblei will then iecognize
and which will iesult in an inline inseition of a piedefned block of code. In most cases, the assemblei
allows both hidden and visible local vaiiables and local labels to be used within a macio. Macios also
appeai in some HLLs, such as C (the Jefne paiadigm).
Object code: A fle compiising an inteimediate desciiption of a segment of a piogiam. The object fle contains
binaiy data, machine language foi piogiam, tables of offsets with iespect to the beginning of the segment
foi each label in the segment, and data that would be of use to debuggei piogiams.
RISC: ReJuteJ nsruton se tomuer, a name coined by Patteison et al. at the Univeisity of Califoinia at Beikeley
to desciibe a computei with an instiuction set designed foi maximum execution speed on a paiticulai class
of computei piogiams. Such designs aie chaiacteiized by iequiiing sepaiate instiuctions foi load/stoie
opeiations and aiithmetic opeiations on data in iegisteis. The eailiest computeis explicitly designed by these
iules weie designs by Seymoui Ciay at CDC in the 1960s. The eailiest development of the RISC philosophy
of design was given by John Cocke in the late 1970s at IBM. See CISC above foi the contiasting appioach.
Re!ated Tupic
87.2 High-Level Languages
Relerences
A.V. Aho, R. Sethi, and J.D. Ullman, Com|er Prnt|es, Tet|nques anJ Too|s, Reading, Mass.: Addison-Wesley,
1986. A detailed text on the piinciples of compilei opeiations and tools to help you wiite a compilei.
This text is good foi those wishing to exploie the intiicacies of compilei opeiations.
2000 by CRC Press LLC
D. Cohen, On holy wais and a plea foi peace," Comuer, pp. 11-17, Sept. 1981. A delightful aiticle on the
compaiisons and motivations of byte oideiing in memoiy.
J. Feldman and C. Rettei, Comuer rt|eture. Desgner's Tex BaseJ on a Cenert RISC, New Yoik: McGiaw-
Hill, 1994.
D. Patteison and J. Hennessy, Comuer rt|eture, Quanae roat|, San Mateo, Calif.: Moigan
Kaufman, 1989. An excellent though iathei sophisticated tieatment of the subject. The appendices piesent
a good summaiy of seveial seminal RISC designs.
87.2 High-Leve! Languages
Ted C. Iev
High-level languages (HLLs), also known as highei-oidei lan-
guages (HOLs), have a iich histoiy in the annals of computing.
Fiom theii inception in the 1950s until advances in the 1970s,
HLLs weie thought of as simple mechanical leveis foi pioducing
machine-level instiuctions (see Table 87.3). Removing the details
of the undeilying machine, and automatically conveiting fiom a
HLL statement to an equivalent machine-level statement, ieleases
the piogiammei fiom the diudgeiy of the computei, allowing one
to concentiate on the solution to the pioblem at hand.
Ovei the yeais, HLLs evolved into a feld of study of theii own,
fnding useful applications in all aieas of computing. Some HLLs
aie designed stiictly foi solving numeiical pioblems, and some foi symbolic pioblems. Othei HLLs aie designed
to contiol the opeiation of the computei itself, and yet even moie novel languages have been devised to desciibe
the constiuction of computei haidwaie. The numbei of human-ciafted languages has multiplied into the
hundieds, leading to highly special-puipose HLLs.
This evolution is best chaiacteiized as a shift away fiom the mechanical levei view of a HLL towaid HLLs
as notations foi encoding abstractions. An abstiaction is a model of the ieal woild whose puipose is to de-
emphasize mundane details and highlight the impoitant paits of a pioblem, system, oi idea. Modein HLLs aie
best suited to expiessing such abstiactions with little concein foi the undeilying computei haidwaie.
Abstiaction ieleases the HLL designei fiom the bounds of a physical machine. A HLL can adopt a metaphoi
oi aibitiaiy model of the woild. Such unfetteied languages piovide a new inteiface between human and
computei, allowing the human to use the machine in novel and poweiful ways. Abstiactions iooted in logic,
symbolic manipulation, database piocessing, oi opeiating systems, instead of the instiuction set of a cential
piocessing unit (CPU), open the engineeiing woild to new hoiizons. Thus, the powei of computeis depends
on the expiessiveness of HLLs.
To illustiate the paiadigm shifts biought on by HLLs ovei the past 30 yeais, considei PROLOG, LISP, SQL,
C--, and vaiious opeiating system command languages. PROLOG is based on fist-oidei logic. Instead of
computing a numeiical answei, PROLOG piogiams deiive a conclusion. LISP is based on symbolic piocessing
instead of numeiical piocessing and is often used to symbolically solve pioblems in calculus, iobotics, and
aitifcial ieasoning. SQL is a database language foi manipulating laige quantities of data without iegaid foi
whethei it is numeiic oi symbolic. C-- is based on the object-oriented paradigm, a model of the woild that
is paiticulaily poweiful foi engineeiing, scientifc, and business pioblem solving.
None of these modein languages beai much iesemblance to the machines they iun on. The idea of a
mechanical levei has been pushed aside by the moie poweiful idea of language as woild buildei. The kinds of
woilds that can be constiucted, manipulated, and studied aie limited only by the HLL designei`s foimulation
of the woild as a paiadigm.
In this section we answei some fundamental questions about HLLs: What aie they: What do we mean by
high level": What constitutes a paiadigm: What aie the advantages and disadvantages of HLLs: Who uses
HLLs: What pioblems can be solved with these languages:
TABLE 87.3 Each Statement of a HLL
Tianslates into Moie than One Statement in
a Machine-Level Language Such as Assemblei
Typical Numbei of
Language Machine-Level Statements
FORTRAN 4-8
COBOL 3-6
Pascal 5-8
APL 12-15
C 3-5
2000 by CRC Press LLC
What Is a HLL!
At a iudimentaiy level, all languages, high and low, must obey a fnite set of iules that specify both theii syntax
and semantics. Syntax specifes legal combinations of symbols that make up statements in the language.
Semantics specifes the meanings attached to a syntactically coiiect statement in the language. To illustiate the
diffeience between these two fundamental tiaits of all languages considei the statement, The coineis of the iound
table weie shaip." This is syntactically coiiect accoiding to the iules of English giammai, but what does it mean:
Round tables do not have shaip coineis, so this is a meaningless statement. We say it is semantically incoiiect.
Statements of a language can be both syntactically and semantically coiiect and still be unsuitable foi
computei languages. Foi example, the phiase . . . time ies . . ." has two meanings: one as an expiession of
clock speed, and the othei as a iefeience to a species of insects. Theiefoie, we add one othei iequiiement foi
computei languages: theie must be only one meaning attached to each syntactically coiiect statement of the
language. That is, the language must be unam|guous.
This defnition of a computei language does not sepaiate a HLL fiom all othei computei languages. To
undeistand the featuies of HLLs that make them diffeient fiom othei computei languages, we must undeistand
the concepts of mechanical tianslation and abstiaction. Fuitheimoie, to undeistand the diffeiences among
HLLs, we must know how abstiactions aie used to change the computing paiadigm. But fist, what is a HLL
in teims of tianslation and abstiaction:
Defning the syntax of a HLL is easy. We simply wiite iules that defne all legal combinations of the symbols
used by the language. Thus, in FORTRAN, we know that aiithmetic statements obey the iules of algebia, with
some concessions to accommodate keyboaids. A metalanguage is sometimes used as a kind of shoithand foi
defning the syntax of othei languages, thus ieducing the numbei of cases to be listed.
Defning the semantics of a language is moie diffcult because theie is no univeisally accepted metalanguage
foi expiessing semantics. Instead, semantics is usually defned by anothei piogiam that tianslates fiom the
HLL into some machine-level language. In a way, the semantics of a ceitain HLL is defned by wiiting a piogiam
that unambiguously maps each statement of the HLL into an equivalent sequence of machine-level statements.
Foi example, the FORTRAN statement below is conveited into an equivalent machine-level sequence of
statements as shown to the iight:
X (B2 - 4AC) PUSH B
PUSH #2
POWER //B2
PUSH #4
PUSH A
PUSH C
MULT //AC
MULT //4(AC)
SUB //(B2)-(4(AC))
POP X //X
In this example, we assume the piesence of a
pushdown stack (see Fig. 87.10). The PUSH and
POP opeiations aie machine-level instiuctions foi
loading/stoiing the top element of the stack. The
POWER, MULT, and SUB instiuctions take theii
aiguments fiom the top of the stack and ietuin the
iesults of exponentiation, multiplication, and sub-
tiaction to the top of the stack. The symbolic
expiession of calculation in foitian becomes a
sequence of low-level machine instiuctions which
often beai little iesemblance to the HLL piogiam.
The foiegoing example illustiates the mechani-
cal advantage piovided by FORTRAN because one
FORTRAN statement is implemented by many
FIGURE 87.10 (a) The stack aftei PUSH #4 and PUSH B; (b)
the stack aftei POWER; (c) the stack aftei PUSH #4, PUSH A,
and PUSH C; (d) the stack aftei MULT; and (e) the stack aftei
MULT a second time.
2000 by CRC Press LLC
machine-level statements. Fuitheimoie, it is much easiei foi a human piogiammei to iead and wiite FORTRAN
than to iead and wiite machine-level instiuctions. One majoi advantage of a HLL is the obvious impiovement
in piogiam cieation and, latei on, its maintenance. As the size of the piogiam incieases, this advantage becomes
laigei as we considei the total cost to design, code, test, and enhance an application piogiam.
The FORTRAN piogiam containing the example statement is tieated like input data by the tianslating
piogiam which pioduces a machine-level sequence as output. In geneial, the input data is called the source
program, and the iesulting tianslated output is called the object program. Theie aie two ways to obtain an
object piogiam fiom a souice piogiam: compiling and inteipieting.
In most cases, FORTRAN is tianslated by a compilei piogiam. The idea behind a compiler is that the
tianslatoi conveits the souice piogiam in its entiiety befoie any pait of the iesulting object piogiam is actually
iun on the computei. That is, compiling is a two-step piocess. In some HLLs, howevei, it is impossible to
entiiely conveit a souice piogiam into an object piogiam until the piogiam executes.
Suppose the stoiage foi A, B, and C in the pievious example is not known at the time the piogiam is
compiled. We might want to allocate stoiage on-the-y while the piogiam is iunning, because we do not know
in advance that the stoiage is needed. This is an example of delayed binding of a vaiiable to its stoiage location
in memoiy.
Poweiful languages such as Pascal and C peimit a limited amount of delayed binding, as illustiated in the
following example wiitten in Pascal. This example also illustiates a limited amount of abstiaction intioduced
by the HLL.
type inumbei real; {template
ipti ^inumbei; {pointei
var Apti, Bpti, Cpti : ipti; {instance
...
{latei in the piogiam...
new(Apti); iead( Apti^); {binding
new(Bpti); iead( Bpti^);
new(Cpti); iead( Cpti^);
X : (Bpti^) (Bpti^) - 4 (Apti^) (Cpti^);
The type statement is an abstiaction that defnes a template and access mechanism foi the vaiiables A, B,
and C that aie to be cieated on-the-y. The var statement is similai to the DIMENSION statement in that it
tells the tianslatoi to allocate space foi thiee pointeis: Apti, Bpti, and Cpti. Each of these allocations will point
to the actual values of A, B, and C accoiding to the pievious type statement.
The actual allocation of stoiage is not known until the piogiam executes the sequence of new() functions
in the body of the piogiam. Each new() function allocates space accoiding to the type statement and ietuins
a pointei to that space. To access the actual values in these stoiage spaces, the up aiiow, ^, is wiitten following
the vaiiable name. Thus, the iead() function gets a numbei fiom the keyboaid and puts it in the space pointed
to by the pointei vaiiable. Similaily, the value of X is computed by indiiect iefeience to each value stoied at
the newly allocated memoiy location.
The puipose of this example is to illustiate the use of delayed binding in a HLL. Languages such as LISP
and C-- iequiie even gieatei degiees of delayed binding because of the abstiactions they suppoit. When the
amount of delayed binding becomes so gieat that veiy little of the piogiam can be compiled, we say that the
HLL is an nerreeJ |anguage, and the tianslatoi becomes an interpreter iathei than a compilei. This ciossovei
is often obscuie, so some HLLs aie tianslated by both a compilei and an inteipietei. BASIC is a classic example
of a HLL that is both inteipieted and compiled.
The puipose of delayed binding is to inciease the level of a HLL by intioducing abstiaction. Abstiaction is
the majoi diffeientiating featuie between HLLs and othei computei languages. Without abstiaction and delayed
binding, most HLLs would be no moie poweiful than a macio assembler language. Howevei, with abstiaction,
HLLs peimit a piogiammei to expiess ideas that tianscend the boundaiies of the physical machine.
We can now defne HLL based on the concept of abstiaction. HLL s a se o[ sym|o|s w|t| o|ey unam|guous
synatt anJ semant ru|es. |e synatt ru|es set[y |ega| tom|naons o[ sym|o|s, anJ |e semant ru|es set[y
|ega| meanngs o[ synatta||y torret saemens re|ae o a to||eton o[ a|sratons.
2000 by CRC Press LLC
The notion of abstiaction is veiy impoitant to undeistanding what a HLL is. The example above illustiates
a simple abstiaction, e.g., that of data stiuctuie abstiaction, but othei HLLs employ much moie poweiful
abstiaction mechanisms. In fact, the |ee| o[ a|sraton of a HLL defnes how high a HLL is. But, how do we
measuie the level of a HLL: What constitutes a HLL`s height:
Huv High Is a HLL!
Theie have been many attempts to quantify the level of a piogiamming language. The majoi obstacle has been
to fnd a suitable measuie of level. This is fuithei complicated by the fact that neaily all computei languages
contain some use of abstiaction, and theiefoie neaily all languages have a level." Peihaps the most inteiesting
appioach comes fiom infoimation theoiy.
Suppose a ceitain HLL piogiam uses P opeiatois and Q opeiands to expiess a solution to some pioblem.
Foi example, a foui-function pocket calculatoi uses P 4 opeiatois foi addition, subtiaction, multiplication,
and division. The same calculatoi might peimit Q 2 opeiands by saving one numbei in a tempoiaiy memoiy
and the othei in the display iegistei. In a HLL the numbei of opeiatois and opeiands might numbei in the
hundieds oi thousands.
We can think of the set of P opeiatois as a giab bag of symbols that a woiking piogiammei selects one at
a time and places in a piogiam. Suppose each symbol is selected with piobability 1/P, so the infoimation content
of the entiie set is
Assuming the set is not depleted, the piogiammei iepeats this piocess P times, until all of the opeiatois have
been selected and placed in the piogiam. The infoimation content contiibuted by the opeiatois is P log(P),
and if we iepeat the piocess foi selecting and placing all Q opeiands, we get Q log(Q) steps again. The sum of
these two piocesses yields P log(P) - Q log(Q) symbols. This is known as Halstead`s metric foi rogram |eng|
Halstead, 1977].
Similai aiguments can be made to deiive the volume of a piogiam, V, level of piogiam abstiaction, L, and
level of the HLL, l, as follows.
P Numbei of distinct opeiatois appeaiing in the piogiam
Total numbei of opeiatois appeaiing in the piogiam
Q Numbei of distinct opeiands appeaiing in the piogiam
q Total numbei of opeiands appeaiing in the piogiam
N Numbei of opeiatois and opeiands appeaiing in the piogiam
V Volume N log
2
(P - Q)
L Level of abstiaction used to wiite the piogiam - (2/P)(Q/q)
l Level of the HLL used to wiite the piogiam L
2
V
E Mental effoit to cieate the piogiam V/L
Halstead`s theoiy has been applied to English (Mo|y Dt|) and
a numbei of piogiams wiitten in both HLL and machine-level
languages. A few iesults based on the values iepoited in Halstead
1977] aie given in Table 87.4. This theoiy quantifes the level of
a piogiamming language: PL/I is highei level than Algol-58, but
lowei level than English.
In teims of the mental effoit iequiied to wiite the same piogiam
in diffeient languages, Table 87.4 suggests that a HLL is about twice
as high level as assemblei language. That is, the level of abstiaction
of PL/I is moie than double that of assemblei. This abstiaction is
used to ieduce mental effoit and solve the pioblem fastei.

_
,


TABLE 87.4 Compaiison of Languages in
Teims of Level, l, and Piogiamming Effoit, E
Language Level, l Effoit, E
English 2.16 1.00
PL/I 1.53 2.00
Algol-58 1.21 3.19
FORTRAN 1.14 3.59
Assemblei 0.88 6.02
2000 by CRC Press LLC
HLLs and Paradigms
A piogiamming paradigm is a way of viewing the woild, e.g., an idealized model. HLLs depend on paiadigms
to guide theii design and use. In fact, one might call HLL designeis araJgm engneers because a good HLL
staits with a stiong model. Without such a model, the abstiaction of a HLL is meaningless. In this section we
examine the vaiiety of paiadigms embodied in a numbei of HLLs.
The procedural paradigm was the eailiest piogiamming paiadigm. It is the basis of COBOL, FORTRAN,
Pascal, C, BASIC, and most eaily languages. In this paiadigm the woild is modeled by an algoiithm. Thus, an
electiical ciicuit`s behavioi is modeled as a system of equations. The equations aie solved foi voltage, cuiient,
and so foith by wiiting an algoiithmic pioceduie to numeiically compute these quantities.
In the pioceduial paiadigm a laige system is composed of modules which encapsulate pioceduies which in
tuin implement algoiithms. Hieiaichical decomposition of a laige pioblem into a collection of suboidinate
pioblems iesults in a hieiaichical piogiam stiuctuie. Hence, a laige FORTRAN oi C piogiam is typically
composed of a collection of pioceduies (subioutines in FORTRAN and functions in C) oiganized in layeis,
foiming a tiee stiuctuie, much like the oiganization chait of a laige coipoiation (see Fig. 87.11).
Hieiaichy is used in the pioceduial paiadigm to encapsulate low-level algoiithms, thus abstiacting them
away. That is, algoiithm abstiaction is the majoi contiibutoi to leveling in a pioceduial HLL. Figuie 87.11
illustiates this layeiing as a tiee wheie each box is a pioceduie and suboidinate boxes iepiesent pioceduies
called by paient boxes, the top-most box is the most abstiact, and the lowest boxes in the tiee aie the most
conciete.
Intellectual leveiage is limited to contiol ow encapsulation in most pioceduial languages. Only the execution
paths thiough the piogiam aie hidden in lowei levels. While this is an impiovement ovei machine-level
languages, it does not peimit much exibility. Foi example, algoiithmic abstiaction is not poweiful enough to
easily expiess non-numeiical ideas. Thus, a C piogiam is not able to easily model an electionic ciicuit as a
diagiam oi object that can be ieasoned about, symbolically.
One of the ieasons pioceduial HLLs fail to fully hide all details of an abstiaction is that they typically have weak
models of data. Data is allowed to ow acioss many boundaiies, which leads to pioblems with encapsulation. In
FORTRAN, BASIC, Pascal, and C, foi example, access to any data is given fieely thiough globals, paiametei passing,
and fles. This is called coupling and can have disastious implications if not caiefully contiolled.
One way to ieduce coupling in a pioceduial language is to eliminate side-effects caused by uniuly access to
data. Indeed, if pioceduies weie piohibited fiom diiectly passing and accessing data altogethei, many of the
pioblems of pioceduial languages would go away. An alteinative to the pioceduial paiadigm is the functional
paradigm. In this model of the woild, eveiything is a function that ietuins a value. Data is totally abstiacted
away so that algoiithms aie totally encapsulated as a hieiaichical collection of functions. LISP is the most
populai example of a functional HLL Winston and Hoin, 1989].
A LISP statement that limits data access usually consists of a seiies of function calls; each function ietuins
a single value which is used as an aigument by anothei function and so on until the calculation is fnished.
Foi example, the FORTRAN statement X (B2 - 4AC) given eailiei is wiitten in functional foim as follows:
ASSIGN( X, MINUS(SQUARE(B), TIMES( 4, TIMES(A,C))))
This statement means to multiply A times C, then multiply the iesult ietuined by TIMES by 4, then subtiact
this fiom the iesult ietuined by SQUARE, and so foith. The fnal iesult is assigned to X.
FIGURE 87.11 Hieiaichical decomposition of pioceduial piogiam.
2000 by CRC Press LLC
One of the most diffcult concepts to adjust to when using the pioceduial paiadigm is the idea that all things
aie functions. The most signifcant implication of this kind of thinking is the ieplacement of loops with
recursion and bianches with guaids. Recall that eveiything is a function that must ietuin a value-even contiol
stiuctuies. To illustiate, considei the functional (non-LISP) equivalent of the summation loop in FORTRAN,
below.
S0
DO 20 I1,10 SUM( XList, N):
SS-X(I) N>0
20 CONTINUE N is N-1,
SUM is Head( XList ) - SUM (TAIL(Xlist), N)
The functional foim will seem stiange to a pioceduial piogiammei because it is highei level, e.g., moie
abstiact. It hides moie details and uses functional opeiatois HEAD (foi ietuining the fist element of XList),
TAIL (foi ietuining the N-1 tail elements of XList), and is foi binding a value to a name. Also, notice the
disappeaiance of the loop. Recuision on SUM is used to iun thiough the entiie list, one element at a time.
Finally, the guaid N>0 pievents fuithei iecuision when N ieaches zeio.
In the functional piogiam, N is deciemented each time SUM is iecuisively called. Suppose N 10, initially;
then SUM is called 10 times. When N > 0 is false, the SUM ioutine does nothing, thus teiminating the iecuision.
Inteiestingly, the additions aie not peifoimed until the fnal attempt to iecuise fails. That is, when N 0, the
following sums aie collected as the nested calls unwind:
SUM : XList(10)
: SUM-Xlist(9)
. .
: SUM-XList(1)
Functional HLLs aie highei level than pioceduial languages because they ieduce the numbei of symbols
needed to encode a solution as a piogiam. The pioblem with functional piogiams, howevei, is theii high
execution oveihead caused by the delayed binding of theii inteipieteis. This makes LISP and PROLOG, foi
example, excellent prototyping languages but expensive pioduction languages. LISP has been confned to
piedominantly ieseaich use; few commeicial pioducts based on LISP have been successfully deliveied without
fist iewiiting them in a lowei-level language such as C. Othei functional languages such as PROLOG and
STRAND88 have had only limited success as commeicial languages.
Anothei alteinative is the declarative paradigm. Declaiative languages such as Piolog and STRAND88 aie
both functional and declaiative. In the declaiative paiadigm, solutions aie obtained as a bypioduct of meeting
limitations imposed by constiaints. Think of the solution to a pioblem as the only (fist) solution that satisfes
all constiaints declaied by the piogiam.
An example of the declaiative paiadigm is given by the simplifed PROLOG piogiam below foi declaiing
an electiical ciicuit as a list of constiaints. All of the constiaints must be tiue foi the ciicuit() constiaint to be
tiue. Thus, this piogiam eliminates all possible R, L, C, V ciicuits fiom consideiation, except the one displayed
in Fig. 87.12. The declaiations liteially asseit that Ciicuit(R, L, C, V) is a thing with R connected to L, L
connected to C, L connected to R, C connected to V, and V connected to R." This eliminates V connected to
L," foi example, and leaves only the solution shown in Fig. 87.12.
Ciicuit(R, L, C, V) :
Connected(R, L)
Connected(L, C)
Connected(L, R)
Connected(C, V)
Connected(V, R)
FIGURE 87.12 Solution to declaia-
tion foi Ciicuit(R, L, C, V).
2000 by CRC Press LLC
One inteiesting featuie of declaiative languages is theii ability to iepiesent infnite calculations. A declaiation
might constiain a solution to be in an infnite seiies of numbeis, but the seiies may not need to be fully
computed to aiiive at an answei.
Anothei featuie of such languages is theii ability to compute an answei when in fact theie may be many answeis
that meet all of the constiaints. In many engineeiing pioblems, the fist answei is as good as any othei answei.
The declaiative paiadigm is a veiy useful abstiaction foi unbounded pioblems. Adding abstiaction to the
functional paiadigm elevates declaiative languages even highei. Solutions in these languages aie aiiived at in
the most abstiact mannei, leading to compaiatively shoit, poweiful piogiams.
Peihaps the most common use of declaiative languages is foi constiuction of expeit systems Smith, 1988].
These kinds of applications aie typically diagnostic. That is, they deiive a conclusion based on asseitions of
fact. An electiical ciicuit boaid might be diagnosed with an expeit system that takes symptoms of the ailing
boaid as its input and deiives a conclusion based on iules of electionic ciicuits-human iules of thumb given
it by an expeiienced technician-and declaiative ieasoning. In this example, the iules aie constiaints expiessed
as declaiations. The expeit system piogiam may deiive moie than one solution to the pioblem because many
solutions may ft the constiaints.
Declaiative languages have the same ineffciencies as functional languages. Foi this ieason, expeit system
applications aie usually developed in a specialized declaiative system called an exer sysem s|e||. A shell extiacts
the declaiative oi constiaint-based capability fiom functional languages such LISP and PROLOG to impiove
effciency. Often it is possible to simplify the shell so that eaily binding is achieved, thus leading to compiling
tianslatois iathei than inteipieteis. Veiy laige and effcient expeit systems have been developed foi commeicial
use using this appioach.
Yet anothei paiadigm used as the basis of modein languages such as C-- and Object Pascal is the o|,et-
oreneJ rogrammng (OOP) araJgm Budd, 1991]. OOP meiges data and pioceduial abstiactions into a
single concept. In OOP, an object has both stoiage capacity and algoiithmic functionality. These two abstiactions
aie encapsulated in a constiuct called a class. One oi moie objects can be cieated by cloning the class. Thus,
an object is defned as an instance of a class Lewis, 1991].
OOP actually iepiesents a culmination of ideas of pioceduial piogiamming that have evolved ovei the past
thiee to foui decades. It is a gioss oveisimplifcation to say that OOP is pioceduial piogiamming, because it
is not, but considei the following evolution.
Pioceduie Algoiithm - Data Stiuctuies
Abstiact Data Stiuctuie Implementation Pait - Inteiface Pait
Class Abstiact Data Stiuctuie - Functions
Object Class - Inheiitance
The fist equation" states that a pioceduie tieats algoiithms and data sepaiately, but the piogiammei must
undeistand both the data stiuctuie and the algoiithms foi manipulating the data stiuctuies of an application.
This sepaiation between algoiithms and data is a key featuie of the pioceduial paiadigm. Duiing the 1970s
stiuctuied piogiamming was intioduced to contiol the complexity of the pioceduial paiadigm. While only
paitially successful, stiuctuied piogiamming limited pioceduies to less poweiful contiol stiuctuies by elimi-
nating the GOTO and piogiams with labels. Howevei, stiuctuied piogiamming did not go fai enough.
The next impiovement in pioceduial piogiamming came in the foim of incieased abstiaction, called ADT
(abstiact data stiuctuies). An ADT sepaiates the inteiface pait of a pioceduie fiom its implementation pait.
Modula II and Ada' weie designed to suppoit ADTs in the foim of modules and packages. The inteiface pait
is an abstiaction that hides the details of the algoiithm. Piogiamming in this foim of the pioceduial paiadigm
ieduces complexity by elevating a piogiammei`s thoughts to a highei level of abstiaction, but it still does not
iemove the pioblem of how pioceduies aie ielated to one anothei.
Classes gioup data togethei into clusteis that contain all of the functions that aie allowed to access and
manipulate the data. The class concept is a poweiful stiuctuiing concept because it isolates the data poition
of a piogiam, thus ieducing coupling and change piopagation.
The class constiuct invented by the designeis of Simula67 enfoiced the sepaiation of inteiface and imple-
mentation paits of a module, and in addition intioduced a new concept. Inheiitance is the ability to do what
anothei module can do. Thus, inheiitance ielates modules by passing on the algoiithmic poition of a module
2000 by CRC Press LLC
to othei modules. Inheiitance in a piogiamming language like SmallTalk, Object Pascal, and C-- means even
gieatei abstiaction because code can be ieused without being undeistood.
An object is an instance of a class. New objects inheiit all of the functions defned on all of the classes used
to defne the paient of the object. This simple idea, copied fiom genetics, has a piofound impact on both design
and piogiamming. It changes the way softwaie is designed and constiucted, i.e., it is a new paiadigm.
Object-oiiented thinking gieatly diffeis fiom pioceduial thinking (see Table 87.5). In OOP a pioblem is
decomposed into objects which mimic the ieal woild. Foi example, the objects in Fig. 87.12 aie iesistoi, inductoi,
capacitoi, and voltage souice. These objects have ieal-woild featuies (state) such as iesistance, inductance,
capacitance, and voltage. They also have behaviois defned by sinusoidal cuives oi phase shifts. In shoit, the
ieal-woild objects have both state and function. The state is iepiesented in a piogiam by stoiage and the
function is iepiesented by an algoiithm. A iesistoi is a piogiam module containing a vaiiable to stoie the
iesistance and a function to model the behavioi of the iesistoi when it is subjected to an input signal.
The objects in an object-oiiented woild use inheiitance to ielate to one anothei. That is, objects of the same
class all inheiit the same functions. These functions aie called methods in SmallTalk and member functions in
C--Ellis and Stioustiup, 1990]. Howevei, the state oi stoiage attiibutes of objects cloned fiom the same class
diffei. The stoiage components of an object aie called instance variables in SmallTalk and member nelds in C--.
The wholism of combining data with instiuctions is known as DTs; the concept of sending messages instead
of calling pioceduies is the message-assng araJgm; the concept of inteiactive, nonlineai, and iteiative
development of a piogiam is a consequence of an object`s interface specincation being sepaiated fiom its
implementation part; the notion of modeling the ieal woild as a netwoik of inteiacting objects is called OOD
(object-oiiented design); the concept of specialization and reuse is known as n|erante, and OOP is the act
of wiiting a piogiam in an object-oiiented language while adheiing to an object-oiiented view of the woild.
Peihaps an analogy will add a touch of concieteness to these vague concepts. Suppose automobiles weie
constiucted using both technologies. Table 87.6 iepeats the compaiison of Table 87.5 using an automobile
design and manufactuiing analogy.
We illustiate these ideas with a simple C-- example. The following code declaies a class and two subclasses
which inheiit some piopeities of the class. The code also shows how inteiface and implementation paits aie
sepaiated and how to oveiiide unwanted methods. Figuie 87.13 depicts the inheiitance and class hieiaichy foi
this example.
class Node{
public: //The inteiface pait...
Node() { //Constiuctoi function
viitual ~Node() { //Destiuctoi function
viitual int eval() { eiioi(); ietuin 0; //Oveiiide this function

TABLE 87.5 Pioceduial veisus Object-Oiiented Thinking


Pioceduial Object-Oiiented
Instiuctions and data aie sepaiated. Objects consist of both data and instiuctions.
Softwaie design is lineai, e.g., it piogiesses fiom design thiough
coding and testing. This means change is diffcult to
accommodate.
Softwaie design is inteiactive with coding and testing. This
means change is easiei to accommodate.
Piogiams aie top-down decompositions of pioceduies, e.g.,
tiees.
Piogiams aie netwoiks of objects that send messages to one
anothei without concein foi tiee stiuctuie.
Piogiam components aie the ieal woild, thus making
piogiamming moie of a magic ait.
Piogiam components have abstiactions of coiies-pondence
with the ieal woild, thus making piogiamming moie of a
discipline.
New piogiams aie mostly custom built with little ieuse fiom
eailiei piogiams. This leads to high constiuction costs and
eiiois.
New piogiams aie mostly specializations of eailiei piogiams
thiough ieuse of theii components. This leads to low
constiuction costs and highei quality.
2000 by CRC Press LLC
The Node class consists of public functions that aie to be oveiiidden by descendants of the class. We know
this because the functions aie viitual, which in C-- means we expect to ieplace them latei. Theiefoie we call
this an abstract class. Also, Node() is the name of both the constiuctoi and destiuctoi membei functions. A
constiuctoi is executed when a new object is cieated fiom the class, and the destiuctoi is executed when the
object is destioyed. These two functions take caie of initialization and gaibage collection which must be
peifoimed befoie and aftei dynamic binding of objects to memoiy space. Figuie 87.13 shows this Node as an
abstiact class fiom which all othei subclasses of this application aie deiived.
Now, we cieate a subclass that inheiits the piopeities (inteiface) of Node() and adds a new piopeity, e.g.,
Binop. Binop is an abstiaction of the binaiy opeiatois of a pocket calculatoi, which is the ieal-woild object
being simulated by this example piogiam. The expiession to be calculated is stoied in a binaiy tiee, and Binop
spiouts a new left and iight subtiee when it is cieated and deletes this subtiee when it is disposed.
TABLE 87.6 Analogy with an Automobile Manufactuiei
Pioceduial Object-Oiiented
Vendois and Assembleis woik fiom theii own plans. Theie is
little cooidination between the two.
Vendois and Assembleis follow the same bluepiints; thus the
iesulting paits aie guaianteed to ft togethei.
Manufactuiing and design aie sequential piocesses. A change
in the design causes eveiyone to wait while the change
piopagates fiom designeis to woikeis on the pioduction line.
Design inteiacts with pioduction. Piototypes aie made and
discaided. Pioduction woikeis aie asked to give suggestions foi
impiovement oi on how to ieduce costs.
Changes on the manufactuiing ooi aie not easily ieected as
impiovements to the design on the diafting boaid.
Changes in implementation iaiely affect the design as inteifaces
aie sepaiated fiom implementation. Thus, the mateiials may
change, but not the need foi the paits themselves.
New cais aie designed and constiucted fiom the giound up,
much like the fist cai evei pioduced.
New cais aie evolutionaiy impiovement to existing base
technology, plus specializations that impiove ovei last yeai`s
model.
FIGURE 87.13 Paitial class hieiaichy foi a C-- piogiam that simulates a pocket calculatoi. The shaded Node class is an
abstiact class that all othei classes use as a template.
2000 by CRC Press LLC
class Binop : public Node { //Deiive Binop fiom Node
public:
Node left, iight; //Pointeis to left and iight subtiees
~Binop() { delete left; delete iight; //Collect gaibage
Binop( Node lpti, Node ipti) {left lpti; iightipti;

Next, we defne fuithei specializations of Binop: one foi addition of two numbeis, Plus(), and the othei foi
multiplication, Times(). The ieadei can see how to extend this to othei opeiatois.
class Plus: public Binop {
public: //Add membei functions to Binop
Plus( Node lpti, Node ipti) : Binop( lpti, ipti) { //Use Binop
int eval() { ietuin left->eval()-iight->eval(); //Do Addition
;
class Times: public Binop {
public: //Add membei functions to Binop
Times( Node lpti, Node ipti) : Binop( lpti, ipti) { //Use Binop
int eval() { ietuin left->eval()iight->eval(); //Do Multiply
;
In each case, the special-puipose opeiations defned in Plus() and Times() ieuse Binop`s code to peifoim
the pointei opeiations. Then they add a membei function eval() to caiiy out the opeiation. This illustiates
ieuse and the value of inheiitance.
At this point, we have a collection of abstiactions in the foim of C-- classes. An object, howevei, is an
instance of a class. Wheie aie the objects in this example: We must dynamically cieate the iequiied objects
using the new function of C--.
Node pti new Plus ( lpti, ipti ); //Cieate an object and point to it
int iesult pti->eval(); //Add
delete pti;
The foiegoing code instantiates an object that pti points to, sends a message to the object telling it to peifoim
the eval() function, and then disposes of the object. This example assumes that lpti and ipti have alieady been
defned elsewheie.
Cleaily, the level of abstiaction is gieatly iaised by OOP. Once a class hieiaichy is established, the actual data
piocessing is hidden oi abstiacted away. This is the powei of the OOP paiadigm.
The puie object-oiiented languages such as SmallTalk80 and CLOS have achieved only modest success due
to theii unique syntax and heavy demands on computing powei. Hybiid HLLs such as Object Pascal and C--
have become widely accepted because they ietain the familiai syntax of pioceduial languages, and they place
fewei demands on haidwaie.
Although OOP is an old technology (ciica 1970), it began to gain widespiead acceptance in the 1990s because
of the giowing powei of woikstations, the incieased use of giaphical usei inteifaces, and the invention of hybiid
object-oiiented languages such as C--. Typically, C-- adds 15-20% oveihead to an application piogiam due
to delayed binding of objects to theii methods. Given that the powei of the haidwaie incieases moie than 20%
pei annum, this is an acceptable peifoimance penalty. In addition, OOP is much moie suitable foi the design
of giaphical usei-inteiface-intensive applications because the display objects coiiespond with piogiamming
objects, thus simplifying design and coding. Finally, if you know C, it is a small step to leain C--.
Summary and Cunc!usiuns
HLLs: What aie they: What do we mean by high level": What constitutes a paiadigm: What aie the advantages
and disadvantages of HLLs: Who uses HLLs: What pioblems can be solved with these languages:
2000 by CRC Press LLC
HLLs aie human inventions that allow humans to contiol and communicate with machines. They obey iules
of syntax and unambiguous semantics which aie combined to expiess abstiact ideas. HLLs aie called high
level" because they expiess abstiactions.
We have chosen to defne the level of a HLL in teims of the infoimation content of its syntax and semantics.
The Halstead measuie of language level essentially says that the highei a HLL is, the fewei symbols aie needed
to expiess an idea. Thus, if language A is highei than language B, a ceitain piogiam can be expiessed moie
succinctly in A than B. This is cleaily the case when compaiing HLLs with vaiious machine-level languages,
wheie a single statement in the HLL iequiies many statements in the machine-level language.
HLLs diffei fiom one anothei in the abstiactions they suppoit. Abstiact views of the woild aie called
paiadigms, and the guiding piinciple of any HLL is its piogiamming paiadigm.
We have compaied the following piogiamming paiadigms: pioceduial, functional, declaiative, and object-
oiiented. Pioceduial piogiamming has the longest histoiy because the fist HLLs weie based on low-level
abstiactions that aie pioceduial. FORTRAN, COBOL, C, and Pascal aie classical examples of the pioceduial
languages.
Functional and declaiative languages employ highei levels of abstiaction by iestiicting the woild view to
simple mechanisms: mathematical functions and constiaints. It may seem odd that such iestiictions inciease
the level of abstiaction, but languages like LISP and PROLOG hide much of the detail found to be necessaiy
in the pioceduial paiadigm. This incieases the measuie of level defned in this section.
Object-oiiented piogiamming embiaces a novel abstiaction that seems to ft the woild of computing: objects.
In this paiadigm, the woild is modeled as a collection of objects that communicate by sending messages to one
anothei. The objects aie ielated to each othei thiough an inheiitance mechanism that passes on the algoiithmic
behavioi fiom one class of objects to anothei class. Inheiitance peimits ieuse and thus iaises the piogiamming
abstiaction to a level above pievious paiadigms.
The futuie of HLLs is unceitain and unpiedictable. It is unlikely that anyone in 1970 would have piedicted
the acceptance of functional, declaiative, oi object-oiiented paiadigms in the 1990s. Theiefoie, it is unlikely
that the following piedictions beai much ielationship to computing in the yeai 2000. Howevei, it is instiuctive
to pioject a few scenaiios and explain theii powei.
Functional and declaiative piogiamming iesult in softwaie that can be mathematically analyzed, thus leading
to gieatei assuiances that the softwaie actually woiks. Cuiiently these paiadigms consume too much memoiy
and machine cycles. Howevei, in the yeai 2000, veiy high-speed machines will be commonplace. What will we
use these poweiful machines foi: One answei is that we will no longei be conceined with the execution effciency
of a HLL. The diawbacks of functional and declaiative languages will fade, to be ieplaced by concein foi the
coiiectness and expiessiveness of the HLL. If this occuis, functional and declaiative languages will be the
piefeiied HLLs because of the elevated abstiactions suppoited by the functional and declaiative paiadigms.
Applications constiucted fiom these HLLs will exhibit moie sophisticated logic, communicate in non-numeiic
languages such as speech and giaphics, and solve pioblems that aie beyond the ieach of cuiient HLLs.
Object-oiientation is an appealing idea whose time has come. OOP will be to the 1990s what stiuctuied
piogiamming was to the 1970s. Computei haidwaie is becoming incieasingly distiibuted and iemote. Netwoiks
of woikstations ioutinely solve pioblems in conceit iathei than as stand-alone systems. This places gieatei
demands on exibility and functionality of applications. Considei the next step beyond objects-seiveis:
Seivei Object - Piocess
A seivei is an object that is instantiated as an opeiating system piocess oi task. The seivei sends messages
to othei seiveis to get woik done. The seiveis live" on any piocessoi located anywheie on the netwoik. Softwaie
is distiibuted and so is the woik. OOP offeis the gieatest hope foi distiibuting applications in this fashion
without loss of contiol. Should this scenaiio come tiue, the OOP paiadigm will not only be appiopiiate, but
contiibute to gieatei HLL leveiage thiough ieusable objects, distiibuted seiveis, and delayed binding of methods
to these seiveis.
Object-oiiented languages, databases, and opeiating systems aie on the immediate hoiizon. Giaphical usei-
inteiface seiveis such as X-Windows alieady exist, lending ciedibility to this scenaiio. At least foi the neai
futuie, HLLs aie most likely to become identical with the object-oiiented paiadigm.
2000 by CRC Press LLC
Dehning Terms
Abstract class: A class consisting of only an inteiface specifcation. The implementation pait is unspecifed,
because the puipose of an abstiact class is to establish an inteiface.
Abstraction: Abstiaction in computei languages is a measuie of the amount of sepaiation between the
haidwaie and an expiession of a piogiamming idea. The level of abstiaction of a high-level language
defnes the level of that language.
ADT: An abstiact data type (ADT) is a softwaie module that encapsulates data and functions allowed to be
peifoimed on that data. ADTs also sepaiate the inteiface specifcation of a module fiom the implemen-
tation pait to minimize coupling among modules.
Assembler: A computei piogiam foi tianslating symbolic machine instiuctions into numeiical machine
instiuctions. Assembleis aie consideied low-level languages foi piogiamming a computei.
Class: A specifcation foi one oi moie objects that defnes state (data) and functions (algoiithms) that all
objects may inheiit when cieated fiom the class. A class is a template foi implementing objects.
Compiler: A computei piogiam that tianslates the souice piogiam statements of a high-level language into
lowei-leveled object piogiam statements. Compileis diffei fiom inteipieteis in that they do not imme-
diately peifoim the opeiations specifed in the souice piogiam. Instead, a compilei pioduces an object
piogiam that in tuin peifoims the intended opeiations when it is iun.
Coupling: A measuie of the amount of inteiaction between modules in a computei piogiam. High coupling
means that a change in one module is likely to affect anothei module. Low coupling means theie is little
impact on othei modules whenevei a change is made in one module.
Declarative paradigm: A piogiamming paiadigm in which the woild is modeled as a collection of iules and
constiaints.
Delayed binding: The piocess of postponing the meaning of a piogiamming object until the object is
manipulated by the computei. Delayed binding is used by inteipieteis and compileis, but moie often it
is employed by inteipieteis.
Functional paradigm: A piogiamming paiadigm in which the woild is modeled as a collection of mathe-
matical functions.
HLL (also HOL): A HLL is a set of symbols which obey unambiguous syntactic and semantic iules: the
syntactic iules specify legal combinations of symbols, and the semantic iules specify legal meanings of
syntactically coiiect statements ielative to a collection of abstiactions.
Implementation part: The defnition oi algoiithm foi a piogiamming module which gives the details of how
the module woiks.
Instance variables: Data encapsulated by an object.
Interface specincation: The defnition of a piogiamming module without any indication of how the module
woiks.
Interpreter: A computei piogiam that tianslates and peifoims the intended opeiations of the souice statements
of a high-level language piogiam. Inteipieteis diffei fiom compileis in that they immediately peifoim the
intended opeiations specifed in the souice piogiam, and they do not pioduce an object piogiam.
Member nelds: Instance vaiiables of a C-- object.
Member functions: Methods defned on a C-- object.
Metalanguage: A foimal language foi defning othei languages. A metalanguage is typically used to defne
the syntax of a high-level language.
Methods: Functions allowed to be peifoimed on the data of an object.
Metric: A measuie of a computei piogiam`s complexity, claiity, length, diffculty, etc.
Object: An instance of a class. Objects have state (data) and function (algoiithms) that aie allowed to
manipulate the data.
Object-oriented paradigm: A piogiamming paiadigm in which the woild is modeled as a collection of self-
contained objects that inteiact by sending messages. Objects aie modules that contain data and all
functions that aie allowed to be peifoimed on the encapsulated data. In addition, objects aie ielated to
one anothei thiough an inheiitance hieiaichy.
Object program: Machine foim of a computei piogiam, which is the output fiom a tianslatoi.
2000 by CRC Press LLC
Paradigm: An idealized model, typically used as a conceptual basis foi softwaie design. Piogiamming paia-
digms dictate the appioach taken by a piogiammei to oiganize, and then wiite, a computei piogiam.
Procedural paradigm: A piogiamming paiadigm in which the woild is modeled as a collection of pioceduies
which in tuin encapsulate algoiithms.
Prototyping: A simplifed veision of a softwaie system is a piototype. Piototyping is the piocess of designing a
computei piogiam thiough a seiies of veisions; each veision becomes a closei appioximation to the fnal one.
Pushdown stack: A data stiuctuie containing a list of elements which aie iestiicted to inseitions and deletions
at one end of the list, only. Inseition is called a push opeiation and deletion is called a pull opeiation.
Recursion: A pioceduie is called iecuisive if it calls itself.
Reuse: Piogiamming modules aie ieused when they aie copied fiom one application piogiam and used in
anothei. Reusability is a piopeity of module design that peimits ieuse.
Semantics: The pait of a foimal defnition of a language that specifes the meanings attached to a syntactically
coiiect statement in the language.
Source program: Symbolic foim of a computei piogiam, which is the input to a tianslatoi.
Syntax : The pait of a foimal defnition of a language that specifes legal combinations of symbols that make
up statements in the language.
Re!ated Tupic
87.1 Assembly Language
Relerences
T. Budd, O|,et-OreneJ Programmng, Reading, Mass.: Addison-Wesley, 1991.
M.A. Ellis and B. Stioustiup, T|e nnoaeJ C++ Re[erente Manua|, Reading, Mass.: Addison-Wesley, 1990.
M.H. Halstead, E|emens o[ So[ware Stente, New Yoik: Elseviei Noith-Holland, 1977.
T.G. Lewis, CSE. Comuer-JeJ So[ware Engneerng, New Yoik: Van Nostiand Reinhold, 1991.
P. Smith, Exer Sysem Dee|omen n Pro|og anJ Tur|o-Pro|og, New Yoik: Halsted Piess, 1988.
P.H. Winston and B.K.P. Hoin, LISP, Reading, Mass.: Addison-Wesley, 1989.
87.3 Data Types and Data Structures
jo|onne j. Morrn
The study of Jaa yes and Jaa srutures is a pait of the discipline of computei piogiamming. The teims
iefei to the two aspects of data objects: theii usage and theii implementation, iespectively. The study of data
yes deals with the Jenftaon of (abstiact) data objects in the context of a piogiamming pioject and with
methods of theii moie oi less foimal setftaon; the study of data srutures, on the othei hand, is conceined
with the m|emenaon of such objects using alieady existing data objects as iaw mateiial.
Concietely, the aiea addiesses a basic pioblem of piogiamming: the ieduction of complex objects, such as
vectois, tensois, text, giaphic images, sound, functions, diiectoiies, maps, coipoiate oiganizations, models of
ecosystems oi machineiy, oi anything else that a piogiam may have to deal with, to the only native objects of
digital computeis: aiiays of binaiy digits (bits). The fundamental pioblem of this ieduction is managing
piogiam complexity. Two oiganizational tools aie essential to its solution: abstiaction and hieiaichical stiuc-
tuiing. Abstiaction iefeis to the sepaiation of w|a computational objects aie used foi fiom |ow they aie
ieduced to (i.e., implemented by means of) simplei ones. Hieiaichical stiuctuiing iefeis to bieaking this
ieduction up into small manageable steps. Thiough seveial steps of abstiaction moie and moie complex objects
aie constiucted, each one ieduced to the pievious, simplei geneiation of objects. This piocess ends when the
desiied objects have been composed.
Abstract Data Types
An abstract data type is one oi moie ses of computational objects togethei with some basic opeiations on
those objects. One of these sets is defned by the type eithei by enumeiation oi by geneiating opeiations and
2000 by CRC Press LLC
is called the tarrer se of the type. Customaiily it is given the same name as the type. All othei sets aie called
auxiliaiy sets of the type. In exceptional cases a type may have moie than one caiiiei set.
The heait of the specifcation of an abstiact data type is the defnition of its functions, theii syntax and
semantics. Theii syntax is specifed by theii functionalities and theii semantics by algebiaic axioms. Foi moie
details see, e.g., Maitin, 1986].
With sets A and B, the expiession A B denotes the set of all functions that have the domain A and the
codomain B. Functions f e A B (tiaditionally denoted by f: A B) aie said to have the functionality A B.
The collection of basic opeiations does not need to be minimal. It should, howevei, be iich enough so that
all othei opeiations that one might wish to peifoim on the objects of the caiiiei set can be expiessed exclusively
by these basic opeiations. The type Boo|ean, foi example, consists of the set of Boolean values, Boolean {tiue,
false, with, e.g., the opeiations no, anJ, and or.
In geneial, things aie not quite this simple. To be useful foi piogiamming puiposes, even the type Boolean
iequiies at least one additional function. This function, called a tonJona| exresson, piovides a choice of one
of two given values depending on a given Boolean value. It has the foim:
f: Boolean 3 SomeType 3 SomeType SomeType
and, with a, b e SomeType, is defned by:
f (tiue, a, b) a and
f (false, a, b) b
The syntactical foim of conditional expiessions vaiies foi diffeient piogiamming languages that piovide this
constiuct. Foi example, in the language C it has the foim:
Boolean : SomeType : SomeType. / with the iesult type of SomeType /
The set SomeType is an auxiliaiy set of the type Boolean.
Fundamenta! Data Types
The fundamental types listed next aie suppoited by almost all modein high-level piogiamming languages
(iefeience books on Pascal, Modula II, C, and Ada aie listed among the iefeiences at the end of this section):
Integei, Real (sometimes called Float), Chaiactei, and Boolean
Since theii caiiiei sets aie oideied (one of the opeiations of these types is s), these types aie also called scalai
types. All piovide opeiations foi compaiing values; in addition, Integei and Real come equipped with the usual
aiithmetic opeiations (-, -, , /) and Boolean with the basic logical opeiations (not, and, oi). Most computeis
suppoit these opeiations by haidwaie instiuctions. Thus, while bit aiiays aie the oiiginal native objects of a
digital computei, the fundamental scalai types may be viewed as the given elementaiy building blocks foi the
constiuction of all othei types.
Type Cunstructurs
Enumerated Types
Beginning with Pascal, modein languages piovide a iathei useful constiuctoi foi scalai types, called enumeiated
types. Enumeiated types have fnite (small) caiiiei sets that the piogiammei defnes by enumeiating the
constants of the type (specifed as identifeis). Foi example, if the type Boolean weie not available in Pascal,
its caiiiei set could simply be defned by:
type Boolean (false, tiue)
In Pascal, enumeiated types aie automatically equipped with opeiations foi compaiison as well as with the
functions succ and pied, i.e., successoi and piedecessoi. In the above example, succ(false) tiue, pied(tiue)
false; succ(tiue) and pied(false) aie not allowed.
2000 by CRC Press LLC
Recurds
Values of scalai types can be aiianged into tuples by a constiuct called a retorJ (also called a sruture in some
languages). Into piogiamming languages, iecoids intioduce the equivalent of the Caitesian pioduct. Tuples
can be viewed as abstiact data types. Considei paiis as an example:
The type Pars.
Caiiiei set: Pars,
Auxiliaiy sets: , B;
Opeiations: paii e 3 B Pars;
fist e Pars ;
scnd e Pars B;
wheie V a e , b e B
fist (paii (a, b)) a; scnd (paii(a, b)) b;
Using Pascal notation this type is defned by:
type Paiis record fist: A; scnd: B end
By pioviding the so-called feld names, fist and scnd, the piogiammei implicitly chooses the names foi the
selectoi functions. Pascal does not piovide the function paii." Instead, it peimits the declaiation of vaiiables
of type Paiis whose component values may be set (by assignment) and ietiieved:
p: Paiis; {declaiation of p as a vaiiable of type Paiis
p.fist : a; p.scnd : b; {p now has the value of paii(a,b)" above
if p.fist x then . . . else . . . {p.fist is Pascals notation foi fist(p)"
The sets A and B can be of any type including iecoids. Fuitheimoie, since theie is no iestiiction on the
numbei of felds iecoids may have, they can iepiesent aibitiaiy tuples.
Arrays
Aiiays peimit the constiuction of simple function spaces, I A. The domain I, a scalai type-in some languages
iestiicted to a subset {0, 1, ..., n of the integeis-is called the index set of the aiiay; A is an aibitiaiy type. In
Pascal, the mathematical notation f e I A assumes the foim:
f: arrayI] of A
I has to be a fnite scalai type (e.g., 0 .. 40). The function f can now be defned by associating values of type A
with the values of the domain I using the assignment opeiation:
fi] : a; wheie i e I and a e A
Application of f to a value j e I is expiessed by fj]. This expiession has a value of type A and can be used
as such.
As with iecoids, Pascal allows the naming of the function space I A by the defnition:
type myFunctions arrayI] of A
and the subsequent declaiation of a specifc function (aiiay) by:
f : myFunctions
Functions of seveial aiguments aie iepiesented by so-called multidimensional aiiays:
f e I
1
3 . . . 3 I
n
A
is defned by
f : arrayI
1
, . . ., I
n
] of A
2000 by CRC Press LLC
Yariant Recurds
Vaiiant iecoids model Js,on (also called aggeJ) unons. In contiast to an oidinaiy union C A . B, a disjoint
union D A - B is foimed by tagging the elements of A and B befoie foiming D such that elements of D can
be iecognized as elements of A oi of B. In piogiamming, this amounts to cieating vaiiables that can be used
to house values of both type A and type B. A tag feld, (usually) pait of the vaiiable, is used to keep tiack of
the type of the value cuiiently stoied in the vaiiable. In Pascal, D A - B is expiessed by:
type tagType (inA, inB) {an enumeiated type;
D record
case kind: tagType of
inA: (aValue: A);
inB: (bValue:B);
end.
Vaiiables of type D aie now used as follows:
mix: D; {mix is declaied to be of type D
mix.kind : inA;
mix.aValue : a;
. . .
if mix.kind inA
then {do something with mix.aValue, which is of type A
else {do something with mix.bValue, which is of type B
Conceptually, only one of the two felds, mix.aValue oi mix.bValue, exists at any one time. The piopei use
of the tag is policed in some languages (e.g., Ada) and left to the piogiammei in otheis (e.g., Pascal).
An Examp!e ul a Lser-Dehned Abstract Data Type
Most caiiiei sets aie assumed to contain a distinguished value: error. Error is not a piopei computational object:
a function is consideied to compute error if it does not ietuin to the point of its invocation due to some eiioi
condition. Functions aie called strict if they compute the value error whenevei one oi moie of theii aiguments
have the value error.
The following example models a cafeteiia tiay stack with the following opeiations:
1. cieate a new stack with n tiays;
2. iemove a tiay fiom a stack;
3. add a tiay to the stack;
4. check if the stack is empty;
Specifcation:
Cs (cafeteiia tiay stacks) is the caiiiei set of the type;
Boo|ean and Ineger aie auxiliaiy sets;
newStack, iemove, add, isEmpty aie the opeiations of the type
wheie
newStack e Integei Cts; {cieate a stack of n tiays
iemove, add e Cts Cts;
isEmpty e Cts Boolean;
Axioms (logical expiessions that desciibe the semantics of the opeiations): All functions aie stiict and, foi
all non-negative values of n,
1. iemove (newStack(n)) if n 0 then error else newStack(n - 1);
2. add(newStack(n)) newStack(n - 1);
3. isEmpty (newStack(n)) (n 0).
2000 by CRC Press LLC
These axioms suffce to desciibe the desiied behavioi of Cts exactly, i.e., using these axioms, aibitiaiy
expiessions built with the above opeiations can be ieduced to error oi newStack(m) foi some m.
Im|emenaon.
Foi the iepiesentation of Cts (i.e. its data stiuctuie) we will choose the type Integei.
type Cts integei;
function newStack(n: Integei):Integei;
begin if n < 0 then eiioi ('n must be > 0`) else newStack : n end;
function iemove (s: Cts):Cts;
begin if s 0 then eiioi ('stack empty`) else iemove : s - 1 end;
function add (s: Cts):Cts; begin add : s - 1 end;
function isEmpty (s: Cts):Boolean; begin isEmpty : (s 0) end;
Above, eiioi" is a function that piints an eiioi message and stops the piogiam.
Dynamic Types
The caiiiei sets of dynamic types contain objects of vastly diffeient size. Foi these types, vaiiables (memoiy
space) must be allocated dynamically, i.e., at iun time, when the actual sizes of objects aie known. Examples
foi dynamic types aie chaiactei stiings, lists, tiee stiuctuies, sets, and giaphs. A classical example of a dynamic
type is a special type of a list: a queue. As the name suggests, a queue object is a sequence of othei objects with
the paiticulai iestiictive piopeity that objects aie inspected at and iemoved fiom its fiont and added to its ieai.
Specihcatiun ul Queues
Caiiiei set: Queues
Auxiliaiy sets: Boo|ean, ( contains the items to be queued)
Opeiations: newQueue, isEmpty, queue, pop, fiont
1. newQueue e Queues; {a new, empty queue
2. isEmpty e Queues Boo|ean; {check if a queue is empty
3. queue e Queues Queues; {add an object to the ieai of a queue
4. fiont e Queues ; {ietuin fiont element foi inspection
5. pop e Queues Queues; {iemove fiont element fiom a queue
Axioms: All functions aie stiict and, foi a e A and s e Queues,
isEmpty (newQueue); (i.e. isEmpty (newQueue) is tiue)
not isEmpty (queue(a, s)); (i.e. isEmpty (queue(a, s)) is false)
pop (newQueue) eiioi;
pop (queue(a, s)) if s newQueue
then newQueue else queue(a, pop(s));
fiont(newQueue) eiioi;
fiont(queue(a, s)) if s newQueue
then a else fiont(s).
Imp!ementatiun ul Queues
The following implementation iepiesents queues of the foim queue(a, s) by the oideied paii (a, s) and a new,
empty queue by the null paii n|. Foi the moment we assume that a data type, Pars, which piovides paiis on
demand at iun time, alieady exists and is defned as follows:
Specihcatiun ul Pairs
Caiiiei Set: Pars
Auxiliaiy sets: Boo|ean,
2000 by CRC Press LLC
Opeiations: nil, isNil, paii, fist, scnd
1. nil e Paiis, {a distinguished paii, the null paii
2. isNil e Paiis Boolean, {test foi the null paii
3. paii e A 3 Paiis Paiis, {combine an item and a paii to a new paii
4. fist e Paiis A, {the fist component, i.e., the item
5. scnd e Paiis Paiis, {the second component, i.e., the paii
Axioms: All functions aie stiict and, foi a e A and p e Paiis,
isNil(nil); (i.e. isNil(nil) is tiue)
not isNil(paii(a,p)); (i.e. isNil(paii(a,p)) is false)
fist (nil) eiioi;
fist(paii(a,p)) a;
scnd (nil) eiioi;
scnd(paii(a,p)) p;
With paiis, queues may now be implemented as follows:
type Queues Paiis;
function newQueue : Queues; begin newQueue : nil end;
function isEmpty (s : Queues) : Boolean; begin isEmpty : (s nil) end;
function queue (x : A; s : Queues) : Queues; begin queue : paii(x, s) end;
function pop (s : Queues) : Queues;
begin
if isNil(s)
then eiioi ('cannot pop empty queue`)
else if scnd(s) nil
then pop : nil
else pop : paii (fist(s), pop (scnd(s)))
end;
function fiont (s : Queues) : A;
begin
if isNil(s)
then eiioi ('an empty queue does not have a fiont`)
else if scnd(s) nil
then fiont : fist(s)
else fiont : fiont(scnd(s))
end;
The logic of these piogiams echoes the axioms. Such implementations aie sometimes not veiy effcient but
useful foi piototype piogiams, since the piobability of theii coiiectness is veiy high. The queues behave as
a|ues, i.e., the functions queue(a,s) and pop(s) do not modify the queues s but compute new queues; aftei the
execution of, e.g., s1 : pop(s) theie aie two independent queue values, s and s1. This is exactly the behavioi
postulated by the axioms. Howevei, piactical applications fiequently deal with mua||e o|,ets, objects that can
be modifed. With mutable objects memoiy may often be used moie effciently, since it is easiei to decide when
a memoiy cell, used, e.g., foi stoiing an oideied paii, is no longei needed and thus may be iecycled. If queues
aie viewed as mutable objects, the opeiations queue and o aie implemented as pioceduies that modify a
queue. In oidei to apply the style of axioms intioduced above foi the desciiption of mutable objects, these
objects aie best viewed as containeis of values. The roteJure queue(a, qobj), foi example, takes the queue
value, e.g., s, out of the containei qobj, applies the [unton queue(a, s) (desciibed by the axioms), and puts
the iesult back into qobj.
If moie than one place in a piogiam needs to maintain a iefeience to such an object, the object must be
implemented using a |eaJ te||. a stoiage location that iepiesents the object and that is not ieleased. The following
2000 by CRC Press LLC
implementation uses a head cell with two felds, one pointing to the fiont and one to the ieai of the queue.
We assume that the type Pars has two additional functions:
6. paiiH e Paiis 3 Paiis Paiis; {cieate a paii with 2 paii felds
7. fistH e Paiis Paiis; {ietiieve the fist feld of such a 2-paii cell
and thiee pioceduies:
8. setfistH ( p: Paiis; q: Paiis); {change the fistH feld of q to p
9. setscnd ( p: Paiis; q: Paiis); {change the scnd feld of q to p
10. delete (s: Paiis) {fiee the stoiage space occupied by s
type Queues Paiis;
procedure newQueue(vai q : Queues); begin q : paiiH(nil, nil) end;
function isEmpty (s : Queues) : Boolean; begin isEmpty : (fistH(s) nil) end;
procedure queue (x : A; s : Queues);
var temp: Paiis;
begin temp : paii(x, nil);
if isNil(fistH(s)) then setfistH(temp, s) else setscnd(temp, scnd(s));
setscnd (temp, s);
end;
function pop (s : Queues) : Queues;
var temp : Paiis;
begin
if isNil(fistH(s))
then eiioi ('cannot pop empty queue`)
else begin temp : fistH(s); setfistH(scnd(temp), s); delete(temp) end
end;
function fiont (s : Queues) : A;
begin
if isNil(fistH(s))
then eiioi ('an empty queue does not have a fiont`)
else fiont : fist(fistH(s))
end;
Compaied to the value implementation given eailiei, this implementation impioves the peifoimance of [ron
and o fiom O(n) to O(1).
An algoiithm has O(f(n)) (pionounced: oidei f(n) oi piopoitional to f(n)) time peifoimance if theie exists
a constant c, such that, foi aibitiaiy values of the input size n, the time that the algoiithm needs foi its
computation is t s c f(n).
Most modein piogiamming languages suppoit the implementation of the type ars (n-u|es) whose
instances can be cieated dynamically. It iequiies two opeiations, called new and Jsose in Pascal, that dynam-
ically allocate and deallocate vaiiables, iespectively. These opeiations depend on the concept of a re[erente (oi
oner), which seives as a name foi a vaiiable. Refeiences always occupy the same stoiage space independently
of the type of vaiiable they iefei to. The following implementation of Pars explains the concept.
type CellKind (headCell, bodyCell);
Paiis ^PaiiCell; {Paiis aie iefeiences to PaiiCells
PaiiCell record tail: Paiis;
case kind: CellKind of
headCell: (fint: Paiis);
bodyCell: (val: A)
end;
function paii(item: A; next:Paiis):Paiis;
vai p: Paiis;
2000 by CRC Press LLC
begin
new(p, bodyCell); {a new bodyCell" is cieated and accessible thiough p
p^.kind : bodyCell; p^.val : item; p^.tail : next; paii : p
end;
function fist(p: Paiis):A;
begin
if p nil then eiioi('...`)
else if p^.kind bodyCell then fist : p^.val else eiioi('...`)
end;
procedure setfistH(p, q:Paiis);
begin
if q nil then eiioi('...`)
else if q^.kind headCell then q^.fint : p else eiioi('...`)
end;
(Note: The Pascal constant nil denotes the null pointei, a iefeience to nothing.)
function isNil(p: Paiis): Boolean; begin isNil : (p nil) end;
The ieadei should have no diffculty flling in the iest of the implementation of Paiis. Most of the algoiithms
on dynamic data stiuctuies have been developed in the 1960s; still, an excellent iefeience is Knuth 1973].
Mure Dynamic Data Types
Stacks and Lists vith a Puint ul Interest
A queue is the type of (lineai) list used to iealize fist-come-fist-seived behavioi. In contiast, anothei lineai
type, the sat|, iealizes last-come-fist-seived behavioi. Sometimes it is necessaiy to scan a list object without
dismantling it. This is accomplished by giving the list a point of inteiest (see Fig. 87.14). This iequiies foui
additional opeiations:
iestait(l: List); {moves point of inteiest to beginning of list l
cuiient(l:List):A; {ietuins object at point of inteiest
advance(l:List); {advances point of inteiest by one towaid end of list l
endOfList(l : List): Boolean; {tiue, if end of list has been ieached
The type can be extended fuithei by allowing inseitions and deletions at the point of inteiest.
N-ary, Binary, and Genera! Trees
An n-ary tree is the smallest set containing the emy ree and all oideied n-1-tuples (a,
1
, ...,
n
) wheie a
is membei of some auxiliaiy set and the

aie n-aiy tiees. The element a is called the roo e|emen oi simply
the ioot of and the

aie called su|rees of .


Note that in this sense, a list is a unaiy tiee. Binaiy tiees used as seaichtiees access fnite oideied sets.
A binary searchtree is a tiee that accesses a set. A tiee attesses a set s if the ioot of is some element a of
s, and s
1
, called the left subtiee of , accesses the subset {xx e s anJ x < a; anJ s
2
, ta||eJ |e rg| su|ree o[ ,
attesses |e su|se ;xx e s anJ x > a;.
FIGURE 87.14 A list implementation with a point of inteiest and access to fiont and ieai.
2000 by CRC Press LLC
If the left and iight subtiees of the above defnition aie of similai size, then the time foi fnding an element
in the set is piopoitional to log(n) wheie n is the caidinality of the set.
Quateinaiy tiees (usually called quad tiees) and octonaiy tiees (called oct tiees) aie used to access two-
dimensionally and thiee-dimensionally oiganized data, iespectively. As with lists, the implementation of n-aiy
tiees is based on n-1-tuples. A minimal set of opeiations foi binaiy tiees includes:
nilTiee e Tiees; {the empty tiee, iepiesented by nil
isNil e Tiees Boolean; {test if tiee is empty
tiee e A 3 Tiees 3 Tiees Tiees; {build tiee fiom an item and subtiees
ioot e Tiees A; {ietiieve ioot item of tiee
left e Tiees Tiees; {ietiieve left subtiee
iight e Tiees Tiees; {ietiieve iight subtiee
A general tree is the smallest set containing all oidei paiis (a,s) wheie a is a membei of some auxiliaiy
set and s is a possibly empty list of geneial tiees. The element a is called the ioot element oi simply the ioot
of and the tiees in s aie called subtiees of .
Note that theie is no empty geneial tiee; the simplest tiee has a ioot and an empty list of subtiees. Geneial
tiees aie useful foi the iepiesentation of hieiaichical oiganizations such as the table of contents of a book oi
the oiganization of a coipoiation.
Functiuns, Sets, Re!atiuns, Graphs
Functions with ieasonably small domains can be iepiesented by aiiays, as desciibed eailiei. Similaily, sets
foimed fiom a ieasonably small univeisal set, ielations on small domains, and giaphs with not too many veitices
can be iepiesented by theii chaiacteiistic functions implemented as bit aiiays. In fact, Pascal piovides a type
constiuctoi foi sets that aie deiived fiom small univeisal sets.
Fiequently domains aie fai too laige foi this appioach. Foi example, the symbol table that a compilei of a
piogiamming language maintains is a function fiom the set of valid identifeis to some set of attiibutes. The
set of valid identifeis is infnite, oi, if some length limitation is imposed, fnite but exceedingly laige (theie aie
neaily 300 billion identifeis of eight chaiacteis oi less). Foi most of its domain a symbol table ietuins the
default value new oi no [ounJ. It is theiefoie economical to stoie the mapping only foi those domain values
that map to a value diffeient fiom the default value. A function of this soit is usually specifed as follows:
Setftaon o[ Funtons.
Caiiiei Set: Funtons
Auxiliaiy sets: Dom, CoJ (domain and codomain)
Opeiations: newFun, apply, update
1. newFun e Functions, (ietuins default eveiywheie)
2. apply e Functions 3 Dom Cod,
3. update e Functions 3 Dom 3 Cod Functions;
Axioms: All functions aie stiict and, foi x,z e Dom, y e Cod and f e Functions,
apply(newFun, x) default;
apply (update(f,x,y), z) if x z then y else apply (f, z);
An implementation based on these axioms amounts to iepiesenting a function as a list of those of its
individual mappings that diffei fiom Je[au| and leads to an O(1) peifoimance foi uJae and an O(n)
performance of a|y. Bettei is an implementation by binaiy seaichtiees with a peifoimance of O(log(n)) foi
both a|y and uJae.
Hashing
The fastest method foi the implementation of functions is |as| toJng oi |as|ng. By means of a |as| [unton,
h e Dom 0 .. k-1, the domain of the function is paititioned into k sections and each section is associated
with an index. Foi each paitition a simple list implementation is used and the lists aie stoied in an aiiay A:
2000 by CRC Press LLC
aiiay1 .. k-1]. In oidei to compute apply(f,x) oi update(f,x,y), the list at Ahash(x)] is seaiched oi updated.
If the hash function has been piopeily chosen and if k and the numbei of function values diffeient fiom default
aie of similai size, then the individual lists can be expected to be veiy shoit and independent of the numbei
of nondefault entiies of the function; thus peifoimance foi apply and update is O(1).
The above discussion applies also to sets, ielations, and giaphs, since these objects can be iepiesented by
theii chaiacteiistic functions.
Object-Oriented Prugramming
In languages that suppoit object-oiiented piogiamming, t|asses (i.e., types) of objects aie defned by specifying
(1) the vaiiables that each object will own as nsante ara||es and (2) opeiations, called me|oJs, applicable
to the objects of the class. As a diffeience in style, these methods aie not invoked like functions oi pioceduies,
but aie sen to an object as a message. The expiession wnJow moeTo : x : y] is an example of a message in
the piogiamming language O|,ete C, a dialect of C. Heie the object wnJow, which may iepiesent a window
on the scieen, is instiucted to apply to itself the method moeTo using the paiameteis x and y.
New objects of a class aie cieated-usually dynamically-by [atory me|oJs addiessed to the class itself.
These methods allocate the equivalent of a iecoid whose felds aie the instance vaiiables of the object and
ietuin a iefeience to this iecoid, which iepiesents the new object. Aftei its cieation an object can ieceive
messages fiom othei objects.
To data abstiaction, object-oiiented piogiamming adds the concept of inheiitance: fiom an existing class
new (sub)classes can be deiived by adding additional instance vaiiables and/oi methods. Each subclass inheiits
the instance vaiiables and methods of its supeiclass. This encouiages the use of existing code foi new puiposes.
As an example, considei a class of a |s objects. Each object has two instance vaiiables pointing to the fiont
and the ieai of the list. In Objective C, the specifcation of the inteiface foi this list class, i.e., the declaiation
of the instance vaiiables and headeis (functionalities) of the methods, has the following foim:
Cinteiface MyLists : Object / Object is the univeisal (system) class fiom which
all classes aie deiived diiectly oi indiiectly /
{ / declaiation of the instance vaiiables;
listRef fiont; listRef is the type of a pointei to a list assumed
listRef ieai; to be defned elsewheie /

- initList; / initializes instance vaiiables with null pointeis /


- (BOOL) isEmpty; / test foi empty list; note: paiametei |s is implied /
- add : (item) theThing; / item is the type of things on the list /
- pop;
- (item) fiont;
Cend
As a companion of the ner[ate fle theie is also an m|emenaon fle that contains the executable code foi
the methods of the class. A list with a point of inteiest can be defned as a subclass of MyList as follows:
Cinteiface ScanList : MyList / ScanList is made a subclass of MyList /
{ listRef pointOfInteiest;
- iestait;
- (BOOL)endOfList;
- advance;
- (Item)cuiient;
Cend
If we also need a list that can add and delete at the point of inteiest, we defne:
Cinteiface InseitionList : ScanList
{ / theie aie no new instance vaiiables /
2000 by CRC Press LLC
- inseit : (item) theThing;
- shiink; / iemoves item at the point of inteiest /
Cend
If a subclass defnes a method alieady defned in the supeiclass, the new defnition oveiiides the old one.
Suppose we need a list wheie items aie kept in ascending oidei:
Cinteiface SoitedList : MyList
{
- add : (item) theThing; / this veision of add inseits |eT|ng at the piopei
place to keep the list soited /
Cend
Dehning Terms
Abstract data type: One oi moie sets of computational objects togethei with some basic opeiations on those
objects. One of these sets is defned by the type eithei by enumeiation oi by geneiating opeiations and
is called the tarrer se of the type. Customaiily it is given the same name as the type. All othei sets aie
called auxiliaiy sets of the type. In exceptional cases a type may have moie than one caiiiei set.
Binary searchtree: A tiee that accesses a set. A tiee accesses a set s if the ioot of is some element a of s,
and s
1
, called the left subtiee of , accesses the subset {x x e s anJ x < a;, anJ s
2
, ta||eJ |e rg| su|ree
o[ , attesses |e su|se ;x x e s anJ x > a;.
Functionality: With sets A and B, the expiession A B denotes the set of all functions that have the domain
A and the codomain B. Functions f e A B (tiaditionally denoted by f: A B) aie said to have the
functionality A B.
General tree: The smallest set containing all oideied paiis (a, s) wheie a is membei of some auxiliaiy set
and s is a possibly empty list of geneial tiees. The element a is called the ioot element oi simply the ioot
of and the tiees in s aie called subtiees of .
n-ary tree: The smallest set containing the emy ree and all oideied n-1-tuples (a,
1
, . . .,
n
) wheie a
is membei of some auxiliaiy set and the

aie n-aiy tiees. The element a is called the ioot element oi


simply the ioot of and the

aie called subtiees of .


O(f(n)) performance: An algoiithm has O(f(n)) (pionounced: oidei f(n) oi piopoitional to f(n)) time
peifoimance if theie exists a constant c, such that, foi aibitiaiy values of the input size n, the time that
the algoiithm needs foi its computation is t s c f(n).
Strictness: Most caiiiei sets aie assumed to contain a distinguished value: error. Error is not a piopei
computational object: a function is consideied to compute error if it does not ietuin to the point of its
invocation due to some eiioi condition. Functions aie called srt if they compute the value error
whenevei one oi moie of theii aiguments have the value error.
Re!ated Tupic
90.3 Piogiamming Methodology
Relerences
A.V. Aho, Daa Srutures anJ |gor|ms, Reading, Mass.: Addison-Wesley, 1988.
T.H. Coimen, InroJuton o |gor|ms, New Yoik: McGiaw-Hill, 1995.
K. Jensen and N. Wiith, Pasta|. User Manua| anJ Reor, Beilin, Spiingei-Veilag, 1974.
B.W. Keinighan and D.M. Ritchie, T|e C Programmng Language, Englewood Cliffs, N.J.: Pientice-Hall, 1988.
D.E. Knuth, T|e r o[ Comuer Programmng, vol. 1, Reading, Mass.: Addison-Wesley, 1973, chap. 2.
J.J. Maitin, Daa Tyes anJ Daa Srutures, C.A.R. Hoaie, Seiies Ed., Englewood Cliffs, N.J.: Pientice-Hall
Inteinational, 1986.
NeXT Step Concepts, Chaptei 3, O|,ete C, NeXT Developeis` Libiaiy, NeXT Inc.
2000 by CRC Press LLC
S. Sahni, FunJamena|s o[ Daa Srutures n C++, Computei Science Piess, 1995.
R. Sedgewick, |gor|ms n C++, Reading, Mass.: Addison-Wesley, 1993.
B. Stioustiup, T|e C++ Programmng Language, Reading, Mass.: Addison-Wesley, 1991.
United States Depaitment of Defense, Re[erente Manua| [or |e Ja

Programmng Language, Washington D.C.:
U.S. Goveinment Piinting Offce, 1983.
N. Wiith, Programmng n MoJu|a-2, Beilin: Spiingei-Veilag, 1983.
Further Inlurmatiun
Theie is a wealth of textbooks on data stiuctuies. Papeis on special aspects of data types and theii ielation to
piogiamming languages aie found iegulaily in peiiodicals such as CM Transatons on Programmng Languages
anJ Sysems, |e Journa| o[ |e ssotaon [or Comung Mat|nery, IEEE Transatons on Comuers, IEEE
Transatons on So[ware Engneerng, oi ta In[ormata.

You might also like