Sie sind auf Seite 1von 5

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
Number
Name
0
zero
1
23

$at
$v0$v1

47

$a0$a3

815

$t0$t7

1623

$s0$s7

2425

$t8$t9

2627

$k0$k1

28

$gp

29

$sp

30

$s8/$fp

31

$ra

Description
thevalue0
(assemblertemporary)reservedbytheassembler
(values)fromexpressionevaluationandfunctionresults
(arguments)Firstfourparametersforsubroutine.
Notpreservedacrossprocedurecalls
(temporaries)Callersavedifneeded.Subroutinescanusew/outsaving.
Notpreservedacrossprocedurecalls
(savedvalues)Calleesaved.
Asubroutineusingoneofthesemustsaveoriginalandrestoreitbeforeexiting.

Preservedacrossprocedurecalls
(temporaries)Callersavedifneeded.Subroutinescanusew/outsaving.
Theseareinadditionto$t0$t7above.

Notpreservedacrossprocedurecalls.
reservedforusebytheinterrupt/traphandler
globalpointer.
Pointstothemiddleofthe64Kblockofmemoryinthestaticdata
segment.
stackpointer
Pointstolastlocationonthestack.
savedvalue/framepointer
Preservedacrossprocedurecalls
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)

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:
array1:

array2:

.word
.byte

.space

3
#createasingleintegervariablewithinitialvalue3
'a','b'#createa2elementcharacterarraywithelementsinitialized

#toaandb
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

.text
__start:

23

#declarestorageforvar1;initialvalueis23

lw
li
sw
done

$t0,var1
$t1,5
$t1,var1

#loadcontentsofRAMlocationintoregister$t0:$t0=var1
#$t1=5("loadimmediate")

#storecontentsofregister$t1intoRAM:var1=$t1

IndirectandBasedAddressing
Usedonlywithloadandstoreinstructions
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

array1:

__start:

.data
.space 12

.text
la
$t0,array1
li
$t1,5
sw$t1,($t0)
li$t1,13

sw$t1,4($t0)
li$t1,7

sw$t1,8($t0)
done

#declare12bytesofstoragetoholdarrayof3integers

#loadbaseaddressofarrayintoregister$t0
#$t1=5("loadimmediate")
#firstarrayelementsetto5;indirectaddressing
#$t1=13
#secondarrayelementsetto13
#$t1=7
#thirdarrayelementsetto7

ArithmeticInstructions
mostuse3operands
alloperandsareregistersnoRAMorindirectaddressing
operandsizeisword(4bytes)

add
sub
addi
addu
subu

$t0,$t1,$t2
$t2,$t3,$t4
$t2,$t3,5
$t1,$t6,$t7
$t1,$t6,$t7

#$t0=$t1+$t2;addassigned(2'scomplement)integers
#$t2=$t3$t4
#$t2=$t3+5;"addimmediate"(nosubimmediate)
#$t1=$t6+$t7;addasunsignedintegers
#$t1=$t6+$t7;subtractasunsignedintegers

mult

div

mfhi
mflo

$t3,$t4

$t5,$t6

$t0

$t1

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

move

$t2,$t3#$t2=$t3

ControlStructures

Branches
comparisonforconditionalbranchesisbuiltintoinstruction

b
beq
blt
ble
bgt
bge
bne

target
$t0,$t1,target
$t0,$t1,target
$t0,$t1,target
$t0,$t1,target
$t0,$t1,target
$t0,$t1,target

#unconditionalbranchtoprogramlabeltarget
#branchtotargetif$t0=$t1
#branchtotargetif$t0<$t1
#branchtotargetif$t0<=$t1
#branchtotargetif$t0>$t1
#branchtotargetif$t0>=$t1
#branchtotargetif$t0<>$t1

j
jr

target #unconditionaljumptoprogramlabeltarget
$t3

#jumptoaddresscontainedin$t3("jumpregister")

Jumps

SubroutineCalls
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
in$v0

Service

Arguments

print_int
print_float

1
2

$a0=integertobeprinted
$f12=floattobeprinted

print_double

$f12=doubletobeprinted

print_string

$a0=addressofstringinmemory

read_int
read_float

5
6

read_double

integerreturnedin$v0
floatreturnedin$v0
doublereturnedin$v0

$a0=memoryaddressofstringinput
buffer
$a1=lengthofstringbuffer(n)

sbrk

$a0=amount

exit

10

read_string

Results

addressin$v0

Theprint_stringserviceexpectstheaddresstostartanullterminatedcharacterstring.Thedirective.asciizcreatesanullterminatedcharacterstring.
Theread_int,read_floatandread_doubleservicesreadanentirelineofinputuptoandincludingthenewlinecharacter.
Theread_stringservicehasthesamesemanticesastheUNIXlibraryroutinefgets.
Itreadsupton1charactersintoabufferandterminatesthestringwithanullcharacter.
Iffewerthann1charactersareinthecurrentline,itreadsuptoandincludingthenewlineandterminatesthestringwithanullcharacter.
Thesbrkservicereturnstheaddresstoablockofmemorycontainingnadditionalbytes.Thiswouldbeusedfordynamicmemoryallocation.
Theexitservicestopsaprogramfromrunning.
e.g.Printoutintegervaluecontainedinregister$t2

li
$v0,1

move
$a0,$t2
syscall

#loadappropriatesystemcallcodeintoregister$v0;
#codeforprintingintegeris1
#moveintegertobeprintedinto$a0:$a0=$t2
#calloperatingsystemtoperformoperation

e.g.Readintegervalue,storeinRAMlocationwithlabelint_value(presumablydeclaredindatasection)

li
$v0,5

syscall

sw
$v0,int_value

#loadappropriatesystemcallcodeintoregister$v0;
#codeforreadingintegeris5
#calloperatingsystemtoperformoperation
#valuereadfromkeyboardreturnedinregister$v0;

#storethisindesiredlocation

e.g.Printoutstring(usefulforprompts)

string1

.data
.asciiz"Printthis.\n"

#declarationforstringvariable,
#.asciizdirectivemakesstringnullterminated

main:

.text
li
$v0,4

la
$a0,string1
syscall

#loadappropriatesystemcallcodeintoregister$v0;
#codeforprintingstringis4
#loadaddressofstringtobeprintedinto$a0
#calloperatingsystemtoperformprintoperation

e.g.Toindicateendofprogram,useexitsystemcall;thuslastlinesofprogramshouldbe:

li
$v0,10
syscall

#systemcallcodeforexit=10

#calloperatingsys

Das könnte Ihnen auch gefallen