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

computer systems
SMART_READER_LITE
LIVE PREVIEW

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()


slide-1
SLIDE 1

CS 230 - Spring 2020 2-1

CS 230 – Introduction to Computers and Computer Systems Lecture 12 – Subroutines

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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)

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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)

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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)

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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”

slide-13
SLIDE 13

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

slide-14
SLIDE 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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

slide-26
SLIDE 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

slide-27
SLIDE 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) lw $31, 0($30) addi $30, $30, 12 jr $31

CS 230 - Spring 2020 2-27

slide-28
SLIDE 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) jal fact jal fact lw $31, 0($30) addi $30, $30, 12 jr $31

CS 230 - Spring 2020 2-28

slide-29
SLIDE 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) addi $4, $1, 0 jal fact jal fact lw $31, 0($30) addi $30, $30, 12 jr $31

CS 230 - Spring 2020 2-29

slide-30
SLIDE 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 jal fact lw $31, 0($30) addi $30, $30, 12 jr $31

CS 230 - Spring 2020 2-30

slide-31
SLIDE 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) jal fact lw $31, 0($30) addi $30, $30, 12 jr $31

CS 230 - Spring 2020 2-31

slide-32
SLIDE 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 $31, 0($30) addi $30, $30, 12 jr $31

CS 230 - Spring 2020 2-32

slide-33
SLIDE 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) lw $31, 0($30) addi $30, $30, 12 jr $31

CS 230 - Spring 2020 2-33

slide-34
SLIDE 34

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

slide-35
SLIDE 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.

CS 230 - Spring 2020 2-35

slide-36
SLIDE 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: jr $31

CS 230 - Spring 2020 2-36

slide-37
SLIDE 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) jal fact lw $31, 0($30) addi $30, $30, 8 jr $31

CS 230 - Spring 2020 2-37

slide-38
SLIDE 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) add $4, $4, $5 jal fact lw $31, 0($30) addi $30, $30, 8 jr $31

CS 230 - Spring 2020 2-38

slide-39
SLIDE 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) lw $31, 0($30) addi $30, $30, 8 jr $31

CS 230 - Spring 2020 2-39

slide-40
SLIDE 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 lw $31, 0($30) addi $30, $30, 8 jr $31

CS 230 - Spring 2020 2-40

slide-41
SLIDE 41

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

slide-42
SLIDE 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.

CS 230 - Spring 2020 2-42

slide-43
SLIDE 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: jr $31

CS 230 - Spring 2020 2-43

slide-44
SLIDE 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) lw $31, 0($30) addi $30, $30, 4 jr $31

CS 230 - Spring 2020 2-44

slide-45
SLIDE 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 jal fact lw $31, 0($30) addi $30, $30, 4 jr $31

CS 230 - Spring 2020 2-45

slide-46
SLIDE 46

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