Documente Academic
Documente Profesional
Documente Cultură
BhaskaranRaman DepartmentofCSE,IITBombay
http://www.cse.iitb.ac.in/~br/ http://www.cse.iitb.ac.in/synerg/doku.php?id=public:courses:cs305autumn10:start
Today'sTopics
TheMIPSinstructionset(continued)
Branchinginstructions Instructionencoding
TestYourUnderstanding...
Whatisthemaximumarrayindexwhichcanbe supportedinasingleloadinstruction
Assumethatthearrayisof32bitintegers
TestYourUnderstanding(continued)...
TranslatethefollowingCcodeintoassemblylang.:
NotionofRegisterAssignment
Instructionsfor BitWiseLogicalOperations
Logical C/C++/Java Operators Operators Shift Left << Shift Right >> Bit-by-bit AND & Bit-by-bit OR | Bit-by-bit NOT ~ MIPS Instructions sll srl and, andi or, ori nor
InstructionEncoding
Encoding:representinginstructionsasnumbers/bits
Recall:instructionsarealsostoredinmemory! Encoding==(assemblylanguage>machinelanguage)
MIPS:allinstructionsareencodedas32bits(why?) Also,allinstructionshavesimilarformat(why?)
MIPSInstructionFormat
opcode (6) rs (5) rt (5) rd (5) shamt (5) funct (6)
I-type instruction: loads, stores, all immediates, conditional branch, jump register, jump and link register
opcode (6) offsetrelativetoPC (26)
TheNotionoftheProgramCounter
Program (inmemory) PC
Theprogramisfetchedand executedinstructionby instruction ProgramCounter(PC) Aspecial32bitregister Pointstothecurrent instruction Forsequentialexecution PC+=4foreach instruction Nonsequentialexecution Implementedthrough manipulationofthePC
BranchingInstructions
Instructionsetsupportfortheseisrequired...
ConditionalandUnconditionalBranches
Twoconditionalbranchinstructions:
Anunconditionalbranch,orjumpinstruction:
Branch(orjump)targetspecification:
Inassemplylanguage:itisalabel Inmachinelanguage,itisaPCrelativeoffset
Assemblercomputesthisoffsetfromtheprogram
UsingBranchesforIfThenElse
if(i==j){f=g+h;}else{f=gh;} #Conventioninmyslides: #s0,s1...assignedtovariables #inorderofappearance #s0isi,s1isj #s2isf,s3isg,s4ish bne $s0,$s1,ELSE add $s2,$s3,$s4 j EXIT ELSE: sub $s2,$s3,$s4 EXIT: #Furtherinstructionsbelow
UsingBranchesforLoops
while(a[i]==k)i++;
#s0isa,s1isi,s2isk BEGIN: sll $t0,$s1,2 add $t0,$t1,$s0 lw $t1,0($t0) bne $t1,$s2,EXIT addi $s1,$s1,1 j BEGIN EXIT: #Furtherinstructionsbelow
TestingOtherBranchConditions
slt <dst>,<reg1>,<reg2>
slt==setlessthan <dst>issetto1if<reg1>islessthan<reg2>,0otherwise
Denoted$zerointheassemblylanguage Programsuse0incomparisonoperationsveryfrequently
Whynotsinglebltorbltiinstruction?
ForLoop:AnExample
for(i=0;i<10;i++){a[i]=0;}
#s0isi,s1isa addi $s0,$zero,0 BEGIN: slti $t0,$s0,10 beq $t0,$zero,EXIT sll $t1,$s0,2 add $t2,$t1,$s1 sw 0($t2),$zero j BEGIN EXIT: #Furtherinstructionsbelow