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

Info icon This 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
Image of page 1

Info icon This 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
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?
Image of page 3

Info icon This 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.
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
Image of page 5

Info icon This 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.
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
Image of page 7

Info icon This 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
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
Image of page 9

Info icon This 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 ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern