Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

.

data
array: .space 400
msg: .asciiz "Enter the number of elements: "
msg1: .asciiz "Enter element "
msg2: .asciiz "Minimum positive element: "
msg3: .asciiz "Position: "
msg4: .asciiz "\n"
size: .word 0
no_positive_msg: .asciiz "No positive numbers in the array.\n"
min_pos_value: .word 2147483647
min_pos_position: .word 0

.text

#Prompt for the number of elements


li $v0, 4
la $a0, msg
syscall

#Read the number of elements from the user


li $v0, 5
syscall
move $t0, $v0
sw $t0, size # Set size

la $t1, array # Address of array[


li $t2, 0 # i = 0

#Loop to read elements into the array


input_loop:
bge $t2, $t0, find_min_positive
li $v0, 4
la $a0, msg1
syscall
li $v0, 5 # Read integer
syscall
sw $v0, ($t1) # Store the input element in the array
addi $t1, $t1, 4 # Move to a[i+1]
addi $t2, $t2, 1 # i++
j input_loop

find_min_positive:
la $t1, array # Load the address of the array into $t1
li $t5, 0 # i = 0

find_loop:
bge $t5, $t0, print_results # If i == n, print the results
lw $t4, ($t1) # Load the element from the array into $t4
bgtz $t4, update_min_positive # Branch if the element is positive
addi $t1, $t1, 4 # Move to a[i+1]
addi $t5, $t5, 1 # i++
j find_loop # Jump back to the loop

update_min_positive:
blt $t4, $0, next_iteration # If $t4 < 0, skip
lw $t6, min_pos_value # Load the value of min_pos_value into $t6
blt $t4, $t6, update_value # If $t4 < min_pos_value, update the value
addi $t1, $t1, 4 # Move to a[i+1]
addi $t5, $t5, 1 # i++
j find_loop # Jump back to the loop

update_value:
sw $t4, min_pos_value # Update the minimum positive value
sw $t5, min_pos_position # Store the index
addi $t1, $t1, 4 # Move to a[i+1]
addi $t5, $t5, 1 # i++
j find_loop # Jump back to the loop

next_iteration:
addi $t1, $t1, 4 # Move to a[i+1]
addi $t5, $t5, 1 # i++
j find_loop # Go back to the loop

print_results:
lw $t5, min_pos_position # Load the index of the minimum positive element
li $v0, 4
la $a0, msg2
syscall

#Check if min_pos_value still holds the default value


lw $t6, min_pos_value
beq $t6, 2147483647, no_positive_number # If there are no positive numbers, jump to
no_positive_number
li $v0, 1
lw $a0, min_pos_value # Print the minimum positive element
syscall
li $v0, 4
la $a0, msg4
syscall
li $v0, 4
la $a0, msg3
syscall
li $v0, 1
lw $a0, min_pos_position # Print the position of the minimum positive element
syscall
j exit_program

no_positive_number:
li $v0, 4
la $a0, no_positive_msg
syscall

exit_program:
li $v0, 10 # Exit the program
syscall

You might also like