Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE331 W04.1Irwin&Li 2006 PSU CSE 331 Computer Organization and Design Fall 2006 Week 4 Section 1: Mary Jane Irwin (www.cse.psu.edu/~mji)www.cse.psu.edu/~mji.

Similar presentations


Presentation on theme: "CSE331 W04.1Irwin&Li 2006 PSU CSE 331 Computer Organization and Design Fall 2006 Week 4 Section 1: Mary Jane Irwin (www.cse.psu.edu/~mji)www.cse.psu.edu/~mji."— Presentation transcript:

1 CSE331 W04.1Irwin&Li 2006 PSU CSE 331 Computer Organization and Design Fall 2006 Week 4 Section 1: Mary Jane Irwin (www.cse.psu.edu/~mji)www.cse.psu.edu/~mji Section 2: Feihui Li (www.cse.psu.edu/~feli )www.cse.psu.edu/~feli Course material on ANGEL: cms.psu.edu [ adapted from D. Patterson slides ]

2 CSE331 W04.2Irwin&Li 2006 PSU Head’s Up  Last week’s material l MIPS control flow and logic operations  This week’s material l Supporting procedure calls and returns; addressing modes -Reading assignment - PH: 2.7-2.9, A.6, D.2  Next week’s material l Assemblers, linkers and loaders -Reading assignment - PH: 2.10, A.1-A.5  Reminders l Class is cancelled on Thursday, Sept 28 l HW 3 (another spim assignment) is due Wednesday, Sept 27 (by 11:55pm) l Quiz 2 is due Friday, Sept 29 (by 11:55pm) l Exam #1 is Tuesday, Oct 10, 6:30-7:45pm -Please email if you have a conflict

3 CSE331 W04.3Irwin&Li 2006 PSU 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

4 CSE331 W04.4Irwin&Li 2006 PSU Review: MIPS Organization Processor Memory 32 bits 2 30 words read/write addr read data write data word address (binary) 0…0000 0…0100 0…1000 0…1100 1…1100 Register File src1 addr src2 addr dst addr write data 32 bits src1 data src2 data 32 registers ($zero - $ra) 32 5 5 5 PC ALU 32 0123 7654 byte address (big Endian) Fetch PC = PC+4 DecodeExec Add 32 4 Add 32 br offset

5 CSE331 W04.5Irwin&Li 2006 PSU Programming Styles  Procedures (subroutines, functions) allow the programmer to structure programs making them l easier to understand and debug and l allowing code to be reused  Procedures allow the programmer to concentrate on one portion of the code at a time l 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)

6 CSE331 W04.6Irwin&Li 2006 PSU 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

7 CSE331 W04.7Irwin&Li 2006 PSU  MIPS procedure call instruction: jalProcAddress#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 Instruction for Calling a Procedure op 26 bit address J format 3 ????

8 CSE331 W04.8Irwin&Li 2006 PSU 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

9 CSE331 W04.9Irwin&Li 2006 PSU Spilling Registers  What if the callee needs to use more registers than allocated to argument and return values? l it uses a stack – a last-in-first-out queue low addr high addr $sp  One of the general registers, $sp ( $29 ), is used to address the stack (which “grows” from high address to low address) l add data onto the stack – push $sp = $sp – 4 data on stack at new $sp l remove data from the stack – pop data from stack at $sp $sp = $sp + 4 top of stack

10 CSE331 W04.10Irwin&Li 2006 PSU 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

11 CSE331 W04.11Irwin&Li 2006 PSU 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

12 CSE331 W04.12Irwin&Li 2006 PSU Nested Procedures  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:jalrt_1 next:... rt_1:bne$a0, $zero, to_2 add$v0, $zero, $zero jr$ra to_2:addi$a0, $a0, -1 jalrt_2 jr$ra rt_2:...

13 CSE331 W04.13Irwin&Li 2006 PSU Nested Procedures Outcome caller:jalrt_1 next:... rt_1:bne$a0, $zero, to_2 add$v0, $zero, $zero jr$ra to_2:addi$a0, $a0, -1 jalrt_2 jr$ra rt_2:...  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 ?

14 CSE331 W04.14Irwin&Li 2006 PSU 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 jalrt_2 bk_2:lw$a0, 0($sp) lw$ra, 4($sp) addi$sp, $sp, 8 jr$ra  Save the return address (and arguments) on the stack low addr high addr  $sp $ra old TOS

15 CSE331 W04.15Irwin&Li 2006 PSU 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 jalrt_2 bk_2:lw$a0, 0($sp) lw$ra, 4($sp) addi$sp, $sp, 8 jr$ra  Save the return address (and arguments) on the stack caller rt addr high addr  $sp low addr old $a0 $ra caller rt addr bk_2 old TOS

16 CSE331 W04.16Irwin&Li 2006 PSU 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 jalrt_2 bk_2:lw$a0, 0($sp) lw$ra, 4($sp) addi$sp, $sp, 8 jr$ra  Save the return address (and arguments) on the stack low addr high addr  $sp $ra old TOS

17 CSE331 W04.17Irwin&Li 2006 PSU 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 jalrt_2 bk_2:lw$a0, 0($sp) lw$ra, 4($sp) addi$sp, $sp, 8 jr$ra  Save the return address (and arguments) on the stack caller rt addr high addr  $sp low addr old $a0 $ra bk_2caller rt addr old TOS

18 CSE331 W04.18Irwin&Li 2006 PSU MIPS Register Convention NameRegister Number UsagePreserve on call? $zero0the constant 0n.a. $v0 - $v12-3returned valuesno $a0 - $a34-7argumentsyes $t0 - $t78-15temporariesno $s0 - $s716-23saved valuesyes $t8 - $t924-25temporariesno $gp28global pointeryes $sp29stack pointeryes $fp30frame pointeryes $ra31return addressyes

19 CSE331 W04.19Irwin&Li 2006 PSU End of Lecture #7

20 CSE331 W04.20Irwin&Li 2006 PSU Head’s Up  Last week’s (and today’s) material l Supporting procedure calls and returns; addressing modes -Reading assignment - PH: 2.7-2.9, A.6, D.2  This Thursday’s material l Assemblers, linkers and loaders -Reading assignment - PH: 2.10, A.1-A.5  Reminders l HW 4 (another spim assignment) is due Friday, Oct 6 (by 11:55pm) l Quiz 3 is due Monday, Oct 9 (by 11:55pm) l Exam #1 is Tuesday, Oct 10, 6:30-7:45pm -Please email if you have a conflict -A practice exam is now on-line through ANGEL

21 CSE331 W04.21Irwin&Li 2006 PSU 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

22 CSE331 W04.22Irwin&Li 2006 PSU 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

23 CSE331 W04.23Irwin&Li 2006 PSU 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 L1:addi$a0, $a0, -1#n >=1, so decrement n jalfact#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 addi$sp, $sp, 8#adjust stack pointer mul$v0, $a0, $v0#$v0 = n * fact(n-1) jr$ra#return to caller

24 CSE331 W04.24Irwin&Li 2006 PSU A Look at the Stack for $a0 = 2, Part 1  $sp $ra $a0 $v0 old TOS  Stack state after execution of first encounter with the jal instruction (second call to fact routine with $a0 now holding 1) l save return address to caller routine (i.e., location in the main routine where first call to fact is made) on the stack save original value of $a0 on the stack

25 CSE331 W04.25Irwin&Li 2006 PSU A Look at the Stack for $a0 = 2, Part 1  $sp $ra $a0 $v0  $sp caller rt addr $a0 = 2 21 bk_f old TOS  Stack state after execution of first encounter with the jal instruction (second call to fact routine with $a0 now holding 1) l 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

26 CSE331 W04.26Irwin&Li 2006 PSU A Look at the Stack for $a0 = 2, Part 2  $sp $ra $a0 $v0 old TOS  Stack state after execution of second encounter with the jal instruction (third call to fact routine with $a0 now holding 0) save return address of instruction in caller routine (instruction after jal ) on the stack save previous value of $a0 on the stack

27 CSE331 W04.27Irwin&Li 2006 PSU A Look at the Stack for $a0 = 2, Part 2 $ra $a0 $v0  $sp caller rt addr $a0 = 2 10 bk_f old TOS  $sp $a0 = 1 bk_f  Stack state after execution of second encounter with the jal instruction (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

28 CSE331 W04.28Irwin&Li 2006 PSU A Look at the Stack for $a0 = 2, Part 3  $sp $ra $a0 $v0 old TOS  Stack state after execution of first encounter with the first jr instruction ( $v0 initialized to 1) l stack pointer updated to point to third call to fact

29 CSE331 W04.29Irwin&Li 2006 PSU A Look at the Stack for $a0 = 2, Part 3 $ra $a0 $v0  $sp bk_f $a0 = 1 0 old TOS  $sp $a0 = 0 bk_f $a0 = 2 caller rt addr 1  $sp  Stack state after execution of first encounter with the first jr instruction ( $v0 initialized to 1) l stack pointer updated to point to third call to fact

30 CSE331 W04.30Irwin&Li 2006 PSU A Look at the Stack for $a0 = 2, Part 4  $sp $ra $a0 $v0 old TOS  Stack state after execution of first encounter with the second jr instruction (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 l stack pointer updated to point to second call to fact

31 CSE331 W04.31Irwin&Li 2006 PSU A Look at the Stack for $a0 = 2, Part 4 $ra $a0 $v0  $sp bk_f $a0 = 1 0 old TOS $a0 = 0 bk_f $a0 = 2 caller rt addr 1  $sp 1 bk_f 1 * 1  Stack state after execution of first encounter with the second jr instruction (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 l stack pointer updated to point to second call to fact

32 CSE331 W04.32Irwin&Li 2006 PSU A Look at the Stack for $a0 = 2, Part 5  $sp $ra $a0 $v0 old TOS  Stack state after execution of second encounter with the second jr instruction (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 l stack pointer updated to point to first call to fact

33 CSE331 W04.33Irwin&Li 2006 PSU A Look at the Stack for $a0 = 2, Part 5 $ra $a0 $v0  $sp bk_f $a0 = 1 1 old TOS $a0 = 0 bk_f $a0 = 2 caller rt addr 1 * 1  $sp 2 bk_f 2 * 1 * 1  Stack state after execution of second encounter with the second jr instruction (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 l stack pointer updated to point to first call to fact caller rt addr

34 CSE331 W04.34Irwin&Li 2006 PSU 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 low addr high addr $sp Saved argument regs (if any) Saved return addr Saved local regs (if any) Local arrays & structures (if any) $fp

35 CSE331 W04.35Irwin&Li 2006 PSU 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 0x 0000 0000 Text (Your code) Reserved Static data 0x 0040 0000 0x 1000 0000 0x 1000 8000 0x 7f f f f f f c Stack Dynamic data (heap) $sp $gp PC

36 CSE331 W04.36Irwin&Li 2006 PSU 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

37 CSE331 W04.37Irwin&Li 2006 PSU 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||

38 CSE331 W04.38Irwin&Li 2006 PSU Review: MIPS Instructions, so far CategoryInstrOpCExampleMeaning Arithmetic (R & I format) add0 & 20add $s1, $s2, $s3$s1 = $s2 + $s3 subtract0 & 22sub $s1, $s2, $s3$s1 = $s2 - $s3 add immediate8addi $s1, $s2, 4$s1 = $s2 + 4 shift left logical0 & 00sll $s1, $s2, 4$s1 = $s2 << 4 shift right logical 0 & 02srl $s1, $s2, 4$s1 = $s2 >> 4 (fill with zeros) shift right arithmetic 0 & 03sra $s1, $s2, 4$s1 = $s2 >> 4 (fill with sign bit) and0 & 24and $s1, $s2, $s3$s1 = $s2 & $s3 or0 & 25or $s1, $s2, $s3$s1 = $s2 | $s3 nor0 & 27nor $s1, $s2, $s3$s1 = not ($s2 | $s3) and immediatecand $s1, $s2, ff00$s1 = $s2 & 0xff00 or immediatedor $s1, $s2, ff00$s1 = $s2 | 0xff00 load upper immediate flui $s1, 0xffff$s1 = 0xffff0000

39 CSE331 W04.39Irwin&Li 2006 PSU Review: MIPS Instructions, so far CategoryInstrOpCExampleMeaning Data transfer (I format) load word23lw $s1, 100($s2)$s1 = Memory($s2+100) store word2bsw $s1, 100($s2)Memory($s2+100) = $s1 load byte20lb $s1, 101($s2)$s1 = Memory($s2+101) store byte28sb $s1, 101($s2)Memory($s2+101) = $s1 load half21lh $s1, 101($s2)$s1 = Memory($s2+102) store half29sh $s1, 101($s2)Memory($s2+102) = $s1 Cond. branch (I & R format) br on equal4beq $s1, $s2, Lif ($s1==$s2) go to L br on not equal5bne $s1, $s2, Lif ($s1 !=$s2) go to L set on less than immediate aslti $s1, $s2, 100 if ($s2<100) $s1=1; else $s1=0 set on less than 0 & 2aslt $s1, $s2, $s3if ($s2<$s3) $s1=1; else $s1=0 Uncond. jump jump2j 2500go to 10000 jump register0 & 08jr $t1go to $t1 jump and link3jal 2500go to 10000; $ra=PC+4

40 CSE331 W04.40Irwin&Li 2006 PSU Review: MIPS R3000 ISA  Instruction Categories l Load/Store l Computational l Jump and Branch l Floating Point -coprocessor l Memory Management l Special  3 Instruction Formats: all 32 bits wide R0 - R31 PC HI LO OPrs rt rdshamtfunct OPrs rt 16 bit number OP 26 bit jump target Registers R format I format 6 bits5 bits 6 bits J format


Download ppt "CSE331 W04.1Irwin&Li 2006 PSU CSE 331 Computer Organization and Design Fall 2006 Week 4 Section 1: Mary Jane Irwin (www.cse.psu.edu/~mji)www.cse.psu.edu/~mji."

Similar presentations


Ads by Google