0% found this document useful (0 votes)
375 views15 pages

Yass Programming Model 2 Tutorial

This document provides details about a CPU simulator used to teach computer architecture concepts. The simulator allows students to visualize and experiment with processor components without needing real systems. It displays views of the CPU instruction memory, registers, program stack, and I/O console. The instruction memory view shows program instructions and addressing. The registers views show specialist registers like the program counter and general-purpose registers used for variables. The program stack uses a last-in first-out structure for functions. Students can create and run programs on the simulator to learn about low-level programming models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
375 views15 pages

Yass Programming Model 2 Tutorial

This document provides details about a CPU simulator used to teach computer architecture concepts. The simulator allows students to visualize and experiment with processor components without needing real systems. It displays views of the CPU instruction memory, registers, program stack, and I/O console. The instruction memory view shows program instructions and addressing. The registers views show specialist registers like the program counter and general-purpose registers used for variables. The program stack uses a last-in first-out structure for functions. Students can create and run programs on the simulator to learn about low-level programming models.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ProgrammingModel2

[Link]
Objectives
Attheendofthislabyoushouldbeableto:
Usedirectandindirectaddressingmodesofaccessingdatainmemory
Createaniterativeloopofinstructions
DisplaytextonconsoleusinganIOinstruction
Createasubroutine,callandreturnfromsubroutine
Passparameterstoasubroutine


[Link](CPU)Simulators
Thecomputerarchitecturetutorialsaresupportedbysimulators,whicharecreated
[Link]
simulatorsprovidevisualandanimatedrepresentationofmechanismsinvolvedand
enablethestudentstoobservethehiddeninnerworkingsofsystems,whichwould
[Link]
isthattheyallowthestudentstoexperimentandexploredifferenttechnological
aspectsofsystemswithouthavingtoinstallandconfiguretherealsystems.

[Link]
Theprogrammingmodelofcomputerarchitecturedefinesthoselowlevel
architecturalcomponents,whichincludethefollowing
CPUinstructionset
CPUregisters
Differentwaysofaddressinginstructionsanddataininstructions

[Link]
[Link]
doadditionalreadinginordertoformabetterunderstandingofthedifferentparts
ofamodernCPUarchitecture(refertotherecommendedreadinglistavailableinthe
modulehandbookandontheBB).
[Link]
Thissectionincludessomebasicinformationonthesimulator,whichshouldenable
[Link](s)willbeavailabletohelpanyone
[Link]
applicationrunningonaPCrunningMSWindowsoperatingsystem.
Themainsimulatorwindowiscomposedofseveralviews,whichrepresentdifferent
functionalpartsofthesimulatedprocessor.TheseareshowninImage1belowand
arecomposedof

1
CPUInstructionmemory
SpecialCPUregisters
CPU(generalpurpose)registers
Programstack
Programcreationandrunningfeatures
Memoryinwhichdataisstored
Input,outputconsole

CPUInstruction SpecialCPU CPUregisters


memoryview registersview view
Addprogram
instructionstab Programlist view Programstackview
Clicktoviewprogram Clicktoview
Createprogramtab
datamemory
IOconsole

Image1CPUSimulatorwindow
[Link]
informationcarefullyandtrytoidentifythedifferentpartsontheCPUSimulator
[Link]
sectioninconjunctionwiththeexercisesthatfollow.

2
[Link]
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.

Each instruction is associated
with two addresses: the
physicaladdress(PAdd)andthe
logical address (LAdd). This
view also displays the base
address (Base) against each
instruction. The sequence of
instructions belonging to the
same program will have the
samebaseaddress.

[Link] ThisviewshowsthesetofCPUregisters,
whichhavepredefinedspecialistfunctions:
PC:ProgramCountercontainstheaddress
ofthenextinstructiontobeexecuted.
IR:InstructionRegistercontainsthe
instructioncurrentlybeingexecuted.
SR:StatusRegistercontainsinformation
pertainingtotheresultofthelastexecuted
instruction.
SP:StackPointerregisterpointstothevalue
maintainedatthetopoftheprogramstack
(seebelow).
BR:BaseRegistercontainscurrentbase
address.
MAR:MemoryAddressRegistercontains
thememoryaddresscurrentlybeing
Image3SpecialCPU accessed.
registersview Statusbits:OV:Overflow;Z:Zero;N:
Negative

3
[Link]

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
[Link]
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.

Not all architectures have this many
registers. Some have more (e.g. 128
register) and some others have less
(e.g. 8 registers). In all cases, these
registersservesimilarpurposes.

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
[Link]

Image5Programstackview

Theprogramstackisanotherareawhich
maintains temporary values as the
instructions are executed. The stack is a
LIFO(lastinfirstout)[Link]
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.

[Link]

Image6 ProgramListView

Use the REMOVE PROGRAM button to


remove the selected program from the
list; use the REMOVE ALL PROGRAMS
buttontoremovealltheprogramsfrom
the list. Note that when a program is
removed, its instructions are also
removed from the Instruction Memory
Viewtoo.

[Link]
Image7 Createprogramtab

Tocreateanewprogramenterits
nameintheProgramNamebox
anditsbaseaddressintheBase
AddressboxthenclickontheADD
[Link]
willappearintheProgramListview
(seeImage6).

5
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.

[Link]

Image9Programdatamemoryview

6
TheCPUinstructionsthataccessthatpartofthememorycontainingdatacanwrite
[Link]
[Link]
[Link](logical
address)[Link]
thedisplayrepresents8bytesofdata.ColumnsB0throughtoB7representbytes0
[Link]
[Link]
[Link],inImage9,
[Link]
correspondtodisplayablecharacterscapitalAandB.

Tochangethevaluesofanybytes,firstselecttheline(s)[Link]
usetheinformationintheInitializeDataframetomodifythevaluesofthebytesin
theselectedline(s)asInteger,[Link]
UPDATEbuttontomakethechange.

[Link]

Image10Input,outputconsoleview
Image10aboveshowstheconsolewhichisusedbyprogramstowritemessagesto
[Link]
[Link]
keyboardwindowwhichcanbeusedtoinputdatatoprogramsrequestinginput.

7
E. Lab Exercises - Investigate and Explore
Thelabexercisesareaseriesofactivities,whicharecarriedoutbythestudents
[Link],howisthistutorialconducted?Thestudentsareexpected
tofollowtheinstructionsgiveninordertoidentifyandlocatetherequired
information,toactuponit,makenotesoftheirobservationsandofferexplanations
[Link]
activitiesyoushouldconsulttheinformationinSectionDaboveandalsofrequently
refertotheAppendixforinformationonvariousCPUinstructionsyouwillbeasked
[Link],youneedtocarefullyreadandunderstandthe
instructionsbeforeyouattempteachactivity.
Now,[Link]
MemoryView(seeImage2),representingtheRAMintherealmachine,before
[Link],followthestepsbelow:
IntheProgramtab(seeImage7),firstenteraProgramName,andthenenteraBase
Address(thiscanbeanynumber,butforthisexerciseuse100).ClickontheADD
[Link](seeImage6).
[Link]
LOADbuttontoloadinstructionsfromafile.
[Link]
clickingontheADDNEWbuttonintheInstructionstab(seeImage8).Thiswill
displaytheInstructions:[Link]
[Link]
givesexamplesoftheirusage.
Now,haveagoatthefollowingactivities(enteryouranswersinthetextboxes
provided).Awordofcaution:Regularlysaveyourcodeinafileincasethesimulator
crashesinwhichcaseyoucanrestartthesimulatorandreloadyourfile.

8
1. IntheAppendixattheendofthisdocument,locatetheinstruction,whichis
usedtostoreonebyteofdatainamemorylocation.Useittostorenumber65in
addresslocation20(allnumbersareindecimal).Thisisanexampleofdirect
addressing.RefertoImage9toseehowtodisplaythecontentsofdatamemory.
Makeanotebelowoftheinstructionused:


2. Createaninstructiontomovedecimalnumber22toregisterR01andmakea
noteofitbelow.ExecutethisinstructionandverifytheresultinR01.


3. Createaninstructiontostoredecimalnumber51inmemorylocationthe
[Link]
[Link]@prefixnexttoR01inthiscase.


4. Makeanoteofwhatyouseeindatamemorylocations20and22(refertoImage
9forhelpinformationonhowtodisplaythedatamemory).



5. Now,[Link],[Link]#prefixisusedto
denotealiteralvaluethusdistinguishingitfromanaddressvaluewhichdoesnot
useit.R01representsanarbitraryregister;youcanuseanyoftheregistersfrom
R00toR31.
MOV #0, R01
ADD #1, R01
CMP #5, R01
JNE 0

HLT

6. [Link]
[Link]
inordertomakethecodemoreflexiblewecanuselabelstorepresent
[Link]
instructionsbelowforthis:
HighlighttheaboveMOVinstruction([Link])
ClickontheINSERTBELOWbutton

9
TypelabelnameL0intheboxnexttotheENTERLABELbuttoninthe
windowyouusetoenterinstructions
ClicktheENTERLABELbutton
Thenewcodeshouldnowlooklikethis(modificationsareinredcolour):

MOV #0, R01
L0:
ADD #1, R01
CMP #5, R01
JNE 0
HLT

Next,highlighttheJNEinstruction
ClickontheEDITbutton
SelectL0inthedropdownlistundertheSourceOperandsectionbutton
inthewindowyouusetoenterinstructions
ClicktheEDITbutton
Thenewcodeshouldnowlooklikethis:

MOV #0, R01


L0:
ADD #1, R01
CMP #5, R01
JNE $L0
HLT

7. Asyoucansee,thelabelL0representstheaddressoftheinstruction
immediatelybelowit,[Link]
useL0astheaddresstojumpto.AsthelabelL0canrepresentanyaddressthis
[Link]$sign
[Link]
program,followtheinstructionsbelow:
ClickontheRESETPROGRAMbuttonintheCPUSimulatorwindow
HighlighttheMOVinstruction,[Link]
Adjustthespeedslidertoavalue,say,nearesttothevalue80
ClickontheRUNbutton
[Link]
[Link]
andrepeattheaboveinstructionsonceagain.
WhentheprogramstopsmakeanoteofthevalueinR01below

8. [Link]
programcodesothattheprogramloopisrepeatedaslongasthevalueofR01is
lessthanorequalto3(youmaywishtorefertotheAppendixforthis)andtest

10
it.WhenyougetitrightmakeanoteofthevalueinR01andcopythenewcode
[Link],changethemodifiedinstructionsbacktotheoriginalinstructions
(youcanusetheUNDObuttonforthisseeImage8above).




9. Ok,[Link]
[Link]
[Link]
LABELbuttononlyasexplainedin(6).Also,makesureyouselecttheDirectMem
radiobuttonwhenenteringthefirstoperandvalue24oftheOUTinstruction:
L1:
OUT 24, 0
RET

10. Theabovesubroutinecodesimplydisplaysthetextstartingatdatamemory
location24andreturns(seeRETinstructioninappendix).Forittoworkthere
[Link]
followingthestepsbelow:
ClickontheSHOWPROGRAMDATAMEMORYbutton(seeImage6).
Inthedisplayedwindowhighlighttheline0024underLAddcolumn
UnderInitialiseDataclickontheStringradiobutton
EntersometextinthetextboxlabelledValue,[Link]
ClicktheUPDATEbutton

11. Now,[Link]
itusingtheinstructionsMSFfollowedbyCAL(refertotheAppendix).TheMSF
(MarkStackFrame)isneededtoreserveaplaceforthereturnaddressonthe
[Link]
[Link]
[Link],usingthecodeadded
in(6)and(9)themodifiedprogramshouldlooksomethinglikethis:
MOV #0, R01
L0:
ADD #1, R01
MSF

CAL $L1
CMP #5, R01
JNE $L0
HLT
L1:

OUT 24, 0
RET

11
12. [Link]
[Link](seeImage1)
[Link],followthe
instructionsbelow:
ClickontheRESETPROGRAMbutton
HighlighttheMOVinstruction,[Link]
Adjustthespeedslidertoavaluenearesttothevalue80
ClickontheRUNbutton

13. [Link]
instructionusesdirectmemoryaddressing,i.e.thememoryaddress24ispartof
[Link]
tothatin(3).So,youllneedtoplacethememoryaddress24inaregister(any
spareregister).ThenyouneedtohavetheOUTinstructionusethisregister
[Link]
[Link].
UsetheUNDObuttontorestoretheinstructionsbeforethismodification:

14. Ok,[Link]
[Link],nowwewillhavetwosubroutineswhere
[Link]
HLTinstructionischangedtotheRETinstructionandthenewinstructionsMSF,
CALandHLTareaddedtogetherwiththenewlabelL2atthetopofthecode.
CAL$L2callsthesubroutinewiththeloopandCAL$L1callsthesubroutinethat
displaysthetext.
MSF
CAL $L2
HLT

L2:
MOV #0, R01
L0:
ADD #1, R01
MSF
CAL $L1
CMP #5, R01
JNE $L0
RET
L1:
OUT 24, 0
RET


Now,[Link]
programandverifytheresultintheconsolewindowasbefore.

12
15. Whystophere![Link]
[Link]
loopsasaparametertothesubroutine(startingatlabelL2).Forthiswewilluse
thePSHandPOPinstructions(seetheAppendix).Modifyyourcodetolooklike
theonebelowandrunitobservingthedisplaysontheconsole:
MSF
PSH #8
CAL $L2
HLT

L2:
POP R02
MOV #0, R01
L0:
ADD #1, R01
MSF
CAL $L1
CMP

R02, R01
JNE $L0
RET
L1:
OUT 24, 0
RET
16. Examinetheabovecodeandbrieflyexplainhowtheparameterpassingworks:

17. Finally,asarealchallenge,modifytheabovecodesothatasecondparameteris
passedtothesubroutine(startingatlabelL2)inthesamewayasthefirst
parameterispassed.ThesecondparameterisusedtoinitialisetheregisterR01
[Link]
ofthelastmodificationintheboxbelow:

13
AppendixSimulatorInstructionSubset
Inst Description
Datatransferinstructions
Movedatatoregister;moveregistertoregister
MOV e.g.
MOV#2,R01movesnumber2intoregisterR01
MOVR01,R03movescontentsofregisterR01intoregisterR03
Loadabytefrommemorytoregister
LDB e.g.
LDB1022,R03loadsabytefrommemoryaddress1022intoR03
LDB@R02,R05loadsabytefrommemorytheaddressofwhichisinR02
LDW Loadaword(2bytes)frommemorytoregister
SameasinLDBbutaword(i.e.2bytes)isloadedintoaregister
Storeabytefromregistertomemory
STB STBR07,2146storesabytefromR07intomemoryaddress2146
STBR04,@R08storesabytefromR04intomemoryaddressofwhichisin
R08
STW Storeaword(2bytes)fromregistertomemory
SameasinSTBbutaword(i.e.2bytes)isloadedstoredinmemory
Pushdatatotopofhardwarestack(TOS);pushregistertoTOS
PSH e.g.
PSH#6pushesnumber6ontopofthestack
PSHR03pushesthecontentsofregisterR03ontopofthestack
Popdatafromtopofhardwarestacktoregister
e.g.
POP POPR05popscontentsoftopofstackintoregisterR05
Note:IfyoutrytoPOPfromanemptystackyouwillgettheerrormessage
Stackunderflow.
Arithmeticinstructions
Addnumbertoregister;addregistertoregister
e.g.
ADD ADD#3,R02addsnumber3tocontentsofregisterR02andstoresthe
resultinregisterR02.
ADDR00,R01addscontentsofregisterR00tocontentsofregisterR01
andstorestheresultinregisterR01.
SUB Subtractnumberfromregister;subtractregisterfromregister
MUL Multiplynumberwithregister;multiplyregisterwithregister
DIV Dividenumberwithregister;divideregisterwithregister
Controltransferinstructions
Jumptoinstructionaddressunconditionally
JMP e.g.
JMP100unconditionallyjumpstoaddresslocation100wherethereis
anotherinstruction

14
JLT Jumptoinstructionaddressiflessthan(afterlastcomparison)
JGT Jumptoinstructionaddressifgreaterthan(afterlastcomparison)
Jumptoinstructionaddressifequal(afterlastcomparisoninstruction)
e.g.
JEQ JEQ200jumpstoaddresslocation200ifthepreviouscomparison
instructionresultindicatesthatthetwonumbersareequal,[Link]
statusflagisset(theZboxwillbecheckedinthiscase).
JNE Jumptoinstructionaddressifnotequal(afterlastcomparison)
MarkStackFrameinstructionisusedinconjunctionwiththeCAL
instruction.
MSF e.g.
MSFreserveaspaceforthereturnaddressonprogramstack
CAL1456savethereturnaddressinthereservedspaceandjumpto
subroutineinaddresslocation1456
Jumptosubroutineaddress(savesthereturnaddressonprogramstack)
CAL [Link]
[Link]
above
RET Returnfromsubroutine(usesthereturnaddressonstack)
SWI Softwareinterrupt(usedtorequestOShelp)
HLT Haltsimulation
Comparisoninstruction
Comparenumberwithregister;compareregisterwithregister
e.g.
CMP#5,R02comparenumber5withthecontentsofregisterR02
CMPR01,R03comparethecontentsofregistersR01andR03
CMP Note:
IfR01=R03thenthestatusflagZwillbeset,[Link].
IfR03>R01thennonofthestatusflagswillbeset,[Link]
flagboxesarechecked.
IfR01>R03thenthestatusflagNwillbeset,[Link]
checked.
Input,outputinstructions
IN Getinputdata(ifavailable)fromanexternalIOdevice
OutputdatatoanexternalIOdevice
OUT e.g.
OUT16,0outputscontentsofdatainlocation16totheconsole(the
secondparametermustalwaysbea0)

15

You might also like