CS/COE 447: Spring 2010 Lab 7 YOUR NAME: YOUR PARTNER'S NAME: -------------------------------------------------------------------- The MIPS translation of the C segment while (save[i] == k) i += 1; below uses both a conditional branch and an unconditional jump each time through the loop. Only poor compilers would produce code with this loop overhead. Rewrite the assembly code so that it uses at most one branch or jump each time through the loop. Also, if you initialize $t1 to the address of array[i] before the loop, you can make your code even more efficient. Make that change as well. Assuming the number of iterations of the loop is 10, how many instructions are executed by the original version, and how many instructions are executed by your new version? Original Code: .data array: .word 3,4,5,5,5,5,5,5,5,5,5,5,6,0,1,2,8 .text li $s3,2 # $s3 = i = 2 (arbitrary value) li $s5,5 # $s5 = k = 5 (arbitrary value) la $s6, array loop: sll $t1,$s3,2 add $t1,$t1,$s6 lw $t0,0($t1) bne $t0,$s5,exit addi $s3,$s3,1 j loop exit: ---------------------------------------------------------------------------- In this question, we'll look in detail at factorial.asm, to see how the stack is used to store activation records. Below is code which can be found at: http://www.cs.pitt.edu/~wiebe/courses/CS447/Sp10/factorial.asm First, we'll call the code with n == 0. So, we are starting with the base case. ################################################################ ## int factorial(int n) ## ## { ## ## if (n < 1) ## ## return 1; ## ## ## ## return n * factorial(n - 1); ## ## } ## ################################################################ .text li $a0, 0 jal factorial j exit factorial: ***Step through the code until here (stop right after calling factorial.) Question: What value does $sp contain at this point? Question: What value does $ra contain at this point? Make sure you understand why it contains this value. Ask MARS to show you the stack part of memory (there is an option where memory is displayed). Before stepping through the next 4 instructions: addi $sp, $sp, -4 # push the return address. sw $ra, 0($sp) addi $sp, $sp, -4 # push the value n. sw $a0, 0($sp) Question: What values do you think are stored where by the "sw" instructions? The answers are after some spoiler space: The answers are: Address Contents ========== ========== 0x7fffeff8 0x00400008 0x7fffeff4 0x00000005 Make sure you understand this (and what you are seeing in MARS) before going on. slti $t0, $a0, 1 beq $t0, $zero, recurse addi $v0, $zero, 1 addi $sp, $sp, 8 Question: What does $sp contain now? What values are stored on the stack (conceptually)? jr $ra ========== Now that you have watched what happens as things are pushed on and popped off of the stack, now change the code so that n ($a0) is initialized to 3, and trace through the stack as you execute the code. Don't forget to re-select "current $sp" memory option so that MARS keeps showing you the relevant parts of memory. Below are the relevant memory locations drawn vertically. Fill in values. As you pop things off the stack, use a sequence of arrows to show where $sp is pointing at each point (crossing out the previous ones.) +0 0x7fffefdc 0x7fffefe0 0x7fff0fe4 0x7fffefe8 0x7fffefec 0x7fffeff0 0x7fffeff4 0x7fffeff8 0x7fffeffc -------------------------------------------------------------------- For this question, we'll think about what the computer must do to execute various types of instructions. This looks ahead, when we will consider datapath and control. The first step for any instruction is memory access with the PC to fetch the next instruction. That is, the contents of Memory[PC] are fetched (and placed in something call the "Instruction Register"). Then, 4 is added to the PC so that PC points to the following instruction. Now, what steps does the computer need to perform to execute the following instructions? We'll be less formal now than we will be later in the course. add $t0,$t1,$t2 -- instruction fetch (IR <-- Memory[PC]) -- PC = PC + 4 finish this: lw $t0,8($t2) -- instruction fetch (IR <-- Memory[PC]) -- PC = PC + 4 finish this: beq $t0,$t1,L (assume L: is a label 10 instructions below) -- instruction fetch (IR <-- Memory[PC]) -- PC = PC + 4 finish this: j L (assume L: is a label for 0x0040005c) -- instruction fetch (IR <-- Memory[PC]) -- PC = PC + 4 finish this: