Question: This mips code: # initialize B [ 0 ] to 1 ( A [ 0 ] ^ 0 = 1 ) addi $t 0 ,

This mips code:
# initialize B[0] to 1(A[0]^0=1)
addi $t0, $t0,1
sw $t0,0($s2)
# initialize n2 to 1(since B[0] is already set)
ori $s3, $zero, 1
# initialize loop var j to 1
ori $t1, $zero, 1
start:
# exit loop if j >= n1
bge $t1, $s1, loopend
# calculate A[j] address and load A[j]
sll $t2, $t1,2 # offset = j*4
add $t3, $s0, $t2 # find address of A[j]
lw $t4,0($t3) # load A[j] into t4
# find exponent(A[j],
j)
move $a0, $t4 # a[j]= argument 1
move $a1, $t1 # j = arg 2
jal exp # call to exponent callee
move $t5, $v0 # store result (v0) in t5
#move result into B
move $a0, $s2 # pass base adress of B
move $a1, $s3 # pass length of B (n2
move $a2, $t5 # store result into t5
jal append # call to append func
addi $s3, $s3,1 #n2++
#increment loop counter (j)
addi $t1, $t1,1
j start
# exponent func - int exp(intx, inty)
exp:
move $t0, $a0 # move x to t0
move $t1, $a0 # y to t1
ori $t2, $zero, 1 # exp =1
beq $t1, $zero, exp_end #if y =0, return 1
exploop:
mul $t2, $t2, $t0 # exp *=x
addi $t1, $t1,-1 # y =-1
slt $t3, $zero, $t1 # t3=1 if t1=0
bne $t3, $zero, exploop # t3!=0, repeat until y=0
exp_end:
move $v0, $t2 # return val in v0
jr $ra
append:
sll $t0, $a1,2 # find offset = n2*4
add $t1, $a0, $t0 # calc adress of B[n2]
sw $a2,0($t1) #store exp at B[n2]
jr $ra
loopend:
/////////////////////
Is supposed to mimic the following C pseudocode:
# Change your C code here (if different)
#int main(){
# B[0]=1; //0th element = A[0]^0=1
# for (int j =1; j < n1; j++){
# n2= j; // Current length of array B
# exp = exponent(A[j], j);
# append(B, n2, exp);
# }
# n2++;
# }
#int exponent(int x, int y){
# int exp = x;
# for (int j =1; j < y; j++){
# exp = exp * x;
# }
# return(exp);
#}
#void append(int* B, int n2, int exp){
# B[n2]= exp;
#}
Where register s0 is A, s1 is n1, s2 is B, and s3 is n2.
I cannot find a reason as to why it hasn't been outputting the expected values i've tested it with,
If someone (NOT CHATGPT PLS) could help me with this it'd be greatly appreciated.
Registers Variables
$s0 A
$s1 n1
$s2 B
$s3 n2
Addresses Contents
$s0 A[0]
$s0+4 A[1]
......
$s0+4*(n-1)// A[n-1]
Gets tested with:
Registers Data
$s04000
$s15
$s28000
$s30
Addresses Contents
400010
40045
4008-5
4012-2
40160
Expect to get:
The resultant registers will be:
Registers Data
$s28000
$s35
The resultant array B is:
Addresses Contents
80001
80045
800825
8012-8
80160

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!