2.22 Exercises 2.3 15) For the following C statement, what is the corresponding
ID: 2248111 • Letter: 2
Question
2.22 Exercises 2.3 15) 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, $$1, 5$2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers s6 and $s7, respectively. 2.4 15) For the MIPS assembly instructions below, what is the corresponding C statement? Assume that the variables f, g, h, 1, and j are assigned to registers $ s0,Ss1,5s2, ss3, and ss4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively. s11 add $t0 sto. $50.2 $$6, $t0 # $to-f #sto-, &A;[f] addi St2. St0, 4 add sto. StOExplanation / Answer
2.3 Solution : B [8] = A[ij];
Let k = i – j
then B [8] = A[k];
1.sub $t0, $s3, $s4 # k = i – j
2. add $t0, $t0, $t0 #$t0 = $t0 +$t0 = 2$t0 = 2k
3. add $t0, $t0, $t0 #$t0 = $t0 +$t0 = 4$t0 = 4k
4. add $t1, $t0, $s6 #$t1 = 4k + $s6
5. lw $t2, 0($t1) #$t2 = Memory [0 + 4k + $s6]
6. sw $t2, 32($s7) # Memory [32 + $s7] = $t2
# B [8] is in spot 32 from $s7
2.4 Solution:
Here Base address of A=$s6
Base address of B=$s7
sll $t0, $s0, 2 # $t0 = f * 4 result as multiplying by 2^n where n=2
add $t0, $s6, $t0 # $t0 = &A[f] adds the value of $t0 to basesaddress of A
sll $t1, $s1, 2 # $t1 = $s1 * 2^i where i is 2
add $t1, $s7, $t1 # $t1 =&B[g] Indicates address of B5.
lw $s0, 0($t0) # f = A[f] copies the content of A[f] into $s0;loads data from memory
addi $t2, $t0, 4 # $t2 =&A[f] + 4 this is the same as A[g]
lw $t0, 0($t2) #$t0 = A[g]
add $t0, $t0, $s0 #$t0 = $t0 + for $t0 = A[g] +A[f]
sw $t0, 0($t1) #B[g] = $t0
B[3] $s7+12 A [3] $s6+12 B[2] $s7+8 A [2] $s6+8 B[1] $s7+4 A [1] $s6+4 B[0] $s7 A [0] $s6Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.