Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Computer Architecture and Organization

Worksheet-1

1. Use a minimal number of MIPS assembly instructions and identify all possible
addressing modes.
a) g = 4*h + 8-h;
b) while (h== 5)
g = 2*h + 8-h;
g = 8*h+ (8-h)
1. Use a minimal number of MIPS assembly instructions and identify all possible
addressing modes.
a) f = 2*g*h+ (h − 5);
b) while (i== 5
f = = 2*g*h + (h − 5);
f = 4*h – (h+8)
2. Use a minimal number of MIPS assembly instructions and identify all possible
addressing modes.
a) for (i=1;i<=4;i++)
{
for (j=1; j<=4;j++)
{
g = 2*h + 8-h;
}
}

3. Use a minimal number of MIPS assembly instructions and identify all possible
addressing modes.
b) for (i=1;i<=4;i++)
{
for (j=1; j<=4;j++)
{
f = = 2*g*h + (h − 5);
}
}

Taye G. (Asst. Professor) Page 1


4. Use a minimal number of MIPS assembly instructions and identify all possible
addressing modes.
a) while (a[i] == 2)
i += 1;
i = i-4
b) while (a[i] == b[j])
i += 1;
i = i-2
2. For the following C statement, what is the corresponding MIPS assembly code?
Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2,
$s3, and $s4, respectively. Assume that the base address of the arrays A and B
are in registers $s6 and $s7, respectively.

B[8] = A[i−j];

3. For the MIPS assembly instructions below, what is the corresponding C


statement? Assume that the variables f, g, h, i, and j are assigned to registers
$s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the
arrays A and B are in registers $s6 and $s7, respectively.

sll $t0, $s0, 2 # $t0 = f * 4


add $t0, $s6, $t0 # $t0 = &A[f]
sll $t1, $s1, 2 # $t1 = g * 4
add $t1, $s7, $t1 # $t1 = &B[g]
lw $s0, 0($t0) # f = A[f]
addi $t2, $t0, 4
lw $t0, 0($t2)
add $t0, $t0, $s0
sw $t0, 0($t1)

Taye G. (Asst. Professor) Page 2


4. Translate the following C code to MIPS. Assume that the variables f, g, h, i, and j
are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that
the base address of the arrays A and B are in registers $s6 and $s7,
respectively. Assume that the elements of the arrays A and B are 4-byte words:

B[8] = A[i] + A[j];

5. Translate the following MIPS code to C. Assume that the variables f, g, h, i, and j
are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that
the base address of the arrays A and B are in registers $s6 and $s7,
respectively.

addi $t0, $s6, 4


add $t1, $s6, $0
sw $t1, 0($t0)
lw $t0, 0($t0)
add $s0, $t1, $t0

6. Assume that registers $s0 and $s1 hold the values 0x80000000 and
0xD0000000, respectively.
a) What is the value of $t0 for the following assembly code?

add $t0, $s0, $s1

b) For the contents of registers $s0 and $s1 as specified above, what is

the value of $t0 for the following assembly code?

sub $t0, $s0, $s1

c) For the contents of registers $s0 and $s1 as specified above, what is
the value of $t0 for the following assembly code?

add $t0, $s0, $s1


add $t0, $t0, $s0

Taye G. (Asst. Professor) Page 3


7. Assume the following register contents: $t0 = 0xAAAAAAAA, $t1 = 0x12345678
a) For the register values shown above, what is the value of $t2 for the
following sequence of instructions?

sll $t2, $t0, 44


or $t2, $t2, $t1
b) For the register values shown above, what is the value of $t2 for the
following sequence of instructions?

sll $t2, $t0, 4


andi $t2, $t2, −1

8. For the following C statement, write a minimal sequence of MIPS assembly


instructions that does the identical operation. Assume $t1 = A, $t2 = B, and $s1
is the base address of C.

A = C[0] << 4;

9. Assume $t0 holds the value 0x00101000. What is the value of $t2 after the
following instructions?

slt $t2, $0, $t0


bne $t2, $0, ELSE
j DONE
ELSE: addi $t2, $t2, 2
DONE:

Taye G. (Asst. Professor) Page 4


10. Consider the following MIPS loop:

LOOP: slt $t2, $0, $t1


beq $t2, $0, DONE
subi $t1, $t1, 1
addi $s2, $s2, 2
j LOOP
DONE:

a) Assume that the register $t1 is initialized to the value 10. What is the
value in register $s2 assuming $s2 is initially zero?

b) For each of the loops above, write the equivalent C code routine.
Assume that the registers $s1, $s2, $t1, and $t2 are integers A, B, i,
and temp, respectively.

c) For the loops written in MIPS assembly above, assume that the
register $t1 is initialized to the value N. How many MIPS instructions
are executed?

11. Consider the following MIPS loop:

LOOP: slt $t2, $0, $t1


beq $t2, $0, DONE
subi $t1, $t1, 1
addi $s2, $s2, 2
addi $t1, $t1, 1
j LOOP
DONE:

a) Assume that the register $t1 is initialized to the value 10. What is the
value in register $s2 assuming $s2 is initially zero?

Taye G. (Asst. Professor) Page 5


12. Consider the following MIPS loop:

LOOP: slt $t2, $0, $t1


beq $t2, $0, DONE
subi $t1, $t1, 1
addi $s2, $s2, 2
subi $s2, $s2, -2
j LOOP
DONE:

a) Assume that the register $t1 is initialized to the value 10. What is the
value in register $s2 assuming $s2 is initially zero?

13. Translate the following Java statement into MIPS assembly code. Assume that
x, y, z, q are stored in registers $s1-$s4. You may use the other registers to hold
intermediate results.

x = x + y + z - q;

14. How would you implement in assembly a shift-left- logical (SLL) pseudo-
instruction for a machine that didn’t have this particular instruction? Be sure your
SLL instruction can shift up to W bits where W is the machine word size in bits.
Assume the pseudo-instruction you’re implementing for this particular answer is
SLL $s0,$s1,23

Taye G. (Asst. Professor) Page 6


15. Use the register and memory values in the tables below for the next questions.
Assume a 32-bit machine. Assume each of the following questions starts from
the table values; that is, DO NOT use value changes from one question as
propagating into future parts of the question.

Register Value Memory Address Value

$S1 12 12 16

$S2 16 16 20

$S3 20 20 24

$S4 24 24 28

a) Give the values of registers $1, $2, and $3 after this instruction is

executed:

add $3, $2, $1

b) Give the values of registers $1 and $3 after this instruction is executed:

lw $3, 12($1)

c) Give the values of registers $2 and $3 after this instruction is executed:

addi $2, $3, 16

16. Write the sequence of instructions needed to:


a) push registers $s0, $s1 and $s2 onto the stack
b) pop registers stored in part (a)

Taye G. (Asst. Professor) Page 7


17. Consider the following MIPS code and C++ code, then check that the two
programs are equivalent or not.

C++ fragment Code:

for i = 0 to 39
A[i] = 2 * i – A[i];

MIPS Code:
addi $s1, $zero, -1

Loop: addi $s1, $s1, 1

slt $s3, $s2, $s1


bne $s3, $zero, Exit

sll $t1, $s1, 1

add $t2, $t1, $t1

add $s4, $s0, $t2

lw $t3, 0($s4)

sub $t4, $t1, $t3

sw $t4, 0($s4)

j Loop

Exit:

18. Consider the following C++ code and write the equivalent MIPS code.
i = 0;
while (i < 47)
{
A[i] = i + 33;
i++;
}

Taye G. (Asst. Professor) Page 8


19. For the following sequence of MIPS instructions, identify all registers used and
their values after the code has executed. The first column of the table is filled in
as example.
li $t0,4

li $t1,7

li $t2,3

sub $t3,$t1,$t2

beq $t0,$t3, next

add $s0,$zero,$t3

j end

next:

add $s0,$t1,$t2

end:

Register Name: $t0 $t1 $t2 $t3 $s0

Register Value 4

20. Write MIPS code that adds a number in $t1 to the constant 100 and puts the
result into $t3.
21. Write MIPS code that does the computation:
F = A + (((B - C) + D) - E)
Assume that variable A is in register $t0, B is in register $t1, C is in register $t2,
D is in register $t3, E is in register $t4 and F is in register $t5 (the result).

Taye G. (Asst. Professor) Page 9


22. What is the value of $t2 after the code below is executed?

ori $t0,$0,255
addi $t1,$t0,1
sll $t2,$t1,2

23. What is the value of $t2 after the code below is executed?

ori $t0,$0,512
slt $t1,$0,$t0
bne $t1,$0,L0
ori $t2,$0,1024
j L1
L0: ori $t2,$0,2048
L1:

Taye G. (Asst. Professor) Page 10

You might also like