MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI...

Preview:

Citation preview

MIPSandBasicAssemblyLanguage

CS64:ComputerOrganizationandDesignLogicLecture#4

ZiadMatni

Dept.ofComputerScience,UCSB

4/12/18 Matni,CS64,Sp18 2

ThisWeekon

“DidjaKnowDat?!”

4/12/18 Matni,CS64,Sp18 3

WhydoCPUprogrammerscelebrateChristmasandHalloween

onthesameday?

BecauseOct-31=Dec-25

LectureOutline

•  MIPScoreprocessingblocks

•  Basicprogramminginassembly

•  Arithmeticprograms

•  Controllogicprograms

4/12/18 Matni,CS64,Sp18 4

AnyQuestionsFromLastLecture?

4/12/18 Matni,CS64,Sp18 5

4/12/18 Matni,CS64,Sp18 6

MIPS

WhyMIPS?•  MIPS:

–  areducedinstructionsetcomputer(RISC)architecturedevelopedbyMIPSTechnologies(1981)

•  Relevantintheembeddedsystemsdomain–  Computersystemswithadedicatedfunctionwithinalarger

mechanicalorelectricalsystem–  Example:lowpowerandlowcostprocessorsinMP3playersor

“Smart”Appliances

•  AllmoderncommercialprocessorssharethesamecoreconceptsasMIPS,justwithextrastuff...butmoreimportantly...

4/12/18 Matni,CS64,Sp18 7

MIPSisSimpler…

…thanotherinstructionsetsforCPUsSoit’sagreatlearningtool

•  Dozensofinstructions(asopposedtohundreds)•  Lackofredundantinstructionsorspecialcases•  5stagepipelineversus24stages

4/12/18 Matni,CS64,Sp18 8

Note:PipelininginCPUs

4/12/18 Matni,CS64,Sp18 9

•  PipeliningisafundamentaldesigninCPUs•  Allowsmultipleinstructionstogoonatonce– a.k.ainstruction-levelparallelism

CodeonMIPS

Originalx=5;y=7;z=x+y;

4/12/18 Matni,CS64,Sp18 10

MIPSli$t0,5li$t1,7add$t3,$t0,$t1

CodeonMIPS

Originalx=5;y=7;z=x+y;

4/12/18 Matni,CS64,Sp18 11

MIPSli$t0,5li$t1,7add$t3,$t0,$t1loadimmediate:putthegivenvalueintoaregister$t0:temporaryregister0

CodeonMIPS

Originalx=5;y=7;z=x+y;

4/12/18 Matni,CS64,Sp18 12

MIPSli$t0,5li$t1,7add$t3,$t0,$t1loadimmediate:putthegivenvalueintoaregister$t1:temporaryregister1

CodeonMIPS

Originalx=5;y=7;z=x+y;

4/12/18 Matni,CS64,Sp18 13

MIPSli$t0,5li$t1,7add$t3,$t0,$t1add:addtherightmostregisters,puttingtheresultinthefirstregister$t3:temporaryregister3

AvailableRegistersinMIPS

•  32registersinall– Refertoyour

MIPSReferenceCard

•  Forthemoment,let’sonlyconsiderregisters$t0thru$t9

4/12/18 Matni,CS64,Sp18 14

Assembly

•  ThecodethatyouseeisMIPSassembly

•  Assemblyis*almost*whatthemachinesees.Forthemostpart,itisadirecttranslationtobinaryfromhere(knownasmachinecode)

•  Anassemblertakesassemblycodeandchangesitintotheactual1’sand0’sformachinecode– AnalogoustoacompilerforHLcode

4/12/18 Matni,CS64,Sp18 15

li$t0,5li$t1,7add$t3,$t0,$t1

MachineCode/Language

•  WhataCPUactuallyacceptsasinput•  Whatactuallygetsexecuted

•  Eachinstructionisrepresentedwith32bits

•  Therearethreedifferentinstructionformats:R,I,andJ–  Theseallowforinstructionstotakeondifferentroles–  R-Formatisusedwhenit’sallaboutregisters–  I-Formatisusedwhenyouinvolvenumbers–  J-Formatisusedwhenyoudocode“jumping”(i.e.branching)

4/12/18 Matni,CS64,Sp18 16

4/12/18 Matni,CS64,Sp18 17

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

4/12/18 Matni,CS64,Sp18 18

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

4/12/18 Matni,CS64,Sp18 19

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

4/12/18 Matni,CS64,Sp18 20

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

4/12/18 Matni,CS64,Sp18 21

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

4/12/18 Matni,CS64,Sp18 22

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

4/12/18 Matni,CS64,Sp18 23

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

4/12/18 Matni,CS64,Sp18 24

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

4/12/18 Matni,CS64,Sp18 25

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

4/12/18 Matni,CS64,Sp18 26

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

4/12/18 Matni,CS64,Sp18 27

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

AddingMoreFunctionality

•  Whatabout:displayresults????Yes,that’skindaimportant…•  Whatwouldthisentail?–  EngagingwithInput/Outputpartofthecomputer–  i.e.talkingtodevices

•  Q:Whatusuallyhandlesthis?

•  Soweneedawaytotell theoperatingsystemtokickin

4/12/18 Matni,CS64,Sp18 28

A:theoperatingsystem

TalkingtotheOS

•  WearegoingtoberunningonMIPSemulatorcalledSPIM–  Optionally,throughaprogramcalledQtSPIM(GUIbased)– Whatisanemulator?

•  We’renotactuallyrunningourcommandsonaMIPSprocessor!!!

…so,inotherwords…we’re“fakingit”

•  Ok,sohowmightweprintsomethingontostd.out?

4/12/18 Matni,CS64,Sp18 29

SPIMRoutines

•  MIPSfeaturesasyscallinstruction,whichtriggersasoftwareinterrupt,orexception

•  Outsideofanemulator(i.e.intherealworld),theseinstructionspausetheprogramandtelltheOStogodosomethingwithI/O

•  Insidetheemulator,ittellstheemulatortogodosomethingwithI/O

4/12/18 Matni,CS64,Sp18 30

syscall

•  SowehavetheOS/emulator’sattention,buthowdoesitknowwhatwewant?

•  TheOS/emulatorhasaccesstotheregisters

•  Weputspecialvalues(codes)intheregisterstoindicatewhatwewant–  Thesearecodesthatcan’tbeusedforanythingelse,so

they’reunderstoodtobejustforsyscall

4/12/18 Matni,CS64,Sp18 31

ProgramFilesforMIPSAssembly

•  Thefileshavetobetext

•  Typicalfileextensiontypeis.asm

•  Toleavecomments, use#atthestartoftheline

4/12/18 Matni,CS64,Sp18 32

(Finally)PrintinganInteger•  ForSPIM,ifregister$v0contains1andthenweissueasyscall,

thenSPIMwillprintwhateverintegerisstoredinregister$a0ßthisisaspecificruleusingaspecificcode–  Note:$v0isusedforotherstuffaswell–moreonthatlater…–  When$v0=1,syscallisexpectinganinteger!

•  Othervaluesin$v0indicateothertypestosyscallExamples:–  $v0=3meansdouble(orthememaddressofone)in$a0–  $v0=4meansstring(orthememaddressofone)in$a0–  We’llexploresomeoftheselater,butcheckMIPSrefcardforallofthem

4/12/18 Matni,CS64,Sp18 33

(Finally)PrintinganInteger•  Remember,theusualsyntaxtoloadimmediateavalueintoa

registeris: li<register>,<value>–  Example: li$v0,1 #PUTSTHENUMBER1INTOREG.$v0

•  Tomakesurethattheregister$a0hasthevalueofwhatyouwanttoprintout(let’ssayit’sinanotherregister),usethemovecommand:

move<toregister>,<fromregister>–  Example: move$a0,$t0#PUTSTHEVALUEINREG.$t0INTOREG.$a0

4/12/18 Matni,CS64,Sp18 34

4/12/18 Matni,CS64,Sp18 35

Ok…SoAboutThoseRegistersMIPShas32registers,each32bits

AugmentingwithPrinting#Mainprogramli$t0,5li$t1,7add$t3,$t0,$t1#Printanintegertostd.outputli$v0,1move$a0,$t3syscall

4/12/18 Matni,CS64,Sp18 36

We’reNotQuiteDoneYet!ExitinganAssemblyPrograminSPIM•  IfyouareusingSPIM,thenyouneedtosaywhen

youaredoneaswell–  MostHLLprogramsdothisforyouautomatically

•  Howisthisdone?–  Issueasyscallwithaspecialvaluein$v0=10(decimal)

4/12/18 Matni,CS64,Sp18 37

AugmentingwithExiting#Mainprogramli$t0,5li$t1,7add$t3,$t0,$t1#Printtostd.outputli$v0,1move$a0,$t3syscall#Endprogramli$v0,10syscall

4/12/18 Matni,CS64,Sp18 38

4/12/18 Matni,CS64,Sp18 39

MIPSSystemServicesExamplesWe’veSeenSoFar…

stdout

stdin

FileI/O

NowLet’sMakeitaFullProgram(almost)

•  Weneedtotelltheassembler(anditssimulator)whichbitsshouldbeplacedwhereinmemory–  Bits?

(remember:everythingendsupbeingabunchof1’sand0’s!)

4/12/18 Matni,CS64,Sp18 40

AllocatedasprogramRUN

AllocatedatprogramLOAD

Constantstobeusedintheprogram(likestrings)

mutableglobalvariables

thetextoftheprogram

MarkingtheCode

•  Forthesimulator,you’llneeda.textdirectivetospecifycode

4/12/18 Matni,CS64,Sp18 41

AllocatedasprogramRUN

AllocatedatprogramLOAD

Constantstobeusedintheprogram(likestrings)

mutableglobalvariables

thetextoftheprogram

.text#Mainprogramli$t0,5li$t1,7add$t3,$t0,$t1#Printtostandardoutputli$v0,1move$a0,$t3syscall#Endprogramli$v0,10syscall

Themoveinstruction

•  ThemoveinstructiondoesnotactuallyshowupinSPIM

•  Itisapseudo-instructionforustouse,butit’sactuallytranslatedintoanactualinstruction

ORIGINAL: move$a0,$t3ACTUAL: addu$a0,$zero,$t3 #what’saddu?what’s$zero?

4/12/18 Matni,CS64,Sp18 42

WhyPseudocodes?Andwhat’sthis$zero??

•  $zero–  Specifiedlikeanormalregister,

butdoesnotbehavelikeanormalregister–  Writesto$zeroarenotsaved–  Readsfrom$zeroalwaysreturn0value

•  Whyhavemoveasapseudo-instructioninsteadofasanactualinstruction?–  It’sonelessinstructiontoworryabout–  OnedesigngoalofRISCistocutoutredundancy–  moveisn’ttheonlyone!liisanotheronetoo!

4/12/18 Matni,CS64,Sp18 43

4/12/18 Matni,CS64,Sp18 44

ListofallCoreInstructionsinMIPS

4/12/18 Matni,CS64,Sp18 45

ListoftheArithmeticCoreInstructionsinMIPS

4/12/18 Matni,CS64,Sp18 46

ListofallPsuedoInstructionsinMIPSThatYouAreAllowedtoUseinCS64!!!

ALLOFTHISANDMOREISONYOURHANDY“MIPSREFERENCECARD”NOWFOUNDONTHECLASSWEBSITE

MIPSPeculiarity:NORusedaNOT

•  HowtomakeaNOTfunctionusingNORinstead•  Recall:NOR=NOTOR•  Truth-Table:

•  So,intheabsenceofaNOTfunction, useaNORwitha0asoneoftheinputs!

4/12/18 Matni,CS64,Sp18 47

A B ANORB0 00 11 01 1

1

000

Notethat:0NORxx=NOTxx

Let’sRunThisProgramAlready!UsingSPIM

•  We’llcallitsimpleadd.asm•  RunitonCSILas:$spim–fsimpleadd.asm

•  We’llalsorunotherarithmeticprogramsandexplainthemaswegoalong– TAKENOTES!

4/12/18 Matni,CS64,Sp18 48

YOURTO-DOs

•  ReviewALLthedemocode– Availableviatheclasswebsite

•  Assignment#1– DueFriday

•  Assignment/Lab#2–  LabonMONDAY–  Submititviaturnin

4/12/18 Matni,CS64,Sp18 49

4/12/18 Matni,CS64,Sp18 50

Recommended