Islamic University of Gaza Faculty of Engineering Computer

Islamic University of Gaza
Faculty of Engineering
Computer Engineering Department
Computer Architecture
Exercises For Chapter 2
Q1 :
Q2 :
Q3 :
Q4 :
1. F ill in the blanks to obtain all 4 possible comparisons between
variables s1 and s2 (placed in registers $s1 and $s2 respectively).
Then, summarize your results in the shown table .
a)
q If(s1<s2) goto less
Slt $t0, $__, $__
B__ $t0, $zero, less
q If(s1<s2) goto lessOrEqual
Slt $t0, $__, $__
B__ $t0, $zero, lessOrEqual
q If(s1>=s2) goto greaterOrEqual
Slt $t0, $__, $__
B__ $t0, $zero, greaterOrEqual
If(s1<s2) goto greater
Slt $t0, $__, $__
B__ $t0, $zero, greater
The answer :
 If(s1<s2) goto less
Slt $t0, $s1, $s2
Bne $t0, $zero, less
 If(s1<s2) goto lessOrEqual
Slt $t0, $s2, $s1
Beq $t0, $zero, lessOrEqual
 If(s1>=s2) goto greaterOrEqual
Slt $t0, $s1, $s2
Beq $t0, $zero, greaterOrEqual
 If(s1<s2) goto greater
Slt $t0, $s2, $s1
Bne $t0, $zero, greater
b):
First in
Slt $S1/ $S2
$S1
$S1
$S2
$S2
Beq/Bne
Beq
Bne
Beq
Bne
Resulting
Comparison
greaterOrEqual
less
lessOrEqual
greater
Q5:
Q6 :
Q7 :
Write MIPS assembly code to calculate the absolute value of $t1 without
using branch instructions.
The answer :
sra $t0,$t1,31 #get the sign bit of $t1 in all bits of $t0
xor $t0,$t1,$t0 #if sign bit was 1, we're flipping bits of $t1
and placing result in $t0, else this is nop
andi $t1,$t1,1 #$t1 will be 1 if sign bit was 1, else 0
addu $t0,$t1,$t0 #flipped the bits, now add 1 if sign was 1,
else just adding 0, so nop
Q8 :
Convert the following C fragment to equivalent MIPS assembly
language.
...
int i,j;
i = 0;
j = -1;
while(i < 10){
if((i & 0x0001)==1) j += i; //if i is odd, add it to j
i++;
}
...
The answer :
addi $t0,$0,0x0000 #i = 0
addi $t1,$0,0xFFFF #j = -1
loop: slti $t2,$t0,0x000A
beq $0,$t2,end #go to end when i reaches 10 ($t2==0)
andi $t3,$t0,0x0001
beq $t3,$0,noif #go to noif if i is even
add $t1,$t1,$t0 #if i is odd, add it to j
noif: addi $t0,$t0,0x0001 #increment i
j loop
end:
Q9 :
For the following C code, we show a partial assembly implementation.
Fill in the missing instructions.
int foo(int *a, int x, int y)
{
if (a[x] > a[y])
{
return a[x] + a[y];
}
else
{
return a[x] - a[y];
}
}
The answer:
foo: # prologue
# foo is leaf function with no local storage
# no stack frame is necessary
# body
sll $t0, $a1, 2
sll $t1, $a2, 2
add $t0, $t0, $a0
add $t1, $t1, $a0
lw $t0, 0($t0)
# load value of a[x] into $t0
lw $t1, 0($t1)
# load value of a[y] into $t1
sub $t2, $t0,$t1
blez $t2, else
# branch if a[x]<=a[y]
add $v0,$t1,$t0
j end
else:
sub $v0,$t0,$t1
end:
# epilogue -- no stack frame to pop
jr $ra
# return to caller
2. Why are the sll instructions necessary in the code given above?
We need to multiply x (content of $a1) and y (content of $a2) by 4 to
produce a word aligned effective address. Shifting left by 2 bits is
equivalent to multiplying by 22 = 4.
Q10 :
a) Translate the following program from C to MIPS. Assume that $t0 holds the value
of x, and $t1 holds the value of y.
if (x > 0)
y = x + 10;
else
y = x - y;
Answer:
# if $t0 < $0 then $t2 = 1, else $t2 = 0
slt $t2, $t0, $0
beq $t2, $0, L1 # if $t2 == $0 then
# branch to L1
addiu $t1, $t0, 10
beq $0, $0, L2 # same as: b L2
L1: addiu $t1, $t0, -10
L2: ...
b) Change your solution in (a) assuming that the values of x and y are stored in
memory (and not registers) initially.
.data
x: .word 43
y: .word 39
.text
# your solution begins here:
Answer:
la $t0, x # load the address that x is bound to
la $t1, y # load the address that y is bound to
lw $t0, 0($t0) # load the word stored at address $t0
# and it put into $t0
lw $t1, 0($t1) # load the word stored at address $t1
# and it put into $t1
Q11 :
MIPS to C and vise versa
a) Rewrite the MIPS code shown below in Java or C. Use the high-level
language syntax and idioms you would expect for the equivalent code.
li $t0, 0
li $a0, 3
lablel1:
addi $t2, $t2, 2
addi $t0, $t0, 1
blt $t0, $a0, label1
label2:
for(int t0=0; t0<3; t0++)
t2 = t2 + 2;
// or t2 += 2;
}
b) Translate the following program from C to MIPS. Assume $t0 holds
the value of i, and $s0 holds the value of s.
i = 1;
s = 0;
while ( i < 10 )
s = s + i;
i++;
}
{
li $t0, 1
li $s0, 0
loop:
exit:
slti $t1,
beq $t1,
add $s0,
addi $t0,
j loop
$t0,
$0,
$s0,
$t0,
10
exit
$t0
1
Q12 :
Q 13:
Consider the following C function:
char * delete_vowels(char *str)
{
char *src = str;
char *dest = src;
while (*src != ’\0’) {
switch (*(src)) {
case ’a’:
case ’e’:
case ’i’:
case ’o’:
case ’u’:
/* Do nothing. */
break;
default:
*dest = *src;
dest++;
}
src++;
}
*dest = ’\0’;
return str;
}
As its name suggests, the function removes vowels from a string. For example, the output
from this code fragment:
char a[] = "midterm";
printf("...%s...\n", delete_vowels(a));
would be
...mdtrm...
Translate the function into SPIM assembly language. Follow the calling conventions used in
lectures and labs, and use only instructions from the Midterm Instruction Subset described on
the Reference Material page. Use register $s0 for the local variable src and register $s1
for the local variable
dest.
You are not required to set up a jump table for the switch statement.
Here is a useful table:
character constant :ASCII code
’\0’ 0
’a’ 97
’e’ 101
’i’ 105
’o’ 111
’u’ 117
Q14 :
Part a. Consider the following C function, which returns the index of a maximum value
among a[0], a[1], . . . a[n-1]:
int index_of_max(const int *a, int n)
{
const int *p;
const int *q;
int max;
const int *x;
p = a + 1;
q = a;
max = *a;
x = a + n;
while (p < x) {
if (*p > max) {
max = *p;
q = p;
}
p++;
}
return q - a;
}
Translate the function into SPIM assembly language. Follow the calling conventions used in
lectures and labs, and use only instructions from the Midterm Instruction Subset described on
the Reference Material page. You must allocate registers as follows: $s0 for p, $s1 for q,
$s2 for max, and $s3 for x.
Part b. Consider the following C function:
void put_zero(char *a, int n)
{
int i;
for (i = n - 1; i >= 0; i--)
a[i] = 0;
}
Translate the function into SPIM assembly language. Follow the calling conventions used in
lectures and labs, and use only instructions from the Midterm Instruction Subset described on
the Reference Material page. You must allocate registers as follows: $s0 for i.
Q15 : Compiling-Assembly-Linking
a) What is the difference between an absolute address and a relative address?
Answer:
a) An absolute address is an address that has only one possible place in memory.
A relative address is an address that can reside at a number of places in memory
with respect to another address.
b) What is the job of the assembler? the linker? the loader?
Answer:
The job of the assembler is to do MAL to TAL conversion and resolve relative
labels.
The job of the linker is to resolve absolute references. The job of the loader is to
move the program into memory and begin to execute the program.
c) What is the difference between a compiler and an interpreter?
Answer:
A compiler transforms a language from one form to another by running through
the code. This transformation is only done once. In contrast, an interpreter runes
through code each and every time the code is invoked.
d) What is the purpose of the symbol table? the relocation table?
Answer:
The purpose of the symbol table is to list all the labels used in a source file. A
relocation table is used to list all instructions that have labels that are still
unresolved.
e) What are the three things a linker does?
Answer:
A linker resolves all items found in a relocation table, gives instructions and data
absolute addresses, and produces an executable.
f) For each of the tasks below, label it with the first two letters of the system whose
job it is: COmpiler, ASsembler, LInker, or LOader.
___ Resolve undefined labels using the relocation information and symbol table
___ Copy the parameters (if any) to the main program onto the stack
___ Change move $t0,$t1 into add $t0,$zero,$t1
Good Luck