0% found this document useful (0 votes)
21 views

COE480 - Lecture3 Exercises MIPS

Uploaded by

10121258
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

COE480 - Lecture3 Exercises MIPS

Uploaded by

10121258
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

COE480

Computer Architecture
Instruction Set Architecture
MIPS

Lecture 3
Exercises
1
Exercise 1
• 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.
a) For the following MIPS assembly instruction, what is the
corresponding C statement?
lw $s0, 4($s6)

b) For the following C statement, what is the corresponding


MIPS assembly code?
f = g - A[B[4]];
2
Exercise 1
lw $s0, 4($s6) Address Value
• The address of the loaded data to $s0 is: value in $s6 A[0]
4 + $s6 +1 x 4 A[1]
+2 x 4 A[2]
• Then f = A[1]
+3 x 4 A[3]
+4 x 4 A[4]]
.
.
.

3
Exercise 1 - Solution
f = g - A[B[4]]

Method:
Step1: find the address of B[4]
Step2: load the data stored in memory at the address B[4] and
store it in a register
Step3: use the loaded data to compute the address of A[B[4]]
Step4: load the data stored in memory at the computed
address and store it in a register
Step5: subtract the stored data from the data in $s1

4
Exercise 1 - Solution
f = g - A[B[4]] Address Value
Step1: find the address of B[4] value in $s7 B[0]
- The base address of B is in $s7, then the +1 x 4 B[1]
address of B[4] is: +2 x 4 B[2]
4x4 + the value in $s7 => offset = 16 +3 x 4 B[3]
+4 x 4 B[4]
.
Step2: load the data stored in memory at the .
address (16+ $t0) and store it in a register .

lw $t0, 16($s7) // $t0 = B[4]

5
Exercise 1 - Solution
f = g - A[B[4]] Address Value
Step3: use the loaded data to compute the value in $s6 A[0]
address of A[B[4]]: +1 x 4 A[1]
• The value of B[4] is in $t0 +2 x 4 A[2]
.
• Then, the offset of A[B[4]] from A[0] is 4x$t0
.
• Shift left by i is a multiplication by 2 i .
shift by 2  x 4 +B[4] x 4 A[B[4]]

• The address of A[B[4]] = offset + A[0] address .


.
.
lw $t0, 16($s7) // $t0 = B[4]
sll $t0, $t0, 2 // $t0 = $t0 *4 = B[4]*4
add $t0, $t0, $s6 // $t0 = &A[$t0] = &A[B[4]]
6
Exercise 1 - Solution
f = g - A[B[4]] Address Value
Step4: load the data stored in memory at value in $s6 A[0]
the computed address and store it in a +1 x 4 A[1]
register +2 x 4 A[2]
.
Step5: subtract the stored data from the .
data in $s1 .
+B[4] x 4 A[B[4]]
.
.
lw $t0, 16($s7) // $t0 = B[4] .
sll $t0, $t0, 2 // $t0 = $t0 *4 = B[4]*4
add $t0, $t0, $s6 // $t0 = &A[$t0] = &A[B[4]]
lw $t0, 0($t0) // $t0 = A[$t0 ] = A[B[4]]
7
sub $s0, $s1, $t0 // f = g - f = g - A[B[4]]
Exercise 2
• 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.

translate from C to MIPS the following instructions:


a) f = - g - A[4];
b) B[8] = A [ i - j ];

8
Exercise 2- Solution
a) f = - g - A[4]; Address Value
- Find the address of A[4]: offset + $s6 = 16+$s6 value in $s6 A[0]
- Load data from memory to a register as +1 x 4 A[1]
arithmitic operations are register operand +2 x 4 A[2]
operations +3 x 4 A[3]
- Subtract the loaded value from “0“ to get –A[4] +4 x 4 A[4]
.
- Subtract the value of g in $s1 from –A[4]
.
lw $s0, 16($s6) .

sub $s0, $0, $s0


sub $s0, $s0, $s1

9
Exercise 2- Solution
b) B[8] = A [ i - j ];
Address Value
- Find i – j and store the value in a register
value in $s6 A[0]
- Find the address of A[i-j]:
+1 x 4 A[1]
- Offset = 4 x (i-j) +2 x 4 A[2]
- Address A[i-j] = offset + base address of A[0] .
- Load data from memory to a register as .
.
arithmitic operations are register operand
operations +(i-j) x 4 A[i-j]
.
- Subtract the loaded value from “0“ to get –A[4] .
- Subtract the value of g in $s1 from –A[4] .
sub $t0, $s3, $s4 // $t0 = i-j
sll $t0, $t0, 2 // $t0 =4x( i-j)
add $t0, $s6, $t0 // $t0 = A[$t0] + 4x( i-j)
lw $t1, 0($t0)
sw $t1, 32($s7) 10
Exercise 2- Solution
b) B[8] = A [ i - j ];
Address Value
- Find i – j and store the value in a register
value in $s6 A[0]
- Find the address of A[i-j]:
+1 x 4 A[1]
- Offset = 4 x (i-j)
+2 x 4 A[2]
- Address A[i-j] = offset + base address of A[0]
.
- Load data from memory to a register .
.
- Find the address of B[8] (&B[8]) = 4x8 +&B[0]
+(i-j) x 4 A[i-j]
= 32 +&B[0]
.
- Store the data in the register to memory at .
address B[8] .
sub $t0, $s3, $s4 // $t0 = i-j
sll $t0, $t0, 2 // $t0 =4x( i-j)
add $t0, $s6, $t0 // $t0 = A[$t0] + 4x( i-j)
lw $t1, 0($t0) 11
sw $t1, 32($s7)
Exercise 3
Translate the following MIPS statement 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.
sll $t0, $s0, 2
add $t0, $t0, $s6
lw $t1, 0($t0)
lw $t0, 4($t0)
add $t0, $t0, $t1
sll $t1, $s1, 2
add $t1, $t1, $s7
sw $t0, 0($t1) 12
Exercise 3 - Solution
Address Value
sll $t0, $s0, 2 value in $s6 A[0]
add $t0, $t0, $s6 Finding addresses to
+1 x 4 A[1]
lw $t1, 0($t0) load data from memory
+2 x 4 A[2]
lw $t0, 4($t0) .
.
add $t0, $t0, $t1 Adding the loaded data .

sll $t1, $s1, 2 +fx4 A[f]


add $t1, $t1, $s7 + (f+1) x 4 A[f+1]
sw $t0, 0($t1) .
.
.

sll $t0, $s0, 2  shift left two steps  multiplication of f by 4  $t0 = 4×f
add $t0, $t0, $s6  $t0 = address of A[0] + 4 × f = &A[f]
lw $t1, 0($t0) $t1 = A [f]
lw $t0, 4($t0) $t0 = A [f+1]
13
add $t0, $t0, $t1  $t0 = A [f+1] + A [f]
Exercise 3 - Solution
Address Value
sll $t0, $s0, 2 value in $s7 B[0]
add $t0, $t0, $s6 +1 x 4 B[1]
lw $t1, 0($t0) +2 x 4 B[2]
lw $t0, 4($t0) .
.
add $t0, $t0, $t1 .

sll $t1, $s1, 2 Finding addresses to +gx4 B[g]


add $t1, $t1, $s7 store data from memory + (g+1) x 4 A[g+1]
sw $t0, 0($t1) .
.
.

sll $t1, $s1, 2  shift left two steps  multiplication of f by 4  $t1 = 4×g
add $t1, $t1, $s7  $t0 = address of B[0] + 4 × g  &B[g]
sw $t0, 0($t1)) B[g] = $t0 = A [f+1] + A [f]

14
Exercise 4
Translating from C to MIPS.
Write a minimal sequence of MIPS assembly instructions that
does the identical operation of the following C-level logical
statement:
A = C[0] << 4;
Assume A is assigned to $t1 and $s1 is the base of Array C

C[0] lw $t3, 0($s1)

A = C[0] << 4 sll $t1, $t3, 4

15
Exercise 5
The table below shows 32-bit values of an array stored in memory.
Address Data
24 2 Array[0]

28 4 Array[1]
32 3 Array[2]
36 6 Array[3]
40 1 Array[4]

a) For the memory locations in the table above, write C code to sort
the data from lowest to highest, placing the lowest value in the
smallest memory location shown in the figure. Assume that the data
shown represents the C variable called Array, which is an array of
type int, and that the first number in the array shown is the first
element in the array. Assume that this particular machine is a byte-
addressable machine and a word consists
16
of four bytes.
Exercise 5
The table below shows 32-bit values of an array stored in memory.
Address Data
24 2 Array[0]

28 4 Array[1]
32 3 Array[2]
36 6 Array[3]
40 1 Array[4]

b) For the memory locations in the table above, write MIPS


code to sort the data from lowest to highest, placing the lowest value
in the smallest memory location. Use a minimum number of MIPS
instructions. Assume the base address of Array is stored in register
$s6.

17
Exercise 5
a) C code
Address Data
24 2 Array[0]
28 4 Array[1]
32 3 Array[2]
36 6 Array[3]
40 1 Array[4]

temp = Array[0];
temp2 = Array[1];
Array[0] = Array[4];
Array[1] = temp;
Array[4] = Array[3];
Array[3] = temp2; 18
Exercise 5
a) MIPS code
Address Data
the base address
of Array is stored $s6 24 2 Array[0]
in register $s6. 4+$s6 28 4 Array[1]
8+$s6 32 3 Array[2]
12+$s6 36 6 Array[3]
16+$s6 40 1 Array[4]

lw $t0, 0($s6) #$t0 = Array[0] = 2


lw $t1, 4($s6) #$t1 = Array[1] = 4
lw $t2, 16($s6) #$t2 = Array[4] =1
sw $t2, 0($s6) # Array[0] = Array[4] =1
sw $t0, 4($s6) # Array[1] = 2
lw $t0, 12($s6) # t0 = Array[3] = 6
sw $t0, 16($s6) # Array[4] = 6 19
sw $t1, 12($s6) # Array[3] = 4

You might also like