Chapter 2
Chapter 2
Chapter 2
CHAPTER 2
EXERCISE 2.4
The following problems deal with translating
from C 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.
a. f = -g - A[4];
b. B[8] = A[i - j]
2.4.1 For the C statement above, what is the
corresponding MIPS assembly code?
Solution:
a. lw $s0, 16($s6)
sub $s0, $0, $s0
sub $s0, $s0, $s1
b. sub $t0, $s3, $s4
slli $t0, $t0, 2
add $t0, $s6, $t0
lw $t1, 0($t0)
sw $t1, 32($s7)
2.4.2 For the C statements above, how many
MIPS assembly instructions are needed to
perform the C statement?
Solution:
a. 3
b. 5
2.4.3 For the C statement above, how many
different registers are needed to carry out the C
statement?
Solution:
a. 3
b. 6
The following problems deal with translating from
MIPS 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.
a. slli $s2, $s4, 1 // h=j*2
add $s0, $s2, $s3 // f=j*2+i
add $s0, $s0, $s1 // f=j*2+i+g
b. add $t0, $s6, $s0 //t0= &A[f/4]
add $t1, $s7, $s1 // t1= &B[g/4]
lw $s0, 0($t0) //s0= A[f/4]
addi $t2, $t0, 4 //t2= &A[f/4+1]
lw $t0, 0($t2) // t0= A[f/4+1]
add $t0, $t0, $s0 // t0= A[f/4]+ A[f/4+1]
sw $t0, 0($t1) // B[g/4]= A[f/4]+ A[f/4+1]
2.4.4 For the MIPS assembly instructions above,
what is the corresponding C statement?
Solution:
a. f = 2j + i + g;
b. B[g] = A[f] + A[1+f];
2.4.5 For the MIPS assembly instructions above,
rewrite the assembly code to minimize the
number if MIPS instructions (if possible) needed
to carry out the same function.
Solution:
a. 5 as written, 5 minimally
b. 7 as written, 6 minimally
EXERCISE 2.6
The following problems deal with translating
from C 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 arrays A and B are 4-byte words:
a. f = -g+h+B[1]
b. f=A[B[g]+1]
2.6.1 For the C statement above, what is the
corresponding MIPS assembly code?
Solution:
a. 3
b. 6
2.6.3 For the C statements above, how many
registers are needed to carry out the C statement
using MIPS assembly code?
Solution:
a. 5
b. 4
The following problems deal with translating
from MIPS to C. The following problems deal
with translating from C 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.
a. sub $s0, $s0, $s1
sub $s0, $s0, $s3
add $s0, $s0, $s1
b. addi $t0, $s6, 4
add $t1, $s6, $0
sw $t1, 0($t0)
lw $t0, 0($t0)
add $s0, $t1, $t0
The following problems deal with translating
from MIPS to C. The following problems deal
with translating from C 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.
a. sub $s0, $s0, $s1 // f=f-g
sub $s0, $s0, $s3 // f=f-g-i
add $s0, $s0, $s1 //f=f-i
b. addi $t0, $s6, 4 // t0=&(A[1])
add $t1, $s6, $0 // t1=&A[0]
sw $t1, 0($t0) // A[1]= &A[0]
lw $t0, 0($t0) // t0=&A[0]
add $s0, $t1, $t0 // f0=&A[0]+&A[0]
2.6.4 For the MIPS assembly instructions above,
what is the corresponding C statement?
Solution:
a. f = f – i;
b. f = 2 * (&A);
2.6.5 For the MIPS assembly above, assume that
the registers $s0, $s1, $s2, and $s3 contain the
values 0x0000000a, 0x00000014, 0x0000001e,
and 0x00000028, respectively. Also, assume that
register $s6 contains the value 0x00000100, and
that memory contains the following values:
Address Value
0x00000100 0x00000064
0x00000104 000000c8
0x00000108 0000012c
31 14 + i – j bits 14 0
b 111…111 Field 111…111
2.14.1 Find the shortest sequence of MIPS
instructions that extracts a field from $t0 for the
constant values i = 22 and j = 5 and places the
field into $t1 in the format shown in the data
table.
Solution:
31 14 + i – j bits 14 0
b XXX…XXX Field XXX…XXX
2.14.4 Find the shortest sequence of MIPS
instructions that extracts a field from $t0 for the
constant values i = 17 and j = 11 and places the
field into $t1 in the format shown in the data
table.
Solution:
Recursive version
FIB: L1:
addi $sp, $sp, −12 addi $a0, $a0, −1
sw $ra, 8($sp) jal FIB
sw $s1, 4($sp) addi $s1, $v0, $0
addi $a0, $a0, −1
sw $a0, 0($sp)
jal FIB
HERE:
add $v0, $v0, $s1
slti $t0, $a0, 3
EXIT:
beq $t0, $0, L1 lw $a0, 0($sp)
addi $v0, $0, 1 lw $s1, 4($sp)
j EXIT lw $ra, 8($sp)
addi $sp, $sp, 12
jr $ra
b.
At label HERE, after calling function FIB with
input of 4:
old $sp -> 0xnnnnnnnn ???
−4 contents of register $ra
−8 contents of register $s1
$sp -> −12 contents of register $a0