Answered step by step
Verified Expert Solution
Question
00
1 Approved Answer
Any help would be appreciated, tia! Given an array of at least one integer, write a program to create a new array with elements equal
Any help would be appreciated, tia! Given an array of at least one integer, write a program to create a new array with elements equal to the exponent of each element in the original array raised to the index, ie Bi Aii For this, write two functions that will be called in main function independently. exponent inputs: element Ai and index i task: returns the value of element raised to index Aii append inputs: base address of new array B B current size of B n and the new element Aii task: add the new element at the end. This function does not return any value void Following is a sample C code to perform the required task. You may modify the code for the functions, but the task performed should not be changed. int main Variable Declaration int A B; Base addresses of A and B int n n; Lengths of arrays A and B int exp; Return value from exponent function Task of main function B; th element A for int j ; j n; j n j; Current length of array B exp exponentAj j; appendB n exp; n; int exponentint x int y int exp x; for int j ; j y; j exp exp x; returnexp; void appendint B int n int exp Bn exp; Registers Variables $s A $s n $s B $s n Addresses Contents $s A $s A $sn An Example Test: If the values of $s through $s are initialized in the simulator as: Use the button under the Registers display to initialize register values for $s $s $s and the button under the Memory display to initialize the A array elements. Registers Data $s $s $s $s Addresses Contents The resultant registers will be: Registers Data $s $s The resultant array B is: Addresses Contents This is what I have, but I keep getting an erorr saying Line : Unrecognized Syntax near: data # Change your C code here if different #int main # B; th element A # for int j ; j n; j # n j; Current length of array B # exp exponentAj j; # appendB n exp; # # n; # #int exponentint x int y # int exp x; # for int j ; j y; j # exp exp x; # # returnexp; # #void appendint B int n int exp # Bn exp; # # Write comments explaining each line of your code, all the registers and memory used # Enter your MIPS code here data A: word # Example array A n: word # Length of array A B: space # Allocate space for array B assuming elements n: word # Initialize n to text globl main main: la $s A # $s base address of A lw $s n # $s nlength of A la $s B # $s base address of B lw $s n # $s ninitialize n to # Initialize B to A li $t # $t sw $t$s # B li $t # $tindex j loop: bge $t $s end # if j n exit loop # Calculate address of Aj sll $t $t # $t j byte offset add $t $s $t # $t &Aj lw $a$t # $a Aj move $a $t # $a j # Call exponent function jal exponent # Store result of exponent in Bj sll $t $t # $t j byte offset add $t $s $t # $t &Bj sw $v$t # Bj result of exponent function # Increment n and j addi $s $s # n addi $t $t # j j loop end: # Update n in memory sw $s n # Exit program li $v # syscall for exit syscall # Exponent function: calculates xy exponent: # Input: $a Aj $a j # Output: $v Ajj move $t $a # $t Aj li $v # $vinitial result exponentloop: beqz $a expend # if j exit loop mul $v $v $t # $v Aj addi $a $a # j j exponentloop expend: jr $ra # Return to caller
Any help would be appreciated, tia!
Given an array of at least one integer, write a program to create a new array with elements equal to the exponent of each element in the original array raised to the index, ie Bi Aii
For this, write two functions that will be called in main function independently.
exponent
inputs: element Ai and index i
task: returns the value of element raised to index Aii
append
inputs: base address of new array B B current size of B n and the new element Aii
task: add the new element at the end.
This function does not return any value void
Following is a sample C code to perform the required task. You may modify the code for the functions, but the task performed should not be changed.
int main
Variable Declaration
int A B; Base addresses of A and B
int n n; Lengths of arrays A and B
int exp; Return value from exponent function
Task of main function
B; th element A
for int j ; j n; j
n j; Current length of array B
exp exponentAj j;
appendB n exp;
n;
int exponentint x int y
int exp x;
for int j ; j y; j
exp exp x;
returnexp;
void appendint B int n int exp
Bn exp;
Registers Variables
$s A
$s n
$s B
$s n
Addresses Contents
$s A
$s A
$sn An
Example Test: If the values of $s through $s are initialized in the simulator as: Use the button under the Registers display to initialize register values for $s $s $s and the button under the Memory display to initialize the A array elements.
Registers Data
$s
$s
$s
$s
Addresses Contents
The resultant registers will be:
Registers Data
$s
$s
The resultant array B is:
Addresses Contents
This is what I have, but I keep getting an erorr saying Line : Unrecognized Syntax near: data
# Change your C code here if different
#int main
# B; th element A
# for int j ; j n; j
# n j; Current length of array B
# exp exponentAj j;
# appendB n exp;
#
# n;
#
#int exponentint x int y
# int exp x;
# for int j ; j y; j
# exp exp x;
#
# returnexp;
#
#void appendint B int n int exp
# Bn exp;
#
# Write comments explaining each line of your code, all the registers and memory used
# Enter your MIPS code here
data
A: word # Example array A
n: word # Length of array A
B: space # Allocate space for array B assuming elements
n: word # Initialize n to
text
globl main
main:
la $s A # $s base address of A
lw $s n # $s nlength of A
la $s B # $s base address of B
lw $s n # $s ninitialize n to
# Initialize B to A
li $t # $t
sw $t$s # B
li $t # $tindex j
loop:
bge $t $s end # if j n exit loop
# Calculate address of Aj
sll $t $t # $t j byte offset
add $t $s $t # $t &Aj
lw $a$t # $a Aj
move $a $t # $a j
# Call exponent function
jal exponent
# Store result of exponent in Bj
sll $t $t # $t j byte offset
add $t $s $t # $t &Bj
sw $v$t # Bj result of exponent function
# Increment n and j
addi $s $s # n
addi $t $t # j
j loop
end:
# Update n in memory
sw $s n
# Exit program
li $v # syscall for exit
syscall
# Exponent function: calculates xy
exponent:
# Input: $a Aj $a j
# Output: $v Ajj
move $t $a # $t Aj
li $v # $vinitial result
exponentloop:
beqz $a expend # if j exit loop
mul $v $v $t # $v Aj
addi $a $a # j
j exponentloop
expend:
jr $ra # Return to caller
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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