1 2 EE 109 Unit 15 Subroutines Program Counter and GPRs (especially $sp, $ra, and $fp) REVIEW OF RELEVANT CONCEPTS Stacks 3 4 Review of Program Counter GPR's Used for Subroutine Support Assembler Name Reg. Number Description • PC is used to fetch an instruction $zero $0 Constant 0 value – PC contains the address of the ____________________ $at $1 Assembler temporary – The value in the PC is placed on the address bus and the memory is $v0-$v1 $2-$3 Procedure return values or expression evaluation told to read $a0-$a3 $4-$7 Arguments/parameters – The PC is incremented, and the process is repeated for the next instruction $t0-$t7 $8-$15 Temporaries $s0-$s7 $16-$23 Saved Temporaries Processor Memory PC = Addr = 0 0 PC $t8-$t9 $24-$25 Temporaries Addr 0 inst. 1 $k0-$k1 $26-$27 Reserved for OS kernel op. 1 inst. 2 $gp $28 Global Pointer (Global and static Data = inst.1 machine code 2 inst. 3 ALU variables/data) out in1 Data 3 inst. 4 ADD, SUB, $sp $29 Stack Pointer AND, OR 4 inst. 5 in2 $fp $30 Frame Pointer Control = Read … $0-$31 Control $ra $31 Return address for current procedure FF
5 6 Subroutines (Functions) Subroutines • Subroutines are portions of code that we can call • Subroutines are similar to _____________ from anywhere in our code, execute that subroutine, where we jump to a new location in the code and then ____________________________ C code: C code: void main() { void main() { ... We call the ... subroutine to x = 8; x = 8; calculate the average res = avg(x,4); and return to where res = avg(x,4); we called it ... Call “avg” sub-routine ... 1 will require us to branch } } A subroutine to to that code int avg(int a, int b){ calculate the average int avg(int a, int b){ of 2 numbers return (a+b)/2; return (a+b)/2; } } 7 8 Normal Branches vs. Subroutines Implementing Subroutines • Difference between normal branches and • To implement subroutines in assembly we subroutines branches is that with subroutines we need to be able to: have to return to where we left off • We need to leave a ___________ to the return – ______________ to the subroutine code location _____________ we jump to the – _______________________ to when we finish the subroutine…once in the function its _____________ subroutine void main() { C code: C code: Assembly: ... ... .text x = 8; ... res = avg(x,4); Call res = avg(x,4); jal AVG ... ... Call “avg” sub-routine to 1 ... After subroutine calculate the average } completes, Definition 2 int avg(int a, int b) int avg(int a, int b){ return to the AVG: ... { ... } statement in the return (a+b)/2; jr $ra main code } where we left off
9 10 Jumping to a Subroutine Jumping to a Subroutine • JAL instruction (Jump And Link) • Use the JAL instruction to jump execution to – Format: jal the subroutine and leave a link to the Address/Label – Similar to jump where we load an address into the PC [e.g. following instruction PC = addr] • Same limitations (26-bit address) as jump instruction PC before exec. of jal: • Addr is usually specified by a label 0040 0000 Assembly: • JALR instruction (Jump And Link Register) $ra before exec. of jal: 0x400000 jal AVG jal will cause the program to 0000 0000 0x400004 add jump to the label AVG and – Format: jalr $rs ... store the return address in $ra/$31. – Jumps to address specified by $rs (so we can jump a full PC after exec. of jal: 1 32-bits) AVG: = 0x400810 $ra after exec. of jal: add • In addition to jumping, JAL/JALR ______________ ... ______________________________ to be used as a jr $ra link to return to after the subroutine completes 11 12 Returning from a Subroutine Return Addresses • Use a JR with the $ra register to return to the • No single return address for a subroutine since AVG may be called many times from many places in the code instruction after the JAL that called this • JAL always stores the address of the instruction after it subroutine (i.e. PC of ‘jal’ + 4) PC before exec. of jr: 0040 08ec Assembly: PC 0040 0000 0x400000 jal AVG 0x400004 is the return address for this JAL $ra before exec. of jr: 0x400000 jal AVG jal will cause the program 0x400004 add 0040 0004 0x400004 add to jump to the label AVG ... and store the return ... 0x400028 is the return address for this JAL PC 0040 0024 0x400024 jal AVG address in $ra/$31. PC after exec. of jr: 1 0x400028 sub ... AVG: = 0x400810 add 2 0x400810 ... Go back to where we left AVG ... 0x4008ec jr $ra off using the return jr $ra address stored by JAL
13 14 Return Addresses Dealing with Return Addresses • A further complication Assembly: Assembly: • Multiple return addresses ... ... can be spilled to memory is _______________ jal SUB1 jal SUB1 – “Always” have enough ________________ 0x40001A ... 0x40001A ... 1 1 memory 4 4 • Example: Main routine • Note: Return addresses will calls SUB1 which calls be accessed in _________ SUB1 jal SUB2 SUB1 jal SUB2 _______ as they are stored SUB2 0x400208 jr $ra 0x400208 jr $ra – 0x400208 is the _________ • Must store both return 2 2 RA to be stored but should be 3 3 SUB2 ... SUB2 ... addresses but _______ the _______ one used to return jr $ra jr $ra ___________________ – A _________ is appropriate! 15 16 Stacks Stacks • Stack is a data structure where data is • 2 Operations on stack accessed in reverse order as it is stored 7fffeffc 0000 0000 7fffeffc (a.k.a. LIFO = ________________) – _______: Put new data on top $sp = 0000 0000 7fffeff8 Stack Pointer • Use a stack to store the return addresses of stack Always points to 0000 0000 7fffeff4 and other data top occupied • Decrement $sp Empty stack 0x7fffeffc is the base of • System stack defined as growing towards element of the the system stack for _______________ addresses • Write value to where $sp points 0000 0000 stack 7fffeffc the MARS simulator – MARS starts stack at 0x7fffeffc – _______: Retrieves and $sp = 7fffeff8 $sp = – Normal MIPS starts stack at 0x80000000 7fffeff4 0000 0000 “removes” data from top of 7fffeffc 7fffeffc Push • Top of stack is accessed and maintained 0040 0208 7fffeff8 Push will add a value to the top of stack using $sp=R[29] (stack pointer) the stack 0000 0000 7fffeff4 • Read value from where $sp – $sp points at top _______________ 0000 0000 7fffeff0 0000 0000 7fffeffc location of the stack points 0000 0000 7fffefec $sp = 7fffeff8 • Increment $sp to effectively 7fffeff4 0000 0000 7fffefe8 Pop “delete” top value Stack grows towards Pop will remove the top value from _______ the stack addresses
17 18 Push Operation Pop Operation • Recall we assume $sp points • Pop: Retrieves and Push return address (e.g. 0x00400208) Pop return address at top occupied location "removes" data from top $sp = $sp = of stack • Push: Put new data on top of 7fffeffc 0000 0000 0000 0000 7fffeffc 7fffeffc 7fffeffc – Read value from where SP stack 7fffeff8 0040 0208 7fffeff8 7fffeff8 7fffeff8 points $sp = 7fffeff4 0000 0000 7fffeff4 – Decrement SP • ____________________ • ____________________ Decrement SP by 4 (since pushing Read value that SP points at then – Increment SP to effectively a word), then write value to where • Always decrement by 4 since increment SP (this effectively $sp is now pointing "deletes" top value deletes the value because the next addresses are always stored as push will overwrite it) words (32-bits) • ___________________ – Write return address ($ra) to • Always increment by 4 when Warning : Because the stack grows towards popping addresses where SP points lower addresses, when you push something on the stack you subtract 4 from the SP and • _____________________ when you pop, you add 4 to the SP. 19 20 Subroutines and the Stack Subroutines and the Stack ... 0 • When writing native assembly, programmer must add code to 0000 0000 $sp = 7fffeffc 7fffeffc jal SUB1 0000 0000 7fffeff8 manage return addresses and the stack 0x40001A ... 0000 0000 7fffeff4 $ra = • At the beginning of a routine (PREAMBLE) – Push $ra (produced by 'jal') onto the stack 0 1 0000 0000 7fffeffc $sp = SUB1 addi $sp,$sp,-4 addi _____________ sw $ra,0($sp) 7fffeff8 1 sw _____________ jal SUB2 7fffeff4 $ra = 0x400208 lw $ra,0($sp) • Execute subroutine which can now freely call other routines addi $sp,$sp,4 3 jr $ra • At the end of a routine (POSTAMBLE) 2 $sp = 0000 0000 7fffeffc 7fffeff8 – Pop/restore $ra from the stack SUB2 addi $sp,$sp,-4 $ra = 7fffeff4 lw _____________ sw $ra,0($sp) addi ______________ 2 ... jr $ra 3 $sp = 0000 0000 7fffeffc lw $ra,0($sp) addi $sp,$sp,4 7fffeff8 jr $ra $ra = 7fffeff4
Recommend
More recommend