CS/COE CS447: Lab 3 Your Name:_________________________________ Your Partner's Name:_______________________ To get credit on this lab, attend recitation, work with your partner to answer the questions, and give "reasonable" (if not all correct) answers. Each of you should hand in a hardcopy of your own solution at the beginning of your next lab session. On this lab, you will work with your partner to try to figure out some assembly language programs yourselves. Don't worry if you can't figure everything out completely. Get an idea as best you can of how things work. By trying to figure this code out by yourselves, early in the course, you'll get more out of the lectures when we cover similar code. Also, for those who haven't programmed at this "low" level before, the sooner you dive in, the better! *******PROGRAM 1: http://www.cs.pitt.edu/~wiebe/courses/CS447/Sp10/lab3program1.asm .data # sample0.asm .word c: 3 k: 5 .text la $t0,c # address of c la $t1,k # address of k lw $s0,0($t0) # load the contents of c lw $s1,0($t1) # load the contents of k slt $s3,$s0,$s1 # if $s0 < $s1 then $s3 = 1; else $s3 = 0 beq $s3,$0,notless #if $s3 == 0: go to notless; o/w just go to # the next instruction sw $s0,0($t1) #store contents of c into k sw $s1,0($t0) #store the contents of k into c notless: addi $v0,$0,5 # just to do something here #So, what did we do? If c < k then we swapped their values. #If not, we just left them alone. ***Question: Check the symbol table (Go to Settings, show symbols). What is "c" a label for? What is "k" a label for? What is "notless" a label for? ***Question: In memory, where is the 5 stored? (What numerical address?) ***Question: Step through the two "la" instructions in Mars. Now, what do $t0 and $t1 contain? ***Question: Step through the two lw instructions. Now, what do $s0 and $s1 contain? (Make sure you understand why!) ***Question: Step through the slt instruction. Is $s3 set to 1 or reset to 0? Why? ***Question: Step through the rest of the program. Are the sw instructions executed? If so, exactly which values are stored where by these instructions? ***Change the .data section of the program so that c contains 5 and k contains 3, and step through the program through the slt instruction. (Nothing to write down here.) ***Question: what does the program counter contain at this point? What is stored at that memory location? ***Question: Now, step through the beq instruction. What does the program counter contain at this point? What is stored at that memory location? Now, do you understand how the following are related to each other? the beq instruction the entry in the symbol table for "notless" the program counter You don't have to answer this question on your submission, but be sure you understand this. Here's how: the beq instruction decides whether control should jump to the "addi $v0,$0,5" instruction. "notless" is a label that stands for the address where the "addi $v0,$0,5" instruction is stored. By definition, the program counter contains the address of the next instruction that will be executed. That is, the computer uses the program counter to determine which instruction it should execute next. So, if the "beq" test is true, the program counter is loaded with "notless"; This makes the computer execute the "addi $v0,$0,5" instruction, since "notless" is a label that stands for its address. *******PROGRAM 2: http://www.cs.pitt.edu/~wiebe/courses/CS447/Sp10/lab3program2.asm "sll" is the instruction "shift left logical". Viewing the number as a binary number, all of the bits are shifted to the left. 0's are slotted in from the right to replace it. For example: sll $t1,$s3,2 If $s3 contains 11001100111100001111010101010101 then the above instruction places the following into $t1 00110011110000111101010101010100 [the top 2 bits dropped off the left, and 2 0s replace them on the right] Note that shifting a binary number left by 2 multiplies the number by 4. In general, shifting left by i multiples the number by 2^i. For example: 00000001 == 1 decimal 00000100 == 4 decimal (shift left by 2) 00010000 == 16 decimal (shift left by 4) Now we'll work with an array of 1-word unsigned integers, and also use a loop. # i = 1 # k = 5 # while (save[i] == k) # i += 1 # .data save: .word 5,5,5,5,7,6,6,7 # The array is called 'save'. So, e.g., save[0] is 5, save[4] is 7 # and save[5] is 6. Each is one word, because we used the ".word" # assembler directive. #***Question: before looking at the assembly language, what should "i" # #be after the while loop is executed?______________ .text li $s3,1 # $s3 is i la $s6,save # $s6 is the base address of array li $s5,5 # $s5 is k loop: sll $t1,$s3,2 #shift the value left by 2; this multiplies the #value by 4. So, $t1 now contains i * 4 add $t1,$t1,$s6 #Now $t1 points to (contains the address of) # save[i]. (We needed to mulitply by 4 since each # element is 4 bytes long.) lw $t0,0($t1) #$t0 = save[i]. bne $t0,$s5,exitloop # if $t0 and $s5 are not equal, jump to exitloop addi $s3,$s3,1 # i += 1 j loop exitloop: add $s6,$s3,$zero # just to have an instruction here Before running the code, try to figure out what values are assigned to which registers during each iteration. ***Question: Fill in this table: $t1 $t0 $s3 iteration 1, just before "j loop" iteration 2, just before "j loop" ***Question: Now, run the program, trace through it, and check your answers (You can add a separate sheet of paper if you want to.) *******PROGRAM 3: http://www.cs.pitt.edu/~wiebe/courses/CS447/Sp10/lab3program3.asm This program uses a function call. All you need to know at this point is that: "jal maximum" is the function call; it jumps to the code at label "maximum". Then, "jr $ra" jumps back to the instruction just after the function call (in this case, the "j finish"). We'll see how this is actually accomplished later in the course. Note that the array this time is stored after the code. This is just to show you that this is possible. # Finds the maximum number in an array of 8 non-negative 1-word integers and stores it in $a2 # # index = 0 # maxSoFar = 0 # while not (index == number of elements in the array): # if array1[index] > maxSoFar: # maxSoFar = array1[index] # index = index + 1 # .text main: la $a0, array1 # $a0 pointer to the array addi $a1, $zero, 8 # $a1 = number of elements in the array jal maximum # "maximum" function call j finish ## maximum function. The arguments are passed in $a0 and $a1 # $a0 contains a pointer to the array (the address of the first element) # $a1 contains the number of elements in the array # Remember, each element is 4 bytes long, so the addresses of the # array elements are 4 bytes apart maximum: addi $t7, $zero, 0 # $t7 index = 0 addi $t2, $zero, 0 # $t2 maxSoFar = 0 maximum_loop: # if index == number of elements in the array, exit loop beq $t7, $a1, maximum_finish lw $t3, 0($a0) # $t3 = array1[index] slt $t1, $t3, $t2 # if array1[index] < maxSoFar then $t1 = 1 # o/w, $t1 = 0 bne $t1, $zero, notmore # if $t1 is not equal to $zero, then jump to notmore # o/w ($t1 is 0), stay here # Basically, you stay here if the # above test was false --- # array1[index] >= maxSoFar. So, we # update maxSoFar addi $t2, $t3, 0 # maxSoFar = array1[index] notmore: addi $t7, $t7, 1 # index = index + 1 addi $a0,$a0, 4 # point to the next element of the array j maximum_loop maximum_finish: jr $ra finish: addi $a2, $t2, 0 # $a2 now has maximum element # Now we'll set up the array # .data tells the assembler we are storing things in the data segment # of memory. As we know, that will store them starting at 0x10010000. # But because we use the label "array1", we don't have to use the # actual number in our assembly code. .data array1: .word 3,5,7,9,10,8,6,4 ***Question: Before running the code, trace through it. Write down the registers, and fill in values, crossing out old values and writing in new ones as the program goes on. Figure out a format that works for you, so you don't confuse yourself. (You can add a separate sheet of paper if you want to.) ***Then, run the code and check your answers. (nothing to write down here) ==== Note that Part 1 of Programming Assignment 1 is a modification of program 3 above. The next lab will cover topics that will be helpful for Part 2 of Programming Assignment 1.