Sei sulla pagina 1di 6

CS 3340.

502 - HW 3 Solutions February 23rd, 2012

Exercise 2.4
2.4 Problem Description
The following problems deal with translating from C to MIPS. Assume that the variables f , g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively. a. b. f = g + h + B[4]; f = g A[B[4]];

Exercise 2.4.1. For the C statements shown above, what is the corresponding assembly code? Solution. For part a), we have the following: lw $s0, 16($s7) add $s0, $s0, $s1 add $s0, $s0, $s2 For part b), we have: lw $t0, 16($s7) lw $s0, 0($t0) sub $s0, $s1, $s0

Exercise 2.4.2. For the C statements above, how many MIPS assembly instructions are needed to perform the C statement? Solution. For part a), it took 3 MIPS assembly instructions and for part b), it took 3 MIPS assembly instructions.

Exercise 2.4.3. For the C statements above, how many dierent registers are needed to carry out the C statement? Solution. For part a), 4 registers were needed, and for part b), 4 registers were needed. The following problems deal with translating from MIPS to C. Assume that the variables f , g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively.

a.

b.

add $s0, $s0, $s1 add $s0, $s0, $s2 add $s0, $s0, $s3 add $s0, $s0, $s4 lw $s0, 4($s6)

Exercise 2.4.4. For the MIPS assembly instructions above, what is the corresponding C statement? Solution. For part a), we have the following: f + = g + h + i + j; For part b), we have: f = A[1]; Exercise 2.4.5. For the MIPS assembly instructions above, rewrite the assembly code to minimize the number of MIPS instructions (if possible) need to carry out the same function. Solution. There is no way to reduce the number of assembly instructions to perform the functions for either parts in this exercise.

Exercise 2.4.6. How many registers are needed to carry out the MIPS assembly as written above? If you could rewrite the code above, what is the minimal number of registers needed? Solution. Part a) requires 5 registers and part b) requires 2 registers.

Exercise 2.9
2.9 Problem Description
The table below contains various values for register $s1. You will be asked to evaluate if there would be overow for a given operation. a. b. 2147483647ten 0xD0000000hex

Exercise 2.9.1. Assume that register $s0 = 0x70000000 and $s1 has the value as given in the table. If the instruction: add $s0, $s0, $s1 is executed, will there be overow? Solution. In part (a), we are adding the numbers 0111 1111 1111 1111 1111 1111 1111 1111two and 0111 0000 0000 0000 0000 0000 0000 0000two . It is clear that there will be overow in part (a) when these two numbers are added together. For part (b), we are adding the numbers 1101 0000 0000 0000 0000 0000 0000 0000two and 0111 0000 0000 0000 0000 0000 0000 0000two . In part (b), there will not be any overow when these two numbers are added together.

Exercise 2.9.2. Assume that register $s0 = 0x80000000 and $s1 has the value as given in the table. If the instruction: sub $s0, $s0, $s1 is executed, will there be overow? Solution. For part (a), we are subtracting 0111 1111 1111 1111 1111 1111 1111 1111two from 1000 0000 0000 0000 0000 0000 0000 0000two . This subtraction will result in overload. For part (b), we are subtracting 1101 0000 0000 0000 0000 0000 0000 0000two from 1000 0000 0000 0000 0000 0000 0000 0000two . This subtraction will not result in any overload.

Exercise 2.9.3. Assume that the register $s0 = 0x7FFFFFFF and $s1 has the value as given in the table. If the instruction: sub $s0, $s0, $s1 is executed, will there be overow? Solution. In part (a), we will be subtracting the binary number 0111 1111 1111 1111 1111 1111 1111 1111two from 0111 1111 1111 1111 1111 1111 1111 1111two . Clearly, this result is 0 and there is no overload. In part (b), we will be subtracting the binary number 1101 0000 0000 0000 0000 0000 0000 0000two from 0111 1111 1111 1111 1111 1111 1111 1111two . This will result in overload. The table below contains various values for register $s1. You will be asked to evaluate if there would be overow for a given operation.

a. b.

1010 1101 0001 0000 0000 0000 0000 0010two 1111 1111 1111 1111 1011 0011 0101 0011two

Exercise 2.9.4. Assume that register $s0 = 0x70000000 and $s1 has value given in the table. If the instruction: add $s0, $s0, $s1 is executed, will there be overow? Solution. In part (a), we will be adding the numbers 1010 1101 0001 0000 0000 0000 0000 0010two and 0111 0000 0000 0000 0000 0000 0000 0000two . This addition will not result in overow. In part (b), we will be adding the numbers 1111 1111 1111 1111 1011 0011 0101 0011two and 0111 0000 0000 0000 0000 0000 0000 0000two . This addition will not result in overow.

Exercise 2.9.5. Assume that register $s0 = 0x70000000 and $s1 has value given in the table. If the instruction: add $s0, $s0, $s1 is executed, what is the result in hex? Solution. In part(a), we will be adding the binary numbers 1010 1101 0001 0000 0000 0000 0000 0010two and 0111 0000 0000 0000 0000 0000 0000 0000two . In binary, this will result in 0001 1101 0001 0000 0000 0000 0000 0010two . In hex, this value is: 0x1D100002. In part (b), we will be adding the binary numbers 1111 1111 1111 1111 1011 0011 0101 0011two and 0111 0000 0000 0000 0000 0000 0000 0000two . In binary, this will result in 0110 1111 1111 1111 1011 0011 0101 0011two . In hex, this value is: 0x6FFFB353. Exercise 2.9.6. Assume that register $s0 = 0x70000000 and $s1 has value given in the table. If the instruction: add $s0, $s0, $s1 is executed, what is the result in base ten? Solution. This is a very straightforward computation. We obtain: (a) 487,587,842 (b) 1,879,028,563

Exercise 2.11
2.11 Problem Description
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. a. b. 0xAE0BFFFC 0x8D08FFC0

Exercise 2.11.1. What binary number does the above hexadecimal represent? Solution. This is a very straightforward computation. We obtain: (a) 1010 1110 0000 1011 1111 1111 1111 1100two . (b) 1000 1101 0000 1000 1111 1111 1100 0000two .

Exercise 2.11.2. What decimal number does the above hexadecimal number represent? Solution. This is a very straightforward computation. We obtain: (a) 2,920,022,012 (b) 2,366,177,216

Exercise 2.11.3. What instruction does the above hexadecimal number represent? Solution. Both of these instructions are I-type. We have: (a) sw $t3, -4($s0) (b) lw $t0, -64($t0) In the following problems, the data table contains the values of various elds of MIPS instructions. You will be asked to determine what the instruction is, and nd the MIPS format for the instruction. a. b. op=0, rs=1, rd=3, shamt=0, funct=32 op=0x2B, rs=0x10, rt=0x5, const=0x4

Exercise 2.11.4. What type (I-type, R-type) instruction do the instructions above represent? Solution. (a) R-type (b) I-type

Exercise 2.11.5. What is the MIPS assembly instruction described above? Solution. We have the following MIPS instructions: (a) add $v1, $at, $v0 (b) sw $a1, 4($s0)

Exercise 2.11.6. What is the binary representation of the instructions above? Solution. We have the following binary representations: (a) 0000 0000 0010 0010 00001 10000 0010 0000two . (b) 1010 1110 0000 0101 0000 0000 0000 0100two .

Potrebbero piacerti anche