CSCI206 - Computer Organization & Programming

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/6: Lecture Topics Procedure call Calling conventions The stack
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
Intro to Computer Architecture
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CHAPTER 2 ISA Instructions (logical + procedure call)
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
MIPS Calling Convention. Procedure Calls Procedure must work the same from any call Procedure uses regs that main was using We need a convention to –pass.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
CSCI206 - Computer Organization & Programming
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
MIPS Assembly Language Programming
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
MIPS Instructions.
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Procedures.
Program and memory layout
Procedures and Calling Conventions
Program and memory layout
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
MIPS Functions.
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

CSCI206 - Computer Organization & Programming Introduction to Procedures zyBook: 8.1 (for next class)

Procedures, the basic idea

$a0-$a3 jr $ra $v0-$v1

MIPS Procedure convention Prepare parameters in $a0 through $a3 Return values on $v0, $v1 Call the procedure by jal (jump and link)

Example: compute multiplication mult_begin: # goal: multiply $a0 by $a1 li $v0, 0 # the result will be in $v0 mult_loop: beq $a1, $zero mult_done # check if done ($a1 == 0) add $v0, $v0, $a0 # add $a0 to result one time addi $a1, $a1, -1 # decrement $a1 j mult_loop # repeat mult_done: # the result is in $v0

Re-write it as a MIPS procedure mult_proc: # goal: multiply $a0 by $a1 li $v0, 0 # the result will be in $v0 mult_loop: beq $a1, $zero mult_done # check if done ($a1 == 0) add $v0, $v0, $a0 # add $a0 to result one time addi $a1, $a1, -1 # decrement $a1 j mult_loop # repeat mult_done: # the result is in $v0 Almost identical!!! The difference is how it is used. MIPS procedure call: jal mult_proc

multiply in asm activity Show how you would call the function to compute 2 * 7 and store the result in $s0. Then compute 5 * 9 and store the result in $s1.

$a0-$a3 jr $ra $v0-$v1

Multiply in asm w/ procedure

Terminology $a0-$a3 CALLER CALLEE $v0-$v1 jr $ra

jal instruction details jump and link (J-type) $ra = PC + 8 (from green sheet) PC = JumpAddr procedure: . jr $ra procedure begins after add instruction. jal procedure ← PC add … ← PC + 4 [delay slot instruction ] lw … ← PC + 8 procedure returns before lw instruction.

Assumptions After calling a function return to the “next line” side-effects follow conventions

Question Which registers should CALLER use? for doing work CALLEE Which registers should CALLER use? for doing work for passing arguments to CALLEE for reading the result from CALLEE

Question Which registers should CALLEE use? for doing work CALLER CALLEE Which registers should CALLEE use? for doing work for reading arguments from CALLER for passing the result to CALLER

Problems More arguments than $a registers (4)? Return more results than $v registers (2) Need more registers than $s (caller) or $t (callee) to do work. Callee needs to call another function. jal would overwrite the first $ra! what registers to use (callee vs caller?)

Dynamic storage The solution to all of these problems requires some form of dynamic storage. As the name implies, the stack memory segment is managed as a stack data structure and provides dynamic run-time storage.

Problems Solutions Have more arguments than $a registers (4)? Return more results than $v registers (2) Need more registers than $s (caller) or $t (callee) to do work. Callee needs to call another function. jal would overwrite the first $ra! what registers to use (callee vs caller?) Push extra arguments onto the stack Push extra results onto the stack Use the stack to store/restore register values Push registers (including $ra) onto the stack, callee becomes caller!

Program Memory Map main program initializes $sp to allocate one word points to the last used word! li $sp, 0x80000000 to allocate one word decrement $sp by 4 $sp moves DOWN to deallocate a word increment $sp by 4 $sp moves UP

Stack operations PUSH $x: push reg[x] onto the stack addi $sp, $sp, -4 # allocate one word sw $x, 0($sp) # store reg $x onto the stack POP $x: pop reg[x] from the stack lw $x, 0($sp) # read the top value of stack addi $sp, $sp, 4 # deallocate word from stack

Illustrating a nonleaf procedure int main(){ printf(“result: %d”, nonleaf(1,1,2,3) ); } int nonleaf(int a, int b, int c, int d){ return sum( sum(a,b), sum(c,d)); int sum(int a, int b){ return a+b; nonleaf needs a stack: allocate stack space store $ra [return from nonleaf] call sum 3 times, storing partial results restore $ra deallocate stack

nonleaf assembly main: li $a0, 1 li $a1, 1 li $a2, 2 li $a3, 3 jal nonleaf … <other code> … nonleaf: push $ra # save my $ra push $a3 # save a3 push $a2 # save a2 jal sum # a0/a1 are summed pop $a0 # restore a2 to a0 pop $a1 # restore a3 to a1 push $v0 # store result of sum(a0, a1) jal sum # a0/a1 are used to sum a2/a3 pop $a0 # restore sum(a0, a1) into a0 move $a1, $v0 # move second result to a1 jal sum # compute final sum into $v0 pop $ra # restore $ra jr $ra # return nonleaf assembly int main(){ printf(“result: %d”, nonleaf(1,1,2,3) ); } int nonleaf(int a, int b, int c, int d){ return sum( sum(a,b), sum(b,c)); int sum(int a, int b){ return a+b;

nonleaf uses 4 words of stack space... main: li $a0, 1 li $a1, 1 li $a2, 2 li $a3, 3 jal nonleaf … <other code> … nonleaf: addi $sp, $sp, -16 # allocate space sw $ra, 12($sp) sw $a2, 8($sp) # store a2 sw $a3, 4($sp) # store a3 jal sum # sum (a0,a1) sw $v0, 0($sp) # store sum(a0,a1)! lw $a0, 8($sp) # restore a2 to a0 lw $a1, 4($sp) # restore a3 to a1 jal sum # sum (b, c) lw $a0, 0($sp) # restore sum(a0,a1) move $a1, $v0 # move 2nd to a1 jal sum # final sum into $v0 lw $ra, 12($sp) # restore $ra addi $sp, $sp, 16 # deallocate space jr $ra # return main: li $a0, 1 li $a1, 1 li $a2, 2 li $a3, 3 jal nonleaf … <other code> … nonleaf: push $ra # save my $ra push $a3 # save a3 push $a2 # save a2 jal sum # a0/a1 are summed pop $a0 # restore a2 to a0 pop $a1 # restore a3 to a1 push sum # store result of a0/a1 jal sum # a0/a1 are used to sum a2/a3 pop $a0 # restore first result move $a1, $v0 # move second result to a1 jal sum # compute final sum into $v0 pop $ar # restore $ra jr $ra # return nonleaf uses 4 words of stack space...

nonleaf stack memory map main: li $a0, 1 li $a1, 1 li $a2, 2 li $a3, 3 jal nonleaf … <other code> … nonleaf: addi $sp, $sp, -16 # allocate space sw $ra, 12($sp) sw $a2, 8($sp) # store a2 sw $a3, 4($sp) # store a3 jal sum # a0/a1 are summed sw $v0, 0($sp) # store $v0 to stack! lw $a0, 8($sp) # restore a2 to a0 lw $a1, 4($sp) # restore a3 to a1 jal sum # a0/a1 are used to sum a2/a3 lw $a0, 0($sp) # restore first result move $a1, $v0 # move second result to a1 jal sum # compute final sum into $v0 lw $ra, 12($sp) # restore $ra addi $sp, $sp, 16 # deallocate space jr $ra # return nonleaf stack memory map $fp = $sp+16 BOTTOM of stack $sp+12 nonleaf’s $ra $sp+8 a2 (c) $sp+4 a3 (d) $sp+0 sum(a,b)

General Stack Allocation callee caller return to caller stack frame or activation record: everything stored on the stack for a particular function. $sp points to the end of the record, $fp points to the start of the record (initial $sp before the function changes it)! Access local variables relative to $sp or $fp, it’s up to you!

Procedure Call Outline prepare arguments for procedure jal to procedure allocate all stack space for local variables and to store any preserved registers that will be changed. do some work. restore stack and preserved registers for caller. place result where the caller expects. jr $ra. process result

Square in asm using mult

power with mult