Program and memory layout

Slides:



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

ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
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
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:
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.
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
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Procedure Conventions & The Stack Jen-Chang Liu, Spring 2006 Adapted from
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
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.
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 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.
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
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
Help! How do procedure calls work?
MIPS Assembly Language Programming
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
10/4: Lecture Topics Overflow and underflow Logical operations
CSE 410, Spring 2008 Computer Systems
Program and memory layout
Procedures and Calling Conventions
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
MIPS R3000 Subroutine Calls and Stack
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 5/17/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 5/17/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 5/17/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 5/17/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 5/17/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 $6,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” 453 $6 127 $6 before after 5/17/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 5/17/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 5/17/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 5/17/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 5/17/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 5/17/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 5/17/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 5/17/2019 CSE378 Procedures.