0% found this document useful (0 votes)
128 views4 pages

MIPS Quick Tutorial

1) MIPS is a 32-bit architecture where instructions and data types are 32 bits in length. It uses 32 general-purpose registers for processing as well as special registers like Lo and Hi. 2) Programs are organized into a data section and a text/code section, with labels marking the beginning and end of the main function. Comments begin with #. 3) Load and store instructions are used to access data in memory from registers. Indirect and based addressing allow loading from memory addresses stored in registers.

Uploaded by

El Beppe
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)
128 views4 pages

MIPS Quick Tutorial

1) MIPS is a 32-bit architecture where instructions and data types are 32 bits in length. It uses 32 general-purpose registers for processing as well as special registers like Lo and Hi. 2) Programs are organized into a data section and a text/code section, with labels marking the beginning and end of the main function. Comments begin with #. 3) Load and store instructions are used to access data in memory from registers. Indirect and based addressing allow loading from memory addresses stored in registers.

Uploaded by

El Beppe
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
You are on page 1/ 4

1/9/2016 MIPSQuickTutorial

MIPSArchitectureandAssemblyLanguageOverview
Adaptedfrom:http://edge.mcs.dre.g.el.edu/GICL/people/sevy/architecture/MIPSRef(SPIM).html

[RegisterDescription][I/ODescription]

DataTypesandLiterals
Datatypes:

Instructionsareall32bits
byte(8bits),halfword(2bytes),word(4bytes)
acharacterrequires1byteofstorage
anintegerrequires1word(4bytes)ofstorage

Literals:

numbersenteredasis.e.g.4
charactersenclosedinsinglequotes.e.g.'b'
stringsenclosedindoublequotes.e.g."Astring"

Registers

32generalpurposeregisters
registerprecededby$inassemblylanguageinstruction
twoformatsforaddressing:
usingregisternumbere.g.$0through$31
usingequivalentnamese.g.$t1,$sp
specialregistersLoandHiusedtostoreresultofmultiplicationanddivision
notdirectlyaddressablecontentsaccessedwithspecialinstructionmfhi("movefromHi")andmflo("movefromLo")
stackgrowsfromhighmemorytolowmemory

ThisisfromFigure9.9intheGoodman&Millertext
Register Alternative
Description
Number Name
0 zero thevalue0
1 $at (assemblertemporary)reservedbytheassembler
23 $v0$v1 (values)fromexpressionevaluationandfunctionresults
(arguments)Firstfourparametersforsubroutine.
47 $a0$a3
Notpreservedacrossprocedurecalls
(temporaries)Callersavedifneeded.Subroutinescanusew/outsaving.
815 $t0$t7
Notpreservedacrossprocedurecalls
(savedvalues)Calleesaved.
1623 $s0$s7 Asubroutineusingoneofthesemustsaveoriginalandrestoreitbeforeexiting.
Preservedacrossprocedurecalls
(temporaries)Callersavedifneeded.Subroutinescanusew/outsaving.
2425 $t8$t9 Theseareinadditionto$t0$t7above.
Notpreservedacrossprocedurecalls.
2627 $k0$k1 reservedforusebytheinterrupt/traphandler
globalpointer.
28 $gp Pointstothemiddleofthe64Kblockofmemoryinthestaticdata
segment.
stackpointer
29 $sp
Pointstolastlocationonthestack.
savedvalue/framepointer
30 $s8/$fp
Preservedacrossprocedurecalls
31 $ra returnaddress

SeealsoBrittonsection1.9,Sweetmansection2.21,LarusAppendixsectionA.6

ProgramStructure
justplaintextfilewithdatadeclarations,programcode(nameoffileshouldendinsuffix.stobeusedwithSPIMsimulator)
datadeclarationsectionfollowedbyprogramcodesection

DataDeclarations

placedinsectionofprogramidentifiedwithassemblerdirective.data
declaresvariablenamesusedinprogramstorageallocatedinmainmemory(RAM)

Code
placedinsectionoftextidentifiedwithassemblerdirective.text
containsprogramcode(instructions)
startingpointforcodee.g.ecutiongivenlabelmain:
endingpointofmaincodeshoulduseexitsystemcall(seebelowunderSystemCalls)

http://logos.cs.uic.edu/366/notes/MIPS%20Quick%20Tutorial.htm 1/4
1/9/2016 MIPSQuickTutorial
Comments

anythingfollowing#onaline
#Thisstuffwouldbeconsideredacomment
TemplateforaMIPSassemblylanguageprogram:
#Commentgivingnameofprogramanddescriptionoffunction
#Template.s
#BarebonesoutlineofMIPSassemblylanguageprogram

.data#variabledeclarationsfollowthisline
#...

.text#instructionsfollowthisline

main:#indicatesstartofcode(firstinstructiontoexecute)
#...

#Endofprogram,leaveablanklineafterwardstomakeSPIMhappy

DataDeclarations
formatfordeclarations:

name: storage_type value(s)

createstorageforvariableofspecifiedtypewithgivennameandspecifiedvalue
value(s)usuallygivesinitialvalue(s)forstoragetype.space,givesnumberofspacestobeallocated

Note:labelsalwaysfollowedbycolon(:)

example

var1: .word 3 #createasingleintegervariablewithinitialvalue3
array1: .byte 'a','b'#createa2elementcharacterarraywithelementsinitialized
#toaandb
array2: .space 40 #allocate40consecutivebytes,withstorageuninitialized
#couldbeusedasa40elementcharacterarray,ora
#10elementintegerarray;acommentshouldindicatewhich!

Load/StoreInstructions

RAMaccessonlyallowedwithloadandstoreinstructions
allotherinstructionsuseregisteroperands

load:
lw register_destination,RAM_source

#copyword(4bytes)atsourceRAMlocationtodestinationregister.

lb register_destination,RAM_source

#copybyteatsourceRAMlocationtoloworderbyteofdestinationregister,
#andsigne.g.tendtohigherorderbytes

storeword:

sw register_source,RAM_destination

#storewordinsourceregisterintoRAMdestination

sb register_source,RAM_destination

#storebyte(loworder)insourceregisterintoRAMdestination

loadimmediate:

li register_destination,value

#loadimmediatevalueintodestinationregister

example:
.data
var1: .word 23 #declarestorageforvar1;initialvalueis23

.text
__start:
lw $t0,var1 #loadcontentsofRAMlocationintoregister$t0:$t0=var1
li $t1,5 #$t1=5("loadimmediate")
sw $t1,var1 #storecontentsofregister$t1intoRAM:var1=$t1
done

IndirectandBasedAddressing
Usedonlywithloadandstoreinstructions

http://logos.cs.uic.edu/366/notes/MIPS%20Quick%20Tutorial.htm 2/4
1/9/2016 MIPSQuickTutorial
loadaddress:
la $t0,var1

copyRAMaddressofvar1(presumablyalabeldefinedintheprogram)intoregister$t0

indirectaddressing:

lw $t2,($t0)

loadwordatRAMaddresscontainedin$t0into$t2

sw $t2,($t0)

storewordinregister$t2intoRAMataddresscontainedin$t0

basedorindexedaddressing:

lw $t2,4($t0)

loadwordatRAMaddress($t0+4)intoregister$t2
"4"givesoffsetfromaddressinregister$t0

sw $t2,12($t0)

storewordinregister$t2intoRAMataddress($t012)
negativeoffsetsarefine

Note:basedaddressingisespeciallyusefulfor:

arraysaccesselementsasoffsetfrombaseaddress
stackseasytoaccesselementsatoffsetfromstackpointerorframepointer

example

.data
array1: .space 12 #declare12bytesofstoragetoholdarrayof3integers
.text
__start: la $t0,array1 #loadbaseaddressofarrayintoregister$t0
li $t1,5 #$t1=5("loadimmediate")
sw$t1,($t0) #firstarrayelementsetto5;indirectaddressing
li$t1,13 #$t1=13
sw$t1,4($t0) #secondarrayelementsetto13
li$t1,7 #$t1=7
sw$t1,8($t0) #thirdarrayelementsetto7
done

ArithmeticInstructions
mostuse3operands
alloperandsareregistersnoRAMorindirectaddressing
operandsizeisword(4bytes)
add $t0,$t1,$t2 #$t0=$t1+$t2;addassigned(2'scomplement)integers
sub $t2,$t3,$t4 #$t2=$t3$t4
addi $t2,$t3,5 #$t2=$t3+5;"addimmediate"(nosubimmediate)
addu $t1,$t6,$t7 #$t1=$t6+$t7;addasunsignedintegers
subu $t1,$t6,$t7 #$t1=$t6+$t7;subtractasunsignedintegers

mult $t3,$t4 #multiply32bitquantitiesin$t3and$t4,andstore64bit


#resultinspecialregistersLoandHi:(Hi,Lo)=$t3*$t4
div $t5,$t6 #Lo=$t5/$t6(integerquotient)
#Hi=$t5mod$t6(remainder)
mfhi $t0 #movequantityinspecialregisterHito$t0:$t0=Hi
mflo $t1 #movequantityinspecialregisterLoto$t1:$t1=Lo
#usedtogetatresultofproductorquotient

move $t2,$t3#$t2=$t3

ControlStructures

Branches

comparisonforconditionalbranchesisbuiltintoinstruction

b target #unconditionalbranchtoprogramlabeltarget
beq $t0,$t1,target #branchtotargetif$t0=$t1
blt $t0,$t1,target #branchtotargetif$t0<$t1
ble $t0,$t1,target #branchtotargetif$t0<=$t1
bgt $t0,$t1,target #branchtotargetif$t0>$t1
bge $t0,$t1,target #branchtotargetif$t0>=$t1
bne $t0,$t1,target #branchtotargetif$t0<>$t1

Jumps

j target #unconditionaljumptoprogramlabeltarget
jr $t3 #jumptoaddresscontainedin$t3("jumpregister")

SubroutineCalls

http://logos.cs.uic.edu/366/notes/MIPS%20Quick%20Tutorial.htm 3/4
1/9/2016 MIPSQuickTutorial
subroutinecall:"jumpandlink"instruction

jal sub_label #"jumpandlink"

copyprogramcounter(returnaddress)toregister$ra(returnaddressregister)
jumptoprogramstatementatsub_label

subroutinereturn:"jumpregister"instruction

jr $ra #"jumpregister"

jumptoreturnaddressin$ra(storedbyjalinstruction)

Note:returnaddressstoredinregister$raifsubroutinewillcallothersubroutines,orisrecursive,returnaddressshouldbecopiedfrom$raontostackto
preserveit,sincejalalwaysplacesreturnaddressinthisregisterandhencewilloverwritepreviousvalue

SystemCallsandI/O(SPIMSimulator)
usedtoreadorprintvaluesorstringsfrominput/outputwindow,andindicateprogramend
usesyscalloperatingsystemroutinecall
firstsupplyappropriatevaluesinregisters$v0and$a0$a1
resultvalue(ifany)returnedinregister$v0

Thefollowingtableliststhepossiblesyscallservices.

Code
Service Arguments Results
in$v0
print_int 1 $a0=integertobeprinted
print_float 2 $f12=floattobeprinted
print_double 3 $f12=doubletobeprinted
print_string 4 $a0=addressofstringinmemory
read_int 5 integerreturnedin$v0
read_float 6 floatreturnedin$v0
read_double 7 doublereturnedin$v0
$a0=memoryaddressofstringinput
read_string 8 buffer
$a1=lengthofstringbuffer(n)
sbrk 9 $a0=amount addressin$v0
exit 10

Theprint_stringserviceexpectstheaddresstostartanullterminatedcharacterstring.Thedirective.asciizcreatesanullterminatedcharacterstring.
Theread_int,read_floatandread_doubleservicesreadanentirelineofinputuptoandincludingthenewlinecharacter.
Theread_stringservicehasthesamesemanticesastheUNIXlibraryroutinefgets.
Itreadsupton1charactersintoabufferandterminatesthestringwithanullcharacter.
Iffewerthann1charactersareinthecurrentline,itreadsuptoandincludingthenewlineandterminatesthestringwithanullcharacter.
Thesbrkservicereturnstheaddresstoablockofmemorycontainingnadditionalbytes.Thiswouldbeusedfordynamicmemoryallocation.
Theexitservicestopsaprogramfromrunning.

e.g.Printoutintegervaluecontainedinregister$t2

li $v0,1 #loadappropriatesystemcallcodeintoregister$v0;
#codeforprintingintegeris1
move $a0,$t2 #moveintegertobeprintedinto$a0:$a0=$t2
syscall #calloperatingsystemtoperformoperation

e.g.Readintegervalue,storeinRAMlocationwithlabelint_value(presumablydeclaredindatasection)

li $v0,5 #loadappropriatesystemcallcodeintoregister$v0;
#codeforreadingintegeris5
syscall #calloperatingsystemtoperformoperation
sw $v0,int_value #valuereadfromkeyboardreturnedinregister$v0;
#storethisindesiredlocation

e.g.Printoutstring(usefulforprompts)

.data
string1 .asciiz"Printthis.\n" #declarationforstringvariable,
#.asciizdirectivemakesstringnullterminated

.text
main: li $v0,4 #loadappropriatesystemcallcodeintoregister$v0;
#codeforprintingstringis4
la $a0,string1 #loadaddressofstringtobeprintedinto$a0
syscall #calloperatingsystemtoperformprintoperation

e.g.Toindicateendofprogram,useexitsystemcall;thuslastlinesofprogramshouldbe:

li $v0,10 #systemcallcodeforexit=10
syscall #calloperatingsys

http://logos.cs.uic.edu/366/notes/MIPS%20Quick%20Tutorial.htm 4/4

You might also like