functions and stacks

Functions and Stacks Lecture 7 CAP 3103 06-09-2014 2.3 Operands - PowerPoint PPT Presentation

Functions and Stacks Lecture 7 CAP 3103 06-09-2014 2.3 Operands of the Computer Hardware Register Operands Arithmetic instructions use register operands MIPS has a 32 32-bit register file Use for frequently accessed data


  1. Functions and Stacks Lecture 7 CAP 3103 06-09-2014

  2. §2.3 Operands of the Computer Hardware Register Operands  Arithmetic instructions use register operands  MIPS has a 32 × 32-bit register file  Use for frequently accessed data  Numbered 0 to 31  32- bit data called a “word”  Assembler names  $t0, $t1, …, $t9 for temporary values  $s0, $s1, …, $s7 for saved variables  Design Principle 2: Smaller is faster  c.f. main memory: millions of locations Chapter 2 — Instructions: Language of the Computer — 2

  3. Register Operand Example  C code: f = (g + h) - (i + j);  f, …, j in $s0, …, $s4  Compiled MIPS code: add $t0, $s1, $s2 add $t1, $s3, $s4 sub $s0, $t0, $t1 Chapter 2 — Instructions: Language of the Computer — 3

  4. Memory Operands  Main memory used for composite data  Arrays, structures, dynamic data  To apply arithmetic operations  Load values from memory into registers  Store result from register to memory  Memory is byte addressed  Each address identifies an 8-bit byte  Words are aligned in memory  Address must be a multiple of 4  MIPS is Big Endian  Most-significant byte at least address of a word  c.f. Little Endian: least-significant byte at least address Chapter 2 — Instructions: Language of the Computer — 4

  5. Memory Operand Example 1  C code: g = h + A[8];  g in $s1, h in $s2, base address of A in $s3  Compiled MIPS code:  Index 8 requires offset of 32  4 bytes per word lw $t0, 32($s3) # load word add $s1, $s2, $t0 offset base register Chapter 2 — Instructions: Language of the Computer — 5

  6. MIPS R-format Instructions op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits  Instruction fields  op: operation code (opcode)  rs: first source register number  rt: second source register number  rd: destination register number  shamt: shift amount (00000 for now)  funct: function code (extends opcode) Chapter 2 — Instructions: Language of the Computer — 6

  7. MIPS I-format Instructions op rs rt constant or address 6 bits 5 bits 5 bits 16 bits  Immediate arithmetic and load/store instructions  rt: destination or source register number  Constant: – 2 15 to +2 15 – 1  Address: offset added to base address in rs Chapter 2 — Instructions: Language of the Computer — 7

  8. MIPS J-format Instructions  Jump ( j and jal ) targets could be anywhere in text segment  Encode full address in instruction op address 26 bits 6 bits  (Pseudo)Direct jump addressing  Target address = PC 31…28 : (address × 4) Chapter 2 — Instructions: Language of the Computer — 8

  9. Addressing Mode Summary Chapter 2 — Instructions: Language of the Computer — 9

  10. §2.13 A C Sort Example to Put It All Together C Sort Example  Illustrates use of assembly instructions for a C bubble sort function  Swap procedure (leaf) void swap(int v[], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; }  v in $a0, k in $a1, temp in $t0 Chapter 2 — Instructions: Language of the Computer — 10

  11. The Procedure Swap swap: sll $t1, $a1, 2 # $t1 = k * 4 add $t1, $a0, $t1 # $t1 = v+(k*4) # (address of v[k]) lw $t0, 0($t1) # $t0 (temp) = v[k] lw $t2, 4($t1) # $t2 = v[k+1] sw $t2, 0($t1) # v[k] = $t2 (v[k+1]) sw $t0, 4($t1) # v[k+1] = $t0 (temp) jr $ra # return to calling routine Chapter 2 — Instructions: Language of the Computer — 11

  12. Cfunctions main() { int i,j,k,m; What information must ... compiler/programmer i = mult(j,k); ... keep track of? m = mult(i,i); ... } /* really dumb mult function */ int mult (int mcand, int mlier){ int product = 0; while (mlier > 0) { What instructions can product = product + mcand; accomplish this? mlier = mlier -1; } return product; } Dr Dan Garcia

  13. FunctionCall Bookkeeping   Registers play a major role in keeping track of information for function calls.   Register conventions: $ra  Return address $a0, $a1, $a2, $a3  Arguments $v0, $v1  Return value $s0, $s1, … , $s7  Local variables   Thestack is also used; more later. Dr Dan Garcia

  14. InstructionSupport for Functions(1/6) ... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { C return x+y; } address (shown in decimal) 1000 M In MIPS, all instructions are 4 1004 I 1008 bytes, and stored in memory 1012 P just like data. So here we 1016 show the addresses of where S … the programs are stored. 2000 2004 Dr Dan Garcia

  15. InstructionSupport for Functions(2/6) ... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { C return x+y; } address (shown in decimal) 1000 add $a0,$s0,$zero # x = a M 1004 add $a1,$s1,$zero # y = b I 1008 addi $ra,$zero,1016 #$ra=1016 1012 j sum #jump to sum P 1016 S … 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction Dr Dan Garcia

  16. InstructionSupport for Functions(3/6) ... sum(a,b);... /* a,b:$s0,$s1 */ } int sum(int x, int y) { C return x+y; } • Question: Why use jr here? Why not use j ? M • Answer: sum might be called by many places, so we can’t return to a fixed place. The calling proc to sum must be I able to say “return here” somehow. P S 2000 sum: add $v0,$a0,$a1 2004 jr $ra # new instruction Dr Dan Garcia

  17. InstructionSupport for Functions(4/6)   Single instruction to jump and save return address: jump and link ( jal )   Before: 1008 addi $ra,$zero,1016 #$ra=1016 1012 j sum #goto sum   After: 1008 jal sum # $ra=1012,goto sum   Why have a jal ?   Make the common case fast: function calls very common.  Don’t have to know where code is in memory with jal !  Dr Dan Garcia

  18. InstructionSupport for Functions(5/6)   Syntax for jal (jump and link) is same as for j (jump): jal label   j a l should really be called laj for “link and jump”:  Step 1(link): Save address of next instruction into $ra    Why next instruction? Why not current one?   Step 2 (jump): Jump to the given label Dr Dan Garcia

  19. InstructionSupport for Functions(6/6)   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.   V ery useful for function calls:  j a l stores return address in register ($ra )   j r $ r a jumps back to that address  Dr Dan Garcia

  20. Nested Procedures(1/2) int sumSquare(int x, int y) { return mult (x,x)+ y; }   Something called sumSquare , now sumSquare is calling mult .  Sothere ’ 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 . Dr Dan Garcia

  21. Nested Procedures(2/2)   In general, may need to save some other info in addition to $ra .   When a Cprogram is run, there are 3 important memory areas allocated:   Static: V ariables declared once per program, cease to exist only after execution completes. E.g., C globals ariables declared dynamically via malloc   Heap: V   Stack: Space to be used by procedure during execution; this is where we can save register values Dr Dan Garcia

  22. CMemory Allocation Address ∞ Space for local vars, saved Stack procedure information $sp stack pointer Explicitly created space, Heap i.e., malloc() V ariables declared once per Static program; e.g., globals (doesn’t change size) Program (doesn’t change size) Code 0 Dr Dan Garcia

  23. Stack stack main () Stack { proc_1(1); grows } Stack Pointer down void proc_1 (int a) { proc_2(2); } void proc_2(int b) { proc_3(3); } void proc_3 (int c) {}

  24. Usingthe Stack (1/2)   Sowe have a register $sp which always points to the last used space in the stack.   T o 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; } Dr Dan Garcia

  25. Usingthe Stack (2/2)   Hand-compile int sumSquare(int x, int y) { return mult(x,x)+ y; } sumSquare: addi $sp,$sp,-8 # space on stack sw $ra, 4($sp) # save ret addr “push” 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 “pop” # get ret addr lw $ra, 4($sp) # restore stack addi $sp,$sp,8 jr $ra ... mult: Dr Dan Garcia

  26. The Sort Procedure in C  Non-leaf (calls swap) void sort (int v[], int k) { int i, j; for (i = 0; i < k; i += 1) { for (j = i – 1; j >= 0 && v[j] > v[j + 1]; j -= 1) { swap(v,j); } } }  v in $a0, k in $a1, i in $s0, j in $s1 Chapter 2 — Instructions: Language of the Computer — 26

Recommend


More recommend