COE480 - Lecture3 Exercises MIPS
COE480 - Lecture3 Exercises MIPS
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)
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 .
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]]
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) .
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 $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 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
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]
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]