【问题标题】:Invert an array of integer and find min反转整数数组并找到最小值
【发布时间】:2020-07-02 04:06:48
【问题描述】:

我需要显示原始数组元素,即 {121, -13, 82, 20, 65, 85, -54, 25, 99, -320}。接下来,展示元素的反转数组,即{-320, 99, 25, -54, 85, 65, 20, 82, -13, 121}和数组的最小值,即-320。我需要使用函数来执行反转和最小值。我被困在反转数组中

    .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  

下面是我找到的最小值:

    # 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

欢迎任何帮助和cmets,谢谢!

【问题讨论】:

  • 我可以自己找到最小值,但仍然卡在数组的反转中
  • 您需要检查对swap 的两次调用的参数传递。另外,请记住,当您使用系统调用进行打印时,即使只是为了调试,$a0 也会被清除。
  • 很确定你捕获的是最大值而不是最小值,否则看起来没问题。

标签: arrays mips


【解决方案1】:

我已经修改了您的代码,使其可以正常工作以进行数组的反转:

.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

【讨论】:

    猜你喜欢
    • 2022-08-10
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 2011-02-07
    • 2020-04-02
    相关资源
    最近更新 更多