In: Computer Science
Has to be extremely basic, cannot use stuff like move. Very basic.
Here is what I already have and I am stuck.
.data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a value in the array " .text main: li $v0, 4 la $a0, enter #asking the user to input how many numbers they would like to enter syscall li $v0, 5 syscall #user input add $s1, $v0, $0 #declaring v0 to s1 blt $s1, $0, error #if # isn't + bgt $s1, 10, error #if # is greater than 10 beq $s1, $0, error #if # is 0 add $s0, $s1, $s2 addi $s0, $s1, -50 la $s6, myarray sw $t0, 32($s6) swap: add $t0, $a1, $0 loop: beq $t0, $t1, done addi $s0, $s0, 4 add $s2, $s2, $t5 j loop done: li $v0, 4 la $a0, large syscall #need to print biggest array here# li $v0, 10 syscall error: li $v0, 4 la $a0, invalid syscall j main
Please upvote ,comment if any query . i will change . Be safe .
Note : check attached image for output ,code compiled and tested in MARS MIPS simulator.
this program find largest element from pre defined array of size with some defined values .
Program Plan :
Program :
.data
myarray : .word
0,0,0,0,0,0,0,0,0,0
size_prompt: .asciiz "Enter array size : "
large:
.asciiz "The largest element is : "
array_prompt: .asciiz "enter array element: "
.text
main:
addi $v0, $0, 4 # system call (4) to print string
la $a0, size_prompt # put string memory address in register
$a0
syscall # print string
addi $v0, $0, 5 # system call (5) to get integer from user and
store in register $v0
syscall # get user input for variable "size"
addi $s1,$v0,0 #user input size in $s1
ble $s1,0,main #if user input size less than or equal to 0 go to
main label
bgt $s1,10,main #if user input greater than 10 go to main
label
li $t0,0 #loop count 0
la $s0,myarray #load addresss of my array in $s0
Inputloop:
beq $t0, $s1, endInputLoop #if $t0(loop index i)==$s1(size of array
)
mul $t2, $t0, 4 #t2=$t0*4 index*4
addi $v0, $0, 4 #print array_prompt string
la $a0, array_prompt
syscall
addi $v0, $0, 5 # get input from user and store in $v0
syscall
add $t3, $s0, $t2 #$t3= $s0(base address of array) + $t2(index+word
offset(4))
sw $v0, 0($t3) #myArray[$t0]=$v0
addi $t0, $t0, 1 # i = i + 1
j Inputloop # jump InputLoop
endInputLoop:
li $t0,0
loop: #loop label
beq $t1,10,printLargest #t1==10 go to printLargest label
mul $t3,$t1,4 #$t3=loop index*4
add $t3,$t3,$s0 #t3= address of first element($s0)+ $t3
lw $t2,0($t3) #$t2=myarray[$t1]
ble $t2,$t0,skip #if current element <= $t0(largest element) go
to skip
addi $t0,$t2,0 #else $t0=$t2 make $t2 largest element
skip:#skip label
addi $t1,$t1,1 #increment loop count
j loop #jump to loop
printLargest:#print Largest label
la $a0,large #print large string using syscall 4
li $v0,4
syscall
addi $a0,$t0,0 #$a0=$t0
li $v0,1 #syscall 1
syscall
#terminate program
li $v0,10 #syscall 10 to terminate program
syscall
Output : myarray: .word
2,5,49,484,44,4,473,994,384,93
Please up vote ,comment if any query .