cs345 Exam 2 Solutions Part 1 a1) x: .double 2.0 y: .double 4.0 z: .double 6.0 a2) l.d $f0, x l.d $f2, y l.d $f4, z a3) add.d $f4, $f0, $f4 add.d $f4, $f2, $f4 a4) s.d $f4, z b5) A*0 + B*0 = 0*0 = 0 (false) b6) (A+1) * (B+1) = 1 * 1 = 1 (true) b7) (A+B)*C + D = A*C + B*C + D b8) D * (A + B + C + 1) = D * 1 = D c9) There are 4 bytes in a 32 bit integer c10) There are 8 bits in a byte c11) An ASCII character is one byte Part 2 1. li $t0, 0 #$t0 is x li $t1, 0 #$t1 is i for: bge $t1, 10, endfor add $t0, $t0, $t1 #x = x + i addi $t1, $t1, 1 #i++ endfor: 2. Convert addi $t3, $s2, 53 to binary then to hex addi = 001000 $t3 = 01011 $s2 = 10010 53 = 0000 0000 0011 0101 addi $t3, $s2, 53 == 001000 10010 01011 0000 0000 0011 0101 = 0010 0010 0100 1011 0000 0000 0011 0101 = 0x224B0035 3. 24 = 0001 1000 31 = 0001 1111 => -31 = 1110 0001 0001 1000 +1110 0001 ---------- 1111 1001 = -7d 4. ------------ <-- $sp Call factorial(2) +------------+ <-- $sp | $a0 = 2 | +------------+ <-- $sp + 4 | $ra = main | +------------+ <-- $sp + 8 Call factorial(1) from factorial(2) +------------+ <-- $sp | $a0 = 1 | +------------+ <-- $sp + 4 |$ra = fac(2)| +------------+ <-- $sp + 8 | $a0 = 2 | +------------+ <-- $sp + 12 | $ra = main | +------------+ <-- $sp + 16 Call factorial(0) from factorial(1) +------------+ <-- $sp | $a0 = 0 | +------------+ <-- $sp + 4 |$ra = fac(1)| +------------+ <-- $sp + 8 | $a0 = 1 | +------------+ <-- $sp + 12 |$ra = fac(2)| +------------+ <-- $sp + 16 | $a0 = 2 | +------------+ <-- $sp + 20 | $ra = main | +------------+ <-- $sp + 24 Return to factorial(1) +------------+ <-- $sp | $a0 = 1 | +------------+ <-- $sp + 4 |$ra = fac(2)| +------------+ <-- $sp + 8 | $a0 = 2 | +------------+ <-- $sp + 12 | $ra = main | +------------+ <-- $sp + 16 Return to factorial(2) +------------+ <-- $sp | $a0 = 2 | +------------+ <-- $sp + 4 | $ra = main | +------------+ <-- $sp + 8 Return to main ------------ <-- $sp 5. 304d = 1 0011 0000b 0.875 * 2 1.75 => 0.75 * 2 1.5 => 0.5 * 2 1.0 => 0.875d = 0.111b 304.875d = 1 0011 0000.111b = 1.0011 0000 111 * 2^8d or 1000b 6. cubeFunction: l.d $f0, 0($a0) mul.d $f12, $f0, $f0 #f12 = f0*f0 mul.d $f0, $f12, $f0 #f0 = f0*f0*f0 s.d $f0, 0($a1) jr $ra 7. The fetch-decode-execute cycle is a repetitive cycle that is used in CPUs to process both instructions and data, i.e. to run programs. In the fetch state of this cycle, the Program Counter, which keeps track of the current instruction to execute, is either incremented or modified in response to a branch. The address contained within the program counter is the address of the instruction to execute. This address is provided to Instruction Memory and the next instruction to run is retrieved and stored in an Instruction Register. The control unit causes this instruction to be decoded and decides which portion of the CPU needs to be utilized to execute the instruction. For example, if the instruction utilizes mathematics or logic, the Control Unit uses the data from the instruction to indicate the proper registers to use and the proper math or logic instruction to utilize within the ALU. If the instruction is a move, load, or store, the Control Unit directs the CPU to interact with registers or memory to process the proper instruction. After execution is complete, the Control Unit directs the CPU to return to the fetch state and begin processing another instruction.