CS/COE 447: Spring 2010 Lab 8 YOUR NAME: YOUR PARTNER'S NAME: For all of these questions, please answer them on paper before running them in MARS to check your answer. Also, be sure you understand why the answer is what it is before moving on. This lab will give you experience with two's complement numbers, and will explore how the assembler decides to create the machine code it does when working with immediate values. Question 1. Consider the following code: loop: add $t0,$zero,$t0 add $t1,$zero,$t1 li $t2,0x44 bne $t5,$zero,loop What is the machine code for the BNE instruction? Question 2: Which of the following code segments will cause an overflow exception? Which results in overflow but does not cause an exception? li $t0,0x10000000 addi $t0,$t0,-1 li $t0,0x80000000 addi $t0,$t0,-1 li $t0,0x10000000 addiu $t0,$t0,-1 li $t0,0x80000000 addiu $t0,$t0,0xffffffff Question 3: What value does $t2 contain after the following code is executed? li $t2,55 li $t0,0xffffffff li $t1,-1 beq $t0,$t1,label1 addi $t2,$zero,9 label1: Question 4: The following array has been created in memory: .data nums: .word 4,0x34A45,0x1234567,-5,-53 What values do $t3 and $t4 contain after the following code is executed? la $t0,nums addi $t0,$t0,20 lhu $t3,-6($t0) lbu $t4,-11($t0) Look at the green card entry in the Operation column for lhu and lbu. For the lhu instruction: what is SignExtImm? What is R[rs]+SignExtImm? What is M[R[rs]+SignExtImm?](15:0) For the lbu instruction: what is SignExtImm? What is R[rs]+SignExtImm? What is M[R[rs]+SignExtImm?](7:0) Question 5: What do $t2 and $t3 contain after the following code is executed? li $t0,55 li $t1,0xffffffff slt $t2,$t0,$t1 sltu $t3,$t0,$t1 Question 6: .text # Recall that more than one instruction is needed to work with # immediates that are larger than 2 bytes # For positive numbers, this is the case if there is a 1 somewhere in # bits 15 through 31 (below we'll turn to why this is not bits 16 through 31): addi $t1,$zero,0x00010000 addi $t2,$zero,0x0010ffff addi $t3,$zero,0x00007fff # For negative numbers, leading 1s are like leading zeros. So, for # negative numbers, more than one instruction is needed of there is a 0 somwhere in bits 15 through # 31: addi $t4,$zero,0xffff7fff addi $t5,$zero,0xfffeffff addi $t6,$zero,0xffff8000 Run these instructions and make sure you understand why you are seeing what you are seeing in the "Basic" column. Note that 0x80000000 = 2,147,483,648 Dec 0xffff8000 2c = -32,768 Dec 0x0000ffff = 65,535 Dec 0x00007fff = 32,767 Dec Question 8: The assembler helps you out by allowing you to use 32-bit operands in your code. Its job is to figure out what two's complement 32-bit binary number corresponds to what you wrote in your source code. Then, once it knows that, it figures out what machine instructions to generate. It interprets the numbers in your source code the same, whether you are using a signed or unsigned instruction. Remember, this is an issue of how the *assembler* interprets your source code. Following are two sets of instructions that are the same except the first set uses addi and the second set uses addiu. You'll see that the assember interprets the numbers in the same way for the two sets of instructions. If you click on "Hex Values" (the default) it will show you the bit patterns (in Hex). If you click on "Decimal Values", you can see how it interpreted the numbers. Make sure you understand what you are seeing before moving on: # this is just a +1: addi $t7,$zero,0x1 # This is the positive number 0x00008000 - we'll look further at this # case below. addi $t8,$zero,0x8000 # This is all f's in hex: addi $t9,$zero,-1 # This is the smallest possible negative number in 32 bits: addi $s0,$zero,-2147483648 # is is -32,768 addi $s1,$zero,0xffff8000 addiu $t7,$zero,0x1 addiu $t8,$zero,0x8000 addiu $t9,$zero,-1 addiu $s0,$zero,-2147483648 addiu $s1,$zero,0xffff8000 Question 9: Consider this one: addi $a3,$zero,0x8000 At first, it may seem that this could be done with only one instructions, since the immediate seems to only need 16 bits. But what would happen if the assembler created this machine code instruction? opcode rs rt immediate 001000 00000 00111 1000 0000 0000 0000 0000 What value would be placed in $a3? The answer is 0xffff8000 = -32768! The value that should be loaded is a positive number: 0x00008000. Similarly, if the immediate value is 0xffff7fff, the assembler needs to use multiple instructions. If instead the assembler created one machine instruction with this immediate value: 0111 1111 1111 1111 The hardware would sign extend the 0, and turn this into a positive number. ==== So, that is why the assembler can use a single instruction only if there are no 1s in bits 15 through 31 for positive numbers, and if there are no 0s in bits 15 through 31 for negative numbers. The hardware will sign extend bit 15, turning the numbers into garbage. Again, make sure you understand this before moving on. Question 7: Notice on the green card that addiu sign extends its immediate value! That is, the hardware treats immediates of addi and addiu instructions in exactly the same way. That is why the assembler cannot use only one instruction for this source-code instruction: addiu $a3,$zero,0x8000 If it were to generate machine code with 1000 0000 0000 0000 as the immediate, the hardware would sign extend it and turn it into a negative number. Now, go back through the lab8.asm (with the instructions that causes the overflow exception removed), and review (1) why the assembler generates the machine code it does, and (2) the execution of the code. For (2), switch back and forth between Hex and Decimal values to remind yourself what you are seeing.