如何在MIPS汇编中查找字数组中的最小值和最大值

时间:2017-02-13 17:58:38

标签: arrays max mips min

我一直在努力让我的程序运行起来。我是MIPS汇编的新手,想知道如何在.word数组中找到最大值和最小值。编译时,我一直得到最少16,最多为0。到目前为止,这是我的代码:

# Write MIPS that searches an array of numbers for the largest and smallest element.
# The directive .word is used to set up an array of 20 4-byte words
# in the data section

# t1 -- contains count of elements
# t2 -- contains min value
# t3 -- contains max value
# t4 -- each word from array in turn

# data segment

.data  

anArray: .word 3, 4, 2, 6, 223, 345, 121, 92, 99, 14, 7, 76, 67, 49, 56, 43, 7, 81, 21, 31
count: .word 20
Thursday: .asciiz "Today is Thursday Feb 09, 2017"
result1: .asciiz "\n\n\nThe Minimum Value is = "
result2: .asciiz "\n\n\nThe Maximum Value is = "

# text segment

.text 
.globl main
 main:

    #System Call to print Thursday String 
la $a0, Thursday
li $v0, 4
syscall 

    #System Call to print Minimum Value String 
la $a0, result1
li $v0, 4
syscall

la $t4, anArray
li $t1, 0

    #System Call to print Minimum Value
Loop:
lb $t0, 0($t4)
blt $t0, $t2, anArrayEnd
addi $t1, $t1, 1
addi $t4, $t4, 1
j Loop
anArrayEnd:

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

    #System Call to print Maximum Value String
la $a0, result2
li $v0, 4
syscall

la $t4, anArray
li $t1, 0

    #System Call to print Maximum Value

Loop1:
lb $t0, 0($t4)
bgt $t0, $t3, anArrayEnd1
addi $t1, $t1, 1
addi $t4, $t4, 1
j Loop1
anArrayEnd1:

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

    #System Call to exit
li $v0, 10
syscall 

0 个答案:

没有答案