01 - Programming Model - Tutorial 1 PDF
01 - Programming Model - Tutorial 1 PDF
A.Introduction
Objectives
Attheendofthislabyoushouldbeableto:
UsetheCPUsimulatortocreatebasicCPUinstructions
UsethesimulatortoexecutebasicCPUinstructions
UseCPUinstructionstomovedatatoregisters,comparevaluesinregisters,
pushdatatothestack,popdatafromthestack,jumptoaddresslocations
andaddvaluesheldinregisters.
ExplainthefunctionsofspecialCPUregisterssuchasthePC,SRandSP
registers.
B.Processor(CPU)Simulators
Thecomputerarchitecturetutorialsaresupportedbysimulators,whicharecreated
tounderpintheoreticalconceptsnormallycoveredduringthelectures.The
simulatorsprovidevisualandanimatedrepresentationofmechanismsinvolvedand
enablethestudentstoobservethehiddeninnerworkingsofsystems,whichwould
bedifficultorimpossibletodootherwise.Theaddedadvantageofusingsimulators
isthattheyallowthestudentstoexperimentandexploredifferenttechnological
aspectsofsystemswithouthavingtoinstallandconfiguretherealsystems.
C.BasicTheory
Theprogrammingmodelofcomputerarchitecturedefinesthoselowlevel
architecturalcomponents,whichincludethefollowing
CPUinstructionset
CPUregisters
Differentwaysofaddressinginstructionsanddataininstructions
Italsodefinesinteractionbetweentheabovecomponents.Itisthislowlevel
programmingmodelwhichmakesprogrammedcomputationspossible.
D.SimulatorDetails
Thissectionincludessomebasicinformationonthesimulator,whichshouldenable
thestudentstousethesimulator.Thetutor(s)willbeavailabletohelpanyone
experiencingdifficultyinusingthesimulator.Thesimulatorforthislabisan
applicationrunningonaPCrunningMSWindowsoperatingsystem.
Themainwindowiscomposedofseveralviews,whichrepresentdifferentfunctional
partsofthesimulatedprocessor.TheseareshowninImage1belowandare
composedof
CPUInstructionmemory
SpecialCPUregisters
CPU(generalpurpose)registers
Programstack
Programcreationandrunningfeatures
CPUInstruction
memoryview
Addprogram
instructionstab
Createprogramtab
CPUregistersview
SpecialCPU
registersview
Programlistview
Programstackview
Image1CPUSimulatorwindow
Thepartsofthesimulatorrelevanttothislabaredescribedbelow.Pleasereadthis
informationcarefullyandtrytoidentifythedifferentpartsontheCPUSimulator
windowBEFOREattemptingthefollowingexercises.Usethisinformationin
conjunctionwiththeexercisesthatfollow.
1.CPUinstructionmemoryview
Image2Instructionmemory
view
Thisviewcontainstheprogram
instructions. The instructions
are displayed as sequences of
lowlevel
instruction
mnemonics (assemblerlevel
format)andnotasbinarycode.
This is done for clarity and
makes code more readable by
humans.
2.SpecialCPUregistersview ThisviewshowsthesetofCPUregisters,
Image3SpecialCPU
registersview
whichhavepredefinedspecialistfunctions:
PC:ProgramCountercontainstheaddress
ofthenextinstructiontobeexecuted.
IR:InstructionRegistercontainsthe
instructioncurrentlybeingexecuted.
SR:StatusRegistercontainsinformation
pertainingtotheresultofthelastexecuted
instruction.
SP:StackPointerregisterpointstothevalue
maintainedatthetopoftheprogramstack
(seebelow).
BR:BaseRegistercontainscurrentbase
address.
MAR:MemoryAddressRegistercontains
thememoryaddresscurrentlybeing
accessed.
Statusbits:OV:Overflow;Z:Zero;N:
Negative
3
3.CPUregistersview
Image4 CPURegistersview
The register set view shows the
contents of all the generalpurpose
registers, which are used to maintain
temporary values as the program's
instructionsareexecuted.Registersare
very fast memories that hold
temporary values while the CPU
executesinstructions.
Thisarchitecturesupportsfrom8to64
registers. These registers are often
used to hold values of a program's
variables as defined in highlevel
languages.
Thisviewdisplayseachregister'sname
(Reg),itscurrentvalue(Val)andsome
additional values, which are reserved
for program debugging. It can also be
used to reset the individual register
values manually which is often useful
for advanced debugging. To manually
changearegisterscontent,firstselect
the register then enter the new value
inthetextbox,RegValue,andclickon
the CHANGE button in the Registers
tab.
4.Programstackview
Image5Programstackview
Theprogramstackisanotherareawhich
maintains temporary values as the
instructions are executed. The stack is a
LIFO(lastinfirstout)datastructure.Itis
often used for efficient interrupt
handling and subroutine calls. Each
programhasitsownindividualstack.
TheCPUinstructionsPSH(push)andPOP
are used to store values on top of stack
and pop values from top of stack
respectively.
5.Programlistview
Image6 ProgramListView
6.Programcreation
Image7 Createprogramtab
Tocreateanewprogramenterits
nameintheProgramNamebox
anditsbaseaddressintheBase
AddressboxthenclickontheADD
button.Thenewprogramsname
willappearintheProgramListview
(seeImage6).
Image8Addprogram
instructionstab
Use ADD NEW button to add a
new instruction; use EDIT
button to edit the selected
instruction; use MOVE DOWN/
MOVE UP buttons to move the
selected instruction down or up;
use INSERT ABOVE/INSERT
BELOWbuttonstoinsertanew
instruction above or below the
selectedinstructionrespectively.
Now,letusstart.FirstyouneedtoplacesomeinstructionsintheInstruction
MemoryView(seeImage2),representingtheRAMintherealmachine,before
executinganyinstructions.Todothis,followthestepsbelow:
IntheProgramtab(seeImage7),firstenteraProgramName,andthenenteraBase
Address(thiscanbeanynumber,butforthisexerciseuse100).ClickontheADD
button.AnewprogramnamewillbeenteredintheProgramListview(seeImage6).
YoucanusetheSAVEbuttontosaveinstructionsinafile.Youcanalsousethe
LOADbuttontoloadinstructionsfromafile.
YouarenowreadytoenterinstructionsintotheCPUSimulator.Youdothisby
clickingontheADDNEWbuttonintheInstructionstab(seeImage8).Thiswill
displaytheInstructions:CPU0window.Youusethiswindowtoselectandenterthe
CPUinstructions.Appendixlistssomeoftheinstructionsthissimulatorusesandalso
givesexamplesoftheirusage.
Now,haveagoatthefollowingactivities(enteryouranswersinthetextboxes
provided).Awordofcaution:Regularlysaveyourcodeinafileincasethesimulator
crashesinwhichcaseyoucanrestartthesimulatorandreloadyourfile.
1. Createaninstruction,whichmovesnumber5toregisterR00.
2. Executetheaboveinstruction(todothissimplydoubleclickonitinthe
InstructionMemoryView).ObservetheresultintheCPURegistersview(Image
4).
3. Createaninstruction,whichmovesnumber8toregisterR01.
4. Executeit(Youdothisbydoubleclickingontheinstruction)
5. ObservethecontentsofR00andR01intheCPURegistersview(Image4).
6. Createaninstruction,whichaddsthecontentsofR00andR01.
7. Executeit.
8. Observewhichregistertheresultisputin.
9.
Createaninstruction,whichpushestheaboveresulttothetopof
thehardwarestack,andthenexecuteit.
10. Createaninstructiontopushnumber2ontopofthestackandexecuteit.
ObservethevalueinProgramStack(Image5).
11. ObservethevalueintheSPregister(SpecialCPURegistersviewImage3).
WheneveryoupushavalueonProgramStack,theSPregisterisupdated.
12. CreateaninstructiontocomparethevaluesinregistersR00andR01.
13. Executeit.
14. ObservethevalueintheSRregister(SpecialCPURegistersviewImage3).
15. ObservethestatusoftheOV/Z/Npartsofthestatusregister.Whichboxesare
checkedandwhicharenot?Whatdotheyindicate?
16. Createaninstructiontounconditionallyjumptothefirstinstruction.
17. Executeit.
18. ObservethevalueinthePCregister.Thisistheaddressofthenextinstructionto
beexecuted.Makeanoteofwhichinstructionitispointingto?
19. ObservethevaluesinthePAddandLAddcolumns.Whatdothesevalues
indicate?Aretheydifferent(Hint:CheckouttheBaseAddressvalue)?
20. WhatisthedifferencebetweentheLAddvalueofthefirstinstructionandthe
LAddvalueofthesecondinstruction?Whatdoesthisvalueindicate(Hint:Think
oftheinstructionlengthsinbytes)?
21. CreateaninstructiontopopthevalueontopoftheProgramStackintoregister
R02.
22. Executeit.
23. ObservethevalueintheSPregister.
24. CreateaninstructiontopopthevalueontopoftheProgramStackintoregister
R03.
25. Executeit.
26. ObservethevalueintheSPregister.
27. Executethelastinstructionagain.Whathappened?Explain.
28. Createacompareinstruction,whichcomparesvaluesinregistersR04andR05.
29. ManuallyinserttwoequalvaluesinregistersR04andR05(Image4).
30. Oneagainexecutethecompareinstructioninstep28above.
31. WhichofthestatusflagsOV/Z/Nisset(i.e.boxischecked)?Why?
32.
ManuallyinsertavalueinregisterR05greaterthanthatinregisterR04.
33.
Executethecompareinstructioninstep28above.
34.
WhichofthestatusflagsOV/Z/Nisset?Why?
35.
ManuallyinsertavalueinregisterR04greaterthanthatinregisterR05.
36.
Executethecompareinstructioninstep28above.
37.
WhichofthestatusflagsOV/Z/Nisset?Why?
38.
Createaninstruction,whichwilljumptothefirstinstructionifthevaluesin
registersR04andR05areequal.
39.
TesttheaboveinstructionbymanuallyputtingequalvaluesinregistersR04
andR05,thenfirstexecutingthecompareinstructionfollowedbyexecutingthe
jumpinstruction(Remember:Youexecuteaninstructionbydoubleclickingon
it).Diditwork?
40.
SavetheinstructionsintheInstructionMemoryViewinafilebyclickingon
theSAVEbutton(Image7).
***Endofexercises***
10
Description
e.g.
MOV #2, R01 moves number 2 into register R01
MOV R01, R03 moves contents of register R01 into register R03
LDB
LDW
STB
STW
PSH
e.g.
PSH #6 pushes number 6 on top of the stack
PSH R03 pushes the contents of register R03 on top of the stack
Pop data from top of hardware stack to register
e.g.
POP
Arithmetic instructions
Add number to register; add register to register
e.g.
ADD
ADD #3, R02 adds number 3 to contents of register R02 and stores
the result in register R02.
ADD R00, R01 adds contents of register R00 to contents of register
R01 and stores the result in register R01.
SUB
MUL
DIV
11
e.g.
JMP 100 unconditionally jumps to address location 100
JLT
JGT
JEQ
JNE
CAL
RET
SWI
HLT
Halt simulation
Comparison instruction
Compare number with register; compare register with register
e.g.
CMP #5, R02 compare number 5 with the contents of register R02
CMP R01, R03 compare the contents of registers R01 and R03
CMP
Note:
If R01 = R03 then the status flag Z will be set, i.e. the Z box is
checked.
If R03 > R01 then non of the status flags will be set, i.e. none of the
status flag boxes are checked.
If R01 > R03 then the status flag N will be set, i.e. the N status box is
checked.
OUT
12