MIPS Procedures and Functions in CS61C Course - Prof. D. D. Garcia, Study notes of Computer Science

This document from the cs61c course at uc berkeley covers mips procedures, functions, decision making instructions, and memory allocation, with examples and exercises. It includes information about jal, jr, and register conventions.

Typology: Study notes

Pre 2010

Uploaded on 10/01/2009

koofers-user-72b-1
koofers-user-72b-1 🇺🇸

10 documents

1 / 38

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS61C L8 MIPS Procedures (1) Garcia, Fall 2005 © UCB
Lecturer PSOE, new dad Dan Garcia
www.cs.berkeley.edu/~ddgarcia
inst.eecs.berkeley.edu/~cs61c
CS61C : Machine Structures
Lecture #8 – MIPS Procedures
2005-09-26 There is one handout
today at the front and
back of the room!
No. 11 Cal runs over the
Aggies 41-13 are are now 4-0 for 1st
time since 1996. Justin Forsett ran
for 235 yds, Joe Ayoob rushed for 3
TDs. Next Sat…Arizona @ home!
New Mexico St who?
calbears.collegesports.com/sports/m-footbl/recaps/092405aaa.html
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download MIPS Procedures and Functions in CS61C Course - Prof. D. D. Garcia and more Study notes Computer Science in PDF only on Docsity!

Lecturer PSOE, new dad Dan Garcia www.cs.berkeley.edu/~ddgarcia inst.eecs.berkeley.edu/~cs61c

CS61C : Machine Structures

Lecture #8 – MIPS Procedures

There is one handout today at the front and back of the room! No. 11 Cal runs over the Aggies 41-13 are are now 4-0 for 1 st time since 1996. Justin Forsett ran for 235 yds, Joe Ayoob rushed for 3 TDs. Next Sat…Arizona @ home!

New Mexico St who? ⇒

calbears.collegesports.com/sports/m-footbl/recaps/092405aaa.html

Review

  • A Decision allows us to decide what to execute at run-time rather than compile-time.
  • C Decisions are made using conditional statements within if , while , do while , for.
  • MIPS Decision making instructions are the conditional branches: beq and bne.
  • In order to help the conditional branches make decisions concerning inequalities, we introduce a single instruction: “Set on Less Than”called slt , slti , sltu , sltiu
  • Unsigned add/sub don t cause overflow
  • New MIPS Instructions: beq, bne, j, sll, srl slt, slti, sltu, sltiu addu, addiu, subu

Function Call Bookkeeping

  • Registers play a major role in

keeping track of information for

function calls.

  • Register conventions:
    • Return address $ra
    • Arguments $a0, $a1, $a2, $a
    • Return value $v0, $v
    • Local variables $s0, $s1, … , $s
  • The stack is also used; more later.

Instruction Support for Functions (1/6)

... sum(a,b);... /* a,b:$s0,$s1 */

int sum(int x, int y) {

return x+y;

address

C

M

I

P

S

In MIPS, all instructions

are 4 bytes, and stored in

memory just like data. So

here we show the

addresses of where the

programs are stored.

Instruction Support for Functions (3/6)

... sum(a,b);... /* a,b:$s0,$s1 */

int sum(int x, int y) {

return x+y;

2000 sum: add $v0,$a0,$a

2004 jr $ra # new instruction

C

M

I

P

S

  • Question: Why use jr here? Why not simply use j?
  • Answer: sum might be called by many functions, so we can t return to a fixed place. The calling proc to sum must be able to say “return here” somehow.

Instruction Support for Functions (4/6)

  • Single instruction to jump and save return address: jump and link ( jal )
  • Before: 1008 addi $ra,$zero,1016 #$ra= 1012 j sum #goto sum
  • After: 1008 jal sum # $ra=1012,goto sum
  • Why have a jal? Make the common case fast: function calls are very common. Also, you don t have to know where the code is loaded into memory with jal.

Instruction Support for Functions (6/6)

  • Syntax for jr (jump register): jr register
  • Instead of providing a label to jump to,

the jr instruction provides a register

which contains an address to jump to.

  • Only useful if we know exact address to

jump to.

  • Very useful for function calls:
    • jal stores return address in register ($ra )
    • jr $ra jumps back to that address

Nested Procedures (1/2)

int sumSquare(int x, int y) {

return mult(x,x)+ y;

  • Something called sumSquare , now

sumSquare is calling mult.

  • So there s a value in $ra that

sumSquare wants to jump back to, but

this will be overwritten by the call to

mult.

  • Need to save sumSquare return

address before call to mult.

C memory Allocation review

Address

Code Program

Static

Variables declared

once per program

Heap

Explicitly created space,

e.g., malloc(); C pointers

Stack

Space for saved

procedure information

$sp stack pointer

Using the Stack (1/2)

  • So we have a register $sp which

always points to the last used space

in the stack.

  • To use stack, we decrement this

pointer by the amount of space we

need and then fill it with info.

  • So, how do we compile this? int sumSquare(int x, int y) { return mult(x,x)+ y; }

Steps for Making a Procedure Call

1) Save necessary values onto stack.

2) Assign argument(s), if any.

3) jal call

4) Restore values from stack.

Rules for Procedures

  • Called with a jal instruction, returns

with a jr $ra

  • Accepts up to 4 arguments in $a0 ,

$a1 , $a2 and $a

  • Return value is always in $v0 (and if

necessary in $v1 )

  • Must follow register conventions

(even in functions that only you will

call)! So what are they?

  • We ll see these in a few slides…

Administrivia

  • We have a midterm&review time & date
    • Review: Sun 2005-10-16 @ 2pm in 10 Evans
    • Midterm: Mon 2005-10-17, 5:30-8:30pm here!
    • DSP or Conflicts? Email Jeremy
  • Dan s before-class graphics videos: www.siggraph.org/publications/video-review/SVR.html
  • Project 1 due tonight @ 11:59pm
    • An easy HW3 follows, due Friday

What C code properly fills in the blank in loop below?

Peer Instruction

do {i--;} while(__); Loop:addi $s0,$s0,-1 # i = i - 1 slti $t0,$s1,2 # $t0 = (j < 2) beq $t0,$0 ,Loop # goto Loop if $t0 == 0 slt $t0,$s1,$s0 # $t0 = (j < i) bne $t0,$0 ,Loop # goto Loop if $t0 != 0 1: j < 2 && j < i 2: j ≥ 2 && j < i 3: j < 2 && j ≥ i 4: j ≥ 2 && j ≥ i 5: j > 2 && j < i 6: j < 2 || j < i 7: j ≥ 2 || j < i 8: j < 2 || j ≥ i 9: j ≥ 2 || j ≥ i 0: j > 2 || j < i

($s0=i , $s1=j)