【问题标题】:How do I use a loop to find the min and max values of an array in Assembly?如何使用循环在 Assembly 中查找数组的最小值和最大值?
【发布时间】:2019-10-24 01:17:50
【问题描述】:

我正在尝试在 Assembly 中查找并打印数组的最小值和最大值。我现在的主要问题是增加数组索引的值。当我将文件加载到 QTSimp 时,我不断收到语法错误。我确信还有更多问题,但我稍后会解决。

我的问题是为什么我不能将数组增加到下一个值?什么是语法错误?

这是我的代码:

        .data

    array:      .word   5, 7, 12, 3, 4, 9, 6, 11, 2, 10
    array_size: .word   10
    array_min:  .asciiz "\nMin: " 
    array_max:  .asciiz "\nMax: "



    .text
    .globl main

main:   

    la $a1, array       # loading memory address of array

    addi $t0, $zero, 0  # setting index incrementer to 0
    lw $s1, 0($a1)      # setting $s1 to the smallest index of the array
    lw $s2, 0($a1)      # setting $s2 to the smallest index of the array

while:

    beq $t0, 10, exit   # branch to exit if $t0 is 10
    addi $t1, $t1, 4    # too add the next four bytes for the array index

    blt array($t1), $s1, minimum    # branch to minimum if $array[$t1] < $s1 
    blt array($t1), $s2, maximum    # branch to maximum if $array[$t1] < $s2

    minimum:
        lw $s1, array($t1)
        j while
    maximum:
        lw $s2, array($t1)
        j while



    addi $t0, $t0, 1    # increment $t0 by 1

    j while             # jump to the beginning of the while loop

exit:   

    li $v0, 4           # prints the array_min string
    la $a0, array_min
    syscall

    li $v0, 1           # prints the smallest integer
    move $a0, $s1
    syscall

    li $v0, 4           # prints the array_max string
    la $a0, array_max
    syscall

    li $v0, 1           # prints the largest integer
    move $a0, $s2
    syscall


    li $v0, 10          # terminates program
    syscall

谢谢!

【问题讨论】:

  • 您收到的错误消息的确切措辞是什么?将其包含在您的问题中。
  • 对不起,它说: spim: (parser) syntax error on line 26 of file:* blt array($t1, $s1, minimum # branch to minimum if $array[$t1]
  • blt 不能取内存操作数;只有加载/存储可以做到这一点。 MIPS 是加载/存储 RISC 机器,而不是 CISC。

标签: arrays assembly max mips qtspim


【解决方案1】:

不管怎样,这里的 MIPS 代码可以做到这一点。

.data

array:      .word   5, 7, 12, 3, 4, 9, 6, 11, 2, 10
array_size: .word   10
array_min:  .asciiz "\nMin: " 
array_max:  .asciiz "\nMax: "

.text

main:   
    la $a0, array
    lw $a1, array_size
    lw $t2, ($a0) # max
    lw $t3, ($a0) # min
    loop_array:
        beq $a1, $zero, print_and_exit
        lw $t0, ($a0)
        bge $t0, $t3, not_min # if (current_element >= current_min) {don't modify min} 
        move $t3, $t0
        not_min:
        ble $t0, $t2, not_max # if (current_element <= current_max) {don't modify max}
        move $t2, $t0
        not_max:
        addi $a1, $a1, -1
        addi $a0, $a0, 4
        j loop_array

    print_and_exit:
    # print maximum
    li $v0, 4
    la $a0, array_max
    syscall

    li $v0, 1
    move $a0, $t2
    syscall 

    # print minimum
    li $v0, 4
    la $a0, array_min
    syscall

    li $v0, 1
    move $a0, $t3
    syscall 

    # exit
    li $v0, 10
    syscall

【讨论】:

    猜你喜欢
    • 2021-06-17
    • 2019-09-18
    • 2013-10-05
    • 2018-09-21
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2021-11-10
    • 2022-09-29
    相关资源
    最近更新 更多