Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

CHAPTER–2

NumberRepr
esent
ati
on:
Consi
derann-
bitv
ect
or
B=bn-
1….
.b1b0
Wher
ebi=0or1f or0≤i
≤n-
1.Thi
svect
orcanr
epr
esentunsi
gnedi
ntegerv
aluesVi
nthe
r o2n-
ange0t 1,wher
e
V( 1×2n-
B)=bn- 1+….
.+b1×21+b0×20
Tor epr
esentbot
hposi
ti
veandnegat
ivenumber
s.Thr
eesy
stemsar
e usedf
orr
epr
esent
ing
suchnumbers:
•Si
gn-
and-
magni
tude
•1’
s-compl
ement
•2’
s-compl
ement
 I
nal
lthr
eesy
stems,t
hel
eft
mostbi
tis0f
orposi
ti
venumber
sand1f
ornegat
ive
number
s.

 Thebel
ow f
igur
e i
l
lust
rat
esal
lthr
eer
epr
esent
ati
onsusi
ng4-
bitnumber
s.Posi
ti
ve
v
alueshav
e

i
dent
ical r
epr
esent
ati
ons i
n al
l sy
stems,but negat
ive v
alues hav
e di
ff
erent
r
epr
esent
ati
ons.

 I
nthesi
gn-
and-
magni
tudesy
stems,negat
ivev
aluesar
erepr
esent
edbychangi
ngt
he
most

si
gni
fi
cantbi
t(b3i
nfi
gur
e)f
rom 0t
o1i
ntheBv
ect
oroft
hecor
respondi
ngposi
ti
ve
v
alue.

 Forexampl
e,+5i
srepr
esent
edby0101,
and-
5isr
epr
esent
edby1101.

 I
n1’
s-compl
ementr
epr
esent
ati
on,negat
ivev
aluesar
eobt
ainedbycompl
ement
ing
eachbi
toft
hecor
respondi
ngposi
ti
venumber
.Thus,t
her
epr
esent
ati
onf
or-
3is
obt
ainedbycompl
ement
ingeachbi
tint
hev
ect
or0011t
oyi
eld1100.

 Hence,
the2’
scompl
ementofanumberi
sobt
ainedbyaddi
ng1t
othe1’
scompl
ement
oft
hatnumber
.
B Val
uesrepresented
b3b2b1b0 Si
gnand 1’s 2’s
magnitude compl ement compl ement
0111 +7 +7 +7
0110 +6 +6 +6
0101 +5 +5 +5
0100 +4 +4 +4
0011 +3 +3 +3
0010 +2 +2 +2
0001 +1 +1 +1
0000 +0 +0 +0
1000 -
0 -7 -8
1001 -
1 -6 -7
1010 -
2 -5 -6
1011 -
3 -4 -5
1100 -
4 -3 -4
1101 -
5 -2 -3
1110 -
6 -1 -2
1111 -
7 -0 -1
Fi
gur
e:binary
, si
gned–integerrepresentati
ons

Addi
ti
onofPosi
ti
venumber
s:-
Consi
deraddi
ngtwo1-bi
tnumber
s.Theresul
tsareshowni
nfi
gure.Notet
hatt
hesum of1
and1requi
resthe2-
bitv
ect
or10torepr
esenttheval
ue2.Wesaythatt
hesum i
s0andthe
car
ry-
outi
s1.

0 1 0 1

+0 +0 +1 +1

0 1 1 10

Car
ryout
Fi
gur
e:Addi
ti
onof1-
bitnumber
s.

Memor
ylocat
ionsandaddr
esses
 Numberandchar
act
eroper
ands,
aswel
lasi
nst
ruct
ions,
arest
oredi
nthememor
yofa
comput
er.

 Thememor
yconsi
stsofmanymi
l
li
onsofst
oragecel
l
s,eachofwhi
chcanst
oreabi
t
ofi
nfor
mat
ionhav
ingt
hev
alue0or1.

 Becauseasi
ngl
ebi
trepr
esent
sav
erysmal
lamountofi
nfor
mat
ion,bi
tsar
esel
dom
handl
edi
ndi
vi
dual
l
y.

 Theusual
appr
oachi
stodeal
wit
hthem i
ngr
oupsoff
ixedsi
ze.

 Fort
hispur
pose,t
hememor
yisor
gani
zedsot
hatagr
oupofnbi
tscanbest
oredor
r
etr
iev
edi
nasi
ngl
e,basi
coper
ati
on.

 Eachgr
oupofnbi
tsi
sref
err
edt
oasawor
dofi
nfor
mat
ion,andni
scal
l
edt
hewor
d
l
engt
h.The

 memor
yofacomput
ercanbeschemat
ical
l
yrepr
esent
edasacol
l
ect
ionofwor
dsas
showni
nfi
gur
e.

 Moder
ncomput
ershav
ewor
dlengt
hst
hatt
ypi
cal
l
yrangef
rom 16t
o64bi
ts.I
fthe
wor
dlengt
hofacomput
eri
s32bi
ts,asi
ngl
ewor
dcanst
orea32-
bit2’
scompl
ement
numberorf
ourASCI
Ichar
act
ers,
eachoccupy
ing8bi
ts.Auni
tof8bi
tsi
scal
l
edaby
te.

 Accessi
ngt
hememor
ytost
oreorr
etr
iev
easi
ngl
eit
em ofi
nfor
mat
ion,
eit
herawor
dor
aby
te,
requi
resdi
sti
nctnamesoraddr
essesf
oreachi
tem l
ocat
ion.

Fi
g:Amemor
ywor
d
BYTEADDRESSABI
LITY:
-
 Wenowhav
ethr
eebasi
cinf
ormat
ionquant
it
iest
odealwi
th:t
hebi
t,by
teandwor
d.A
by
tei
sal
way
s8bi
ts,
butt
hewor
dlengt
hty
pical
l
yrangesf
rom 16t
o64bi
ts.

 Thet
erm by
te-
addr
essabl
ememor
yref
erst
oassi
gni
ngdi
sti
nctaddr
essest
oeachby
te
l
ocat
ion.

 By
tel
ocat
ionshav
eaddr
esses0,
1,
2,….Thus,i
fthewor
dlengt
hoft
hemachi
nei
s32
bi
ts,successi
vewor
dsar
elocat
edataddr
esses0,
4,
8,
….,wi
theachwor
dconsi
sti
ngof
f
ourby
tes.

BI
G-ENDI
ANANDLI
TTLE-
ENDI
ANASI
GNMENTS:
-
Ther
ear
etwoway
sthatby
teaddr
essescanbeassi
gnedacr
osswor
ds,
asshown
i
nfi
g.
Big-endi
an:Iti
susedwhenlowerby
teaddr
essesar
eusedf
ort
hemor
esi
gni
fi
cantby
tes(
the
l
eftmostbytes)oft
heword.
Lit
tl
e-endi
an:I
tisusedf ort
heopposi
teor
der
ing,wheret
helowerby
teaddr
essesar
eused
f
orthelesssi
gnif
icantby
tes(t
her
ight
mostbyt
es)oftheword.
WORDALI
GNMENT:
-
Thewor
dlocat
ionshav
eal
i
gnedaddr
essesi
ngener
al,wor
dsar
esai
dtobeal
i
gnedi
n
memor
yift
heybegi
nataby
teaddr
esst
hati
samul
ti
pleoft
henumberofby
tesi
nawor
d.
Thememor
yofby
tesi
nawor
disapowerof2.Hence,i
fthewor
dlengt
his16(
2by
tes)
,
al
i
gnedwor
dsbegi
natby
teaddr
esses0,
2,
4,
…,andf
orawor
dlengt
hof64(
23by
tes)
,al
i
gned
wor
dsbegi
natby
tesaddr
esses0,
8,
16….

ACCESSI
NGNUMBERS,
CHARACTERS,
ANDCHARACTERSTRI
NGS:
-

 Anumberusual
l
yoccupi
esonewor
d.I
tcanbeaccessedi
nthememor
ybyspeci
fyi
ng
i
tswor
daddr
ess.Si
mil
arl
y,i
ndi
vi
dual
char
act
erscanbeaccessedbyt
hei
rby
teaddr
ess.

 I
nmanyappl
i
cat
ions,
iti
snecessar
ytohandl
echar
act
erst
ri
ngsofv
ari
abl
elengt
h.The
begi
nni
ngoft
hest
ri
ngi
sindi
cat
edbygi
vi
ngt
headdr
essoft
heby
tecont
aini
ngi
tsf
ir
st
char
act
er.Successi
veby
tel
ocat
ionscont
ainsuccessi
vechar
act
ersoft
hest
ri
ng.

 Ther
ear
etwoway
stoi
ndi
cat
ethel
engt
hoft
hest
ri
ng.Aspeci
alcont
rolchar
act
erwi
th
t
hemeani
ng“
endofst
ri
ng”canbeusedast
hel
astchar
act
eri
nthest
ri
ng,ora
separ
atememor
ywor
dlocat
ionorpr
ocessorr
egi
stercancont
ainanumberi
ndi
cat
ing
t
hel
engt
hoft
hest
ri
ngi
nby
tes.

Memor
yoper
ati
ons
 Bot
hpr
ogr
am i
nst
ruct
ionsanddat
aoper
andsar
est
oredi
nthememor
y.Toexecut
ean
i
nst
ruct
ion,t
hepr
ocessorcont
rolci
rcui
tsmustcauset
hewor
d(orwor
ds)cont
aini
ng
t
hei
nst
ruct
iont
obet
ransf
err
edf
rom t
hememor
ytot
hepr
ocessor
.Oper
andsand
r
esul
tsmustal
sobemov
edbet
weent
hememor
yandt
hepr
ocessor
.

 Thus,
twobasi
coper
ati
onsi
nvol
vi
ngt
hememor
yar
eneeded,
namel
y,Load(
orReador
Fet
ch)andSt
ore(
orWr
it
e).

 Thel
oadoper
ati
ont
ransf
ersacopyoft
hecont
ent
sofaspeci
fi
cmemor
ylocat
iont
o
t
hepr
ocessor
.Thememor
ycont
ent
sremai
nunchanged.Tost
artaLoadoper
ati
on,
the
pr
ocessorsendst
headdr
essoft
hedesi
redl
ocat
iont
othememor
yandr
equest
sthat
i
tscont
ent
sber
ead.Thememor
yreadst
hedat
ast
oredatt
hataddr
essandsends
t
hem t
othepr
ocessor
.

 Thest
oreoper
ati
ont
ransf
ersani
tem ofi
nfor
mat
ionf
rom t
hepr
ocessort
oaspeci
fi
c
memor
ylocat
ion,
dest
roy
ingt
hef
ormercont
ent
soft
hatl
ocat
ion.Thepr
ocessorsends
t
headdr
essoft
hedesi
redl
ocat
iont
othememor
y,t
oget
herwi
tht
hedat
atobewr
it
ten
i
ntot
hatl
ocat
ion.

I
nst
ruct
ionsandi
nst
ruct
ionsequenci
ng
Acomput
ermusthav
einst
ruct
ionscapabl
eofper
for
mingf
ourt
ypesofoper
ati
ons.
•Dat
atr
ansf
ersbet
weent
hememor
yandt
hepr
ocessorr
egi
ster
s
•Ar
it
hmet
icandl
ogi
coper
ati
onsondat
a
•Pr
ogr
am sequenci
ngandcont
rol
•I
/Ot
ransf
ers
REGI
STERTRANSFERNOTATI
ON:
-

 Tr
ansf
erofi
nfor
mat
ion f
rom one l
ocat
ion i
nthe comput
ert
o anot
her
.Possi
ble
l
ocat
ionst
hatmaybei
nvol
vedi
nsucht
ransf
ersar
ememor
ylocat
ionst
hatmaybe
i
nvol
vedi
nsucht
ransf
ersar
ememor
ylocat
ions,pr
ocessorr
egi
ster
s,orr
egi
ster
sin
t
heI
/O subsy
stem.Mostoft
het
ime,wei
dent
if
yal
ocat
ionbyasy
mbol
i
cname
st
andi
ngf
ori
tshar
dwar
ebi
nar
yaddr
ess.

 Exampl
e,namesf
ort
headdr
essesofmemor
ylocat
ionsmaybeLOC,
PLACE,
A,VAR2;
pr
ocessorr
egi
ster
s names may be R0,R5;and I
/O r
egi
sternames may be
DATAI
N,OUTSTATUS,andsoon.Thecont
ent
sofal
ocat
ionar
edenot
edbypl
aci
ng
squar
ebr
acket
sar
oundt
henameoft
hel
ocat
ion.Thus,
theexpr
essi
on

R1 [
LOC]
Meanst
hatt
hecont
ent
sofmemor
ylocat
ionLOCar
etr
ansf
err
edi
ntopr
ocessorr
egi
sterR1.
Asanot
herexample,consi
dertheoperat
iont
hataddsthecontent
sofr
egi
ster
sR1andR2,
andt
henpl
acestheirsum i
ntor
egist
erR3.Thi
sact
ioni
sindi
catedas
R3 [
R1]+[
R2]
Thi
sty
peofnot
ati
oni
sknownasRegi
sterTr
ansf
erNot
ati
on(
RTN
ASSEMBLYLANGUAGENOTATI
ON:
-

 Anot
hert
ypeofnot
ati
ont
orepr
esentmachi
nei
nst
ruct
ionsandpr
ogr
ams.Fort
his,
we
useanassembl
ylanguagef
ormat
.Forexampl
e,ani
nst
ruct
iont
hatcausest
het
ransf
er
f
rom memor
ylocat
ionLOCt
opr
ocessorr
egi
sterR1,
isspeci
fi
edbyt
hest
atement

Mov
eLOC,
R1

 Thecont
ent
sofLOCar
eunchangedbyt
heexecut
ionoft
hisi
nst
ruct
ion,butt
heol
d
cont
ent
sofr
egi
sterR1ar
eov
erwr
it
ten.

 Thesecondexampl
eofaddi
ngt
wonumber
scont
ainedi
npr
ocessorr
egi
ster
sR1and
R2andpl
aci
ngt
hei
rsum i
nR3canbespeci
fi
edbyt
heassembl
ylanguagest
atement

AddR1,
R2,
R3
BASI
CINSTRUCTI
ONS:
-

 Theoper
ati
onofaddi
ngt
wonumber
sisaf
undament
alcapabi
l
ityi
nanycomput
er.The
st
atement

C=A+B

 I
nahi
gh-
lev
ell
anguagepr
ogr
am i
sacommandt
othecomput
ert
oaddt
hecur
rent
v
aluesoft
het
wov
ari
abl
escal
l
edAandB,
andt
oassi
gnt
hesum t
oat
hir
dvar
iabl
e,C.
Whent
hepr
ogr
am cont
aini
ngt
hisst
atementi
scompi
l
ed,
thet
hreev
ari
abl
es,
A,B,
and
C,
areassi
gnedt
odi
sti
nctl
ocat
ionsi
nthememor
y.

 Thecont
ent
soft
hesel
ocat
ionsr
epr
esentt
hev
aluesoft
het
hreev
ari
abl
es.Hence,
the
abov
ehi
gh-
lev
ell
anguagest
atementr
equi
rest
heact
ion.

C [
A]+[
B]

 Tocar
ryoutt
hisact
ion,t
hecont
ent
sofmemor
ylocat
ionsAandBar
efet
chedf
rom
t
hememor
yandt
ransf
err
edi
ntot
hepr
ocessorwher
ethei
rsum i
scomput
ed.Thi
s
r
esul
tist
hensentbackt
othememor
yandst
oredi
nlocat
ionC.

 Letusf
ir
stassumet
hatt
hisact
ioni
sto beaccompl
i
shed byasi
ngl
emachi
ne
i
nst
ruct
ion.Fur
ther
mor
e,assumet
hatt
hisi
nst
ruct
ioncont
ainst
hememor
yaddr
esses
oft
het
hreeoper
ands–A,
B,andC.Thi
sthr
ee-
addr
essi
nst
ruct
ioncanber
epr
esent
ed
sy
mbol
i
cal
l
yas

AddA,
B,C

 Oper
andsAandBar
ecal
l
edt
hesour
ceoper
ands,Ci
scal
l
edt
hedest
inat
ionoper
and,
andAddi
stheoper
ati
ont
obeper
for
medont
heoper
ands.Agener
ali
nst
ruct
ionoft
his
t
ypehast
hef
ormat
.

Oper
ati
onSour
ce1,
Sour
ce2,
Dest
inat
ion

 Anal
ter
nat
iveappr
oachi
stouseasequenceofsi
mpl
eri
nst
ruct
ionst
oper
for
mthe
samet
ask,wi
theachi
nst
ruct
ionhav
ingonl
yoneort
wooper
ands.Supposet
hatt
wo-
addr
essi
nst
ruct
ionsoft
hef
orm

Oper
ati
onSour
ce,
Dest
inat
ion
AnAddi
nst
ruct
ionoft
hist
ypei
s
AddA,
B
Whi
chper
for
mst
heoper
ati
on B [
A]+[
B].
Using onl
yone-address i
nstr
ucti
ons,the oper
ati
on C [
A]+ [
B]can be per
for
med by
execut
ingthesequenceofinst
ruct
ions
LoadA
AddB
St
oreC
Someear
lycomput
erswer
edesi
gnedar
oundasi
ngl
eaccumul
atorst
ruct
ure.
LetRi
repr
esentagener
al-
pur
poser
egi
ster
.Thei
nst
ruct
ions
LoadA,
Ri
St
oreRi
,Aand
AddA,
Ri
Ar
egener
ali
zat
ionsoft
heLoad,St
ore,andAddi
nst
ruct
ionsf
ort
hesi
ngl
e-accumul
atorcase,
i
nwhi
chr
egi
sterRiper
for
mst
hef
unct
ionoft
heaccumul
ator
.Whenapr
ocessorhassev
eral
gener
al-
pur
poser
egi
ster
s,manyi
nst
ruct
ionsi
nvol
veonl
yoper
andst
hatar
eint
her
egi
ster
.In
f
act
,inmanymoder
npr
ocessor
s,comput
ati
onscanbeper
for
meddi
rect
lyonl
yondat
ahel
d
i
npr
ocessorr
egi
ster
s.

I
nst
ruct
ionssuchas
AddRi
,Rj
Or
AddRi
,Rj
,Rk
I
nbot
hoft
hesei
nst
ruct
ions,t
hesour
ceoper
andsar
ethecont
ent
sofr
egi
ster
sRiandRj
.In
t
he f
ir
sti
nst
ruct
ion,Rjal
so ser
vesast
he dest
inat
ion r
egi
ster
,wher
easi
nthe second
i
nst
ruct
ion,
athi
rdr
egi
ster
,Rk,
isusedast
hedest
inat
ion.

I
NSTRUCTI
ONEXECUTI
ONANDSTRAI
GHT-
LINESEQUENCI
NG:
-
Thef
igur
eshowsapossi
blepr
ogr
am segmentf
ort
hist
askasi
tappear
sint
hememor
yof
acomput
er.
Addr
ess Cont
ent
s

Begin i Mov
eA,
R0 3-i
nst
ruct
ion
executi
o progr
am
i
+4 AddB,
R0
nhere segment
i
+8 Mov
e R0,
C
.
.
.

.
.
.

B Dat
afor
t
he
.
.
.

 Thet
hreei
nst
ruct
ionsoft
hepr
ogr
am ar
einsuccessi
vewor
dlocat
ions,st
art
ingat
l
ocat
ioni
.si
nceeachi
nst
ruct
ioni
s4by
tesl
ong,
thesecondandt
hir
dinst
ruct
ionsst
art
ataddr
essesi
+4andi
+8.

 Thepr
ocessorcont
ainsar
egi
stercal
l
edt
hepr
ogr
am count
er(
PC)
,whi
chhol
dst
he
addr
essoft
hei
nst
ruct
iont
obeexecut
ednext
.

 Tobegi
nexecut
ingapr
ogr
am,t
headdr
essofi
tsf
ir
sti
nst
ruct
ion(
Iinourexampl
e)
mustbepl
acedi
ntot
hePC.Then,
thepr
ocessorcont
rolci
rcui
tsuset
hei
nfor
mat
ioni
n
t
hePC t
ofet
chandexecut
einst
ruct
ions,oneatat
ime,i
ntheor
derofi
ncr
easi
ng
addr
esses.Thi
siscal
l
edst
rai
ght
-l
inesequenci
ng.

 Dur
ingt
heexecut
ionofeachi
nst
ruct
ion,t
hePCi
sincr
ement
edby4t
opoi
ntt
othe
nexti
nst
ruct
ion.Thus,af
tert
heMov
einst
ruct
ionatl
ocat
ioni+8i
sexecut
ed,t
hePC
cont
ainst
hev
aluei+12,whi
chi
stheaddr
essoft
hef
ir
sti
nst
ruct
ionoft
henext
pr
ogr
am segment
.

 Execut
ingagi
veni
nst
ruct
ioni
sat
wo-
phasepr
ocedur
e.I
nthef
ir
stphase,cal
l
ed
i
nst
ruct
ionf
etch,t
hei
nst
ruct
ioni
sfet
chedf
rom t
hememor
ylocat
ionwhoseaddr
ess
i
sint
hePC.
 Thi
sinst
ruct
ion i
s pl
aced i
nthe i
nst
ruct
ion r
egi
ster(
IR)i
nthe pr
ocessor
.The
i
nst
ruct
ioni
nIRi
sexami
nedt
odet
ermi
newhi
choper
ati
oni
stobeper
for
med.

 Thespeci
fi
ed oper
ati
on i
sthen per
for
med byt
hepr
ocessor
.Thi
sof
ten i
nvol
ves
f
etchi
ng oper
ands f
rom t
he memor
yorf
rom pr
ocessorr
egi
ster
s,per
for
ming an
ar
it
hmet
icorl
ogi
coper
ati
on,
andst
ori
ngt
her
esul
tint
hedest
inat
ionl
ocat
ion.

BRANCHI
NG:
-

 Consi
dert
het
askofaddi
ngal
i
stofnnumber
s.I
nst
eadofusi
ngal
ongl
i
stofadd
i
nst
ruct
ions,
iti
spossi
blet
opl
aceasi
ngl
eaddi
nst
ruct
ioni
napr
ogr
am l
oop,
asshown
i
nfi
g.Thel
oopi
sast
rai
ght
-l
inesequenceofi
nst
ruct
ionsexecut
edasmanyt
imesas
needed.I
tst
art
satl
ocat
ionLOOPandendsatt
hei
nst
ruct
ionBr
anch>0.

 Dur
ingeachpasst
hrought
hisl
oop,t
headdr
essoft
henextl
i
stent
ryi
sdet
ermi
ned,
andt
hatent
ry i
s f
etched and addedt
o R0.
Assumet hatthenumberofent r
iesint heli
st,n,isstor
edinmemor ylocat
ionN,as
shown.Regi
sterR1isusedasacountertodet ermi
net henumberoft
imetheloopisexecuted.
Hence,t
hecontentsoflocat
ionNareloadedintoregi
st erR1att
hebegi
nningoftheprogram.
Then,wi
thi
nthebodyoft heloop,
thei
nstruct
ion.
Decr
ementR1

 Reducest
hecont
ent
sofR1by1eacht
imet
hrought
hel
oop.Thi
sty
peofi
nst
ruct
ion
l
oadsanewv
aluei
ntot
hepr
ogr
am count
er.

 Asar
esul
t,t
hepr
ocessorf
etchesandexecut
est
hei
nst
ruct
ionatt
hisnew addr
ess,
cal
l
edt
hebr
ancht
arget
,inst
eadoft
hei
nst
ruct
ionatt
hel
ocat
iont
hatf
oll
owst
he
br
anchi
nst
ruct
ioni
nsequent
ial
addr
essor
der
.

 A condi
ti
onalbr
anchi
nst
ruct
ioncausesabr
anchonl
yifaspeci
fi
ed condi
ti
oni
s
sat
isf
ied.I
fthecondi
ti
oni
snotsat
isf
ied,
thePCi
sincr
ement
edi
nthenor
malway
,and
t
henexti
nst
ruct
ioni
nsequent
ial
addr
essor
deri
sfet
chedandexecut
ed.

Br
anch>0LOOP

 (
branchi
fgr
eat
ert
hat0)i
sacondi
ti
onalbr
anchi
nst
ruct
iont
hatcausesabr
ancht
o
l
ocat
ionLOOP i
fther
esul
toft
hei
mmedi
atel
ypr
ecedi
ngi
nst
ruct
ion,whi
chi
sthe
decr
ement
edv
aluei
nregi
sterR1,i
sgr
eat
ert
hatzer
o.Thi
smeanst
hatt
hel
oopi
s
r
epeat
ed,asl
ongast
her
ear
eent
ri
esi
nthel
i
stt
hatar
eyett
obeaddedt
oR0.att
he
endoft
hent
hpasst
hrought
hel
oop,t
heDecr
ementi
nst
ruct
ionpr
oducesav
alueof
zer
o,andhence,
branchi
ngdoesnotoccur
.

CONDI
TIONCODES:
-
 Thepr
ocessorkeepst
rackofi
nfor
mat
ionaboutt
her
esul
tsofv
ari
ousoper
ati
onsf
or
usebysubsequentcondi
ti
onalbr
anchi
nst
ruct
ions.Thi
sisaccompl
i
shedbyr
ecor
ding
t
her
equi
redi
nfor
mat
ioni
nindi
vi
dualbi
ts,of
tencal
l
edcondi
ti
oncodef
lags.These
f
lagsar
eusual
l
ygr
oupedt
oget
heri
naspeci
alpr
ocessorr
egi
stercal
l
edt
hecondi
ti
on
coder
egi
sterorst
atusr
egi
ster
.Indi
vi
dualcondi
ti
oncodef
lagsar
esett
o1orcl
ear
ed
t
o0,
dependi
ngont
heout
comeoft
heoper
ati
onper
for
med.

Fourcommonl
yusedf
lagsar
e

 N(
negat
ive)Sett
o1i
fther
esul
tisnegat
ive;
other
wise,
clear
edt
o0

 Z(
zer
o)Sett
o1i
fther
esul
tis0;
other
wise,
clear
edt
o0

 V(
over
fl
ow)Setot
1ifar
it
hmet
icov
erf
lowoccur
s;ot
her
wise,
clear
edt
o0

 C(
car
ry)Sett
o1i
facar
ry-
outr
esul
tsf
rom t
heoper
ati
on;
other
wise,
clear
edt
o0

Addr
essi
ngmodes:
Thediff
erentway
si nwhi
chthelocat
ionofanoper
andi
sspeci
fi
edi
nani
nst
ruct
ionar
e
r
efer
redtoasaddr
essi
ngmodes.
Fi
g:Gener
icaddr
essi
ngmodes
I
mmedi
atemode–Theoper
andi
sgi
venexpl
i
cit
lyi
nthei
nst
ruct
ion.
Forexampl
e,t
hei
nst
ruct
ion
Mov
e200immediate,
R0
Pl
acest
hev
alue200i
nregi
sterR0.Cl
ear
ly,t
heI
mmedi
atemodei
sonl
yusedt
ospeci
fyt
he
v
alueofasour
ceoper
and.Acommonconv
ent
ioni
stouset
heshar
psi
gn(
#)i
nfr
ontoft
he
v
aluet
oindi
cat
ethatt
hisv
aluei
stobeusedasani
mmedi
ateoper
and.Hence,wewr
it
ethe
i
nst
ruct
ionabov
eint
hef
orm

Mov
e#200,
R0
Regi
stermode-Theoper
andi
sthecont
ent
sofapr
ocessorr
egi
ster
;thename(
addr
ess)of
t
her
egi
steri
sgi
veni
nthei
nst
ruct
ion.

Sy
ntax:
Ri
Addr
essi
ngf
unct
ionEA=Ri
Absol
utemode–Theoper
andi
sinamemor
ylocat
ion;t
headdr
essoft
hisl
ocat
ioni
sgi
ven
expl
i
cit
lyi
nthei
nst
ruct
ion.(
Insomeassembl
ylanguages,t
hismodei
scal
l
edDi
rect
).The
i
nst
ruct
ion

Mov
eLOC,
R2

Theabov
einst
ruct
ionusesr
egi
sterandabsol
uteaddr
essi
ngmodes.Pr
ocessorr
egi
ster
sar
e
usedast
empor
aryst
oragel
ocat
ionswher
ethedat
aisar
egi
sterar
eaccessedusi
ngt
he
Regi
stermode.TheAbsol
utemodecanr
epr
esentgl
obal
var
iabl
esi
napr
ogr
am.
I
ndi
rectmode–
 Ani
nst
ruct
iondoesnotgi
vet
heoper
andorni
tsaddr
essexpl
i
cit
ly.

 I
nst
ead,i
tpr
ovi
desi
nfor
mat
ionf
rom whi
cht
hememor
yaddr
essoft
heoper
andcan
bedet
ermi
ned.

 Theef
fect
iveaddr
essoft
heoper
andi
sthecont
ent
sofar
egi
sterormemor
ylocat
ion
whoseaddr
essappear
sint
hei
nst
ruct
ion.

 Toexecut
etheAddi
nst
ruct
ioni
nfi
gthepr
ocessorusest
hev
aluewhi
chi
sinr
egi
ster
R1,ast
heef
fect
iveaddr
essoft
heoper
and.I
trequest
sar
eadoper
ati
onf
rom t
he
memor
ytor
eadt
hecont
ent
sofl
ocat
ionB.t
hev
aluer
eadi
sthedesi
redoper
and,
whi
cht
hepr
ocessoraddst
othecont
ent
sofr
egi
sterR0.

I
ndexmode:
-
I
nthi
smode,
theef
fect
iveaddr
essoft
heoper
andi
sgener
atedbyaddi
ngaconst
antv
aluet
o
t
hecont
ent
sofar
egi
ster
.

The r
egi
steruse maybe ei
thera speci
alr
egi
sterpr
ovi
ded f
ort
his pur
pose,or
,mor
e
commonl
y,i
tmaybeanyoneofasetofgener
al-
pur
poser
egi
ster
sint
hepr
ocessor
.iti
s
r
efer
redt
oasi
ndexr
egi
ster
.Wei
ndi
cat
etheI
ndexmodesy
mbol
i
cal
l
yas

X(
Ri)

Wher
eXdenot
est
heconst
antv
aluecont
ainedi
nthei
nst
ruct
ionandRii
sthenameoft
he
r
egi
steri
nvol
ved.Theef
fect
iveaddr
essoft
heoper
andi
sgi
venby

EA=X+[
Rj]

Thecont
ent
soft
hei
ndexr
egi
sterar
enotchangedi
nthepr
ocessofgener
ati
ngt
heef
fect
ive
addr
ess.I
nanassembl
ylanguagepr
ogr
am,
theconst
antXmaybegi
venei
therasanexpl
i
cit
numberoras a sy
mbol
i
c name r
epr
esent
ing a numer
icalv
alue.An al
ter
nat
ive use i
s
i
l
lust
rat
edi
nfi
g.

Her
e,t
heconst
antX cor
respondst
oamemor
yaddr
ess,andt
hecont
ent
soft
hei
ndex
r
egi
sterdef
inet
heof
fsett
otheoper
and.I
nei
thercase,t
heef
fect
iveaddr
essi
sthesum of
t
wov
alues;
onei
sgi
venexpl
i
cit
lyi
nthei
nst
ruct
ion,
andt
heot
heri
sst
oredi
nar
egi
ster
.

Basewi
thi
ndexaddr
essi
ngmode:
Thi
smodeusest
wor
egi
ster
sandsecondoneasbaser
ergi
sterwhi
chcanbedenot
edas
(
Ri,
Rj)
I
nthi
scaset
heEf
fect
iveaddr
esscanbedenot
edas:
EA=[
Ri]
+[Rj
]
TheEffect
iveaddr
essist
hesum oft
hecont
ent
sofr
egi
ster
sRiandRj
.Thesecondr
egi
steri
s
usual
l
ycalledthe“
base”r
egi
ster
.
Basewi
thi
ndexandof
fsetmode:
I
nthi
smodet
hei
nst
ruct
ioncanbedenot
edas:
X(
Ri,
Rj)
TheEf
fect
iveaddr
esscanbewr
it
tenas:
EA=X+[
Ri]
+[Rj
]
I
nthi
scase,
theef
fect
iveaddr
essi
sthesum oft
heconst
antXandt
hecont
ent
sof
r
egi
ster
sRi
andRj
.
.
Rel
ati
vemode–Wehav
edef
inedt
heI
ndexmodeusi
nggener
al-
pur
posepr
ocessorr
egi
ster
s.
Ausef
ulv
ersi
onoft
hismodei
spr
ogr
am count
er,PC,i
susedi
nst
eadofagener
alpur
pose
r
egi
ster
.Then,
X(PC)canbeusedt
oaddr
essamemor
ylocat
iont
hati
sXby
tesawayf
rom t
he
l
ocat
ionpr
esent
lypoi
ntedt
obyt
hepr
ogr
am count
er.

Theef
fect
iveaddr
essi
sdet
ermi
nedbyt
heI
ndexmodeusi
ngt
hepr
ogr
am count
eri
npl
aceof
t
hegener
al-
pur
poser
egi
sterRi
.

EA=X+[
PC]

Aut
oincr
ementmode–
Theef
fect
iveaddr
essoft
heoper
andi
sthecont
ent
sofar
egi
sterspeci
fi
edi
nthei
nst
ruct
ion.
Af
teraccessi
ngt
heoper
and,t
hecont
ent
soft
hisr
egi
sterar
eaut
omat
ical
l
ytopoi
ntt
othe
nexti
tem i
nal
i
st.

(
Ri)
+

Aut
odecr
ementmode–

Thecont
ent
sofar
egi
sterspeci
fi
edi
nthei
nst
ruct
ionar
efi
rstaut
omat
ical
l
ydecr
ement
edand
ar
ethenusedast
heef
fect
iveaddr
essoft
heoper
and.

-
(Ri
)

Basi
cinput
/out
putoper
ati
ons
 At
askt
hatr
eadsi
nchar
act
eri
nputf
rom akey
boar
dandpr
oduceschar
act
erout
puton
adi
spl
ayscr
een.

 Asi
mpl
ewayofper
for
mingsuchI
/Ot
asksi
stouseamet
hodknownaspr
ogr
am-
cont
rol
l
edI
/O.
 Thekey
boar
dandt
hedi
spl
ayar
esepar
atedev
iceasshowni
ntheabov
efi
g.

 The act
ion ofst
ri
ki
ng a keyon t
he key
boar
d doesnotaut
omat
ical
l
ycause t
he
cor
respondi
ngchar
act
ert
obedi
spl
ayedont
hescr
een.

 Onebl
ockofi
nst
ruct
ionsi
ntheI
/Opr
ogr
am t
ransf
erst
hechar
act
eri
ntot
hepr
ocessor
,
andanot
herassoci
atedbl
ockofi
nst
ruct
ionscausest
hechar
act
ert
obedi
spl
ayed.

 St
ri
ki
ngakeyst
orest
hecor
respondi
ngchar
act
ercodei
nan8-
bitbuf
ferr
egi
ster

associ
atedwi
tht
hekey
boar
d.

 Toi
nfor
mthepr
ocessort
hatav
ali
dchar
act
eri
sinDATAI
N,ast
atuscont
rolf
lag,SI
N,
i
sset

t
o1.

 Apr
ogr
am moni
tor
sSI
N,andwhenSI
Nissett
o1,
thepr
ocessorr
eadst
hecont
ent
sof
DATAI
N.Whent
hechar
act
eri
str
ansf
err
edt
othepr
ocessor
,SI
Nisaut
omat
ical
l
y
cl
ear
edt
o0.

 I
fasecondchar
act
eri
sent
eredatt
hekey
boar
d,SI
Nisagai
nsett
o1,andt
he
pr
ocessorr
epeat
s.

 Ananal
ogouspr
ocesst
akespl
acewhenchar
act
ersar
etr
ansf
err
edf
rom t
hepr
ocessor
t
othedi
spl
ay.

 Abuf
ferr
egi
ster
,DATAOUT,
andast
atuscont
rolf
lag,
SOUT,
areusedf
ort
hist
ransf
er.
WhenSOUTequal
s1,
thedi
spl
ayi
sreadyt
orecei
veachar
act
er.

 I
nor
dert
oper
for
mI/
Otr
ansf
ers,weneedmachi
nei
nst
ruct
ionst
hatcancheckt
he
st
ateoft
hest
atusf
lagsandt
ransf
erdat
abet
weent
hepr
ocessorandt
heI
/Odev
ice.

 Thesei
nst
ruct
ionsar
esi
mil
ari
nfor
matt
othoseusedf
ormov
ingdat
abet
weent
he
pr
ocessorandt
hememor
y.

 Forexampl
e,t
hepr
ocessorcanmoni
tort
hekey
boar
dst
atusf
lagSI
Nandt
ransf
era
char
act
erf
rom DATAI
Ntor
egi
sterR1byt
hef
oll
owi
ngsequenceofoper
ati
ons.

READWAI
T--
-Br
ancht
oREADWAI
TifSI
N=0
I
nputf
rom DATAI
NtoR1
TheBranchoper
ati
onisimplementedbytwomachi
nei
nst
ruct
ions.Thef
ir
sti
nst
ruct
iont
est
s
thest
atusfl
agandthesecondperf
ormsthebr
anch.
Ananal
ogoussequenceofoper
ati
onsi
susedf
ort
ransf
err
ingout
putt
othedi
spl
ay.
Anexampl
eis
WRI
TEWAI
T__Br
ancht
oWRI
TEWAI
TifSOUT=0
Out
putf
rom R1t
oDATAOUT
Thef
ir
sti
nst
ruct
ionwai
tloopi
sexecut
edr
epeat
edl
yunt
ilt
hest
atusf
lagi
ssett
o1.

St
acksandqueues
I
nor
dert
oor
gani
zet
hecont
rolandi
nfor
mat
ionl
i
nkagebet
weent
hemai
npr
ogr
am andt
he
subr
out
ine,
adat
ast
ruct
urecal
l
edast
acki
sused.

Ast
acki
sal
i
stofdat
ael
ement
s,usual
l
ywor
dsorby
tes,wi
tht
heaccessi
ngr
est
ri
cti
ont
hat
el
ement
scanbeaddedorr
emov
edatoneendoft
hel
i
stonl
y.Thi
sendi
scal
l
edt
het
opoft
he
st
ack,andt
heot
herendi
scal
l
edt
hebot
tom.St
ackf
oll
owsl
ast
-i
n-f
ir
st-
out(
LIFO)st
ack
mechani
sm t
hel
astdat
ait
em pl
acedont
hest
acki
sthef
ir
stoner
emov
edwhenr
etr
iev
al
begi
ns.

Twooper
ati
onsar
eusedi
nast
ack.
Push:
Toaddanel
ementt
othest
ack
Pop:
Tor
emov
eanel
ementori
tem f
rom st
ack
Fi
gshowsast
ackofwor
ddat
ait
emsi
nthememor
yofacomput
er.
I
tcont
ainsnumer
ical
val
ues,
wit
h43att
hebot
tom and-
28att
het
op.

 Apr
ocessorr
egi
steri
susedt
okeept
rackoft
headdr
essoft
heel
ementoft
hest
ack
t
hati
satt
het
opatanygi
vent
ime.Thi
sregi
steri
scal
l
edt
hest
ackpoi
nter(
SP)
.It
coul
dbeoneoft
hegener
al-
pur
poser
egi
ster
sorar
egi
sterdedi
cat
edt
othi
sfunct
ion.

PUSH:
Subt
ract#4,
SP

MOVENEWI
TEM,
(SP)

 Wher
ethesubt
racti
nst
ruct
ionsubt
ract
sthesour
ceoper
and4f
rom t
hedest
inat
ion
oper
andcont
ainedi
nSP andpl
acest
her
esul
tinSP andmov
ethewor
dlocat
ion
NEWI
TEM ont
othet
opoft
hest
ack.

POP:MOVE(
SP)
,ITEM

ADD #4,
SP

 Theset
wo i
nst
ruct
ions mov
ethet
op v
aluef
rom t
hest
acki
ntoI
TEM and t
hen
i
ncr
ementt
hest
ackpoi
nterby4.
 I
fthepr
ocessorhast
heAut
oincr
ementandAut
odecr
ementaddr
essi
ngmodes,t
hen
t
hepushoper
ati
on,
popoper
ati
onar
eper
for
medbyt
hesi
ngl
einst
ruct
ion

Push:
MOVENEWI
TEM,
(SP)

Pop:
MOVE(
SP)
+,I
TEM

 Anot
herusef
uldat
ast
ruct
uret
hati
ssi
mil
art
othest
acki
scal
l
edaqueue.

 Dat
aar
est
oredi
nandr
etr
iev
edf
rom aqueueonaf
ir
st-
in-
fi
rst
-out(
FIFO)basi
s.

 Thus,i
fweassumet
hatt
hequeuegr
owsi
nthedi
rect
ionofi
ncr
easi
ngaddr
essesi
n
t
hememor
y,

 whi
chi
sacommonpr
act
ice,newdat
aar
eaddedatt
heback andr
etr
iev
edf
rom t
he
f
rontoft
hequeue.

Subr
out
ines
 I
nagi
venpr
ogr
am,
iti
sof
tennecessar
ytoper
for
m apar
ti
cul
arsubt
askmanyt
imeson
di
ff
erentdat
a-v
alues.Suchasubt
aski
susual
l
ycal
l
edasubr
out
ine

 Whenapr
ogr
am br
anchest
oasubr
out
inewesayt
hati
tiscal
l
ingt
hesubr
out
ine.The
i
nst
ruct
iont
hatper
for
mst
hisbr
anchoper
ati
oni
snamedaCal
li
nst
ruct
ion.

 Af
terasubr
out
inehasbeenexecut
ed,t
hecal
l
ingpr
ogr
am mustr
esumeexecut
ion,
cont
inui
ngi
mmedi
atel
yaf
tert
hei
nst
ruct
iont
hatcal
l
edt
hesubr
out
ine.

 Thesubr
out
inei
ssai
dtor
etur
ntot
hepr
ogr
am t
hatcal
l
edi
tbyexecut
ingaRet
urn
i
nst
ruct
ion.

 Thewayi
nwhi
chacomput
ermakesi
tpossi
blet
ocal
landr
etur
nfr
om subr
out
inesi
s
r
efer
redt
oasi
tssubr
out
inel
i
nkagemet
hod.

 Thesi
mpl
estsubr
out
inel
i
nkagemet
hodi
stosav
ether
etur
naddr
essi
naspeci
fi
c
l
ocat
ion,whi
chmaybear
egi
sterdedi
cat
edt
othi
sfunct
ion.Suchar
egi
steri
scal
l
ed
t
hel
i
nkr
egi
ster
.

 Whent
hesubr
out
inecompl
etesi
tst
ask,t
heRet
urni
nst
ruct
ionr
etur
nst
othecal
l
ing
pr
ogr
am bybr
anchi
ngi
ndi
rect
lyt
hrought
hel
i
nkr
egi
ster
.

 TheCal
linst
ruct
ioni
sjustaspeci
albr
anchi
nst
ruct
iont
hatper
for
mst
hef
oll
owi
ng
oper
ati
ons

 St
oret
hecont
ent
soft
hePCi
nthel
i
nkr
egi
ster

 Br
ancht
othet
argetaddr
essspeci
fi
edbyt
hei
nst
ruct
ion

 TheRet
urni
nst
ruct
ioni
saspeci
albr
anchi
nst
ruct
iont
hatper
for
mst
heoper
ati
on

 •
Brancht
otheaddr
esscont
ainedi
nthel
i
nkr
egi
ster.
SUBROUTI
NENESTI
NGANDTHEPROCESSORSTACK:
-

 Subr
out
inecal
lanot
hercal
l
edsubr
out
inenest
ing.I
nthi
scase,
ther
etur
naddr
ess
oft
hesecondcal
lisal
sost
oredi
nthel
i
nkr
egi
ster
,dest
roy
ingi
tspr
evi
ous
cont
ent
s.

 Hence,i
tisessent
ialt
osav
ethecont
ent
soft
hel
i
nkr
egi
steri
nsomeot
her
l
ocat
ionbef
orecal
l
inganot
hersubr
out
ine.Ot
her
wise,t
her
etur
naddr
essoft
he
f
ir
stsubr
out
inewi
l
lbel
ost
.

 Subr
out
inenest
ingcanbecar
ri
edoutt
oanydept
h.Ev
ent
ual
l
y,t
hel
ast

subr
out
inecal
l
edcompl
etesi
tscomput
ati
onsandr
etur
nst
othesubr
out
inet
hat
cal
l
edi
t.

PARAMETERPASSI
NG:
-

 Theexchangeofi
nfor
mat
ionbet
weenacal
l
ingpr
ogr
am andasubr
out
inei
s
r
efer
redt
oas

 par
amet
erpassi
ng.Par
amet
erpassi
ngmaybeaccompl
i
shedi
nsev
eral
way
s.

 Thepar
amet
ersmaybepl
acedi
nregi
ster
sori
nmemor
ylocat
ions,wher
ethey
canbeaccessedbyt
hesubr
out
ine.

 Al
ter
nat
ivel
y,t
hepar
amet
ersmaybepl
acedont
hepr
ocessorst
ackusedf
or
sav
ingt
her
etur
naddr
ess.

Cal
l
ingpr
ogr
am
MOVEN,
R1
MOVE#NUM1,
R2
Cal
lLI
STADD
MOVER0,
SUM
….
Subr
out
ine
LI
SADDcl
earR0
LoopADD(
R2)
+,R0
Decr
ementR1
Br
anch>0LOOP
Ret
urn.
I
ntheabov
efi
gur
eLI
STADDi
sthesubr
out
ine
 I
nthi
saddi
ngofl
i
stofnumber
scanbei
mpl
ement
edwi
tht
hepar
amet
erspassed
t
hroughr
egi
ster
s

 Thesi
zeoft
hel
i
stn,cont
ainedi
nmemor
ylocat
ionNandt
headdr
essNUM1of
t
hef
ir
stnumberar
epassedt
hroughr
egi
ster
sR1andR2

 Thesum comput
ebyt
hesubr
out
inei
spassedbackt
othecal
l
ingpr
ogr
am
t
hroughr
egi
sterR0.

 Thef
ir
stf
ouri
nst
ruct
ionsconst
it
utet
hepar
tsofcal
l
ingpr
ogr
am.

 Ther
etur
noper
ati
oni
sper
for
medbyt
hesubr
out
inet
hesum i
sst
oredi
nmemor
y
l
ocat
ionsum byt
hecal
l
ingf
unct
ion.

THESTACKFRAME:
-
 Dur
ing execut
ion oft
he subr
out
ine,some l
ocat
ions const
it
ute a pr
ivat
e
wor
kspacef
ort
hesubr
out
ine,cr
eat
edatt
he t
opoft
hest
ack.Suchspacei
s
cal
l
edast
ackf
rame.
 I
naddi
ti
ont
othest
ackpoi
nterSP,i
tisusef
ult
ohav
eanot
herpoi
nterr
egi
ster
,
cal
l
edt
hef
ramepoi
nter(
FP)
,forconv
eni
entaccesst
othepar
amet
erspassedt
o
t
hesubr
out
ineandt
othel
ocal
memor
yvar
iabl
esusedbyt
hesubr
out
ine.

Thesel
ocalv
ari
abl
esar
eonl
yusedwi
thi
nthesubr
out
ine,
soi
tisappr
opr
iat
etoal
l
ocat
e
spacef
or
t
hem i
nthest
ackf
rameassoci
atedwi
tht
hesubr
out
ine.

You might also like