Program and memory layout

Slides:



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

10/6: Lecture Topics Procedure call Calling conventions The stack
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
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
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12.
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.
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.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
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.
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
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.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
Help! How do procedure calls work?
MIPS Assembly Language Programming
Prof. Hsien-Hsin Sean Lee
Function Calls in MIPS To call a function: jal func
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Logical and Decision Operations
10/4: Lecture Topics Overflow and underflow Logical operations
CSE 410, Spring 2008 Computer Systems
Procedures and Calling Conventions
Computer Architecture Procedure Calls
Systems Architecture I
Program and memory layout
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
MIPS function continued
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Program and memory layout By convention the layout is: Note that only half of the addressing space is taken by user Other half is O.S. 7fff ffff Stack Dynamic data Static data 1000 0000 Program text 4 0000 Reserved 2/24/2019 CSE378 Procedures.

Procedures Procedures/functions are the major program structuring mechanism Calling and returning form a procedure requires a protocol between caller and callee Protocol is based on conventions 2/24/2019 CSE378 Procedures.

Procedures/Functions -- Protocol Each machine (compiler?) has its own set of protocol(s) Protocol: combination of hardware/software e.g., “jal” is hardware use of register $29 as $sp is software Protocol: sequence of steps to be followed at each call and each return controlled by hardware and/or software In RISC machines hardware performs simple instructions software (compiler/assembler) controls sequence of instructions 2/24/2019 CSE378 Procedures.

Program stack Each executing program (process) has a stack Stack = dynamic data structure accessed in a LIFO manner Program stack automatically allocated by O.S. At the start of the program, register $sp ($29 in MIPS) is automatically loaded to point to the first empty slot on top of stack After that it will be your responsibility to manage $sp By convention, stack grows towards lower addresses to allocate new space (i.e., when you push), decrement $sp to free space on top of stack (pop), increment $sp 2/24/2019 CSE378 Procedures.

Push operation push adds an item on top of stack one instruction to manipulate the data, e.g. “sw $6,0($sp)” one instruction to adjust the stack pointer e.g., “subu $sp,$sp,4” 46 -72 ??? 8($sp) 4($sp) $sp 46 -72 127 ??? 12($sp) 8($sp) 4($sp) $sp 127 $6 127 $6 before after 2/24/2019 CSE378 Procedures.

Pop operation pop removes the item on top of stack and stores it in a register one instruction to adjust the stack pointer e.g., “addu $sp,$sp,4” one instruction to manipulate the data, e.g. “lw $8,0($sp)” 46 -72 127 8($sp) 4($sp) $sp 46 -72 127 ??? 12($sp) 8($sp) 4($sp) $sp Now this has become “garbage” 127 $8 453 $8 before after 2/24/2019 CSE378 Procedures.

Procedure call requirements (caller/callee) Caller must pass the return address to the callee Caller must pass the parameters to the callee Caller must save what is in volatile (registers) that could be used by the callee Callee must save the return address (in case it becomes a caller) Callee must provide (stack) storage for its own use Caller/callee should support recursive calls 2/24/2019 CSE378 Procedures.

Mechanism Registers are used for Stack is used for passing return address in $ra jal target passing a small number of parameters (up to 4 in $a0 to $a3) keeping track of the stack ($sp) returning function values (in $v0 and $v1) Stack is used for saving registers to be used by callee saving info about the caller (return address) passing parameters if needed allocating local data for the called procedure 2/24/2019 CSE378 Procedures.

Procedure calls and register conventions Name Function Comment $0 Zero Always 0 No - op on write $1 $at Reserved for assembler Don’t use it $2 - 3 $v0 - v1 Expr. Eval/funct. Return $4 - 7 $a0 - a3 Proc./func. Call parameters $8 - 15 $t0 - t7 Temporaries; volatile Not saved on proc. Calls $16 - 2 3 $s0 - s7 Temporaries Should be saved on calls $24 - 25 $t8 - t9 Temporaries; volatile Not saved on proc. Calls $26 - 27 $k0 - k1 Reserved for O.S. Don’t use them $28 $gp Pointer to global static memory $29 $sp Stack pointer $30 $fp Frame pointer $31 $ra P roc./funct return address 2/24/2019 CSE378 Procedures.

Who does what on a call (one sample protocol) Caller Saves any volatile register ($t0-$t9) having contents that need to be kept Puts up to 4 arguments in $a0-$a3 If more than 4 arguments, pushes the rest on the stack calls with jal instruction Callee saves $ra on stack saves any non-volatile register ($s0-s7) that it will use 2/24/2019 CSE378 Procedures.

Who does what on return Callee Caller restores any non-volatile register ($s0-$s7) it has used restores $ra puts function results in $v0-$v1 adjusts $sp returns to caller with “jr $ra” Caller restores any volatile register it had saved examines $v0-$v1 if needed 2/24/2019 CSE378 Procedures.

Example of a call sequence Assume 2 arguments in $t0 and $t3 and we want to save the contents of $t6 and $t7 move $a0,$t0 #1st argument in $a0 move $a1,$t3 #2nd argument in $a1 subu $sp,$sp,8 #room for 2 temps on stack sw $t6,8($sp) #save $t6 on stack sw $t7,4($sp) #save $t7 on stack jal target Assume the callee does not need to save registers target: sw $ra,0($sp) #save return address subu $sp,$sp,4 # on stack 2/24/2019 CSE378 Procedures.

Return from the previous sequence The callee will have put the function results in $v0-$v1 addu $sp,$sp,4 #pop lw $ra,0($sp) #return address in $ra jr $ra #to caller The caller will restore $t6 and $t7 and adjust stack lw $t6,8($sp) lw $t7,4($sp) addu $sp,$sp,8 2/24/2019 CSE378 Procedures.

Factorial, The Logic Logic of call: Logic of Operation Put argument in register a0 li $a0,5 Call procedure jal fact Grab result from v0 sw $v0, … Logic of Operation Make a place on the stack Save the argument and return address Decrement the argument Check if recursion is done If not, recurse again If so, compute the result into $v0 Grab return address Restore stack Return $v0 = $a0*($a0-1)! 2/24/2019 CSE378 Procedures.

Factorial, The Code fact: subu $sp,$sp,8 #create 2 spaces on stack sw $ra, 8($sp) #push return address sw $a0,4($sp) #push argument addi $a0,$a0,-1 #decrement argument beqz $a0,base #is recursion done? jal fact #recurse j done #v0 contains a0! base: li $v0,1 #initialize v0 done: lw $a0,4($sp) #get argument back mult $v0,$a0 #figure next term mflo $v0 #grab result & ready for return lw $ra,8($sp) #restore the return address addiu $sp,$sp,8 #pop jr $ra #return $v0 = $a0*($a0-1)! 2/24/2019 CSE378 Procedures.