CA ch2 PDF
CA ch2 PDF
Engineering Faculty
Department of Computer Engineering
ECOM 3010: Computer Architecture Discussion
Chapter 2
Exercises with solutions
October, 2013
2 Computer Architecture Discussion
Discussion exercises
Exercise 1:
Convert the following C statements to equivalent MIPS assembly language. Assume that the variables
f, g, I and j are assigened to registers $s0, $s1, $s2 and $s3 respectively. Assume that the base
address of the array A and B are in registers $s6 and $s7 respectively.
a) f = g + h + B[4]
lw $t0, 16($s7)
add $s0, $s1, $s2
add $s0, $s0, $t0
b) f = g – A[B[4]]
lw $t0,16($s7)
sll $t1, $t0, 2
add $t2, $t1, $s6
lw $t3, 0($t2)
sub $s0, $s1, $t3
31-j = 31-22 = 9
sll $t0, $t3, 9
i+1+9 = 5+1+9 = 15
srl $t0, $t0, 15
Another solution:
i+1 = 6
srl $t0, $t3, 6
j-i = 22-5=17
andi $t0, $t0, 0x0001FFFF
Exercise 5:
Convert the MIPS instruction to machine language:
srl $s1, $t2, 3
Exercise 6:
Translate the following machine code to MIPS:
43 16 11 4
Op(6) rs(5) rt(5) immediate(16)
Op = 43 = sw
16 = $s0
11 = $t3
sw $t3, 4($s0)
Exercise 8:
Convert the following C fragment to equivalent MIPS assembly language. Assume that the
variables a and b are assigened to registers $s0 and $s1 respectively. Assume that the base
address of the array D is in register $s2.
while(a < 10){
D[a] = b + a;
a += 1;
}
Loop: stli $t0, $s0, 10
beq $t0, $zero, exit
sll $t1, $s0, 2
add $t1, $t1, $s2
add $t2, $s1, $s0
sw $t2, 0($t1)
addi $s0, $s0, 1
j Loop
exit:
Exercise 9:
Show the effects on memory and registers of the following instructions. Suppose a portion of
memory contains the following data
Address Data
0x10000000 0x12345678
0x10000004 0x9ABCDEF0
And register $t0 contains 0x10000000 and $s0 contains 0x01234567. Assume each of the
following instructions is executed independently of the others, starting with the values given
above. Hint: Don’t forget that the MIPS architecture is Big-Endian.
The memory:
Address Data
0x10000000 0x12
0x10000001 0x34
0x10000002 0x56
0x10000003 0x78
0x10000004 0x9A
0x10000005 0xBC
0x10000006 0xDE
0x10000007 0xF0
6 Computer Architecture Discussion
a) lw $t1, 0($t0)
$t1 = 0x12345678
b) lw $t2, 4($t0)
$t2 = 0x9ABCDEF0
c) lb $t3, 0($t0)
$t3 = 0x00000012
d) lb $t4, 4($t0)
$t4 = 0xFFFFFF9A lb is sign extended
e) lb $t5, 3($t0)
$t5 = 0x00000078
f) lh $t6, 4($t0)
$t6 = 0XFFFF9ABC lh is sign extended
g) sw $s0, 0($t0)
at address 0x10000000 will contain 0x01234567
Address Data
0x10000000 0x01
0x10000001 0x23
0x10000002 0x45
0x10000003 0x67
0x10000004 0x9A
0x10000005 0xBC
0x10000006 0xDE
0x10000007 0xF0
h) sb $s0, 4($t0)
the address 0x10000004 will contain 0x67BCDEF0
Address Data
0x10000000 0x12
0x10000001 0x34
0x10000002 0x56
0x10000003 0x78
0x10000004 0x67
0x10000005 0xBC
0x10000006 0xDE
0x10000007 0xF0
7 Computer Architecture Discussion
i) sb $s0, 7($t0)
the address 0x10000004 will contain 0x9ABCDE67
Address Data
0x10000000 0x12
0x10000001 0x34
0x10000002 0x56
0x10000003 0x78
0x10000004 0x9A
0x10000005 0xBC
0x10000006 0xDE
0x10000007 0x67
Exercise 10:
Convert the following program into machine code.
0xFC00000C start: ………………
0xFC000010 loop: addi $t0,$t0,-1
0xFC000014 sw $t0, 4($t2)
0xFC000018 bne $t0, $t3, loop
0xFC00001C j start
addi $t0,$t0,-1
-1 = 0xFFFF
0010 00/01 000/0 1000 /1111 1111 1111 1111
8 8 8 -1
Op(6) rs(5) rt(5) immediate(16)
sw $t0, 4($t2)
1010 11/01 010/0 1000 /0000 0000 0000 0100
43 10 8 4
Op(6) rs(5) rt(5) immediate(16)
5 8 11 -3
Op(6) rs(5) rt(5) immediate(16)
j start
= C00000C / 4 = 3000003
2 3000003
Op(6) immediate (26)
The problem is that we are using PC-relative addressing, so if that address is too far away, we
won’t be able to use 16 bits to describe where it is relative to the PC.
If there refers to a location further than 128 KB from the PC, the solution would be:
If there refers to a location further than 256 MB from the PC, the solution would be:
Exercise 12:
Suppose that you have already written a MIPS function with the following signature:
This function calculates the sum of the elements of A starting with element first and ending
with element last. Write a fragment of MIPS assembly language which calls this function and
uses it to calculate the average of all values in A. You may assume that the size of the array A is
N, the base address of A in $a0.
Average:
add $a1, $zero, $zero # index of first element
addi $a2, $zero, N
addi $a2, $a2, -1 # index of last element is N-1
jal sum
add $t0, $zero, $v0 # Save the return value in $t0
addi $t1, $zero, N # Load size of array into $t1
div $t2, $t0, $t1 # This form of div is provided as
a pseudoinstruction.
10 Computer Architecture Discussion
Exercise 13:
Below is a recursive version of the function BitCount. This function counts the number of bits
that are set to 1 in an integer.
Your task is to translate this function into MIPS assembly code. The parameter x is passed to
your function in register $a0. Your function should place the return value in register $v0.
int BitCount(unsigned x) {
int bit;
if (x == 0)
return 0;
bit = x & 0x1;
return bit + BitCount(x >> 1);
}
BitCount:
addi $sp, $sp, -8
sw $s0, 4($sp)
sw $ra, 0($sp)
bne $a0, $0, else
add $v0, $0, $0
addi $sp, $sp, 8
jr $ra
else:
andi $s0, $a0, 1
srl $a0, $a0, 1
jal BitCount
add $v0, $v0, $s0
lw $ra, 0($sp)
lw $s0, 4($sp)
addi $sp, $sp, 8
jr $ra
11 Computer Architecture Discussion
Extra exercises
Exercise 14: (2.29 from book)
Add comments to the following MIPS code describe in one sentence what it computes. Assume
that $a0 and $a1 are used for the input and both initially contain the integers a and b,
respectively. Assume that $v0 used for the output.
t0=0
while(a1 != 0){
t0 = t0 + a0;
a1 = a1 – 1;
}
t0 = t0 + 100;
v0 = t0;
There are multiple bugs in this MIPS program; fix them and turn in a bug-free version.
Bug 1: Count ($v0) is initialized to zero, not –1 to avoid counting zero word.
Bug 2: Count ($v0) is not incremented.
Bug 3: Loops if word copied is equal to zero rather than not equal.
12 Computer Architecture Discussion
Bug-free version:
addi $v0, $zero, -1 # Initialize to avoid counting zero word
loop:lw, $v1, 0($a0) # Read next word from source
addi $v0, $v0, 1 # Increment count words copied
sw $v1, 0($a1) # Write to destination
addi $a0, $a0, 4 # Advance pointer to next source
addi $a1, $a1, 4 # Advance pointer to next destination
bne $v1, $zero, loop # Loop if word copied != zero
Exercise 16:
Convert the following C fragment to equivalent MIPS assembly language. Assume that the variables a,
b, c, d, i and x are assigened to registers $t1, $t2, $t3, $t4, $s0 and $s1 respectively. Assume
that the base address of the array A and B is in register $a0 and $a1 respectively.
a) if ((a<b) && (c==0)) d = 1;
b) if (a > 0)
b = a + 10;
else
b = a - 10;
slt $t0, $0, $t1 # if $0 < $t1 then $t0 = 1, else $t0 = 0
beq $t0, $0, else # if $t0 == $0 then branch to else
addi $t2, $t1, 10
j exit
else: addi $t2, $t1, -10
exit:
Exercise 17:
Convert this high level language code into MIPS code. Do not forget to write MIPS code for the
abs(x) procedure. (i saved in $s0, the address of a in $s1, y in $s2)
i = 3;
y = y + abs(a[i]);
addi $s0,$zero,3
sll $t0,$s0,2
add $t1,$t0,$s1
lw $a0,0($t1)
jal abs
add $s2,$s2,$v0
j exitall
abs:
slt $t0,$a0,$zero
bne $t0,$zero,else
add $v0,$a0,$zero
jr $ra
else:
sub $v0,$zero,$a0
jr $ra
exitall:
Exercise 18:
For each of the following, write the shortest sequence of MIPS assembly instructions to
perform the specified operation.
a) $v0 = 12345678
lui $v0, 188
ori $v0, $vo, 24910
14 Computer Architecture Discussion
c) t1 = 12345678 + 31415924
lui $t0, 188
ori $t0, $t0, 24910
lui $t2, 479
ori $t2, $t2, 24180
add $t1, $t0, $t2
Exercise 19:
Given the register values in two's complement representation
$s1 = 0000 0000 0000 0000 0000 0000 0000 0101 = 5
$s2 = 0000 0000 0000 0000 0000 0000 0000 0011 = 3
$s3 = 1111 1111 1111 1111 1111 1111 1111 1100 = -4
What are the values of registers $s1 through $s4 after executing the following MIPS
instructions:
slt $s1, $s1, $s2 Result: $s1 = 0
slt $s2, $s1, $s3 Result: $s2 = 0
sltu $s3, $s1, $s2 Result: $s3 = 0
sltu $s4, $s1, $s3 Result: $s4 = 1
Best Wishes