(10 pts) Carefully do assigned reading for Chapter 1 (1.1-1.5, 1.7-1.8) (especially important b/c much of this chapter not covered in class). Then answer the following questions. Exercise 1.1 from the book (see end of chapter). Place your answers below (list and briefly describe). 1. 2. 3. 4. (3 pts) Exercise 1-2 • Do exercise 1.2 from the textbook. Follow example below, where some answers are provided for you. Use each term exactly once. a. b. (Hint: think about what these cables are made of) c. Performance via Prediction d. e. Hierarchy of Memories (2.66 x 109) / (12.5 x 106) = 212.8 f. 95W / 3.3 W = 28.8 g. h. (5 pts) Exercise 2-1 • What is the MIPS assembly code for the following: g = g + h - i; Variables g, h, & i are assigned registers $s1, $s2, and $s4 add $s1, $s1, $s2 # g = g + h sub $s1, $s1, $s4 # g = g - i (5 pts) Exercise 2-2 • What is the MIPS assembly code for the following: g = h + A[3]; Variables g, h, & i are assigned registers $s1, $s2, and $s4 Array A base address is assigned register $s3 lw $t0, 12($s3) add $s1, $s2, $t0 # temp = A[3] # g = h + A[3] (Note (5 pts) Exercise 2-3different index calculation here) • What is the MIPS assembly code for the following: g = h + A[i]; Variables g, h, & i are assigned registers $s1, $s2, and $s4 Array A base address is assigned register $s3 What is address of A[i]? (4*i + s3) add $t1, $s4, $s4 # temp = 2 * I add $t1, $t1, $t1 # temp = 4 * I add $t1, $t1, $s3 # temp = &A[i] lw $t0, 0($t1) # temp = A[i] add $s1, $s2, $t0 # g = h + A[i] (extra space) (10 pts) Exercise 2-4: Assume variables a, b, and c are assigned registers $s1, $s2, and $s3, and the address of array A is in $s5. Write the code for the following: b = A[4] - A[5]; lw $t0, 16($s5) lw $t1, 20($s5) sub $s2, $t0, $t1 # $t0 = A[4] # $t1 = A[5] # b = A[4] – A[5] (10 pts) Exercise 2-5: Assume variables a, b, and c are assigned registers $s1, $s2, and $s3, and the address of array A is in $s5. Write the code for the following: b = A[2 * c]; Address of A[2 * c] = (Base of A)+2*4*c = $s6 + 8*c add $t0, $s3, $s3 # $t0 = 2*c add $t0, $t0, $t0 # $t0 = $t0 + $t0 = 4*c add $t0, $t0, $t0 # $t0 = $t0 + $t0 = 8*c add $t0, $t0, $s5 # $t0 = $s6 + 8*c lw $s2, 0($t0) # $t0 = Mem[$s6+8c] = A[2*c] Or use sll to do multiplication. Shift by 3 bits to multiply by 8: sll $t0, $s3, 3 # $t0 = 8*c add $t0, $t0, $s5 # $t0 = $s6 + 8*c lw $s2, 0($t0) # $t0 = Mem[$s6+8c] = A[2*c] (6 pts) Exercise 2-8 (See number discussion in Section 2.5) • What binary number does this hexadecimal number represent: 7fff fffahex? • What decimal number does it represent? Easiest way to compute is to use hex: = 7 * 167 + 15 * 166 + …. + 15 * 161 + 10 * 160 (10 pts) Exercise 2-9 Show the hexadecimal representation of this MIPS instruction: add $t0 , $t1 , $zero Consider the format of the R-type instruction op 6 bits rs 5 bits rt 5 bits rd shamt funct 5 bits 5 bits 6 bits Here we have: opcode = 0 rs = 9 rt = 0 funct = 0x20 rd = 8 shamt = 0 The bit string is: 000000 01001 00000 01000 00000 Rewriting, grouping bits four at a time: 0000 0001 0010 0000 0100 0000 0010 0000 In hex: 0x01204020 100000 (10 pts) Exercise 2-10 What MIPS instruction is represented by this binary entry: (Tip: start by figuring out what the opcode is, then the instruction type) 1000 1101 0000 1001 0000 0000 0100 0100 Start by looking at the opcode (the first six bits): 100011. This is equivalent to 0x23. This is the opcode for load word: lw, which is an I-type instruction, whose format is: op 6 bits rs 5 bits rt constant or address 5 bits 16 bits Looking at the bit values, we see that rs = 8, rt = 9 and the constant is 68. From the table, we see reg #8 is $t0, and reg #9 is $t1. Thus: lw $t1 , 68($t0) (5 pts) Exercise 2-11 • What is the MIPS assembly code for the following: if (g != j) h = g - h; else h = g + h; Variables f to j are assigned to registers $s0 to $s4 beq sub j Else: add Exit: $s1, $s4, Else $s2, $s1, $s2 Exit $s2, $s1, $s2 …. # go to Exit f g h i j $s0 $s1 $s2 $s3 $s4 (5 pts) Exercise 2-12 • What is the MIPS assembly code for the following: if (j == h) g = i + j; Variables f to j are assigned to registers $s0 to $s4 f g h i j $s0 $s1 $s2 $s3 $s4 bne $s4, $s2, Exit add $s1, $s3, $s4 Exit: …. (5 pts) Exercise 2-13 • Notice more advanced control flow. What is the MIPS assembly code for the following: if ( (j == h) && (f != i) ) g = i + j; Variables f to j are assigned to registers $s0 to $s4 bne $s4, $s2, Exit beq $s0, $s3, Exit add $s1, $s3, $s4 Exit: …. f g h i j $s0 $s1 $s2 $s3 $s4 (10 pts) Exercise 2-14 • What is the MIPS assembly code for the following: if ( ( (g != h) && (f == i) ) || (g == i) ) g = i + j; Variables f to j are assigned to registers $s0 to $s4 f g h i j $s0 $s1 $s2 $s3 $s4 Many solutions possible, here is one beq $s1, $s2, SecondChance # check g != h bne $s0, $s3, SecondChance # check f == i j DoAdd SecondChance: # Didn’t pass first test, try second bne $s1, $s3, Exit # check g == i DoAdd: add $s1, $s3, $s4 # really do the add Exit: …. Here is another solution: beq $s1, $s3, DoAdd beq $s1, $s2, Exit bne $s0, $s3, Exit DoAdd: add $s1, $s3, $s4 Exit: …. (blank) # check g == i # check g != h # check f == I # really do the add