computer systems
play

Computer Systems Lecture 12 Subroutines CS 230 - Spring 2020 2-1 - PowerPoint PPT Presentation

CS 230 Introduction to Computers and Computer Systems Lecture 12 Subroutines CS 230 - Spring 2020 2-1 Subroutines Also called functions, methods, or procedures all the same thing in assembly language examples from C: sqrt()


  1. CS 230 – Introduction to Computers and Computer Systems Lecture 12 – Subroutines CS 230 - Spring 2020 2-1

  2. Subroutines  Also called functions, methods, or procedures  all the same thing in assembly language  examples from C: sqrt() or printf()  Important technique to modularize programs  separation of concerns, smaller coding units  Challenges  call/return – how to redirect execution?  how do I get back to where I came from?  arguments (parameters) and result(s) passing CS 230 - Spring 2020 2-2

  3. Calling jal x  J ump A nd L ink  copy (current PC + 4) into register $31  set PC to x (where x is a label)  labels mark the start of functions same type of labels we use for beq and bne   x can also be an immediate (don’t do this) CS 230 - Spring 2020 2-3

  4. Indirect Calling jalr $q  J ump A nd L ink R egister  copy (current PC + 4) into register $31  set PC to contents of register $q  load address of label into $q with lis  value in $q is called a function pointer  allows functions to be passed as parameters CS 230 - Spring 2020 2-4

  5. Returning jr $s  J ump R egister  set PC to $s  usually jr $31  register $31 holds return address  register $31 begins with the outer return address  jr $31 to this address ends the program CS 230 - Spring 2020 2-5

  6. Calling and Returning Example function addTwoNumbers: add $2, $4, $5 jr $31 Call that function jal addTwoNumbers Call that function indirectly lis $1 .word addTwoNumbers jalr $1 Need more than this (wait for slide 9) CS 230 - Spring 2020 2-6

  7. Execution Context  Subroutines can call subroutines  what happens to outer subroutine’s parameters?  what happens to old value of register $31?  How about registers that are in use?  who gets to use which registers?  Save/restore current execution context when a function is called  set of registers following the register conventions  save to the stack CS 230 - Spring 2020 2-7

  8. Register Conventions  $2, $3 – function return values  $4 - $7 – function arguments (parameters)  $1, $8 - $15, $24, $25 – unsaved temporary  $16 - $23 – saved temporary  $26 - $29 – reserved for operating system  $30 – stack pointer  $31 – return address (always save on stack) CS 230 - Spring 2020 2-8

  9. Calling Expanded Calling a function addi $30, $30, -4 sw $31, 0($30) jal addTwoNumbers lw $31, 0($30) addi $30, $30, 4 Calling a function indirectly lis $1 .word addTwoNumbers addi $30, $30, -4 sw $31, 0($30) jalr $1 lw $31, 0($30) addi $30, $30, 4 Might want to store other registers on stack too CS 230 - Spring 2020 2-9

  10. Arguments and Return  Arguments are in registers $4-$7  in order as needed  example: addTwoNumbers would use $4 and $5  Return values (results) in registers $2 and $3  used in order as needed  functions may have zero, one, or two results  example: addTwoNumbers would place sum in $2 CS 230 - Spring 2020 2-10

  11. Unsaved Temporaries  $1, $8 - $15, $24, $25  “if I call a function, I must assume it overwrites these registers”  “I am free to modify these registers at any time”  “if I call a function but I want to preserve a value from one of these registers, I must store that value on the stack”  “or in a saved temporary”  $2, $3, $4-$7 (function results and arguments) can be treated as unsaved temporaries with extra functionality CS 230 - Spring 2020 2-11

  12. Saved Temporaries  $16 - $23  “if I call a function I can assume it will not modify these registers”  “if I modify one of these registers, I must save the old value on the stack first and then restore that value after I am done using the register” CS 230 - Spring 2020 2-12

  13. Preserving the Stack  A function must preserve the value of the stack pointer  Always add back any value you subtracted from the stack pointer before ending a function  The stack pointer should have the same value at the begging and end of a function CS 230 - Spring 2020 2-13

  14. Example addi $4, $0, 15 addi $5, $0, 13 addi $30, $30, -4 sw $31, 0($30) jal addTwoNumbers lw $31, 0($30) addi $30, $30, 4 jr $31 addTwoNumbers: add $2, $4, $5 jr $31 What’s the value in register $2 at the end of the program? CS 230 - Spring 2020 2-14

  15. Example addi $4, $0, 15 ? 0x64 addi $5, $0, 13 0x68 ? 0x6C ? addi $30, $30, -4 0x70 ? sw $31, 0($30) 0x74 ? jal addTwoNumbers 0x78 ? lw $31, 0($30) 0x7C ? addi $30, $30, 4 0x80 ? SP = jr $31 0x84 ? 0x88 ? addTwoNumbers: add $2, $4, $5 PC = 0x1C 10 jr $31 $0 $1 $2 $3 $4 $5 ... $30 $31 What’s the value in register $2 at the ? … 0x80 0 ? ? ? ? end of the program? CS 230 - Spring 2020 2-15

  16. Example addi $4, $0, 15 ? 0x64 addi $5, $0, 13 0x68 ? 0x6C ? addi $30, $30, -4 0x70 ? sw $31, 0($30) 0x74 ? jal addTwoNumbers 0x78 ? lw $31, 0($30) 0x7C ? addi $30, $30, 4 0x80 ? SP = jr $31 0x84 ? 0x88 ? addTwoNumbers: add $2, $4, $5 PC = 0x20 10 jr $31 $0 $1 $2 $3 $4 $5 ... $30 $31 What’s the value in register $2 at the ? 15 ? … 0x80 0 ? ? end of the program? CS 230 - Spring 2020 2-16

  17. Example addi $4, $0, 15 ? 0x64 addi $5, $0, 13 0x68 ? 0x6C ? addi $30, $30, -4 0x70 ? sw $31, 0($30) 0x74 ? jal addTwoNumbers 0x78 ? lw $31, 0($30) 0x7C ? addi $30, $30, 4 0x80 ? SP = jr $31 0x84 ? 0x88 ? addTwoNumbers: add $2, $4, $5 PC = 0x24 10 jr $31 $0 $1 $2 $3 $4 $5 ... $30 $31 What’s the value in register $2 at the ? 15 13 … 0x80 0 ? ? end of the program? CS 230 - Spring 2020 2-17

  18. Example addi $4, $0, 15 ? 0x64 addi $5, $0, 13 0x68 ? 0x6C ? addi $30, $30, -4 0x70 ? sw $31, 0($30) 0x74 ? jal addTwoNumbers 0x78 ? lw $31, 0($30) 0x7C ? SP = addi $30, $30, 4 0x80 ? jr $31 0x84 ? 0x88 ? addTwoNumbers: add $2, $4, $5 PC = 0x28 10 jr $31 $0 $1 $2 $3 $4 $5 ... $30 $31 What’s the value in register $2 at the ? 15 13 … 0x7C 0 ? ? end of the program? CS 230 - Spring 2020 2-18

  19. Example addi $4, $0, 15 ? 0x64 addi $5, $0, 13 0x68 ? 0x6C ? addi $30, $30, -4 0x70 ? sw $31, 0($30) 0x74 ? jal addTwoNumbers 0x78 ? lw $31, 0($30) 0x7C SP = addi $30, $30, 4 0x80 ? jr $31 0x84 ? 0x88 ? addTwoNumbers: add $2, $4, $5 PC = 0x2C 10 jr $31 $0 $1 $2 $3 $4 $5 ... $30 $31 What’s the value in register $2 at the ? 15 13 … 0x7C 0 ? ? end of the program? CS 230 - Spring 2020 2-19

  20. Example addi $4, $0, 15 ? 0x64 addi $5, $0, 13 0x68 ? 0x6C ? addi $30, $30, -4 0x70 ? sw $31, 0($30) 0x74 ? jal addTwoNumbers 0x78 ? lw $31, 0($30) 0x7C SP = addi $30, $30, 4 0x80 ? jr $31 0x84 ? 0x88 ? addTwoNumbers: add $2, $4, $5 PC = 0x3C 10 jr $31 $0 $1 $2 $3 $4 $5 ... $30 $31 What’s the value in register $2 at the ? 15 13 … 0x7C 0x30 0 ? ? end of the program? CS 230 - Spring 2020 2-20

  21. Example addi $4, $0, 15 ? 0x64 addi $5, $0, 13 0x68 ? 0x6C ? addi $30, $30, -4 0x70 ? sw $31, 0($30) 0x74 ? jal addTwoNumbers 0x78 ? lw $31, 0($30) 0x7C SP = addi $30, $30, 4 0x80 ? jr $31 0x84 ? 0x88 ? addTwoNumbers: add $2, $4, $5 PC = 0x40 10 jr $31 $0 $1 $2 $3 $4 $5 ... $30 $31 What’s the value in register $2 at the ? 28 ? 15 13 … 0x7C 0x30 0 end of the program? CS 230 - Spring 2020 2-21

  22. Example addi $4, $0, 15 ? 0x64 addi $5, $0, 13 0x68 ? 0x6C ? addi $30, $30, -4 0x70 ? sw $31, 0($30) 0x74 ? jal addTwoNumbers 0x78 ? lw $31, 0($30) 0x7C SP = addi $30, $30, 4 0x80 ? jr $31 0x84 ? 0x88 ? addTwoNumbers: add $2, $4, $5 PC = 0x30 10 jr $31 $0 $1 $2 $3 $4 $5 ... $30 $31 What’s the value in register $2 at the ? 28 ? 15 13 … 0x7C 0x30 0 end of the program? CS 230 - Spring 2020 2-22

  23. Example addi $4, $0, 15 ? 0x64 addi $5, $0, 13 0x68 ? 0x6C ? addi $30, $30, -4 0x70 ? sw $31, 0($30) 0x74 ? jal addTwoNumbers 0x78 ? lw $31, 0($30) 0x7C SP = addi $30, $30, 4 0x80 ? jr $31 0x84 ? 0x88 ? addTwoNumbers: add $2, $4, $5 PC = 0x34 10 jr $31 $0 $1 $2 $3 $4 $5 ... $30 $31 What’s the value in register $2 at the ? 28 ? 15 13 … 0x7C 0 end of the program? CS 230 - Spring 2020 2-23

  24. Example addi $4, $0, 15 ? 0x64 addi $5, $0, 13 0x68 ? 0x6C ? addi $30, $30, -4 0x70 ? sw $31, 0($30) 0x74 ? jal addTwoNumbers 0x78 ? lw $31, 0($30) 0x7C addi $30, $30, 4 0x80 ? SP = jr $31 0x84 ? 0x88 ? addTwoNumbers: add $2, $4, $5 PC = 0x38 10 jr $31 $0 $1 $2 $3 $4 $5 ... $30 $31 What’s the value in register $2 at the ? 28 ? 15 13 … 0x80 0 end of the program? CS 230 - Spring 2020 2-24

  25. Example addi $4, $0, 15 ? 0x64 addi $5, $0, 13 0x68 ? 0x6C ? addi $30, $30, -4 0x70 ? sw $31, 0($30) 0x74 ? jal addTwoNumbers 0x78 ? lw $31, 0($30) 0x7C addi $30, $30, 4 0x80 ? SP = jr $31 0x84 ? 0x88 ? addTwoNumbers: add $2, $4, $5 PC = 0x3C 10 jr $31 $0 $1 $2 $3 $4 $5 ... $30 $31 What’s the value in register $2 at the ? 28 ? 15 13 … 0x80 0 end of the program? $2 = 28 10 CS 230 - Spring 2020 2-25

  26. Example Assume there exists a subroutine fact which takes one parameter x returns x! ( x factorial). Write a MIPS assembly language program that uses that subroutine and adds the factorials of the value in register $1 and the value in register $2 together and places the result in register $2. Follow the register conventions. CS 230 - Spring 2020 2-26

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend