Brock Wilcox CS470 Homework Assignment 2 Please complete the following exercises from Chapter 2 of Patterson & Hennessy, Computer Organization & Design: The Hardware/Software Interface, Fourth Edition, Revised Printing, pages 180 ff. 1. Exercise 2.1.1 through 2.1.3 for the following C statement: f = g + (h - i) + 8 Exercise 2.1.1: For the C statements above, what is the corresponding MIPS assembly code? Use a minimal number of MIPS assembly instructions. sub f, h, I add f, g, f addi f, f, 8 Exercise 2.1.2: For the C statements above, how many MIPS assembly instructions are needed to perform the C statement? 3 Exercise 2.1.3: If the variables f, g, h, and i have values 1, 2, 3, and 4, respectively, what is the end value of f? f = 2 + (3 4) + 8 = 2-1 + 8 = 9 2. Exercise 2.2.4 and 2.2.5 for the following MIPS statements: sub f, $0, h addi g, f, 7 Exercise 2.2.4: For the MIPS assembly instructions above, what is a corresponding C statement? NOTE: I decided that $0 makes more sense g = - h + 7 Exercise 2.2.5: If the variables f, g, h, and i have values 1, 2, 3, and 4, respectively, what is the end value of f? f = 0 h = -h 3. Exercise 2.4.4 and 2.4.5, for the following MIPS statements: add $s0, $s2, $s3 add $s0, $s0, $s4
subi $s0, $s0, 7 add $s0, $s0, $s1 sub $s4, $s4, $s0 Exercise 2.4.4: For the MIPS assembly instructions above, what is a corresponding C statement? $s4 = $s4 - ($s2 + $s3 + $s4 7 + $s1) (which simplifies to) $s4 = $s4 - $s2 - $s3 - $s4 + 7 - $s1 $s4 = 7 - $s1 - $s2 - $s3 $s4 = 7 - ($s1 + $s2 + $s3) Exercise 2.4.5: For the MIPS assembly instructions above, rewrite the assembly code to minimize the number of MIPS instructions (if possible) needed to carry out the same function. add $s5, $s1, $s2 add $s5, $s5, $s3 addi $s4, $0, 7 sub $s4, $s4, $s5 What would happen if the subi instruction came first? Why? It would end up being useless. Right after that, $s0 would be overwritten with $s2 + $s3, so the subi value placed in $s0 wouldn't affect anything. The end result would be: $s4 = $s4 - ($s2 + $s3 + $s4 + $s1) 4. Exercise 2.7.1 through 2.7.3, using the following binary value: 1111 1111 1111 1111 0101 1010 0101 1010two Exercise 2.7.1: For the patterns above, what base 10 number does the binary number represent, assuming that it is a two's complement integer? -42406 Exercise 2.7.2: For the patterns above, what base 10 number does the binary number represent, assuming that it is an unsigned integer? 4294924890 Exercise 2.7.3: For the patterns above, what hexadecimal number does it represent? 0xFFFF5A5A
5. Exercise 2.7.4 through 2.7.5, using the following decimal value: 65432ten Exercise 2.7.4: For the base 10 numbers above, convert to 2's complement binary. 0b 0000 0000 0000 0000 1111 1111 1001 1000 Exercise 2.7.5: For the base 10 numbers above, convert to 2's complement hexadecimal. 0x0000FF98 6. Exercise 2.8.1 through 2.8.3, only part b The following problems deal with sign extension and overflow. Registers $s0 and $s1 hold the values shown in the table below. You will be asked to perform an MIPS assembly language instruction on these registers and show the result. b. $s0 = 0x00000001sixteen, $s0 = 0xFFFFFFFFsixteen Exercise 2.8.1: add $t0, $s0, $s1 Exercise 2.8.2: sub $t0, $s0, $s1 Exercise 2.8.3: add $t0, $s0, $s1 add $t0, $t0, $s0 7. Exercise 2.10.1 through 2.10.4, only part b In the following problems, the data table contains bits that represent the opcode of an instruction. You will be asked to interpret the bits as MIPS instructions into assembly code and determine what format of MIPS instruction the bits represent. b. 0000 0001 0100 1011 0100 1000 0010 0010two Exercise 2.10.1: For the binary entries above, what instruction do they represent?
000000 = opcode 0 01010 = 10 = $t2 01011 = 11 = $t3 01001 = 9 = $t1 00000 100010 = 34 = sub So we get: sub $t1, $t2, $t3 Exercise 2.10.2: What type (I-type, R-type, J-type) instruction do the binary entries above represent? This is an R-type, operating on registers Exercise 2.10.3: If the binary entries above were data bits, what number would they represent in hexadecimal? 0x014B4822 In the following problems, the data tables contains MIPS instructions. You will be asked to translate the entries into the bits of the opcode and determine the MIPS instruction format. b. sw $t1, 32($t2) Exercise 2.10.4: For the instructions above, show the binary then hexadecimal representation of these instructions. 1010 1101 0100 1001 0000 0000 0010 0000 0xAD490020 8. Exercise 2.11.1 through 2.11.6, only part b In the following problems, the data table contains bits that represent the opcode of an instruction. You will be asked to translate the entries into assembly code and determine what format of MIPS instruction the bits represent. b. 0x02538822 Exercise 2.11.1: What binary number does the above hexadecimal number represent? 0000 0010 0101 0011 1000 1000 0010 0010 Exercise 2.11.2:
What decimal number does the above hexadecimal number represent? 39028770 Exercise 2.11.3: What instruction does the above hexadecimal number represent? 000000 op 0 10010 18 - $s2 10011 19 - $s3 100001 17 - $s1 00000 100010 - sub sub $s1, $s2, $s3 In the following problems, the data table contains the values of various fields of MIPS instructions. You will be asked to determine what the instruction is, and find the MIPS format for that instruction. b. op=0x23, rs=1, rt=2, const=0x4 Exercise 2.11.4: What type (I-type, R-type) instruction do the instructions above represent? I-type Exercise 2.11.5: What is the MIPS assembly instruction described above? lw $v0, 4 I think the rs gets ignored? Exercise 2.11.6: What is the binary representation of the instructions above? 1000 1100 0000 0010 0000 0000 0000 0100