06LecSp12MachineLangIIx6 - 2/2/12 New ­School Machine...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2/2/12 New ­School Machine Structures (It’s a bit more complicated!) So1ware Hardware •  Parallel Requests CS 61C: Great Ideas in Computer Architecture More MIPS Machine Language Assigned to computer e.g., Search “Katz” •  Parallel Threads Parallelism & Assigned to core e.g., Lookup, Ads Assembler Machine Language Program (MIPS) $t0, 0($2) $t1, 4($2) $t1, 0($2) $t0, 4($2) 0000 1010 1100 0101 1001 1111 0110 1000 1100 0101 1010 0000 Anything can be represented as a number, i.e., data or instrucYons 0110 1000 1111 1001 1010 0000 0101 1100 1111 1001 1000 0110 0101 1100 0000 1010 1000 0110 1001 1111 ! Machine Interpreta4on Logic Gates 2 •  Computer words and vocabulary are called instruc>ons and instruc>on set respecYvely •  MIPS is example RISC instrucYon set in this class •  Rigid format: 1 operaYon, 2 source operands, 1 desYnaYon –  add, sub, mul, div, and, or, sll, srl –  lw, sw to move data to/from registers from/to memory Architecture Implementa4on Logic Circuit DescripCon (Circuit SchemaCc Diagrams) Spring 2012  ­ ­ Lecture #6 3 •  Simple mappings from arithmeYc expressions, array access, if ­then ­else in C to MIPS instrucYons 2/2/12 Endianess –  Most ­significant byte at least address of a word –  c.f. Li>le Endian: least ­significant byte at least address little endian byte 0 3 2 1 0 0 big endian byte 0 1 2 3 lsb msb •  Big Endian: address of most significant byte = word address (xx00 = “Big End” of word): IBM 360/370, MIPS, Sparc •  Li>le Endian: address of least significant byte = word address (xx00 = “Li>le End” of word): Intel 80x86 •  Can only tell if access same data with load/store byte and load/store word Spring 2012  ­ ­ Lecture #6 Spring 2012  ­ ­ Lecture #6 4 Agenda •  MIPS is Big Endian 2/2/12 Today’s •  Programming Languages Lecture 2/2/12 Spring 2012  ­ ­ Lecture #6 Hardware Architecture DescripCon (e.g., block diagrams) 2/2/12 Cache Memory Review temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; lw lw sw sw Core FuncYonal Unit(s) A0+B0 A1+B1 A2+B2 A3+B3 •  Hardware descripYons Big Idea #1: Levels of RepresentaYon/ InterpretaYon Compiler Assembly Language Program (e.g., MIPS) Input/Output InstrucYon Unit(s) >1 data item @ one Yme e.g., Add of 4 pairs of words 1 Core Memory (Cache) •  Parallel Data Spring 2012  ­ ­ Lecture #6 Computer … Core >1 instrucYon @ one Yme e.g., 5 pipelined instrucYons All gates @ one Yme High Level Language Program (e.g., C) Achieve High Performance •  Parallel InstrucYons Instructor: David A. Pa>erson h>p://inst.eecs.Berkeley.edu/~cs61c/sp12 2/2/12 Harness Smart Phone Warehouse Scale Computer 5 •  •  •  •  •  •  •  String Copy Example Administrivia FuncYons Technology Break MIPS register allocaYon convenYon Memory Heap Summary 2/2/12 Spring 2012  ­ ­ Lecture #6 6 1 2/2/12 Fast String Copy in MIPS Assembly Fast String Copy Code in C Get addresses of x and y into $s1, $s2 •  Copy x to y char *p, *q; p = &x[0]; /* p = x */ p and q are assigned to these registers # $t1 = &p (BA), q @ &p + 4 # $s1 = p # $s2 = q Loop: # $t2 = *p BA+4 Q # *q = $t2 BA P # p = p + 1 # q = q + 1 # if *p == 0, go to Exit j Loop # go to Loop /* set p to address of 1st char of x */ q = &y[0]; /* q = y also OK */ /* set q to address of 1st char of y */ while((*q++ = *p++) != ‘\0’) ; Exit: # N characters => N*6 + 3 instrucYons 2/2/12 Spring 2012  ­ ­ Lecture #6 7 Fast String Copy in MIPS Assembly 2/2/12 Spring 2012  ­ ­ Lecture #6 8 Fast String Copy in MIPS Assembly Get addresses of x and y into $s1, $s2 Get addresses of x and y into $s1, $s2 p and q are assigned to these registers lw $t1, Base Address (e.g., BA) # $s1 = p # $s2 = q Loop: # $t2 = *p BA+4 Q # *q = $t2 BA P # p = p + 1 # q = q + 1 # if *p == 0, go to Exit j Loop # go to Loop p and q are assigned to these registers lw $t1, Base Address (e.g., BA) lw $s1,0($t1) # $s1 = p # $s2 = q Loop: # $t2 = *p BA+4 Q # *q = $t2 BA P # p = p + 1 # q = q + 1 # if *p == 0, go to Exit j Loop # go to Loop Exit: # N characters => N*6 + 3 instrucYons 2/2/12 Spring 2012  ­ ­ Lecture #6 Student Roule>e? Exit: # N characters => N*6 + 3 instrucYons 9 Fast String Copy in MIPS Assembly 2/2/12 Spring 2012  ­ ­ Lecture #6 Student Roule>e? 10 Fast String Copy in MIPS Assembly Get addresses of x and y into $s1, $s2 Get addresses of x and y into $s1, $s2 p and q are assigned to these registers lw $t1, Base Address (e.g., BA) lw $s1,0($t1) # $s1 = p lw $s2,4($t1) # $s2 = q Loop: # $t2 = *p BA+4 Q # *q = $t2 BA P # p = p + 1 # q = q + 1 # if *p == 0, go to Exit j Loop # go to Loop p and q are assigned to these registers lw $t1, Base Address (e.g., BA) lw $s1,0($t1) # $s1 = p lw $s2,4($t1) # $s2 = q Loop: lb $t2,0($s1) # $t2 = *p BA+4 Q # *q = $t2 BA P # p = p + 1 # q = q + 1 # if *p == 0, go to Exit j Loop # go to Loop Exit: # N characters => N*6 + 3 instrucYons 2/2/12 Spring 2012  ­ ­ Lecture #6 Student Roule>e? Exit: # N characters => N*6 + 3 instrucYons 11 2/2/12 Spring 2012  ­ ­ Lecture #6 Student Roule>e? 12 2 2/2/12 Fast String Copy in MIPS Assembly Fast String Copy in MIPS Assembly Get addresses of x and y into $s1, $s2 Get addresses of x and y into $s1, $s2 p and q are assigned to these registers lw $t1, Base Address (e.g., BA) lw $s1,0($t1) # $s1 = p lw $s2,4($t1) # $s2 = q Loop: lb $t2,0($s1) # $t2 = *p BA+4 Q sb $t2,0($s2) # *q = $t2 BA P # p = p + 1 # q = q + 1 # if *p == 0, go to Exit j Loop # go to Loop p and q are assigned to these registers lw $t1, Base Address (e.g., BA) lw $s1,0($t1) # $s1 = p lw $s2,4($t1) # $s2 = q Loop: lb $t2,0($s1) # $t2 = *p BA+4 Q sb $t2,0($s2) # *q = $t2 BA P addi $s1,$s1,1 # p = p + 1 # q = q + 1 # if *p == 0, go to Exit j Loop # go to Loop Exit: # N characters => N*6 + 3 instrucYons 2/2/12 Spring 2012  ­ ­ Lecture #6 Student Roule>e? Exit: # N characters => N*6 + 3 instrucYons 13 Fast String Copy in MIPS Assembly 2/2/12 Spring 2012  ­ ­ Lecture #6 Student Roule>e? 14 Fast String Copy in MIPS Assembly Get addresses of x and y into $s1, $s2 Get addresses of x and y into $s1, $s2 p and q are assigned to these registers lw $t1, Base Address (e.g., BA) lw $s1,0($t1) # $s1 = p lw $s2,4($t1) # $s2 = q Loop: lb $t2,0($s1) # $t2 = *p BA+4 Q sb $t2,0($s2) # *q = $t2 BA P addi $s1,$s1,1 # p = p + 1 addi $s2,$s2,1 # q = q + 1 # if *p == 0, go to Exit j Loop # go to Loop p and q are assigned to these registers lw $t1, Base Address (e.g., BA) lw $s1,0($t1) # $s1 = p lw $s2,4($t1) # $s2 = q Loop: lb $t2,0($s1) # $t2 = *p BA+4 Q sb $t2,0($s2) # *q = $t2 BA P addi $s1,$s1,1 # p = p + 1 addi $s2,$s2,1 # q = q + 1 beq $t2,$zero,Exit # if *p == 0, go to Exit j Loop # go to Loop Exit: # N characters => N*6 + 3 instrucYons 2/2/12 Spring 2012  ­ ­ Lecture #6 Student Roule>e? Exit: # N characters => N*6 + 3 instrucYons 15 2/2/12 Spring 2012  ­ ­ Lecture #6 Student Roule>e? 16 Administrivia Which statement is TRUE? char *p, *q; p = &x[0]; q = &y[0]; while((*q++ = *p++) != ‘\0’) ; ☐ $t1 corresponds to p! ☐ $s1 corresponds to p! ☐ $s1 corresponds to q! lw $t1, Base Address (e.g., BA) lw $s1,0($t1) # $s1 = p •  This week in lab and homework: lw $s2,4($t1) # $s2 = q –  Lab #3 EC2 Loop: lb $t2,0($s1) # $t2 = *p •  Note: labs graded on a scale of 0 to 2 sb $t2,0($s2) # *q = $t2 addi $s1,$s1,1 # p = p + 1 –  HW #3 Posted addi $s2,$s2,1 # q = q + 1 •  Note: HWs graded on a scale from 0 to 3 beq $t2,$zero,Exit # if *p == 0, go to Exit –  Project #1 posted j Loop # go to Loop •  Difficulty of Project 1 part 1, Yme waiYng for Cloud CompuYng in Project 1 Part 2 decided to delay HW#3 deadline a week to Sunday Feb 11 Exit: ☐ 17 2/2/12 Spring 2012  ­ ­ Lecture #6 18 3 2/2/12 Know Your Prof: On Wrestling Team in High School & College CS61c In the News •  Apple passes HP as top PC maker when counCng iPad sales, •  •  •  •  •  By Mikey Campbell Monday, January 30, 2012 New data released on Monday by market analyst Canalys shows that Apple's iPad was instrumental to an overall growth in the client PC market for Q4 2011, as excluding the tablet's sales numbers would see the sector posCng a net loss. Record ­sewng earnings for the December quarter put Apple ahead of HP as the world's number one PC vendor, with the company's combined sales of 15.4 million iPads and 5.2 million Macs accounYng for 17 percent of the total 120 million PC shipments worldwide. Canalys notes that desktop, netbook, notebook and tablet sales grew 16 percent from the year ago quarter, however if iPads are not included as PCs, that number would drop to negaCve 0.4 percent. Apple's top performance was followed by HP, Lenovo, Dell and Acer, respecYvely. Last year was said to be "The Year of the Tablet," and Apple CEO Tim Cook quipped during the company's "most people would agree, it was the year of the iPad for the second year in a row." 2/2/12 Spring 2012  ­ ­ Lecture #6 19 2/2/12 Why Would Wrestling Help a CS Prof? (from toast at 50 year reunion) •  Increased self ­confidence –  Physical courage => later intellectual courage •  A lesson in ethics –  Coaching honor system that their wrestlers “made weight” (e.g., weighed ≤ 136 pounds) –  Wrestlers imprint coach’s ethics •  Learned how to form and lead teams –  Despite largely individual sport 2/2/12 Spring 2012  ­ ­ Lecture #6 21 Spring 2012  ­ ­ Lecture #6 20 Assembler Pseudo ­instrucYons •  Register $zero always contains 0 •  Can use “pseudo ­instrucYon” in assembly language to make it programming easier •  Example clear $rt •  Implemented as: add $rt, $zero, $zero 2/2/12 More Pseudo ­InstrucYons Spring 2012  ­ ­ Lecture #6 22 Assembler Pseudo ­instrucYons •  See h>p://en.wikipedia.org/wiki/MIPS_architecture •  Load Address (asm temp regs): $at = Label Address la $at, LabelAddr •  Implemented as: lui $at, LabelAddr[31:16]; ori $at, $at, LabelAddr[15:0] 2/2/12 Spring 2012  ­ ­ Lecture #6 23 2/2/12 Spring 2012  ­ ­ Lecture #6 24 4 2/2/12 Six Fundamental Steps in Calling a FuncYon Agenda •  •  •  •  •  •  •  1.  Put parameters in a place where funcYon can access them 2.  Transfer control to funcYon 3.  Acquire (local) storage resources needed for funcYon 4.  Perform desired task of the funcYon 5.  Put result value in a place where calling program can access it and restore any registers you used 6.  Return control to point of origin, since a funcYon can be called from several points in a program String Copy Example Administrivia FuncYons MIPS register allocaYon convenYon Technology Break Memory Heap Summary 2/2/12 Spring 2012  ­ ­ Lecture #6 25 2/2/12 •  Registers way faster than memory, so use registers •  $a0–$a3: four argument registers to pass parameters •  $v0–$v1: two value registers to return values •  $ra: one return address register to return to the point of origin •  (7 + $zero +$at of 32, 23 le}!) Spring 2012  ­ ­ Lecture #6 •  $t0 ­$t9: 10 x temporaries (intermediates) •  $s0 ­$s7: 8 x “saved” temporaries (program variables) •  18 registers •  32 – (18 + 9) = 5 le} 27 2/2/12 MIPS FuncYon Call InstrucYons –  “link” means form an address or link that points to calling site to allow funcYon to return to proper address –  Jumps to address and simultaneously saves the address of following instrucYon in register $ra jal ProcedureAddress •  Return from funcYon: jump register instrucYon (jr) –  UncondiYonal jump to address specified in register 28 •  Calling program (caller) puts parameters into registers $a0-$a3 and uses jal X to invoke X (callee) •  Must have register in computer with address of currently execuYng instrucYon –  Instead of InstrucYon Address Register (be>er name), historically called Program Counter (PC) –  It’s a program’s counter; it doesn’t count programs! •  jr $ra puts address inside $ra into PC •  What value does jal X place into $ra? ???? jr $ra Spring 2012  ­ ­ Lecture #6 Spring 2012  ­ ­ Lecture #6 Notes on FuncYons •  Invoke funcYon: jump and link instrucYon (jal) 2/2/12 26 MIPS Registers Assembly Language ConvenYons MIPS FuncYon Call ConvenYons 2/2/12 Spring 2012  ­ ­ Lecture #6 29 2/2/12 Spring 2012  ­ ­ Lecture #6 Student Roule>e? 30 5 2/2/12 Where Save Old Registers Values to Restore Them A}er FuncYon Call Example int leaf_example (int g, int h, int i, int j) { int f; f = (g + h) – (i + j); return f; } •  Parameter variables g, h, i, and j in argument registers $a0, $a1, $a2, and $a3, and f in $s0 •  Assume need one temporary register $t0 •  Need a place to place old values before call funcYon, restore them when return, and delete •  Ideal is stack: last ­in ­first ­out queue (e.g., stack of plates) –  Push: placing data onto stack –  Pop: removing data from stack •  Stack in memory, so need register to point to it •  $sp is the stack pointer in MIPS •  ConvenYon is grow from high to low addresses –  Push decrements $sp, Pop increments $sp •  (28 out of 32, 4 le}!) 2/2/12 Spring 2012  ­ ­ Lecture #6 32 2/2/12 leaf_example: •  Need to save old values of $s0 and $t0 Contents of $t0 Contents of $s0 Spring 2012  ­ ­ Lecture #6 34 2/2/12 MIPS Code for leaf_example leaf_example: addi $sp,$sp,-8 2/2/12 Student Roule>e? # adjust stack for 2 int items # save $t0 for use a}erwards # save $s0 for use a}erwards # f = g + h # t0 = i + j # return value (g + h) – (i + j) # restore $s0 for caller # restore $t0 for caller # delete 2 items from stack # jump back to calling rouYne Spring 2012  ­ ­ Lecture #6 Student Roule>e? 35 MIPS Code for leaf_example # adjust stack for 2 int items # save $t0 for use a}erwards # save $s0 for use a}erwards # f = g + h # $t0 = i + j # return value (g + h) – (i + j) # restore register $s0 for caller # restore register $t0 for caller # adjust stack to delete 2 items # jump back to calling rouYne Spring 2012  ­ ­ Lecture #6 33 MIPS Code for leaf_example Stack Before, During, A}er FuncYon 2/2/12 Spring 2012  ­ ­ Lecture #6 36 leaf_example: addi $sp,$sp,-8 sw $t0, 4($sp) 2/2/12 # adjust stack for 2 int items # save $t0 for use a}erwards # save $s0 for use a}erwards # f = g + h # $t0 = i + j # return value (g + h) – (i + j) # restore register $s0 for caller # restore register $t0 for caller # adjust stack to delete 2 items # jump back to calling rouYne Spring 2012  ­ ­ Lecture #6 Student Roule>e? 37 6 2/2/12 MIPS Code for leaf_example leaf_example: addi $sp,$sp,-8 sw $t0, 4($sp) sw $s0, 0($sp) 2/2/12 MIPS Code for leaf_example # adjust stack for 2 int items # save $t0 for use a}erwards # save $s0 for use a}erwards # f = g + h # $t0 = i + j # return value (g + h) – (i + j) # restore register $s0 for caller # restore register $t0 for caller # adjust stack to delete 2 items # jump back to calling rouYne Spring 2012  ­ ­ Lecture #6 Student Roule>e? 38 leaf_example: addi $sp,$sp,-8 sw $t0, 4($sp) sw $s0, 0($sp) add $s0,$a0,$a1 2/2/12 MIPS Code for leaf_example leaf_example: addi $sp,$sp,-8 sw $t0, 4($sp) sw $s0, 0($sp) add $s0,$a0,$a1 add $t0,$a2,$a3 2/2/12 Student Roule>e? 2/2/12 40 Student Roule>e? 39 2/2/12 # adjust stack for 2 int items # save $t0 for use a}erwards # save $s0 for use a}erwards # f = g + h # $t0 = i + j # return value (g + h) – (i + j) # restore register $s0 for caller # restore register $t0 for caller # adjust stack to delete 2 items # jump back to calling rouYne Spring 2012  ­ ­ Lecture #6 Student Roule>e? 41 MIPS Code for leaf_example # adjust stack for 2 int items # save $t0 for use a}erwards # save $s0 for use a}erwards # f = g + h # t0 = i + j # return value (g + h) – (i + j) # restore register $s0 for caller # restore register $t0 for caller # adjust stack to delete 2 items # jump back to calling rouYne Spring 2012  ­ ­ Lecture #6 Student Roule>e? leaf_example: addi $sp,$sp,-8 sw $t0, 4($sp) sw $s0, 0($sp) add $s0,$a0,$a1 add $t0,$a2,$a3 sub $v0,$s0,$t0 MIPS Code for leaf_example leaf_example: addi $sp,$sp,-8 sw $t0, 4($sp) sw $s0, 0($sp) add $s0,$a0,$a1 add $t0,$a2,$a3 sub $v0,$s0,$t0 lw $s0, 0($sp) Spring 2012  ­ ­ Lecture #6 MIPS Code for leaf_example # adjust stack for 2 int items # save $t0 for use a}erwards # save $s0 for use a}erwards # f = g + h # $t0 = i + j # return value (g + h) – (i + j) # restore register $s0 for caller # restore register $t0 for caller # adjust stack to delete 2 items # jump back to calling rouYne Spring 2012  ­ ­ Lecture #6 # adjust stack for 2 int items # save $t0 for use a}erwards # save $s0 for use a}erwards # f = g + h # $t0 = i + j # return value (g + h) – (i + j) # restore register $s0 for caller # restore register $t0 for caller # adjust stack to delete 2 items # jump back to calling rouYne 42 leaf_example: addi $sp,$sp,-8 sw $t0, 4($sp) sw $s0, 0($sp) add $s0,$a0,$a1 add $t0,$a2,$a3 sub $v0,$s0,$t0 lw $s0, 0($sp) lw $t0, 4($sp) 2/2/12 # adjust stack for 2 int items # save $t0 for use a}erwards # save $s0 for use a}erwards # f = g + h # $t0 = i + j # return value (g + h) – (i + j) # restore register $s0 for caller # restore register $t0 for caller # adjust stack to delete 2 items # jump back to calling rouYne Spring 2012  ­ ­ Lecture #6 Student Roule>e? 43 7 2/2/12 MIPS Code for leaf_example leaf_example: addi $sp,$sp,-8 sw $t0, 4($sp) sw $s0, 0($sp) add $s0,$a0,$a1 add $t0,$a2,$a3 sub $v0,$s0,$t0 lw $s0, 0($sp) lw $t0, 4($sp) addi $sp,$sp,8 2/2/12 MIPS Code for leaf_example # adjust stack for 2 int items # save $t0 for use a}erwards # save $s0 for use a}erwards # f = g + h # $t0 = i + j # return value (g + h) – (i + j) # restore register $s0 for caller # restore register $t0 for caller # adjust stack to delete 2 items # jump back to calling rouYne Spring 2012  ­ ­ Lecture #6 Student Roule>e? 44 Print  ­4 ☐ Print 4 ☐ a.out will crash! ☐ 2/2/12 # adjust stack for 2 int items # save $t0 for use a}erwards # save $s0 for use a}erwards # f = g + h # $t0 = i + j # return value (g + h) – (i + j) # restore register $s0 for caller # restore register $t0 for caller # adjust stack to delete 2 items # jump back to calling rouYne Spring 2012  ­ ­ Lecture #6 45 What If a FuncYon Calls a FuncYon? Recursive FuncYon Calls? What will the prin‚ output? ☐ leaf_example: addi $sp,$sp,-8 sw $t0, 4($sp) sw $s0, 0($sp) add $s0,$a0,$a1 add $t0,$a2,$a3 sub $v0,$s0,$t0 lw $s0, 0($sp) lw $t0, 4($sp) addi $sp,$sp,8 jr $ra static int *p; int leaf (int g, int h, int i, int j) { int f; p = &f; f = (g + h) – (i + j); return f; } int main(void) { int x; x = leaf(1,2,3,4); x = leaf(3,4,1,2); … printf(”%d\n”,*p); } •  Would clobber values in $a0 to $a1 and $ra •  What is the soluYon? 46 2/2/12 AllocaYng Space on Stack Spring 2012  ­ ­ Lecture #6 Student Roule>e? 47 Stack Before, During, A}er Call •  C has two storage classes: automaYc and staYc –  Automa>c variables are local to funcYon and discarded when funcYon exits. –  Sta>c variables exist across exits from and entries to procedures •  Use stack for automaYc (local) variables that don’t fit in registers •  Procedure frame or ac>va>on record: segment of stack with saved registers and local variables •  Some MIPS compilers use a frame pointer ($fp) to point to first word of frame •  (29 of 32, 3 le}!) 2/2/12 Spring 2012  ­ ­ Lecture #6 48 2/2/12 Spring 2012  ­ ­ Lecture #6 49 8 2/2/12 Recursive FuncYon Factorial Recursive FuncYon Factorial int fact (int n) { if (n < 1) return (1); else return (n * fact(n-1)); } Fact: L1: # adjust stack for 2 items # Else part (n >= 1) addi $sp,$sp,-8 # arg. gets (n – 1) # save return address addi $a0,$a0,-1 # call fact with (n – 1) sw $ra, 4($sp) # save argument n jal fact sw $a0, 0($sp) # return from jal: restore n # test for n < 1 lw $a0, 0($sp) slti $t0,$a0,1 # restore return address # if n >= 1, go to L1 lw $ra, 4($sp) # adjust sp to pop 2 items beq $t0,$zero,L1 # Then part (n==1) return 1 addi $sp, $sp,8 # return n * fact (n – 1) addi $v0,$zero,1 mul $v0,$a0,$v0 # pop 2 items off stack # return to the caller addi $sp,$sp,8 jr $ra # return to caller jr $ra mul is a pseudo instruc>on 2/2/12 Spring 2012  ­ ­ Lecture #6 50 2/2/12 OpYmized FuncYon ConvenYon –  Caller can rely on values being unchanged –  $ra, $sp, $gp, $fp, “saved registers” $s0 ­ $s7 –  Caller cannot rely on values being unchanged –  Return value registers $v0,$v1, Argument registers $a0 ­$a3, “temporary registers” $t0 ­$t9 52 MIPS Memory AllocaYon 2/2/12 Spring 2012  ­ ­ Lecture #6 •  MIPS convenYon •  Stack starts in high memory and grows down –  Hexadecimal (base 16) : 7fff fffchex •  MIPS programs (text segment) in low end –  0040 0000hex •  sta>c data segment (constants and other staYc variables) above text for staYc variables –  MIPS convenYon global pointer ($gp) points to staYc –  (30 of 32, 2 le}! – will see when talk about OS) 2.  Not preserved across funcYon call Spring 2012  ­ ­ Lecture #6 51 Where is the Stack in Memory? To reduce expensive loads and stores from spilling and restoring registers, MIPS divides registers into two categories: 1.  Preserved across funcYon call 2/2/12 Spring 2012  ­ ­ Lecture #6 •  Heap above staYc for data structures that grow and shrink ; grows up to high addresses 2/2/12 Spring 2012  ­ ­ Lecture #6 53 Register AllocaYon and Numbering 54 2/2/12 Spring 2012  ­ ­ Lecture #6 55 9 2/2/12 And in Conclusion, … Which statement is FALSE? •  C is funcYon oriented; code reuse via funcYons ☐ ☐ ☐ –  Jump and link (jal) invokes, jump register (jr $ra) returns –  Registers $a0-$a3 for arguments, $v0-$v1 for return values MIPS uses jal to invoke a funcYon and jr to return from a funcYon jal saves PC+1 in %ra! •  Stack for spilling registers, nested funcYon calls, C local (automaYc) variables The callee can use temporary registers (%ti) without saving and restoring them! ☐ 56 2/2/12 Spring 2012  ­ ­ Lecture #6 57 10 ...
View Full Document

Ask a homework question - tutors are online