L06-StacksProcedures

# L06-StacksProcedures - Stacks and Procedures I forgot, am I...

This preview shows pages 1–7. Sign up to view the full content.

L06 – Stacks and Procedures 1 Comp 411 – Fall 2009 9/21/09 Stacks and Procedures I forgot, am I the Caller or Callee? Support for High-Level Language constructs are an integral part of modern computer organization. In particular, support for subroutines, procedures, and functions. Don’t know. But, if you PUSH again I’m gonna POP you.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
L06 – Stacks and Procedures 2 Comp 411 – Fall 2009 9/21/09 The Beauty of Procedures Reusable code fragments (modular design) clear_screen(); # code to draw a bunch of lines clear_screen(); Parameterized functions (variable behaviors) line(x1, y1, x2, y2, color); line(x2,y2,x3,y3, color); for (i=0; i < N-1; i++) line(x[i],y[i],x[i+1],y[i+1],color) line(x[i],y[i],x[0],y[0],color);
L06 – Stacks and Procedures 3 Comp 411 – Fall 2009 9/21/09 More Procedure Power Local scope (Independence) int x = 9; int fee(int x) { return x+x-1; } int foo(int i) { int x = 0; while (i > 0) { x = x + fee(i); i = i - 1; } return x; } main() { fee(foo(x)); } These are different “x”s This is yet another “x” How do we keep track of all the variables That “fee()” seems odd to me? And, foo()’s a little square.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
L06 – Stacks and Procedures 4 Comp 411 – Fall 2009 9/21/09 Using Procedures A “calling” program ( Caller ) must: Provide procedure parameters. In other words, put the arguments in a place where the procedure can access them Transfer control to the procedure. Jump to it A “called” procedure ( Callee ) must: Acquire the resources needed to perform the function Perform the function Place results in a place where the Caller can find them Return control back to the Caller Solution (a least a partial one): Allocate registers for these specific functions
L06 – Stacks and Procedures 5 Comp 411 – Fall 2009 9/21/09 MIPS Register Usage Conventions designate registers for procedure arguments (\$4-\$7) and return values (\$2-\$3). The ISA designates a “linkage register” for calling procedures (\$31) Transfer control to Callee using the jal instruction Return to Caller with the jr \$31 or jr \$ra instruction Name Register number Usage \$zero 0 the constant value 0 \$at 1 assembler temporary \$t0-\$t7 8-15 temporaries \$s0-\$s7 16-23 saved by callee \$t8-\$t9 24-25 more temporaries \$k0-\$k1 26-27 reserved for operating system \$gp 28 global pointer \$sp 29 stack pointer \$fp 30 frame pointer The “linkage register” is where the return address of back to the callee is stored. This allows procedur es to be called from any place, and for the caller to come back to the place where it was invoked.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
L06 – Stacks and Procedures 6 Comp 411 – Fall 2009 9/21/09 And It “Sort Of” Works Example: .globl x .data x: .word 9 Callee Works for special cases where the Callee needs few resources and calls no other functions. This type of function is called a LEAF function. But there are lots of issues: How can fee call functions?
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/31/2009 for the course COMPUTER computer 1 taught by Professor Abedauthman during the Spring '08 term at Aarhus Universitet.

### Page1 / 29

L06-StacksProcedures - Stacks and Procedures I forgot, am I...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online