1 / 19

Lecture 8. MIPS Instructions #4 – Branch Instructions #2

2010 R&E Computer System Education & Research. Lecture 8. MIPS Instructions #4 – Branch Instructions #2. Prof. Taeweon Suh Computer Science Education Korea University. Procedure (Function). Programmers use a procedure (or function) to structure programs

marion
Télécharger la présentation

Lecture 8. MIPS Instructions #4 – Branch Instructions #2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2010 R&E Computer System Education & Research Lecture 8. MIPS Instructions #4 – Branch Instructions #2 Prof. Taeweon Suh Computer Science Education Korea University

  2. Procedure (Function) • Programmers use a procedure (or function) to structure programs • To make the program modular and easy to understand • To allow code to be re-used • Procedures allow the programmer to focus on just one portion of the task at a time • Parameters (arguments) act as an interface between the procedure and the rest of the program

  3. Procedure Calls Definitions • Caller: calling procedure (in this case, main) • Callee: called procedure (in this case, sum) High-level code example void main() { int y; y = sum(42, 7); ... } int sum(int a, int b) { return (a + b); }

  4. MIPS Instructions for Accessing Procedures • Procedure callinstruction:jal ProcedureAddress #jump and link # $ra <- pc + 4 # pc <- jump target • Saves PC+4 in register $ra to have a link to the next instruction for the procedure return • Instruction format (J format): 0x03 26-bit address High-level code int main() { simple(); a = b + c; } void simple() { return; } MIPS assembly code 0x00400200 main: jal simple 0x00400204 add $s0, $s1, $s2 ... 0x00401020 simple: jr $ra compile PC PC+4 jal: jumps to simple and saves PC+4 in the return address register ($ra). In this case, $ra = 0x00400204 after jal executes void means that simple doesn’t return a value.

  5. MIPS Instructions for Accessing Procedures • Return instruction: jr $ra #return // pc <- $ra • Instruction format (R format): High-level code int main() { simple(); a = b + c; } void simple() { return; } MIPS assembly code 0x00400200 main: jal simple 0x00400204 add $s0, $s1, $s2 ... 0x00401020 simple: jr $ra compile $racontains 0x00400204 jr $ra: jumps to address in $ra, in this case 0x00400204. 0 31 0x08

  6. Procedure Calls Procedure calling conventions: • Caller: • passes arguments to callee. • jumps to the callee • Callee: • performs the procedure • returns the result to caller • returns to the point of call • Must not overwrite registers or memory needed by the caller MIPS conventions: • Call procedure: jump and link (jal) • Return from procedure: jump register (jr) • Argument values: $a0 - $a3 • Return values: $v0, $v1

  7. Input Arguments and Return Values High-level code int main() { int y; ... y = diffofsums(2, 3, 4, 5); // 4 arguments ... } int diffofsums(int f, int g, int h, int i) { int result; result = (f + g) - (h + i); return result; // return value }

  8. Input Arguments and Return Values MIPS assembly code # $s0 = y main: ... addi $a0, $0, 2 # argument 0 = 2 addi $a1, $0, 3 # argument 1 = 3 addi $a2, $0, 4 # argument 2 = 4 addi $a3, $0, 5 # argument 3 = 5 jal diffofsums # call procedure add $s0, $v0, $0 # y = returned value ... # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller High-level code int main() { int y; ... y = diffofsums(2, 3, 4, 5); // 4 arguments ... } int diffofsums(int f, int g, int h, int i) { int result; result = (f + g) - (h + i); return result; // return value }

  9. Input Arguments and Return Values • We need a place (called stack) to temporarily store arguments and registers because … • diffofsumsoverwrote 3 registers:$t0, $t1, and $s0 , which may be being used in the main routine • What if the callee (diffofsums) needs to use more registers than allocatedto argument ($a0 - $a3) and return values ($v0, $v1) MIPS assembly code # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller

  10. The Stack • Memory used to temporarily save variables • Like a stack of dishes, stack is a data structure for spilling registers organized as a last-in-first-out (LIFO) queue

  11. The Stack - Spilling Registers • Stack is a data structure for spilling registers organized as a last-in-first-out (LIFO) queue • One of the general registers, $sp ($29), is used to point to the top of the stack • The stack “grows” from high address to low address in MIPS • Push: add data onto the stack • $sp = $sp – 4 • data on stack at new $sp • Pop: remove data from the stack • Data from stack at $sp • $sp = $sp + 4 Main Memory high addr top of stack $sp low addr

  12. Stack Pointer (SP) • Stack pointer ($sp) points to the top of the stack Main Memory Main Memory

  13. How Procedures use the Stack • Called procedures (callee) must not have any unintended side effects to the caller • But, diffofsumsoverwrites 3 registers ($t0, $t1, $s0) as duplicated below: # MIPS assembly # $s0 = result diffofsums: add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 jr $ra # return to caller

  14. Storing Register Values on the Stack # $s0 = result diffofsums: addi $sp, $sp, -12 # make space on stack # to store 3 registers sw $s0, 8($sp) # save $s0 on stack sw $t0, 4($sp) # save $t0 on stack sw $t1, 0($sp) # save $t1 on stack add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 lw $t1, 0($sp) # restore $t1 from stack lw $t0, 4($sp) # restore $t0 from stack lw $s0, 8($sp) # restore $s0 from stack addi $sp, $sp, 12 # deallocate stack space jr $ra # return to caller “Push” (back up) the registers to be used in the callee to the stack “Pop” (restore) the registers from the stack prior to returning to the caller

  15. Preserved and NonPreserved Registers • In the previous example, if the calling procedure does not use the temporary registers ($t0, $t1), the effort to save and restore them is wasted • To avoid this waste, MIPS divides registers into preserved and nonpreserved categories • The preserved registers include $s0 ~ $s7 (saved) • The nonpreserved registers include $t0 ~ $t9 (temporary) • So, a procedure must save and restore any of the preserved registers it wishes to use, but it can change the nonpreserved registers freely • The callee must save and restore any preserved registers it wishes to use • The callee may change any of the nonpreserved registers • But, if the caller is holding active data in a nonpreserved register, the caller needs to save and restore it

  16. Storing Saved Registers on the Stack # $s0 = result diffofsums: addi $sp, $sp, -4 # make space on stack to # store one register sw $s0, 0($sp) # save $s0 on stack # no need to save $t0 or $t1 add $t0, $a0, $a1 # $t0 = f + g add $t1, $a2, $a3 # $t1 = h + i sub $s0, $t0, $t1 # result = (f + g) - (h + i) add $v0, $s0, $0 # put return value in $v0 lw $s0, 0($sp) # restore $s0 from stack addi $sp, $sp, 4 # deallocate stack space jr $ra # return to caller

  17. Nested Procedure Calls • Procedures that do not call others are called leaf procedures • Life would be simple if all procedures were leaf procedures, but they aren’t • For example, • the main program calls procedure A with an argument of 3 (by placing the value 3 into register $a0 and then using jal A) • Procedure A calls procedure B via jal B with an argument 7 (also placed in $a0) • There is a conflict over the use of register $a0 and $ra • Use stack to preserve registers • The caller pushes any argument registers ($a0~$a4) or temporary registers ($t0~$t9) that are needed after the call proc1: addi $sp, $sp, -4 # make space on stack sw $ra, 0($sp) # save $ra on stack jal proc2 ... lw $ra, 0($sp) # restore $s0 from stack addi $sp, $sp, 4 # deallocate stack space jr $ra # return to caller

  18. Recursive Procedure Call MIPS assembly code 0x90 factorial: addi $sp, $sp, -8 # make room 0x94 sw $a0, 4($sp) # store $a0 0x98 sw $ra, 0($sp) # store $ra 0x9C addi $t0, $0, 2 0xA0 slt $t0, $a0, $t0 # a <= 1 ? 0xA4 beq $t0, $0, else # no: go to else 0xA8 addi $v0, $0, 1 # yes: return 1 0xAC addi $sp, $sp, 8 # restore $sp 0xB0 jr $ra # return 0xB4 else:addi $a0, $a0, -1 # n = n - 1 0xB8 jal factorial # recursive call 0xBC lw $ra, 0($sp) # restore $ra 0xC0 lw $a0, 4($sp) # restore $a0 0xC4addi $sp, $sp, 8 # restore $sp 0xC8 mul $v0, $a0, $v0 # n * factorial(n-1) 0xCC jr $ra # return • Recursive procedures invoke “clones” of themselves High-level code int factorial(int n) { if (n <= 1) return 1; else return (n * factorial(n-1)); }

  19. Stack during Recursive Call

More Related