Question
2.29 The following is the C codes and the translated MIPS codes. Assume that the C-level integers t1 and a0 are held in register $t1
2.29 The following is the C codes and the translated MIPS codes. Assume that the C-level integers t1 and a0 are held in register $t1 and $a0, and $s0 holds the base address of the integer MemArray.
void foo () {
int MemArray[100] = { 96, 98, 63, 69, 42, 27, 16, 6, 47, 74, 44, 33, 76, 7, 88, 33, 80, 86, 86, 64, 17, 67, 60, 51, 2, 61, 93, 87, 49, 98, 24, 98, 30, 65, 2, 26, 65, 28, 66, 84, 14, 4, 50, 27, 61, 27, 80, 75, 47, 19, 57, 6, 48, 74, 62, 60, 41, 7, 35, 77, 76, 84, 14, 34, 56, 52, 15, 55, 45, 47, 20, 55, 81, 99, 89, 65, 42, 39, 56, 20, 77, 58, 35, 55, 22, 4, 45, 22, 4, 77, 91, 53, 26, 42, 56, 42, 96, 71, 99, 82};
int a0=0;
for (int t1=0; t1<100; t1++) {
a0 += MemArray[t1];
}
cout << a0 << endl;
}
.data
MemArray: .word 96, 98, 63, 69, 42, 27, 16, 6, 47, 74, 44, 33, 76, 7, 88, 33, 80, 86, 86, 64, 17, 67, 60, 51, 2, 61, 93, 87, 49, 98, 24, 98, 30, 65, 2, 26, 65, 28, 66, 84, 14, 4, 50, 27, 61, 27, 80, 75, 47, 19, 57, 6, 48, 74, 62, 60, 41, 7, 35, 77, 76, 84, 14, 34, 56, 52, 15, 55, 45, 47, 20, 55, 81, 99, 89, 65, 42, 39, 56, 20, 77, 58, 35, 55, 22, 4, 45, 22, 4, 77, 91, 53, 26, 42, 56, 42, 96, 71, 99, 82
.text
la $s0, MemArray # s0 = base address of MemArray
add $a0, $0, $0 # a0 = 0
add $t1, $0, $0 # t1 = 0
LOOP:
lw $s1, 0($s0) # s1 = *MemArray
add $a0, $a0, $s1 # a0 += *MemArray
addi $s0, $s0, 4 # MemArray++
addi $t1, $t1, 1 # t1++
slti $t2, $t1, 100 # if (t1<100) go to LOOP
bne $t2, $0, LOOP #
RESULT:
li $v0, 1
syscall
Rewrite the loop from Exercise 2.29 with memory pointers directly (instead of array) index to reduce the number of MIPS instructions executed.
Hints: You may use the following C codes and translate them to the MIPS codes.
int a0=0;
for ( int* p = MemArray; p a0 += *p; } cout << a0 << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started