533,036 views
18 votes
18 votes
Write a MIPS assembly language program that finds maximum element in an array of integers. Print the Result Array. You do not need to read elements of array from keyboard, Just declare them.

User Aminography
by
2.6k points

1 Answer

18 votes
18 votes

Answer and Explanation:

The program using Assembly language to calculate maximum value in array is given as below:

.data

space: .asciiz " "

X: .word 31, 17, 92, 46, 172, 208, 13, 93, 65, 112

N: .word 10

.text

main: la $a0, X #$a0=load address of array X

lw $a1, N #$a1=10 --number elements

jal readArray #call readArray

li $v0, 10 #exit program

syscall

readArray:

li $t0, 0 #$t0=0

li $t1, 0 #$t1=0

buc: bge $t0, $a1, final #if $t0 >= $a1 then goto final

lw $a0, X($t1) #$a0 = X(i)

li $v0, 1 #Print integer

syscall

la $a0, space #load a space: " "

li $v0, 4 #print string

syscall

addi $t1, $t1, 4 #Every 4 bytes there is an integer in the array

addi $t0, $t0, 1 #$t0=$t0+1

b buc #goto buc

final:

jr $ra #return

The algorithm which are the steps used above is:

Declares array of integers

Calls declared array

Reads declared array and compares integer values stored with if statements to make swap

prints array

Exits

User Teniqua
by
3.2k points