{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

procedures.pdf - COMP-273 Procedures Kaleem Siddiqi...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
From Patterson’s 61C 1 COMP-273 COMP-273 Procedures Kaleem Siddiqi
Background image of page 1

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

View Full Document Right Arrow Icon
From Patterson’s 61C 2 COMP-273 Overview ° C Functions ° MIPS Instructions for Procedures ° The Stack ° Register Conventions ° Another Example
Background image of page 2
From Patterson’s 61C 3 COMP-273 C functions main() { int i,j,k,m; i = mult(j,k); ... ; m = mult(i,i); ... } /* really dumb mult function */ int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product = product + mcand; mlier = mlier -1; } return product; } What information must compiler/programmer keep track of? What instructions can accomplish this?
Background image of page 3

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

View Full Document Right Arrow Icon
From Patterson’s 61C 4 COMP-273 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, $a3 Return value $v0, $v1 Local variables $s0, $s1, … , $s7 ° The stack is also used. ° More on this later.
Background image of page 4
From Patterson’s 61C 5 COMP-273 Instruction Support for Functions (1/4) ... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { return x+y; } address 1000 add $a0,$s0,$zero # x = a 1004 add $a1,$s1,$zero # y = b 1008 addi $ra,$zero,1016 #$ra=1016 1012 j sum #jump to sum 1016 ... 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction C M I P S
Background image of page 5

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

View Full Document Right Arrow Icon
From Patterson’s 61C 6 COMP-273 Instruction Support for Functions (2/4) ° Single instruction to jump and save return address: jump and link ( jal ) ° Before: 1008 addi $ra,$zero,1016 #$ra=1016 1012 j sum #go to sum ° After: 1012 jal sum # $ra=1016,go to sum ° Why have a jal ? Make the common case fast: functions are very common.
Background image of page 6
From Patterson’s 61C 7 COMP-273 Instruction Support for Functions (3/4) ° Syntax for jal (jump and link) is same as for j (jump): jal label ° jal should really be called laj for “link and jump”: Step 1 (link): Save address of next instruction into $ra (Why next instruction? Why not current one?) Step 2 (jump): Jump to the given label
Background image of page 7

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

View Full Document Right Arrow Icon
From Patterson’s 61C 8 COMP-273 Instruction Support for Functions (4/4) ° 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: rarely applicable. ° Very useful for function calls: jal stores return address in register ($ra) jr jumps back to that address
Background image of page 8
From Patterson’s 61C 9 COMP-273 Nested Procedures (1/2) int sumSquare(int x, int y) { return mult (x,x)+ y; } ° Something called
Background image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}