6
Lecture 07-09 MIPS Programs and Procedures Suggested reading: (HP Chapter 2.8) (for extra examples, see Chapters 2.12 and 2.13) 1 2 Processor components vs. Processor comparison HLL code translation The right HW for the right application Writing more efficient code Multicore processors and programming CSE 30321 Fundamental lesson(s) Over the next 3 lectures (using the MIPS ISA as context) I’ll explain: How functions are treated and processed in assembly How system calls are enabled in assembly How exceptions are handled in assembly I’ll also explain why it’s important that register conventions be followed 3 Why it’s important… If you every write a compiler or OS some day, you will need to be aware of, and code for all of the issues to be discussed over the next 3 lectures If you understand what architectural overhead may be associated with (compiled) procedure calls, you should be able to write much more efficient HLL code 4

Lecture 07-09 MIPS Programs and Proceduresmniemier/teaching/2011_B_Fall/lectures/07_PPT... · Lecture 07-09 MIPS Programs and Procedures" ... $s0-$s7" 16-23"Saved" yes" ... – Stack

Embed Size (px)

Citation preview

Page 1: Lecture 07-09 MIPS Programs and Proceduresmniemier/teaching/2011_B_Fall/lectures/07_PPT... · Lecture 07-09 MIPS Programs and Procedures" ... $s0-$s7" 16-23"Saved" yes" ... – Stack

Lecture 07-09 MIPS Programs and Procedures"

Suggested reading:"(HP Chapter 2.8)"

(for extra examples, see Chapters 2.12 and 2.13)"

1" 2"

Processor components"

vs."

Processor comparison"

HLL code translation"The right HW for the right application"

Writing more "efficient code"

Multicore processors and programming"

CSE 30321"

Fundamental lesson(s)"•  Over the next 3 lectures (using the MIPS ISA as context)

I’ll explain:"–  How functions are treated and processed in assembly"–  How system calls are enabled in assembly"–  How exceptions are handled in assembly"

•  I’ll also explain why it’s important that register conventions be followed"

3"

Why it’s important…"•  If you every write a compiler or OS some day, you will

need to be aware of, and code for all of the issues to be discussed over the next 3 lectures"

•  If you understand what architectural overhead may be associated with (compiled) procedure calls, you should be able to write much more efficient HLL code"

4"

Page 2: Lecture 07-09 MIPS Programs and Proceduresmniemier/teaching/2011_B_Fall/lectures/07_PPT... · Lecture 07-09 MIPS Programs and Procedures" ... $s0-$s7" 16-23"Saved" yes" ... – Stack

5"

Practical Procedures"Have already seen that you don’t necessarily make N copies of

for loop body"

for (i=0; i<N; i++) {!!a = b + c;!!d = a + e;!!f = d + i;!

}!

Thus:" Might look like this:"# N = $2, i = $3""subi $2, $2, 1"add $4, $5, $6"add $7, $4, $8"add $9, $7, $10"addi $3, $3, 1"sub $11, $2, $3"bneq $11, $0, loop"

""# N = N -1"# a = b + c"# d = a + e"# f = d + i"# i = i + 1"# $11 = $3 - $2"# if $11 != 0, loop"

You wouldn’t make multiple copies of a machine instruction function either..."

loop:"

6"

Practical Procedures"

int main(void) {!int i;!int j;!!j = power(i, 7);!}!!int power(int i, int n) {!int j, k;!for (j=0; j<n; j++)!

k = i*i;!return k;!}!

For example:" Might look like this:"i = $6""addi $ 5, $0, 7"j power""...."""

""# arg reg. = 7""

add $3, $0, $0"subi $5, $5, 1"mult $6, $6, $6"addi $3, $3, 1"sub $11, $5, $3"bneq $11, $0, loop"add $2, $6, $0"j call"

power:"

call:"

""# data in ret. reg.""

Advantage: Much greater code density."(especially valuable for library routines, etc.)"

""# i in an arg reg.""

loop:"

7"

Procedure calls are so common that there’s significant architectural support."

8"

!  The big picture:" Caller Callee"

! Need “jump” and “return”: ""  jal ProcAddr # issued in the caller"

•  jumps to ProcAddr "•  save the return instruction address in $31"•  PC = JumpAddr, RF[31]=PC+4;"

"  jr $31 ($ra) # last instruction in the callee"•  jump back to the caller procedure"•  PC = RF[31]"

PC

PC+4

r0 r1

r31 b0 bn-1 ...

...

0

PC

HI

LO

������������������� ��jal

jr

MIPS Procedure Handling"

Page 3: Lecture 07-09 MIPS Programs and Proceduresmniemier/teaching/2011_B_Fall/lectures/07_PPT... · Lecture 07-09 MIPS Programs and Procedures" ... $s0-$s7" 16-23"Saved" yes" ... – Stack

9"

MIPS Registers"

Name" R#" Usage" Preserved on Call"$zero" 0"The constant value 0" n.a."

$v0-$v1" 2-3"Values for results & expr. eval." no"$a0-$a3" 4-7"Arguments" no"$t0-$t7" 8-15"Temporaries" no"$s0-$s7" 16-23"Saved" yes"$t8-$t9" 24-25"More temporaries" no"

$gp" 28"Global pointer" yes"$sp" 29"Stack pointer" yes"$fp" 30"Frame pointer" yes"$ra" 31"Return address" yes"

$at" 1"Reserved for assembler" n.a."

$k0-$k1" 26-27"Reserved for use by OS" n.a."

(and the “conventions” associated with them)"

10"

! What about passing parameters and return values?""  registers $4 - $7 ($a0-$a3) are used to pass first 4

parameters""  returned values are in $2 and $3 ($v0-$v1)"

!  32x32-bit GPRs (General purpose registers)""  $0 = $zero "" $2 - $3 = $v0 - $v1 (return values)""  $4 - $7 = $a0 - $a3 (arguments)"

"  $8 - $15 = $t0 - $t7 (temporaries)""  $16 - $23 = $s0 - $s7 (saved)""  $24 - $25 = $t8 - $t9 (more temporaries)""  $31 = $ra (return address)"

r0 r1

r31 b0 bn-1 ...

...

32 bits

0

PC

MIPS Procedure Handling (cont.)"

Take away: HW support for SW tasks."

Swap Procedure Example"

11"

A"

12"

!  More complex procedure calls"" What if your have more than 4 arguments?"" What if your procedure requires more registers than available?"" What about nested procedure calls?"" What happens to $ra if proc1 calls proc 2 which calls proc3,…"

What if ... ?"

Page 4: Lecture 07-09 MIPS Programs and Proceduresmniemier/teaching/2011_B_Fall/lectures/07_PPT... · Lecture 07-09 MIPS Programs and Procedures" ... $s0-$s7" 16-23"Saved" yes" ... – Stack

13"

!  Register contents across procedure calls are designated as either caller or callee saved"

!  MIPS register conventions: (although could make caller/callee do all)"" $t*, $v*, $a*: not preserved across call "

•  caller saves them if required"" $s*, $ra: preserved across call "

•  callee saves them if required"

More complex cases"

14"

Recall…"

Name" R#" Usage" Preserved on Call"$zero" 0"The constant value 0" n.a."

$v0-$v1" 2-3"Values for results & expr. eval." no"$a0-$a3" 4-7"Arguments" no"$t0-$t7" 8-15"Temporaries" no"$s0-$s7" 16-23"Saved" yes"$t8-$t9" 24-25"More temporaries" no"

$gp" 28"Global pointer" yes"$sp" 29"Stack pointer" yes"$fp" 30"Frame pointer" yes"$ra" 31"Return address" yes"

$at" 1"Reserved for assembler" n.a."

$k0-$k1" 26-27"Reserved for use by OS" n.a."

(MIPS registers and associated “conventions)"

15"

! Stack"" A dedicated area of memory"" First-In-Last-Out (FILO)"" Used to"

# Hold values passed to a procedure as arguments"# Save register contents when needed"# Provide space for variables local to a procedure"

! Stack operations"" push: place data on stack (sw in MIPS)"" pop: remove data from stack (lw in MIPS)"

! Stack pointer"" Stores the address of the top of the stack"" $29 ($sp) in MIPS "

Where is all this stuff saved to?"

16"

Memory Structure

Data segment

Instruction segment

Reserved

PC

SP Higher Mem Addr

Stack segment

Lower Mem Addr

. . .

Addr

i-2 i-1 i

i+1 i+2 $sp = i

Top of stack

Where is the stack located?"

Page 5: Lecture 07-09 MIPS Programs and Proceduresmniemier/teaching/2011_B_Fall/lectures/07_PPT... · Lecture 07-09 MIPS Programs and Procedures" ... $s0-$s7" 16-23"Saved" yes" ... – Stack

17"

!  Each procedure is associated with a call frame"!  Each frame has a frame pointer: $fp ($30)"

Argument 5 is in 0($fp)

$sp

$fp

Snap shots of stack

main

proc1

proc2

proc3

main { … proc1 …} proc1 { … proc2 …} proc2 { … proc3 …}

Local variables

Saved Registes

($fp) ($ra)

Argument 6 Argument 5

Call frames"

Because $sp can change dynamically, often easier/intuitive to reference extra arguments via stable $fp – although can use $sp with a little extra math"

18"

Procedure call essentials: Good Strategy"

•  Caller at call time"–  put arguments in $a0..$a4"–  save any caller-save temporaries"–  jal ..., $ra"

•  Callee at entry"–  allocate all stack space"–  save $ra, $fp + $s0..$s7 if necessary"

•  Callee at exit"–  restore $ra, $fp + $s0..$s7 if used"–  deallocate all stack space"–  put return value in $v0"

•  Caller after return"–  retrieve return value from $v0"–  restore any caller-save temporaries"

most of the work"

do most work at callee entry/exit"

19"

Procedure call essentials"•  Summary"

–  Caller saves registers "•  (outside the agreed upon convention i.e. $ax) at point of call"

–  Callee saves registers "•  (per convention i.e. $sx) at point of entry"

–  Callee restores saved registers, and re-adjusts stack before return"

–  Caller restores saved registers, and re-adjusts stack before resuming from the call"

20"

Why so strict?"

Page 6: Lecture 07-09 MIPS Programs and Proceduresmniemier/teaching/2011_B_Fall/lectures/07_PPT... · Lecture 07-09 MIPS Programs and Procedures" ... $s0-$s7" 16-23"Saved" yes" ... – Stack

21"

Examples:"•  Previous Exam Questions"•  MIPS leaf procedure"•  Nested function calls"

–  Stack pointers and frame pointers"•  Capstone example:"

–  Recursive Factorial!"

"

int fact(int n)!{! if (n < 1)! return (1);! else! return (n * fact(n-1));!}!

B-F"