Professional Documents
Culture Documents
Trần Quốc Toản - B7
Trần Quốc Toản - B7
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
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
no_positive_number:
li $v0, 4
la $a0, no_positive_msg
syscall
exit_program:
li $v0, 10 # Exit the program
syscall