CSE 331 Computer Organization and Design Fall 2007 Week 4

Slides:



Advertisements
Similar presentations
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.
Advertisements

The University of Adelaide, School of Computer Science
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
ELEN 468 Advanced Logic Design
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
CSE331 W04.1Irwin&Li 2006 PSU CSE 331 Computer Organization and Design Fall 2006 Week 4 Section 1: Mary Jane Irwin (
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
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
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.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
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.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
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
Prof. Hsien-Hsin Sean Lee
Lecture 6: Assembly Programs
Computer Architecture Instruction Set Architecture
Computer Architecture Instruction Set Architecture
Instructions: Language of the Computer
Lecture 4: MIPS Instruction Set
ELEN 468 Advanced Logic Design
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Haojin Zhu ( ) Course Website:
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
MIPS Instructions.
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Instruction encoding The ISA defines Format = Encoding
Lecture 5: Procedure Calls
Logical and Decision Operations
Chapter 2 Instructions: Language of the Computer part 2
10/4: Lecture Topics Overflow and underflow Logical operations
Program and memory layout
Lecture 6: Assembly Programs
Systems Architecture I
Program and memory layout
Computer Architecture
CS352H Computer Systems Architecture
Program and memory layout
Program and memory layout
Presentation transcript:

CSE 331 Computer Organization and Design Fall 2007 Week 4 Section 1: Mary Jane Irwin (www.cse.psu.edu/~mji) Section 2: Krishna Narayanan Course material on ANGEL: cms.psu.edu [adapted from D. Patterson slides]

Head’s Up Last week’s material This week’s material MIPS control flow and logic operations This week’s material Supporting procedure calls and returns; addressing modes Reading assignment - PH: 2.7-2.9, A.6, D.2 Next week’s material Assemblers, linkers and loaders Reading assignment - PH: 2.10, A.1-A.5 Reminders HW 3 (another spim assignment) is due Wednesday, Sept 19th (by 11:55pm) Quiz 2 is due Friday, Sept 21st (by 11:55pm) Exam #1 is Tuesday, Oct 2, 6:30-7:45pm, 113 IST Please email if you have a conflict

The reality is that most code is not heavily performance-bound by the lack of one instruction. … So the belief that the microprocessor is but one or two instructions away from blindingly higher speed is almost always wrong. Bob Colwell, The Pentium Chronicles

Review: MIPS Organization Processor Memory Register File 1…1100 src1 addr src1 data 5 32 src2 addr 32 registers ($zero - $ra) 5 dst addr read/write addr 5 src2 data write data 32 230 words 32 32 32 bits br offset read data 32 Add PC 32 32 32 32 Add 32 4 write data 0…1100 Fetch PC = PC+4 Decode Exec 32 0…1000 32 4 5 6 7 0…0100 32 ALU 1 2 3 0…0000 32 word address (binary) 32 bits 32 byte address (big Endian)

Programming Styles Procedures (subroutines, functions) allow the programmer to structure programs making them easier to understand and debug and allowing code to be reused Procedures allow the programmer to concentrate on one portion of the code at a time parameters act as barriers between the procedure and the rest of the program and data, allowing the procedure to be passed values (arguments) and to return values (results) barriers concept is important!!

Six Steps in Execution of a Procedure Main routine (caller) places parameters in a place where the procedure (callee) can access them $a0 - $a3: four argument registers Caller transfers control to the callee Callee acquires the storage resources needed Callee performs the desired task Callee places the result value in a place where the caller can access it $v0 - $v1: two value registers for result values Callee returns control to the caller $ra: one return address register to return to the point of origin

Instruction for Calling a Procedure MIPS procedure call instruction: jal ProcAddress #jump and link Saves PC+4 in register $ra as the link to the following instruction to set up the procedure return Machine format: Then can do procedure return with just jr $ra #return J format op 26 bit address jr jumps to the address stored in the register – in this case $ra – which is just what we want. 3 ????

Basic Procedure Flow For a procedure that computes the GCD of two values i (in $t0) and j (in $t1) gcd(i,j); The caller puts the i and j (the parameters values) in $a0 and $a1 and issues a jal gcd #jump to routine gcd The callee computes the GCD, puts the result in $v0, and returns control to the caller using gcd: . . . #code to compute gcd jr $ra #return

Spilling Registers What if the callee needs to use more registers than allocated to argument and return values? callee uses a stack – a last-in-first-out queue One of the general registers, $sp ($29), is used to address the stack (which “grows” from high address to low address) add data onto the stack – push $sp = $sp – 4 data on stack at new $sp remove data from the stack – pop data from stack at $sp $sp = $sp + 4 high addr top of stack $sp Note that temporary registers $t0 through $t9 can also be used as by MIPS convention they are not preserved by the callee (kept intact) across subroutine boundaries However, saved registers $s0 through $s7 must be preserved by the calle - I.e., if the callee uses one, it must first save it and then restore it to its old value before returning control to the caller low addr

Compiling a C Leaf Procedure Leaf procedures are ones that do not call other procedures. Give the MIPS assembler code for int leaf_ex (int g, int h, int i, int j) { int f; f = (g+h) – (i+j); return f; } where g, h, i, and j are in $a0, $a1, $a2, $a3 leaf_ex: addi $sp,$sp,-8 #make stack room sw $t1,4($sp) #save $t1 on stack sw $t0,0($sp) #save $t0 on stack add $t0,$a0,$a1 add $t1,$a2,$a3 sub $v0,$t0,$t1 lw $t0,0($sp) #restore $t0 lw $t1,4($sp) #restore $t1 addi $sp,$sp,8 #adjust stack ptr jr $ra For lecture When using temporary registers, must assume their old values must be saved and restored. Show the stack contents on the board as you progress through the code

What happens to return addresses with nested procedures? int rt_1 (int i) { if (i == 0) return 0; else return rt_2(i-1); } caller: jal rt_1 next: . . . rt_1: bne $a0, $zero, to_2 add $v0, $zero, $zero jr $ra to_2: addi $a0, $a0, -1 jal rt_2 rt_2: . . . So caller must save $ra on the stack and callee must save any other registers that it uses on the stack

Nested Procedures Outcome caller: jal rt_1 next: . . . rt_1: bne $a0, $zero, to_2 add $v0, $zero, $zero jr $ra to_2: addi $a0, $a0, -1 jal rt_2 rt_2: . . . So caller must save $ra on the stack and callee must save any other registers that it uses on the stack On the call to rt_1, the return address (next in the caller routine) gets stored in $ra. What happens to the value in $ra (when i != 0) when rt_1 makes a call to rt_2?

Saving the Return Address, Part 1 Nested procedures (i passed in $a0, return value in $v0) rt_1: bne $a0, $zero, to_2 add $v0, $zero, $zero jr $ra to_2: addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) addi $a0, $a0, -1 jal rt_2 bk_2: lw $a0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 Save the return address (and arguments) on the stack high addr old TOS  $sp caller rt addr old $a0 $sp low addr For lecture Note that the original values of $ra and I ($a0) are restored on return NEXT TIME - Add tracking of the PC to the animation!! caller rt addr bk_2 $ra

Saving the Return Address, Part 2 Nested procedures (i passed in $a0, return value in $v0) rt_1: bne $a0, $zero, to_2 add $v0, $zero, $zero jr $ra to_2: addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) addi $a0, $a0, -1 jal rt_2 bk_2: lw $a0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 Save the return address (and arguments) on the stack high addr old TOS $sp caller rt addr old $a0  $sp low addr For lecture caller rt addr bk_2 $ra

MIPS Register Convention Name Register Number Usage Preserve on call? $zero the constant 0 n.a. $v0 - $v1 2-3 returned values no $a0 - $a3 4-7 arguments yes $t0 - $t7 8-15 temporaries $s0 - $s7 16-23 saved values $t8 - $t9 24-25 $gp 28 global pointer $sp 29 stack pointer $fp 30 frame pointer $ra 31 return address $a0-$a3 – used to pass parameters, loaded by caller, read by callee and preserved by callee $v0-$v1 – used to pass results back from callee to caller (note, can also use to pass parameters from caller to callee by is NOT preserved)

Bob Colwell, The Pentium Chronicles We all go through stages of computer maturity when learning computer science and programming. At first, the computer strikes us as really obtuse and stupidly designed, and then positively malevolent, as though it is actually sentient and working very hard to make our lives miserable. Eventually, the student outgrows this feeling and no longer takes it personally when an attempted compile generates a long list of error messages. In effect, the student learns that the computer is simply following its internal rules and has no opinion about that student whatsoever, so its is clearly a waste of indignation to react to it emotionally. Bob Colwell, The Pentium Chronicles

Compiling a Recursive Procedure A procedure for calculating factorial int fact (int n) { if (n < 1) return 1; else return (n * fact (n-1)); } A recursive procedure (one that calls itself!) fact (0) = 1 fact (1) = 1 * 1 = 1 fact (2) = 2 * 1 * 1 = 2 fact (3) = 3 * 2 * 1 * 1 = 6 fact (4) = 4 * 3 * 2 * 1 * 1 = 24 . . . Assume n is passed in $a0; result returned in $v0

Compiling a Recursive Procedure fact: addi $sp, $sp, -8 #adjust stack pointer sw $ra, 4($sp) #save return address sw $a0, 0($sp) #save argument n slti $t0, $a0, 1 #test for n < 1 beq $t0, $zero, L1 #if n >=1, go to L1 addi $v0, $zero, 1 #else return 1 in $v0 addi $sp, $sp, 8 #adjust stack pointer jr $ra #return to caller (1st) L1: addi $a0, $a0, -1 #n >=1, so decrement n jal fact #call fact with (n-1) #this is where fact returns bk_f: lw $a0, 0($sp) #restore argument n lw $ra, 4($sp) #restore return address mul $v0, $a0, $v0 #$v0 = n * fact(n-1) jr $ra #return to caller (2nd) Note, no need to load $ra, and $a0, since they have not changed values (expect when n is >= to 1 as on next page) We assume a multiply instruction is available, even though we haven’t covered it yet!

A Look at the Stack for $a0 = 2, Part 1 Stack state after execution of the first encounter with jal (second call to fact routine with $a0 now holding 1) saved return address to caller routine (i.e., location in the main routine where first call to fact is made) on the stack saved original value of $a0 on the stack old TOS  $sp caller rt addr $a0 = 2 $sp For lecture NEXT TIME - Add tracking of the PC to the animation!! caller rt addr bk_f $ra 2 1 $a0 $v0

A Look at the Stack for $a0 = 2, Part 2 Stack state after execution of the second encounter with jal (third call to fact routine with $a0 now holding 0) saved return address of instruction in caller routine (instruction after jal) on the stack saved previous value of $a0 on the stack old TOS caller rt addr $a0 = 2  $sp bk_f $a0 = 1 $sp For lecture bk_f bk_f $ra 1 $a0 $v0

A Look at the Stack for $a0 = 2, Part 3 Stack state after execution of the first encounter with the first jr ($v0 initialized to 1) stack pointer updated to point to third call to fact old TOS caller rt addr $a0 = 2 bk_f $a0 = 1 $sp  $sp bk_f $a0 = 0  $sp For lecture bk_f $ra $a0 1 $v0

A Look at the Stack for $a0 = 2, Part 4 Stack state after execution of the first encounter with the second jr (return from fact routine after updating $v0 to 1 * 1) return address to caller routine (bk_f in fact routine) restored to $ra from the stack previous value of $a0 restored from the stack stack pointer updated to point to second call to fact old TOS caller rt addr $a0 = 2 $sp bk_f $a0 = 1  $sp bk_f $a0 = 0 For lecture bk_f $ra 1 $a0 1 * 1 1 $v0

A Look at the Stack for $a0 = 2, Part 5 Stack state after execution of the second encounter with the second jr (return from fact routine after updating $v0 to 2 * 1 * 1) return address to caller routine (main routine) restored to $ra from the stack original value of $a0 restored from the stack stack pointer updated to point to first call to fact old TOS $sp caller rt addr $a0 = 2  $sp bk_f $a0 = 1 bk_f $a0 = 0 For lecture caller rt addr bk_f $ra 2 1 $a0 2 * 1 * 1 1 * 1 $v0

Allocating Space on the Stack The segment of the stack containing a procedure’s saved registers and local variables is its procedure frame (aka activation record) The frame pointer ($fp) points to the first word of the frame of a procedure – providing a stable “base” register for the procedure $fp is initialized using $sp on a call and $sp is restored using $fp on a return high addr $fp Saved argument regs (if any) Saved return addr Saved local regs (if any) Local arrays & structures (if any) $sp On the other hand, the stack pointer (sp) might change during the procedure (not the case for all of our examples) so references to a local variable in memory might have different offsets wrt the sp depending on where they are in the procedure. low addr

Allocating Space on the Heap Static data segment for constants and other static variables (e.g., arrays) Dynamic data segment (aka heap) for structures that grow and shrink (e.g., linked lists) Allocate space on the heap with malloc() and free it with free() Memory $sp 0x 7f f f f f f c Stack Dynamic data (heap) $gp 0x 1000 8000 Static data 0x 1000 0000 Text (Your code) Stack and heap grow towards each other. Malloc() allocates space on the heap and returns a pointer to it. Memory allocation is controlled by programs (in C) and is the source of many common and difficult bugs. Forgetting to free space leads to “memory leak” and the need for “garbage collection” and freeing space to early to “dangling pointers”. PC 0x 0040 0000 Reserved 0x 0000 0000

MIPS Addressing Modes Register addressing – operand is in a register Base (displacement) addressing – operand is at the memory location whose address is the sum of a register and a 16-bit constant contained within the instruction Immediate addressing – operand is a 16-bit constant contained within the instruction PC-relative addressing –instruction address is the sum of the PC and a 16-bit constant contained within the instruction Pseudo-direct addressing – instruction address is the 26-bit constant contained within the instruction concatenated with the upper 4 bits of the PC

Addressing Modes Illustrated 1. Register addressing op rs rt rd funct Register word operand op rs rt offset 2. Base addressing base register Memory word or byte operand 3. Immediate addressing op rs rt operand 4. PC-relative addressing op rs rt offset Program Counter (PC) Memory branch destination instruction 5. Pseudo-direct addressing op jump address Program Counter (PC) Memory jump destination instruction ||

Review: MIPS Instructions, so far Category Instr OpC Example Meaning Arithmetic (R & I format) add 0 & 20 add $s1, $s2, $s3 $s1 = $s2 + $s3 subtract 0 & 22 sub $s1, $s2, $s3 $s1 = $s2 - $s3 add immediate 8 addi $s1, $s2, 4 $s1 = $s2 + 4 shift left logical 0 & 00 sll $s1, $s2, 4 $s1 = $s2 << 4 shift right logical 0 & 02 srl $s1, $s2, 4 $s1 = $s2 >> 4 (fill with zeros) shift right arithmetic 0 & 03 sra $s1, $s2, 4 $s1 = $s2 >> 4 (fill with sign bit) and 0 & 24 and $s1, $s2, $s3 $s1 = $s2 & $s3 or 0 & 25 or $s1, $s2, $s3 $s1 = $s2 | $s3 nor 0 & 27 nor $s1, $s2, $s3 $s1 = not ($s2 | $s3) and immediate c and $s1, $s2, ff00 $s1 = $s2 & 0xff00 or immediate d or $s1, $s2, ff00 $s1 = $s2 | 0xff00 load upper immediate f lui $s1, 0xffff $s1 = 0xffff0000

Review: MIPS Instructions, so far Category Instr OpC Example Meaning Data transfer (I format) load word 23 lw $s1, 100($s2) $s1 = Memory($s2+100) store word 2b sw $s1, 100($s2) Memory($s2+100) = $s1 load byte 20 lb $s1, 101($s2) $s1 = Memory($s2+101) store byte 28 sb $s1, 101($s2) Memory($s2+101) = $s1 load half 21 lh $s1, 101($s2) $s1 = Memory($s2+102) store half 29 sh $s1, 101($s2) Memory($s2+102) = $s1 Cond. branch (I & R format) br on equal 4 beq $s1, $s2, L if ($s1==$s2) go to L br on not equal 5 bne $s1, $s2, L if ($s1 !=$s2) go to L set on less than immediate a slti $s1, $s2, 100 if ($s2<100) $s1=1; else $s1=0 set on less than 0 & 2a slt $s1, $s2, $s3 if ($s2<$s3) $s1=1; else $s1=0 Uncond. jump jump 2 j 2500 go to 10000 jump register 0 & 08 jr $t1 go to $t1 jump and link 3 jal 2500 go to 10000; $ra=PC+4

Instruction Categories Review: MIPS R3000 ISA Instruction Categories Load/Store Computational Jump and Branch Floating Point coprocessor Memory Management Special 3 Instruction Formats: all 32 bits wide Registers R0 - R31 PC HI LO 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits R format OP rs rt rd shamt funct I format OP rs rt 16 bit number J format OP 26 bit jump target