CS 230 - Spring 2020 2-1
Computer Systems Lecture 12 Subroutines CS 230 - Spring 2020 2-1 - - PowerPoint PPT Presentation
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()
CS 230 - Spring 2020 2-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-3
Calling
jal x
Jump And Link 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-4
Indirect Calling
jalr $q
Jump And Link Register 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-5
Returning
jr $s
Jump Register 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-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-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-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-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-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-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
- n 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-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
- ld value on the stack first and then restore that
value after I am done using the register”
CS 230 - Spring 2020 2-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
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
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-15
0x64 0x68 0x6C 0x70 0x74 0x78 0x7C 0x80 0x84 0x88
$0 $1 $2 $3 $4 $5 ... $30 $31 ? ? ? ? ? … 0x80
SP = ? ? ? ? ? ? ? ? ? ?
PC = 0x1C10
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-16
0x64 0x68 0x6C 0x70 0x74 0x78 0x7C 0x80 0x84 0x88
$0 $1 $2 $3 $4 $5 ... $30 $31 ? ? ? 15 ? … 0x80
SP = ? ? ? ? ? ? ? ? ? ?
PC = 0x2010
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-17
0x64 0x68 0x6C 0x70 0x74 0x78 0x7C 0x80 0x84 0x88
$0 $1 $2 $3 $4 $5 ... $30 $31 ? ? ? 15 13 … 0x80
SP = ? ? ? ? ? ? ? ? ? ?
PC = 0x2410
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-18
0x64 0x68 0x6C 0x70 0x74 0x78 0x7C 0x80 0x84 0x88
$0 $1 $2 $3 $4 $5 ... $30 $31 ? ? ? 15 13 … 0x7C
SP = ? ? ? ? ? ? ? ? ? ?
PC = 0x2810
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-19
0x64 0x68 0x6C 0x70 0x74 0x78 0x7C 0x80 0x84 0x88
$0 $1 $2 $3 $4 $5 ... $30 $31 ? ? ? 15 13 … 0x7C
SP = ? ? ? ? ? ? ? ? ?
PC = 0x2C10
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-20
0x64 0x68 0x6C 0x70 0x74 0x78 0x7C 0x80 0x84 0x88
$0 $1 $2 $3 $4 $5 ... $30 $31 ? ? ? 15 13 … 0x7C 0x30
SP = ? ? ? ? ? ? ? ? ?
PC = 0x3C10
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-21
0x64 0x68 0x6C 0x70 0x74 0x78 0x7C 0x80 0x84 0x88
$0 $1 $2 $3 $4 $5 ... $30 $31 ? 28 ? 15 13 … 0x7C 0x30
SP = ? ? ? ? ? ? ? ? ?
PC = 0x4010
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-22
0x64 0x68 0x6C 0x70 0x74 0x78 0x7C 0x80 0x84 0x88
$0 $1 $2 $3 $4 $5 ... $30 $31 ? 28 ? 15 13 … 0x7C 0x30
SP = ? ? ? ? ? ? ? ? ?
PC = 0x3010
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-23
0x64 0x68 0x6C 0x70 0x74 0x78 0x7C 0x80 0x84 0x88
$0 $1 $2 $3 $4 $5 ... $30 $31 ? 28 ? 15 13 … 0x7C
SP = ? ? ? ? ? ? ? ? ?
PC = 0x3410
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-24
0x64 0x68 0x6C 0x70 0x74 0x78 0x7C 0x80 0x84 0x88
$0 $1 $2 $3 $4 $5 ... $30 $31 ? 28 ? 15 13 … 0x80
SP = ? ? ? ? ? ? ? ? ?
PC = 0x3810
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? $2 = 2810
CS 230 - Spring 2020 2-25
0x64 0x68 0x6C 0x70 0x74 0x78 0x7C 0x80 0x84 0x88
$0 $1 $2 $3 $4 $5 ... $30 $31 ? 28 ? 15 13 … 0x80
SP = ? ? ? ? ? ? ? ? ?
PC = 0x3C10
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
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.
addi $30, $30, -12 sw $31, 0($30) lw $31, 0($30) addi $30, $30, 12 jr $31
CS 230 - Spring 2020 2-27
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.
addi $30, $30, -12 sw $31, 0($30) jal fact jal fact lw $31, 0($30) addi $30, $30, 12 jr $31
CS 230 - Spring 2020 2-28
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.
addi $30, $30, -12 sw $31, 0($30) addi $4, $1, 0 jal fact jal fact lw $31, 0($30) addi $30, $30, 12 jr $31
CS 230 - Spring 2020 2-29
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.
addi $30, $30, -12 sw $31, 0($30) sw $2, 4($30) addi $4, $1, 0 jal fact jal fact lw $31, 0($30) addi $30, $30, 12 jr $31
CS 230 - Spring 2020 2-30
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.
addi $30, $30, -12 sw $31, 0($30) sw $2, 4($30) addi $4, $1, 0 jal fact sw $2, 8($30) jal fact lw $31, 0($30) addi $30, $30, 12 jr $31
CS 230 - Spring 2020 2-31
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.
addi $30, $30, -12 sw $31, 0($30) sw $2, 4($30) addi $4, $1, 0 jal fact sw $2, 8($30) lw $4, 4($30) jal fact lw $31, 0($30) addi $30, $30, 12 jr $31
CS 230 - Spring 2020 2-32
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.
addi $30, $30, -12 sw $31, 0($30) sw $2, 4($30) addi $4, $1, 0 jal fact sw $2, 8($30) lw $4, 4($30) jal fact lw $5, 8($30) lw $31, 0($30) addi $30, $30, 12 jr $31
CS 230 - Spring 2020 2-33
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.
addi $30, $30, -12 sw $31, 0($30) sw $2, 4($30) addi $4, $1, 0 jal fact sw $2, 8($30) lw $4, 4($30) jal fact lw $5, 8($30) add $2, $2, $5 lw $31, 0($30) addi $30, $30, 12 jr $31
CS 230 - Spring 2020 2-34
Example
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called example that takes 3 parameters x, y, and z and calculates fact(x+y)-z2. Follow the register conventions.
CS 230 - Spring 2020 2-35
Example
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called example that takes 3 parameters x, y, and z and calculates fact(x+y)-z2. Follow the register conventions.
example: jr $31
CS 230 - Spring 2020 2-36
Example
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called example that takes 3 parameters x, y, and z and calculates fact(x+y)-z2. Follow the register conventions.
example: addi $30, $30, -8 sw $31, 0($30) jal fact lw $31, 0($30) addi $30, $30, 8 jr $31
CS 230 - Spring 2020 2-37
Example
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called example that takes 3 parameters x, y, and z and calculates fact(x+y)-z2. Follow the register conventions.
example: addi $30, $30, -8 sw $31, 0($30) add $4, $4, $5 jal fact lw $31, 0($30) addi $30, $30, 8 jr $31
CS 230 - Spring 2020 2-38
Example
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called example that takes 3 parameters x, y, and z and calculates fact(x+y)-z2. Follow the register conventions.
example: addi $30, $30, -8 sw $31, 0($30) sw $6, 4($30) add $4, $4, $5 jal fact lw $6, 4($30) lw $31, 0($30) addi $30, $30, 8 jr $31
CS 230 - Spring 2020 2-39
Example
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called example that takes 3 parameters x, y, and z and calculates fact(x+y)-z2. Follow the register conventions.
example: addi $30, $30, -8 sw $31, 0($30) sw $6, 4($30) add $4, $4, $5 jal fact lw $6, 4($30) mult $6, $6 mflo $8 lw $31, 0($30) addi $30, $30, 8 jr $31
CS 230 - Spring 2020 2-40
Example
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called example that takes 3 parameters x, y, and z and calculates fact(x+y)-z2. Follow the register conventions.
example: addi $30, $30, -8 sw $31, 0($30) sw $6, 4($30) add $4, $4, $5 jal fact lw $6, 4($30) mult $6, $6 mflo $8 sub $2, $2, $8 lw $31, 0($30) addi $30, $30, 8 jr $31
CS 230 - Spring 2020 2-41
Try it Yourself
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called tryit that takes 1 parameter x and calculates fact(fact(x)). Follow the register conventions. Assume the result fits in 32-bits.
CS 230 - Spring 2020 2-42
Try it Yourself
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called tryit that takes 1 parameter x and calculates fact(fact(x)). Follow the register conventions. Assume the result fits in 32-bits.
tryit: jr $31
CS 230 - Spring 2020 2-43
Try it Yourself
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called tryit that takes 1 parameter x and calculates fact(fact(x)). Follow the register conventions. Assume the result fits in 32-bits.
tryit: addi $30, $30, -4 sw $31, 0($30) lw $31, 0($30) addi $30, $30, 4 jr $31
CS 230 - Spring 2020 2-44
Try it Yourself
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called tryit that takes 1 parameter x and calculates fact(fact(x)). Follow the register conventions. Assume the result fits in 32-bits.
tryit: addi $30, $30, -4 sw $31, 0($30) jal fact jal fact lw $31, 0($30) addi $30, $30, 4 jr $31
CS 230 - Spring 2020 2-45
Try it Yourself
Assume there exists a subroutine fact which takes one parameter x returns x! (x factorial). Write a MIPS subroutine called tryit that takes 1 parameter x and calculates fact(fact(x)). Follow the register conventions. Assume the result fits in 32-bits.
tryit: addi $30, $30, -4 sw $31, 0($30) jal fact addi $4, $2, 0 jal fact lw $31, 0($30) addi $30, $30, 4 jr $31
CS 230 - Spring 2020 2-46