thursday 1 october 2015
play

Thursday, 1 October 2015 Exam one week from today (See Web site for - PowerPoint PPT Presentation

Thursday, 1 October 2015 Exam one week from today (See Web site for review exercises) Next Tuesday: review for exam Today: Short intro to pointers in C Values vs. addresses; lw vs. la More on functions in MIPS: the stack Keep reading P&H


  1. Thursday, 1 October 2015 Exam one week from today (See Web site for review exercises) Next Tuesday: review for exam Today: Short intro to pointers in C Values vs. addresses; lw vs. la More on functions in MIPS: the stack Keep reading P&H 2.8; begin K&R ch. 5

  2. Pointers in C Sample C program: OUTPUT: $ gcc pt.c #include <stdio.h> $ ./a.out int main() { Value of i = 30 int i = 20; Why?? int *j = &i; *j = 30; printf(“Value of i = %d\n”,i); }

  3. Pointers in C This means that j is a pointer variable (more #include <stdio.h> specifically, a “pointer to int main() { an int”) int i = 20; This means “address of int *j = &i; variable i” *j = 30; printf(“Value of i = %d\n”,i); } This means “put 30 into address pointed at by j”

  4. Pointers in MIPS .data .align 2 i: .word 20 Load the ADDRESS of i j: .space 4 .text Load the CONSTANT 30 la $t0,i sw $t0,j li $t0,30 Load the CONTENTS of j lw $t1,j sw $t0,0($t1) Offset/base notation (p. 69)

  5. Pointers in MIPS The example on the previous slide is saved as program “pointers.asm” in the “oct1” folder of the shared repository.

  6. Back to Functions We introduced pointers so that we could explain the behavior of functions in C. Problem: write a function with two int arguments, a and b. The function’s job is to swap the values of a and b. Here’s the obvious (and wrong) solution:

  7. Back to Functions void swap(int a, int b) { int temp = a; It doesn’t work! If you try this: a = b; int i = 10; b = temp; int j = 20; } swap(i,j); printf(“%d %d\n”, i,j); you get “ 10 20 ” for output.

  8. Back to Functions Memory: void swap(int a, int b) { a: 10 int temp = a; 20 b: a = b; C uses “pass by value” in b = temp; function calls } 20 i: 10 ... j: 10 temp: int i = 10,j = 20; swap(i,j); The swap is performed on a COPY!

  9. Back to Functions void swap(int * i, int * j) { int temp = *i; Use pointers to integers, *i = *j; rather than integers *j = temp; } (See program “swap.asm” in oct1 folder of shared repository) ... int i = 10, j = 20; Send the addresses of i and swap(&i, &j); j, rather than their values

  10. Now...let’s write “swap” in MIPS! Desired behavior: ... a: .space 4 (or .word ...value... ) b: .space 4 (or .word ...value... ) ... la $a0,a # first argument reg. la $a1,b # second argument reg. jal swap # contents of locations a, b are now swapped

  11. “Swap” in MIPS! Desired Behavior ... Compare to C: a: .space 4 b: .space 4 swap(&a, &b); ... la $a0,a la $a1,b Load address (“ la ”) jal swap rather than “ lw ”

  12. Before We Go On... A few words about “register conventions.” In a MIPS program, you can use (almost) any register you want in a calculation– $t0 , $t1 , …, $a0 , $a1 , …, $v0 , $v1 , $s0 , $s1 , … . BUT there are certain agreed-upon conventions that MIPS programmers use, both to make programs easier to understand and to prevent errors. See Figure 2.14, page 105 in P&H

  13. Register Conventions Page 105 in P&H The “ a ”-registers: a rgument registers When calling a function, use the $a0 , $a1 , $a2 , and $a3 registers to hold (up to 4) arguments to the function. (If you have more than 4 arguments, use other registers, but always start with the “ a ” registers….). NOTE: when we make syscalls for printing an int or printing a string, we must put things in $a0 .

  14. Register Conventions Page 105 in P&H The “ s ”-registers: s aved-value registers When using registers to hold important program values that will be needed at multiple places in the program (data values, loop indices, and other things you might declare in a C program), use $s0 , $s1 , …, $s7 . So, for instance, a payroll program might have variables like “hrs_worked”, “pay_rate”, etc. These might be saved in s -registers.

  15. Register Conventions Page 105 in P&H The “ v ”-registers: return- v alue registers Functions that return values should place the return value in register $v0 . (Register $v1 is also sometimes needed to return values, but usually only $v0 is required.) NOTE: for syscall s, $v0 is also used to identify which syscall operation is being used: print_int or print_str or read_int or …).

  16. Register Conventions Page 105 in P&H The “ t ”-registers: t emporary registers Any value that is needed only “for a moment,” like an intermediate result of a complicated calculation or a place to store the result of a “ slt ” comparison test, can go into one of the t - registers. Unlike s -registers, t -registers are assumed to hold values that don’t need to be saved beyond their brief, momentary use.

  17. Doing a Swap in MIPS Assume that the addresses of a and b , the locations to be swapped are already loaded into $a0 and $a0 . Here’s the swap: lw $t0,0($a0) # Load contents of a into t0 lw $t1,0($a1) # Load contents of b into t1 sw $t0,0($a1) # swap: copy t0 into b sw $t1,0($a0) # swap: copy t1 into a Offset/base notation (p. 69)

  18. Doing a Swap in MIPS Here’s our first approximation to a function in MIPS that does a swap–we just add “ jr $ra ”: swap : lw $t0,0($a0) # Load contents of a into t0 lw $t1,0($a1) # Load contents of b into t1 sw $t0,0($a1) # swap: copy t0 into b sw $t1,0($a0) # swap: copy t1 into a jr $ra # return to calling program

  19. A “Safer” Version of Swap Suppose we aren’t very good at respecting the register conventions and we’re worried that registers $t0 and $t1 might contain important values that we don’t want to lose. Or suppose we are writing the swap function for some other programmer who may not follow the register conventions and who needs the values in $t0 and $t1 to be preserved. We want to play it safe and “back up” the values of $t0 and $t1 .

  20. The Stack Special area in memory for holding temporary values. The stack grows “down” from higher memory locations to lower ones.

  21. How to Use the Stack (pp. 96-104) The stack operations of “push” and “pop” are achieved by changing the value of the register $sp (the “ s tack p ointer” register). This contains an address in memory (see previous slide). To push a certain number of bytes, we reduce the value of $sp by that number, then fill in the values of the new bytes.

  22. How to Use the Stack (pp. 96-104) Example: addi $sp,$sp,-8 # grow stack by 8 bytes sw $t0,0($sp) # put something on stack sw $t1,4($sp) # put something on stack 3456012 stack stuff $sp = 3456012 3456012 stack stuff 3456011 3456011 contents 3456010 3456010 of register 3456009 3456009 addi 3456008 3456008 t1 $sp = 3456008 $sp,$sp,-8 3456007 3456007 contents 4($sp) 3456006 3456006 of 3456005 3456005 register t0 3456004 3456004 $sp = 3456004 0($sp) 3456003 3456003

  23. How to Use the Stack (pp. 96-104) To pop a certain number of bytes off the stack, we just copy those bytes somewhere else and then add that number of bytes to $sp . 3456012 stack stuff 3456012 stack stuff lw $t0,0($sp) $sp = 3456012 3456011 3456011 contents lw $t1,4($sp) 3456010 3456010 of register 3456009 3456009 3456008 t1 3456008 addi $sp,$sp,8 3456007 3456007 contents 3456006 3456006 of 3456005 register 3456005 t0 3456004 $sp = 3456004 3456004 0($sp) 3456003 3456003

  24. How to Use the Stack (pp. 96-104) The function should do all the work. So … if we want to perform swap, but preserve registers $t0 and $t1 so we don’t accidentally overwrite important information, we must: ■ grow the stack ■ back up values of $t0 , $t1 onto stack ■ use $t0 , $t1 in our function ■ restore values from stack back into $t0 , $t1 ■ shrink the stack ■ return

  25. Swap Function in MIPS swap: Refer back to previous slides!! addi $sp,$sp,-8 # Reserve space on stack sw $t0,0($sp) # Save current value of t0 on stack sw $t1,4($sp) # Save current value of t1 on stack lw $t0,0($a0) # Load first value into t0 lw $t1,0($a1) # Load second value into t1 sw $t0,0($a1) # swap: copy t0 into second address sw $t1,0($a0) # swap: copy t1 into first address lw $t0,0($sp) # Restore old t0 from stack lw $t1,4($sp) # Restore old t1 from stack addi $sp,$sp,8 # Shrink stack to original size jr $ra # return

  26. MIPS Functions in General If you follow register conventions, no need to preserve “temporary” registers as we just did. But still need to preserve things like s-registers if they are used by the function. One other thing may need to be preserved: the return address register (next time).

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend