111 Computer Organization - Midterm
111 Computer Organization - Midterm
Attention: 1. For fairness and justice, students should be self-disciplined to maintain school’s and students’ honor.
2. Chatting, taking examination papers out, peeping, and other misconduct are prohibited during the
examination. Violators will be seriously punished.
ATRX-Q03-001-FM256-01
( C ) 8. (7%) Supposed the program counter (PC) is set to 0x2000 2000. Is it possible to use the jump (j) and
branch-on-equal (beq) MIPS assembly instructions to set the PC to the address 0x2000 0000?
(A) jump – yes, beq – no (B) jump – no, beq – no
(C) jump – yes, beq – yes (D) jump – no, beq – yes
( D ) 9. (7%) Multiply 10ten x 11ten (1010two x 1011two) using the hardware described in Figure-1. Assume
these two numbers are unsigned 4-bit integers. What is the value in the Product field in Table-1 after
the 2nd iteration?
(A) P2 = 0101 0101 (B) P2 = 0111 0101
(C) P2 = 0101 1010 (D) P2 = 0111 1010
( A ) 10. (7%) In which of the following situations does an overflow never occur?
(A) Adding a negative integer to a positive integer
(B) Subtracting a negative integer from a positive integer
(C) Adding a negative integer to a negative integer
(D) Subtracting a positive integer from a negative integer
(Question 11~12) For the following C statement:
int leaf_example (int g, int h, int i, int j)
{
int f;
f = (g + h) – (i + j);
return f;
}
The parameter variables g, h, i, and j correspond to the argument registers $a0, $a1, $a2, and $a3, and
f corresponds to $s0. Please finish the MIPS assembly code for this C segment.
MIPS code:
X # adjust stack to make room for 1 items
Y # save register $s0 for use afterwards
add $t0,$a0,$a1 # register $t0 contains g + h
add $t1,$a2,$a3 # register $t1 contains i + j
sub $s0,$t0,$t1 # f = $t0 – $t1, which is (g + h)–(i + j)
add $v0,$s0,$zero # returns f ($v0 = $s0 + 0)
Z # restore register $s0 for caller
W # adjust stack to delete 1 items
jr $ra # jump back to calling routine
( A ) 11. (8%) What are the MIPS assembly codes in the blank X and W?
(A) X: addi $sp, $sp, –4 (B) X: addi $sp, $sp, –2
W: addi $sp, $sp, 4 W: addi $sp, $sp, 2
(C) X: sll $sp, $sp, 4 (D) X: sll $sp, $sp, 2
W: srl $sp, $sp, 4 W: srl $sp, $sp, 2
( B ) 12. (8%) What are the MIPS assembly codes in the blank Y and Z?
(A) Y: lw $s0, 0($sp) (B) Y: sw $s0, 0($sp)
Z: sw $s0, 0($sp) Z: lw $s0, 0($sp)
(C) Y: lw $s0, 4($sp) (D) Y: sw $s0, 4($sp)
Z: sw $s0, 4($sp) Z: lw $s0, 4($sp)
( A ) 13. (7%) Which of the following is the MIPS instruction of the binary value: 0000 0000 0001 0000
0101 0001 0000 0000
(A) sll $t2, $s0, 4 (B) add $s0, $t2, $0
(C) sll $t2, $s0, 2 (D) add $0, $s0, $t2
( C ) 14. (7%) Divide 11ten by 2ten (1011two / 0010two) using the hardware described in Figure-2. Assume these
two numbers are unsigned 4-bit integers. What is the value in the Quotient and Remainder fields in
Table-2 after 3rd iteration?
(A) Q3 = 0010; R3 = 0000 0011 (B) Q3 = 0010; R3 = 0000 0001
(C) Q3 = 0001; R3 = 0000 0011 (D) Q3 = 0001; R3 = 0000 0001
2
Figure-1
Table-1
Multiplicand
Iteration Step Product (& Multiplier)
(Mcand)
0 Initial values 1 0 1 0 0 0 0 0 1 0 1 1
1a: M0 = 1 → Prod(LH) = Prod(LH) + Mcand
1
1b: M0 = 0 → No operation
2: Shift right product
1a: M0 = 1 → Prod(LH) = Prod(LH) + Mcand
2
1b: M0 = 0 → No operation
2: Shift right product P2 = ?
⚫ M0 = Rightmost bit of the Product (& Multiplier) register
⚫ Prod(LH) = Left-Hand side of the Product (& Multiplier) register
Figure-2
Table-2
Iter Step Quotient Divisor Remainder
0 Initial values 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1
3
◼ CPU (Execution) Time
= CPU Clock Cycles Clock Cycle Time
= CPU Clock Cycles / Clock Rate
= Instruction Count (IC) Cycles per Instruction (CPI) Clock Cycle Time
= Instruction Count (IC) Cycles per Instruction (CPI) / Clock Rate
Clock Cycles n
Instruction Count i
◼ Weighted average CPI = = CPI i
Instruction Count i =1 Instruction Count
4
5
Category Instruction Format Syntax Example Meaning
add unsigned R addu rd, rs, rt addu $s1, $s2, $s3 $s1=$s2+$s3
load half unsigned I lhu rt, offset(rs) lhu $s1, 20($s2) $s1=Memory[$s2+20]
Data transfer
store half I sh rt, offset(rs) sh $s1, 20($s2) Memory[$s2+20]=$s1
load byte
I lbu rt, offset(rs) lbu $s1, 20($s2) $s1=Memory[$s2+20]
unsigned
store byte I sb rt, offset(rs) sb $s1, 20($s2) Memory[$s2+20]=$s1
AND R and rd, rs, rt and $s1, $s2, $s3 $s1=$s2 & $s3
AND immediate I andi rt, rs, immediate andi $s1, $s2, 100 $s1=$s2 & 100
OR immediate I ori rt, rs, immediate ori $s1, $s2, 100 $s1=$s2 | 100
Logical NOR R nor rd, rs, rt nor $s1, $s2, $s3 $s1= ~ ($s2 | $s3)
XOR R xor rd, rs, rt xor $s1, $s2, $s3 $s1=$s2 ^ $s3
shift left logical R sll rd, rt, shamt sll $s1, $s2, 10 $s1=$s2 << 10
shift right logical R srl rd, rt, shamt srl $s1, $s2, 10 $s1=$s2 >> 10
Shift right
R sra rd, rt, shamt sra $s1, $s2, 10 $s1=$s2 >> 10
arithmetic
if ($s1==$s2)
branch on equal I beq rs, rt, offset beq $s1, $s2, 25
go to (PC+4+100)
branch on not if ($s1!=$s2)
I bne rs, rt, offset bne $s1, $s2, 25
equal go to (PC+4+100)
Conditional if ($s2 < $s3) $s1=1;
set on less than R slt rd, rs, rt slt $s1, $s2, $s3
branch else $s1=0
set less than if ($s2 < 100) $s1=1;
I slti rt, rs, immediate slti $s1, $s2, 100
immediate else $s1=0
set less than if ($s2 < $s3) $s1=1;
R sltu rd, rs, rt sltu $s1, $s2, $s3
unsigned else $s1=0