MIPS(数组和循环)

时间:2015-05-13 04:14:15

标签: arrays loops mips

所以我正在开发一个项目,询问用户数字(整数),然后按升序重新排列并打印出来。 现在我被卡住了,因为我想按降序打印它们但是在一个单独的数组中使用相同的数字。谁能帮我吗? 到目前为止这是我的代码,用于升序数字: (顺便说一句,这些评论只适合我的指导)

.data 
array1: .space 100
array2: .space 100
array3: .space 100

msg1: .asciiz "Enter at least 4 integers: Enter the number 500 to exit \n"
msg2:  .asciiz "Numbers you entered are: \n"
msg3: .asciiz "The numbers you entered in ascending order are: \n"
commas: .asciiz ","
msg4: .asciiz "The numbers you entered in descending order are: \n"

.text
.globl main
main:
la $a1, array1  #load pointer to array1
la $a2, array2  ##load pointer to array2
la $a3, array3  #load pointer to array3
li $t1, 500                   # once 500 is entered it will exit the input and produce an output
li $t0, 0


loopset:

la $a0, msg1    #loads msg1 text into $a
li $v0, 4        #loads 4 into $v0 (prints string) 
syscall         
li $v0, 5           #loads 5 into $v0 (read interger)
syscall        
beq $v0,$t1,swap
addi $t0,$t0,4      #add 4 to $t0, save to $t0
sw $v0, ($a1)       #stores input into array
addi $a1, $a1,4     #add 4 to $a1, save to $a1
j loopset


swap:

la $t4, array1      #loads array1 to $t4
la $t1, array1      #loads array1 to $t1
addi $t1, $t1, 4    #add 4 to $t1, save to $t1 
la $t8, array1      #loads array to $t8
add $t8,$t0,$t8     #add $t8 to $t0, save to $t8
la $t9,array1   #loads array1 to $t9 
add $t9, $t0, $t9   #add $t9 to $t0, save to $t9
addi $t9, $t9, -4   #subtracts 4 from $t9, save to $t9

loop1:

lw $t2, ($t4)       #load input into $t2
lw $t3, ($t1)       #load input into $t3
blt $t2, $t3, loop2     #if $t2 > $t3, go to loop2
sw $t3, ($t4)       #store $t3 in $t4
sw $t2, ($t1)       #store $t2 in $t1

loop2:

addi $t1, $t1, 4        #add 4 to $t1, save to $t1
blt $t1, $t8, loop1     #if $t1< $t8, go to loop1
addi $t4, $t4, 4        #add 4 to $t4, save to $t4
move $t1, $t4
addi $t1, $t1, 4        #add 4 to $t1, save to $t1
blt $t4, $t9, loop1     #if $t4 < $t9, to go loop1


print:

la $a1, array1          #loads array to $a1
la $a0, msg3    #loads msg3 to $a0
li $v0, 4
syscall
la $a0, array1      #loads array1 to $a0
li $v0, 4           #loads 4 into #v0
syscall

loop3:

blez $t0, EXIT       #if $t0 <= 0, go to EXIT
li $v0, 1           #loads 1 into $v0
lw $a0, 0($a1)      #load an input into $a0
syscall
la $a0, commas      #loads commas into $a0
li $v0, 4           #loads 4 into $v0
syscall
addi $a1, $a1, 4    #add 4 to $a1, save to $a1
addi $t0, $t0, -4   #subtracts 4 from #t0, save to $t0
j loop3


EXIT:
li $v0,10              #exit 
syscall

1 个答案:

答案 0 :(得分:0)

尝试将所有blt(分支小于)更改为bgt(分支大于)并假设您的程序正常工作,应该按降序获取数组。如果你希望它在降序的不同数组中,你需要复制你的loop1,loop2,loop3,并打印并更改它们的名称,从而切换bgt调用的blt调用。