Invert an array of integer and find min

Viewed 87

I need to show the original array elements, which is {121, -13, 82, 20, 65, 85, -54, 25, 99, -320}. Next, to show the inversion array of elements, which is {-320, 99, 25, -54, 85, 65, 20, 82, -13, 121} and the minimum value of the array, which is -320. I need to use function to perform the inversion and the minimum value. I am stuck in the inversion array

    .data
    array: .word 121, -13, 82, 20, 65, 85, -54, 25, 99, -320    # Define array with elements inside
    length: .word 10
    Msg1: .asciiz "Original array:"
    Msg2: .asciiz "New array:"
    Msg3: .asciiz "The minimum element = "
    NewLine: .asciiz "\n"

.text
.globl _start
_start:

    # Print Msg1
    la $a0, Msg1
    li $v0, 4
    syscall

    # Print NewLine
    la $a0, NewLine
    li $v0, 4
    syscall

    addi $t0, $zero, 0  # Clear $t0 to 0

    while:
        beq $t0, 40, exit   # Exit the while loop when all 40 bits are done

        lw $t1, array($t0)

        addi $t0, $t0, 4    # 4 is added to the index

        li $v0, 1   # Print the element
        addi $a0, $t1, 0    # Value of element from $t1 move to $a0
        syscall 

        # Print new line
        la $a0, NewLine
            li $v0, 4
            syscall

        j while
    exit:

    jal swap    # Call funcrion swap

    Terminate: 
        # End of program
        li $v0, 10
        syscall

    swap:       # The swap function
    # Print NewLine
    la $a0, NewLine
    li $v0, 4
    syscall

    # Print Msg2
    la $a0, Msg2
    li $v0, 4
    syscall 

    # Print NewLine
    la $a0, NewLine
    li $v0, 4
    syscall


        addi $sp, $sp, -4   # Prepare stack 
        sw $ra, 0($sp)      # Push
        bltz $a0, end_swap
        lb $t0, 0($a1)
        subi $a0, $a0, 1
        subi $a1, $a1, 1
        sb $t0, 0($a2)
        addi $a2, $a2, 1
        jal swap

        # Print new line
        la $a0, NewLine
            li $v0, 4
            syscall

            end_swap:
            lw $ra, 0($sp)
            addi $sp, $sp, 4
        jr $ra  

Below is the find minimum value I have:

    # Print NewLine
    li      $v0,4
        la      $a0, NewLine
        syscall 

    la      $s0, array                 
        addi    $s1,$s0,40              # end = 40 
        add     $s3,$zero,$zero         # total = 0

        lw      $s2,0($s0)              # min = array[0]

check_min:
        beq     $s0,$s1,min_done       # if (p == end) goto L2

        lw      $t0,0($s0)              # $t0 = *p
        addi    $s0,$s0,4               # p++

        add     $s3,$s3,$t0             # total += $t0

        slt     $t2,$s2,$t0             # *p < min?
        bne     $t2,$zero, check_min     # no, loop

        move    $s2,$t0                 # set new/better min value
        j       check_min

min_done:
        li      $v0, 4
        la      $a0, Msg3
        syscall

        li      $v0,1
        move    $a0,$s2                 # get min value
        syscall

        li      $v0,4
        la      $a0, NewLine
        syscall

Any helps and comments are welcome, thank you!

1 Answers

I have modified your code to make it work correctly for the inversion of array:

.data
array: .word 121, -13, 82, 20, 65, 85, -54, 25, 99, -320    # Define array with elements inside
length: .word 10
Msg1: .asciiz "Original array:\n"
Msg2: .asciiz "\nNew array:\n"
NewLine: .asciiz "\n"
space: .asciiz " "

.text
.globl main
.ent main 
main:

# Print Msg1
la $a0, Msg1
li $v0, 4
syscall

addi $t0, $zero, 0  # Clear $t0 to 0

while:
    beq $t0, 40, exit   # Exit the while loop when all 40 bits are done
    lw $t1, array($t0)
    addi $t0, $t0, 4    # 4 is added to the index
    li $v0, 1   # Print the element
    addi $a0, $t1, 0    # Value of element from $t1 move to $a0
    syscall 

    # Print space
    la $a0, space
    li $v0, 4
    syscall
  j while

exit:
# Print new line
    la $a0, NewLine
    li $v0, 4
    syscall

jal swap    # Call function swap

Terminate: 
    # End of program
    li $v0, 10
    syscall

.end main

.globl swap
.ent swap
swap:       # The swap function
# Print Msg2
la $a0, Msg2
li $v0, 4
syscall 
    
li $t5,-4
li $t0, 36
    
print_inv:
    beq $t0, $t5, end_swap   # Exit the while loop when all 40 bytes are done
    lw $t1, array($t0)
    addiu $t0, $t0, -4    # 4 is subtracted to the index

    li $v0, 1   # Print the element
    addi $a0, $t1, 0    # Value of element from $t1 move to $a0
    syscall 

    # Print space
    la $a0, space
    li $v0, 4
    syscall

    j print_inv

 end_swap:
    jr $ra  
 
 .end swap
Related