AnneBracyCS3410
ComputerScienceCornellUniversity
The slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, McKee, and Sirer.
Write-BackMemory
InstructionFetch Execute
InstructionDecode
extend
registerfile
control
alu
memory
din dout
addrPC
programmemory
newpc
inst
IF/ID ID/EX EX/MEM MEM/WB
imm
BA
ctrl
ctrl
ctrl
BD D
M
computejump/branch
targets
+4
forwardunitdetect
hazard
2
0xfffffffc
0x00000000
top
bottom
0x7ffffffc0x80000000
0x10000000
0x00400000
systemreserved
stack
systemreserved
code(text)
staticdata
dynamicdata(heap)
“DataMemory”
“ProgramMemory”
3
Stackcontainsstackframes(aka“activationrecords”)• 1stackframeperdynamicfunction• Existsonlyforthedurationoffunction• Growsdown,“top”ofstackis$sp,r29• Example:lw $r1,0($sp)putswordattopofstackinto$r1Eachstackframecontains:• Localvariables,returnaddress(later),register
backups(later) myfn stackframe
myfn stackframesystemreserved
stack
systemreserved
codestaticdata
heap
main stackframe
int main(…) {...
myfn(x);}int myfn(int n) {
...
myfn();}
$spà
4
Heapholdsdynamicallyallocatedmemory• Programmustmaintainpointerstoanythingallocated
• Example:if$r3holdsx• lw $r1,0($r3)getsfirstwordxpointsto
• Dataexistsfrommalloc()tofree()
2000bytes
1000bytes
systemreserved
stackxyz
systemreserved
codestaticdata
heap
3000bytes
void some_function() {int *x = malloc(1000);int *y = malloc(2000);free(y);int *z = malloc(3000);
}
5
Datasegmentcontainsglobalvariables• Existforalltime,accessibletoallroutines• Accessedw/globalpointer
• $gp,r28,pointstomiddleofsegment• Example:lw $r1,0($gp)getsmiddle-mostword
(here,max_players)
systemreserved
stack
systemreserved
codestaticdata
heap
int max_players = 4;
int main(...) {...
}
gpà 4
6
int n = 100;int main (int argc, char* argv[ ]) {
int i, m = n, sum = 0; int* A = malloc(4*m + 4);for (i = 1; i <= m; i++) {
sum += i; A[i] = sum; }printf ("Sum 1 to %d is %d\n", n, sum);
}
Variables Visibility Lifetime Location
Function-Local
Global
Dynamic
7
Variables Visibility Lifetime Location
Function-Local
Global
Dynamic
i,m,sum,A
n,str
w/infunction functioninvocation stack
wholeprogram programexecution .data
b/wmallocandfree heap
Anywherethathasapointer*A
int n = 100;int main (int argc, char* argv[ ]) {
int i, m = n, sum = 0; int* A = malloc(4*m + 4);for (i = 1; i <= m; i++) {
sum += i; A[i] = sum; }printf ("Sum 1 to %d is %d\n", n, sum);
}8
Don’teverwritecodelikethis!
void some_function() {int *x = malloc(1000);int *y = malloc(2000);free(y);int *z = malloc(3000);y[20] = 7;
}
void f1() {int *x = f2();int y = *x + 2;
}int *f2() {
int a = 3;return &a;
}
Danglingpointersintofreedheapmem
Danglingpointersintooldstackframes
9
Whichofthefollowingistrouble-freecode?
10
int *bubble(){ int a; …return &a;
}
int *toil() { s = malloc(20); … return s;
}
A char *rubble() { char s[20];gets(s); return s;
}
int *trouble() { s = malloc(20); … free(s); … return s;
}
B
C D
int main (int argc, char* argv[ ]) {int n = 9;int result = myfn(n);
}
int myfn(int n) {int f = 1;int i = 1;int j = n – 1;while(j >= 0) {
f *= i;i++;j = n - i;
}return f;
}11
TransferControl• Callerà Routine• Routineà Caller
PassArgumentstoandfromtheroutine• fixedlength,variablelength,recursively• Getreturnvaluebacktothecaller
ManageRegisters• Alloweachroutinetouseregisters• Preventroutinesfromclobberingeachothers’data
WhatisaConvention?Warning: ThereisnoonetrueMIPScallingconvention.
lecture!=book!=gcc !=spim !=web 12
main: j myfn
after1:add $1,$2,$3
j myfnafter2:
sub $3,$4,$5
myfn: …
…j after1
JumpstothecalleeJumpsbackWhataboutmultiplesites?
???Changetargetonthefly???
j after2
1
2
3
4
13
JALsavesthePCinregister$31Subroutinereturnsbyjumpingto$31
r31
main: jal myfn
after1:add $1,$2,$3
jal myfnafter2:
sub $3,$4,$5
myfn: …
…jr $31
after11
2
14
Firstcall
JALsavesthePCinregister$31Subroutinereturnsbyjumpingto$31Whathappensforrecursiveinvocations?
r31
main: jal myfn
after1:add $1,$2,$3
jal myfnafter2:
sub $3,$4,$5
myfn: …
…jr $31
after21
2
4
3
15
Secondcall
Problemswithrecursion:• overwritescontentsof$31
r31
main: jal myfn
after1:add $1,$2,$3
myfn:if (test)jal myfn
after2:
jr $31
after11
16
Firstcall
Problemswithrecursion:• overwritescontentsof$31
r31
main: jal myfn
after1:add $1,$2,$3
myfn:if (test)jal myfn
after2:
jr $31
after21
2
17
RecursiveCall
Problemswithrecursion:• overwritescontentsof$31
r31
main: jal myfn
after1:add $1,$2,$3
myfn:if (test)jal myfn
after2:
jr $31
12
3
18
ReturnfromRecursiveCall after2
Problemswithrecursion:• overwritescontentsof$31
r31
main: jal myfn
after1:add $1,$2,$3
myfn:if (test)jal myfn
after2:
jr $31
12
34 Stuck!
19
ReturnfromOriginalCall??? after2
Problemswithrecursion:• overwritescontentsof$31• Cometothinkofit…overwritesall theregisters!
r31
main: jal myfn
after1:add $1,$2,$3
myfn:
jal myfnafter2:
jr $31
after1
addi r1, r0, 5
addi r1, r1, 5
r1 5
12
…
20
1st timethroughmyfn
Problemswithrecursion:• overwritescontentsof$31• Cometothinkofit…overwritesall theregisters!
r31
main: jal myfn
after1:add $1,$2,$3
myfn:
jal myfnafter2:
jr $31
after2
addi r1, r0, 5
addi r1, r1, 5
r1 10
12
…
21
2nd timethroughmyfn
x2000x1FD0
StackManipulatedbypush/pop operationsContext:after2nd JALtomyfn (frommyfn)PUSH: ADDIU$sp,$sp,-20//movespdown
SW$31,16($sp)//storeretn PC1st
Context:2nd myfn isdone(r31==???)POP: LW$31,16($sp) //restoreretn PCàr31
ADDIU$sp,$sp,20//movespupJR$31 //return
myfn stackframe
mainstackframe
myfn stackframe
after2r31r29
x2000
Fornow:Assumeeachframe=x20bytes(justtomakethisexampleconcrete)
x1FD0
after2
XXXX22
WhydoweneedaJALinstructionforprocedurecalls?
A. TheonlywaytochangethePCofyourprogramiswithaJALinstruction.
B. Thesystemwon’tletyoujumptoaprocedurewithjustaJMPinstruction.
C. IfyouJMPtoafunction,itdoesn’tknowwheretoreturntouponcompletion.
D. Actually,JALonlyworksforthefirstfunctioncall.Withmultipleactivefunctions,JALisnottherightinstructiontouse.
23
TransferControl• Callerà Routine• Routineà Caller
PassArgumentstoandfromtheroutine• fixedlength,variablelength,recursively• Getreturnvaluebacktothecaller
ManageRegisters• Alloweachroutinetouseregisters• Preventroutinesfromclobberingeachothers’data
24
Firstfourarguments:passedinregisters$4-$7
• aka$a0,$a1,$a2,$a3Returnedresult:passedbackinaregister
• Specifically,$2,aka$v0main:
li $a0, 6li $a1, 7jal myfnaddi $r1, $v0, 2
main() {int x = myfn(6, 7);x = x + 2;
}
Note:Thisisnottheentirestoryfor1-4arguments.PleaseseetheFullStoryslides.
25
Firstfourarguments:passedin$4-$7
• aka$a0-$a3Subsequentarguments:”spill”ontothestack
main: li $a0, 0li $a1, 1li $a2, 2li $a3, 3addiu $sp,$sp,-8li $8, 4sw $8, 0($sp)li $8, 5sw $8, 4($sp)jal myfn
main() {myfn(0,1,2,3,4,5);…
}
Note:Thisisnottheentirestoryfor5+arguments.PleaseseetheFullStoryslides.
spà
54
spà
26
Arguments1-4:passedin$4-$7roomonstackArguments5+:placedonstack
main: li $a0, 0li $a1, 1li $a2, 2li $a3, 3addiu $sp,$sp,-24li $8, 4sw $8, 16($sp)li $8, 5sw $8, 20($sp)jal myfn
main() {myfn(0,1,2,3,4,5);…
}
spà
spà
4spacefora3spacefora2spacefora1spacefora0
5
0($sp)
4($sp)
8($sp)
12($sp)
16($sp)
20($sp)
Stackdecrimented bymax(16,#args x4)Here:max(16,24)=24
27
• Consistentwayofpassingargumentstoandfromsubroutines
• Createssinglelocationforallarguments• Callermakesroomfor$a0-$a3onstack• Callee mustcopyvaluesfrom$a0-$a3tostack
à callee maytreatallargs asanarrayinmemory• Particularlyhelpfulforfunctionsw/variablelength
inputs:printf(“Scores: %d %d %d\n”, 1, 2, 3);
• Aside:notabadplacetostoreinputsifcalleeneedstocallafunction(yourinputcannotstayin$a0ifyouneedtocallanotherfunction!)
28
Callowspassingwholestructs• int dist(struct Point p1, struct Point p2);
• Treatedascollectionofconsecutive32-bitarguments– Registersforfirst4words,stackforrest
• Better:int dist(struct Point *p1, struct Point *p2);
Wherearetheargumentsto:void sub(int a, int b, int c, int d, int e);void isalpha(char c);void treesort(struct Tree *root);
Wherearethereturnvaluesfrom:struct Node *createNode();struct Node mynode();
Manycombinationsofchar,short,int,void*,struct,etc.• MIPStreatschar,short,int andvoid*identically 29
Whichisatruestatementabouttheargumentstothefunctionvoid sub(int a, int b, int c, int d, int e);
A. Argumentsa-e areallpassedinregisters.B. Argumentsa-e areallstoredonthestack.C. Onlyeisstoredonthestack,butspaceis
allocatedforall5arguments.D. Onlya-darestoredonthestack,butspaceis
allocatedforall5arguments.30
4spacefora3spacefora2spacefora1spacefora0
5
pink’sRetAddr
blue() {pink(0,1,2,3,4,5);
}pink(int a, int b, int c, int d, int e, int f) {
…}
blue’sRetAddr
pink’sstackframe
spà
spà
blue’sstackframe
Notice• Pink’sargumentsareonblue’s stack• spchangesasfunctionscallother
functions, complicatesaccessesà Convenienttokeeppointertobottomofstack==framepointer
$30,aka$fpcanbeusedtorestore$sponexitß fp
31
TransferControl• Callerà Routine• Routineà Caller
PassArgumentstoandfromtheroutine• fixedlength,variablelength,recursively• Getreturnvaluebacktothecaller
ManageRegisters• Alloweachroutinetouseregisters• Preventroutinesfromclobberingeachothers’data
32
Functions:• Arecompiledinisolation• Makeuseofgeneralpurposeregisters• Callotherfunctionsinthemiddleoftheirexecution
• Thesefunctionsalsousegeneralpurposeregisters!• Nowaytocoordinatebetweencaller&callee
à Needaconventionforregistermanagement
33
Registersthatthecallercaresabout:$t0…$t9Abouttocallafunction?• Needvalueinat-registerafterfunctionreturns?
à saveittothestackbeforefn callà restoreitfromthestackafterfn returns
• Don’tneedvalue?à donothing
Functions• Canfreelyusetheseregisters• Mustassumethattheircontentsaredestroyedbyotherfunctions
void myfn(int a) {int x = 10;int y = max(x, a);int z = some_fn(y);return (z + y);
}
Suppose:$t0holdsx$t1holdsy$t2holdsz
Wheredowesaveandrestore?
34
Registersafunctionintendstouse:$s0…$s9Abouttouseans-register?YouMUST:• Savethecurrentvalueonthestackbeforeusing• Restoretheoldvaluefromthestackbeforefn returns
Functions• Mustsavetheseregistersbeforeusingthem
• Mayassumethattheircontentsarepreservedevenacrossfn calls
void myfn(int a) {int x = 10;int y = max(x, a);int z = some_fn(y);return (z + y);
}
Suppose:$t0holdsx$s1holdsy$s2holdsz
Wheredowesaveandrestore?
35
Assumetheregistersarefreeforthetaking,usewithnooverhead
Sincesubroutineswilldothesame,mustprotectvaluesneededlater:
Savebeforefn callRestoreafterfn call
Notice:Goodregisterstouseifyoudon’tcalltoomanyfunctionsorifthevaluesdon’tmatterlateronanyway.
main:…[use$t0&$t1]…addiu $sp,$sp,-8sw $t1,4($sp)sw $t0,0($sp)jal multlw $t1,4($sp)lw $t0,0($sp)addiu $sp,$sp,8…[use$t0&$t1]
36
AssumecallerisusingtheregistersSaveonentryRestoreonexit
Notice:Goodregisterstouseifyoumakealotoffunctioncallsandneedvaluesthatarepreservedacrossallofthem.Also,goodifcallerisactuallyusingtheregisters,otherwisethesaveandrestoresarewasted.Buthardtoknowthis.
main:addiu $sp,$sp,-32sw $ra,28($sp)sw $fp,24($sp)sw $s1,20($sp)sw $s0,16($sp)addiu $fp,$sp,28
…[use$s0and$s1]…
lw $ra,28($sp)lw $fp,24($sp)lw $s1,20$sp)lw $s0,16($sp)addiu $sp,$sp,32jr $ra 37
• firstfourarg wordspassedin$a0-$a3• remainingargs passedinparent’sstackframe• returnvalue(ifany)in$v0,$v1• stackframe($fpto$sp)contains:
• $ra (clobberedonJALs)• localvariables• spacefor4argumentstoCallees• arguments5+toCallees
• callee saveregs:preserved• callersaveregs:notpreserved• globaldataaccessedvia$gp
savedrasavedfpsavedregs($s0...$s7)
locals
outgoingargs
$fpà
$spà 38
r0 $zero zeror1 $at assemblertempr2 $v0 function
returnvaluesr3 $v1r4 $a0
functionarguments
r5 $a1r6 $a2r7 $a3r8 $t0
temps(callersave)
r9 $t1r10 $t2r11 $t3r12 $t4r13 $t5r14 $t6r15 $t7
r16 $s0
saved(callee save)
r17 $s1r18 $s2r19 $s3r20 $s4r21 $s5r22 $s6r23 $s7r24 $t8 moretemps
(caller save)r25 $t9r26 $k0 reservedfor
kernelr27 $k1r28 $gp globaldatapointerr29 $sp stackpointerr30 $fp framepointerr31 $ra returnaddress 39
Assumeafunctionusestwocallee-saveregisters.Howdoweallocateastackframe?Howlargeisthestackframe?Whatshouldbestoredinthestackframe?Whereshouldeverythingbestored?
savedrasavedfpsavedregs($s0...$s7)
locals
outgoingargs
fpà
spà
40
ADDIU$sp,$sp,-32 #allocateframeSW$ra,28($sp) #save$raSW$fp,24($sp) #saveold$fpSW$s1,20($sp) #save...SW$s0,16($sp) #save...ADDIU$fp,$sp,28 #setnewframeptr… ...BODY… ...LW$s0,16($sp) #restore…LW$s1,20($sp) #restore…LW$fp,24($sp) #restoreold$fpLW$ra,28($sp) #restore$raADDIU$sp,$sp,32 #dealloc frameJR$ra
savedrasavedfpsavedregs($s0...$s7)
locals
outgoingargs
fpà
spà
41
pink’sra
blue() {pink(0,1,2,3,4,5);
}pink(int a, int b, int c, int d, int e, int f) {
int x;orange(10,11,12,13,14);
}orange(int a, int b, int c, int, d, int e) {
char buf[100];gets(buf); // no bounds check!
}
Whathappensifmorethan100bytesiswrittentobuf?
savedregsargs forpink
savedfp
savedfp
savedregs
blue’sra
pink’sstackframe
fpà
blue’sstackframe
xargs fororange
spà
pink’srasavedfpsavedregs
orangestackframe
buf[100]
42
Leaffunctiondoesnotinvokeanyotherfunctionsint f(int x, int y) {
return (x+y); }
Optimizations?Nosavedregs (orlocals)NooutgoingargsDon’tpush$raNoframeatall?Possibly…
savedrasavedfpsavedregs($s0...$s7)
locals
outgoingargs
$fpà
$spà43
int test(int a, int b) {int tmp = (a&b)+(a|b);int s = sum(tmp,1,2,3,4,5);int u = sum(s,tmp,b,a,b,a);return u + a + b;
}
CorrectOrder:1. BodyFirst2. Determinestackframesize3. CompletePrologue/Epilogue
44
int test(int a, int b) {int tmp = (a&b)+(a|b);int s = sum(tmp,1,2,3,4,5);int u = sum(s,tmp,b,a,b,a);return u + a + b;
}
test:
MOVE$s0,$a0MOVE$s1,$a1AND$t0,$a0,$a1OR$t1,$a0,$a1ADD$t0,$t0,$t1MOVE$a0,$t0LI$a1,1LI$a2,2LI$a3,3LI$t1,4SW$t116($sp)LI$t1,5SW$t1,20($sp)SW$t0,24($sp)JALsumNOPLW$t0,24($sp)
Prologue
We’llassumetheyellowinordertoforceyourhandontherest.$s0for$a0/a$s1for$a1/b$t0fortmp
CanwegetridoftheNOP?Wewanttodothelw… 45
int test(int a, int b) {int tmp = (a&b)+(a|b);int s = sum(tmp,1,2,3,4,5);int u = sum(s,tmp,b,a,b,a);return u + a + b;
}
MOVE$a0,$v0#sMOVE$a1,$t0#tmpMOVE$a2,$s1#bMOVE$a3,$s0#aSW$s1,16($sp)#bSW$s0,20($sp)#aJALsumNOP
ADD$v0,$v0,$s0#u+aADD$v0,$v0,$s1#+b
Epilogue
46
int test(int a, int b) {int tmp = (a&b)+(a|b);int s = sum(tmp,1,2,3,4,5);int u = sum(s,tmp,b,a,b,a);return u + a + b;
}
How many bytes dowe need to allocate for the stack frame?a) 24b) 36c) 44d) 48e) 52Minimumstacksizeforastandardfunction?
savedrasavedfpsavedregs
($s0and$s1)
locals($t0)
outgoingargsspacefora0- a3and5th and6th arg 47
int test(int a, int b) {int tmp = (a&b)+(a|b);int s = sum(tmp,1,2,3,4,5);int u = sum(s,tmp,b,a,b,a);return u + a + b;
}savedrasavedfpsavedregs
($s0and$s1)
locals($t0)
outgoingargsspacefora0- a3and5th and6th arg
savedrasavedfp
fpà
spà
savedreg $s1savedreg $s0local$t0
outgoing6th argoutgoing5th argspacefor$a3spacefor$a2spacefor$a1spacefor$a0
48
1216
0
202428323640
How many bytes dowe need to allocate for the stack frame?
44Minimumstacksizeforastandardfunction?
$ra +$fp+4args =6x4bytes=24bytes
48
#allocateframe#save$ra#saveold$fp#callee save...#callee save...#setnewframeptr
...
...
#restore…#restore…#restoreold$fp#restore$ra#dealloc frame
49
#allocateframe#save$ra#saveold$fp#callee save...#callee save...#setnewframeptr
...
...
#restore…#restore…#restoreold$fp#restore$ra#dealloc frame
savedrasavedfp
savedreg $s1savedreg $s0local$t0
outgoing6th argoutgoing5th argspacefor$a3spacefor$a2spacefor$a1spacefor$a0
48
1216
0
202428323640fpà
spà
ADDIU$sp,$sp,-44SW$ra,40($sp)SW $fp,36($sp)SW$s1,32($sp)SW$s0,28($sp)ADDIU$fp,$sp,40
LW$s0,28($sp)LW $s1,32($sp)LW$fp,36($sp)LW$ra,40($sp)ADDIU$sp,$sp,44JR$raNOP
Body(previousslide,Activity#1)
50