In: Computer Science
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i
---------------------------------------------------------------------------------------------------------------------------
Convert Java program to MIPS
What is the address that has been used by the simulator for this array?
Make sure it works on MIPS
Greetings!!
Code:
Assuming that the last 0 in the array indicate the end of the array.
#DATA SEGMENT
.data
A: .word 11,12,-10,13,9,12,14,15,-20,0
max_message: .asciiz "The maximum is "
sum_message: .asciiz "\nThe sum is "
#CODE SEGMENT
.text
#MAIN STARTS HERE
main:
la $a0,A #load address of A
#CALLING TO FUNCTION MAX
jal Max
move $t0,$v0 #return from Max and copy the answer to t0
la $a0,max_message #load the address of the message
li $v0,4 #parameter for string display
syscall #system call for string display
add $a0,$0,$t0 #copy the Max value to a0 for display
li $v0,1 #parameter for display integer
syscall #system call for display integer
la $a0,A #load address of A
#CALLING TO FUNCTION SUM
jal Sum
move $t0,$v0 #return from Sum and save the sum to t0
la $a0,sum_message #load the address of the message
li $v0,4 #parameter for display string
syscall #system call for display string
add $a0,$0,$t0 #load the value of sum to a0 for display
li $v0,1 #parameter for display integer
syscall #system call for display integer
li $v0,10 #parameter for standard termination
syscall #system call for termination
#MAX FUNCTION DEFINITION
Max:
lw $t0,0($a0) #load the first number and assume that is max
move $t1,$t0 #copy to t1
loopm:
beq $t1,$0,endm #if the end of the array then goto label endm
addi $a0,$a0,4 #increment array index
lw $t1,0($a0) #load the next number
bgt $t0,$t1,loopm #if the new number is greater than old number
add $t0,$0,$t1 #else set the new number as max
j loopm #repeat
endm:
add $v0,$0,$t0 #load the max number to v0 for return to main
jr $ra #return to main
#MAX FUNCTION END
#SUM FUNCTION DEFINITION
Sum:
lw $t0,0($a0) #load the first number
move $t1,$t0 #copy to t1
loops:
beq $t1,$0,ends #if end of the array then ends
addi $a0,$a0,4 #else increment the array index
lw $t1,0($a0) #read the next number
add $t0,$t0,$t1 #calculate the sum
j loops #repeat
ends:
add $v0,$0,$t0 #load the sum to v0 for return to main
jr $ra #return to main
#FUNCTION SUM END
Output screenshot:
Hope this helps