Computer Architecture CSCE 350

Slides:



Advertisements
Similar presentations
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Procedures I If cars broadcast their speeds to.
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
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
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
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
Lecture 8: MIPS Instruction Set
 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.
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
1 CS 430 Computer Architecture Clap if you like pizza! Pointless Poll.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 7: Procedures I Partially adapted from Computer Organization and Design, 4 th edition,
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
Lecture 6: Procedure Call (Part I)
CS61C L11 Introduction to MIPS : Procedures I (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Machine Structures Lecture 4 – Introduction to MIPS Procedures I
Procedures I Fall 2010 Lecture 6: Procedures. Procedures I Fall 2010 Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);...
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
CS61C L11 Introduction to MIPS : Procedures I (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L08 MIPS Procedures (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #8: MIPS Procedures
Procedure Conventions & The Stack Jen-Chang Liu, Spring 2006 Adapted from
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 11 – Introduction to MIPS Procedures I Internet2, a non-profit advanced.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
1 CS61C L6 Machine Rep CENG 311 Procedure Conventions & The Stack.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
IT 251 Computer Organization and Architecture Introduction to MIPS Procedures Chia-Chi Teng.
CS 61C L11 Introduction to MIPS: Procedures I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 – Introduction to MIPS Procedures I Apple took a bold step recently.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Architecture & Operations I
Rocky K. C. Chang Version 0.1, 25 September 2017
Lecture 5: Procedure Calls
Procedures 101: There and Back Again
CSCI206 - Computer Organization & Programming
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
MIPS Instructions.
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
CS61C : Machine Structures Lecture 2. 2
Program and memory layout
Procedures and addressing
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Computer Architecture CSCE 350 Rabi Mahapatra Spring 2015

Addressing: Byte vs. word Every word in memory has an address, similar to an index in an array Early computers numbered words like C numbers elements of an array: Memory[0], Memory[1], Memory[2], … Computers needed to access 8-bit bytes as well as words (4 bytes/word) Today machines address memory as bytes, hence word addresses differ by 4 Memory[0], Memory[4], Memory[8], Called the “address” of a word

Notes about Memory Pitfall: Forgetting that sequential word addresses in machines with byte addressing do not differ by 1. Many an assembly language programmer has toiled over errors made by assuming that the address of the next word can be found by incrementing the address in a register by 1 instead of by the word size in bytes. So remember that for both lw and sw, the sum of the base address and the offset must be a multiple of 4 (to be word aligned)

More Notes about Memory: Alignment MIPS requires that all words start at addresses that are multiples of 4 bytes 0 1 2 3 Aligned Not Bytes in Word Word Location Called Alignment: objects must fall on address that is multiple of their size.

Instruction Support for Functions ... 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

Instruction Support for Functions 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.

Instruction Support for Functions 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. Very useful for function calls: jal stores return address in register ($ra) jr jumps back to that address

Nested Procedures int sumSquare(int x, int y) { return mult(x,x)+ y; } Routine called sumSquare; now sumSquare is calling mult. So there’s a value in $ra that sumSquare wants to jump back to, but this will be overwritten by the call to mult. Need to save sumSquare return address before call to mult.

Nested Procedures In general, may need to save some other info in addition to $ra. When a C program is run, there are 3 important memory areas allocated: Static: Variables declared once per program, cease to exist only after execution completes Heap: Variables declared dynamically Stack: Space to be used by procedure during execution; this is where we can save register values

Memory Organization Data segment 0x7fff ffff The memory is byte addressed. If you want to step word by word through the memory, then you have to increase your “pointer” each time by 4. Stack segment Dynamic data Data segment Static data 0x1000 0000 Text segment 0x40 0000 Reserved

C memory Allocation ¥ Space for saved procedure information Stack Address ¥ Stack Space for saved procedure information $sp stack pointer Heap Explicitly created space, e.g., malloc(); C pointers Static Variables declared once per program Code Program

Run-Time Memory Allocation in Executable Programs  Local data in functions, stack frames $ s p g 4 h e x 1 T t S a i c d D y n m k 7 f 8 R r v $sp stack pointer Explicitly allocated space, (C malloc()library proc) Variables allocated once per program (global, C static) global pointer $gp Program instructions

Using the Stack So we have a register $sp which always points to the last used space in the stack. To use stack, we decrement this pointer by the amount of space we need and then fill it with info. So, how do we compile this? int sumSquare(int x, int y) { return mult(x,x)+ y; }

Using the Stack (2/2) Compile by hand sumSquare: addi $sp, $sp, -8 #space on stack sw $ra, 4($sp) #save ret addr sw $a1, 0($sp) # save y add $a1, $a0, $zero # mult(x,x) jal mult # call mult lw $a1, 0($sp) # restore y add $v0, $v0, $a1 # mult()+ y lw $ra, 4($sp) # get ret addr addi $sp, $sp, 8 # restore stack jr $ra

Steps for Making a Procedure Call 1) Save necessary values onto stack. 2) Assign argument(s), if any. 3) jal call 4) Restore values from stack.

Rules for Procedures Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3 Return value is always in $v0 (and if necessary in $v1) Must follow register conventions (even in functions that only you will call)! So what are they? Return address $ra Arguments $a0, $a1, $a2, $a3 Return value $v0, $v1 variables $s0, $s1, , $s7

Register Conventions (1/6) Caller: the calling function Callee: the function being called When callee returns from executing, the caller needs to know which registers may have changed and which are guaranteed to be unchanged. Register Conventions: A set of generally accepted rules as to which registers will be unchanged after a procedure call (jal) and which may be changed.

Register Conventions (2/6) $0: No Change. Always 0. $v0-$v1: Change. These are expected to contain new values. $a0-$a3: Change. These are volatile argument registers. $t0-$t9: Change. That’s why they’re called temporary: any procedure may change them at any time.

Register Conventions (3/6) $s0-$s7: No Change. Very important, that’s why they’re called saved registers. If the callee changes these in any way, it must restore the original values before returning. $sp: No Change. The stack pointer must point to the same place before and after the jal call, or else the caller won’t be able to restore values from the stack. $ra: Change. The jal call itself will change this register.

Register Conventions (4/6) What do these conventions mean? If function A calls function B, then function A must save any temporary registers that it may be using onto the stack before making a jal call. Function B must save any S (saved) registers it intends to use before garbling up their values Remember: Caller/callee need to save only temporary / saved registers they are using, not all registers.

Register Conventions (5/6) Note that, if the callee is going to use some s registers, it must: save those s registers on the stack use the registers restore s registers from the stack jr $ra With the temp registers, the callee doesn’t need to save onto the stack. Therefore the caller must save those temp registers that it would like to preserve though the call.

$at: may be used by the assembler at any time; always unsafe to use Other Registers (6/6) $at: may be used by the assembler at any time; always unsafe to use $k0-$k1: may be used by the kernel at any time; unsafe to use $gp: don’t worry about it $fp: don’t worry about it Note: Read up on $gp and $fp in Appendix A.

Example: Compile This main() { int i,j,k,m; /* i-m:$s0-$s3 */ i = mult(j,k); ... ; m = mult(i,i); ... } int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product += mcand; mlier -= 1; } return product;

Example: Compile This .. __start: add $a0, $s1, $0 # arg0 = j add $a1, $s2, $0 # arg1 = k jal mult # call mult add $s0, $v0, $0 # i = mult() add $a0, $s0, $0 # arg0 = i add $a1, $s0, $0 # arg1 = i jal mult # call mult add $s3, $v0, $0 # m = mult() ... done: ..

Example: Compile This Notes: main function ends with done, not jr $ra, so there’s no need to save $ra onto stack all variables used in main function are saved registers, so there’s no need to save these onto stack

Example: Compile This mult: add $t0,$0,$0 # prod = 0 Loop: slt $t1,$0,$a1 # mlr > 0? beq $t1,$0,Fin # no => Fin add $t0,$t0,$a0 # prod += mc addi $a1,$a1,-1 # mlr -= 1 j Loop # goto Loop Fin: add $v0,$t0,$0 # $v0 = prod jr $ra # return

Example: Compile This Notes: no jal calls are made from mult and we don’t use any saved registers, so we don’t need to save anything onto stack temp registers are used for intermediate calculations (could have used s registers, but would have to save the caller’s on the stack.) $a1 is modified directly (instead of copying into a temp register) since we are free to change it result is put into $v0 before returning