procedures.pdf

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

• 30

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

From Patterson’s 61C 1 COMP-273 COMP-273 Procedures Kaleem Siddiqi

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

From Patterson’s 61C 2 COMP-273 Overview ° C Functions ° MIPS Instructions for Procedures ° The Stack ° Register Conventions ° Another Example
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?

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

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.
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

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

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.
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

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

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
From Patterson’s 61C 9 COMP-273 Nested Procedures (1/2) int sumSquare(int x, int y) { return mult (x,x)+ y; } ° Something called

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

This is the end of the preview. Sign up to access the rest of the document.
• Spring '09
• Vybihal

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern