如何在MIPS中打印完整数组,并对数组的某些整数求和

时间:2012-02-08 09:01:21

标签: arrays assembly integer sum mips

我遇到了以下用于作业的代码的问题。注意,我不希望任何人只是给我代码,我真的想了解MIPS。我正在使用QTSpim模拟器来运行我的MIPS代码。

以下代码应该允许用户从键盘输入10个整数,然后取这些整数并求和小于第一个输入整数的整数(即10,9,8,7,6,5) ,4,3,2,1将除10之外的所有数字相加,等于45)。然后,程序应按照给定的顺序输出数组。目前,我的代码允许用户输入10个整数,但随后会发生有趣的事情。它以我无法遵循的方式对数字求和(最常见的总和是0,4和50),然后只打印4个整数的数组列表(看起来如下:firstNumber,secondNumber, lastNumber,10)我真的很困惑为什么会这样。此外,对于某些整数实例,它将创建一个无限循环输出0.

我已经在这几个小时了,有人可以给我一些建议或指点吗? 所有帮助表示赞赏。谢谢!

# DATA DECLARATION

    .data
request:    .asciiz "Enter an integer:\n"
sumLine:    .asciiz "The sum is: "
aList:      .asciiz "The array contains the following: \n"
return:     .asciiz "\n"
array:      .word 0
aLength:    .word 10
input:      .word 0
count:      .word 0
count2:     .word 0
count3:     .word 0
sum:        .word 0
next:       .word 0
first:      .word 0
one:        .word 1

# PROGRAM CODE

    .text
    .globl main

# PROGRAM EXECUTION

#--------------------------------------------------------------------------
# Procedure main
# Description: Initializes registers and prints the final sum.
# parameters: $s0 = address of array, $t0 =  length
# return value: $v0 = sum
# registers to be used: $s0, $t0, $t1, $t2, $t4, $t5, $v0
#--------------------------------------------------------------------------
main:

la  $s0, array          # s0 = array
lw  $t0, aLength            # t0 = aLength
lw  $t1, count          # t1 = count
lw  $t2, input          # t2 = input
lw  $t3, count2         # t3 = count2
lw  $t4, count3         # t4 = count3
lw  $t5, sum            # t5 = sum
lw  $t6, first          # t6 = first
lw  $t7, next           # t7 = next
lw  $t9, one            # t9 = one

beq $t1, $zero, readArray       # if count=0, goto readArray procedure

la  $a0, sumLine            # load line to print
li  $v0, 4              # print sum output line
syscall
lw  $a0, sum            # load sum to print
li  $v0, 1              # print sum
syscall
la  $a0, return         # load line to print
li  $v0, 4              # print return line
syscall
la  $a0, aList          # load line to print
li  $v0, 4              # print the array list line
syscall

j printArray


#--------------------------------------------------------------------------
# Procedure readArray
# Description: Reads integers from the keyboard.
# parameters: $s0 = address of array, $t0 =  length
# return value: --------
# registers to be used: $v0, $a0, $t0, $t1, $t2, $s0
#--------------------------------------------------------------------------
readArray:

beq $t1, $t0, sumSmallerThanFirst   # if t1=t0 (count = aLength) goto sum procedure
la  $a0, request            # load line to print
li  $v0, 4              # print request line
syscall
li  $v0, 5              # read integer from keyboard
syscall
sw  $v0, input          # store integer to input
lw  $t2, input          # t2 = input
sw  $t2, 0($s0)         # array[i] = t2
addi    $s0, $s0, 4         # increment array (i++)
addi    $t1, $t1, 1         # increment count (count+1)
sw  $t1, count          # store count to t1
beq $t1, $t9, store         # if t1=t9 (count = one) goto store 

j readArray

store:

lw  $t6, 0($s0)         # t6 = array[i]
sw  $t6, first          # t6 = first    

j readArray

#--------------------------------------------------------------------------
# Procedure sumSmallerThanFirst
# Description: Sums the inputted integers if they are < the first integer.
# parameters: $s0 = address of array, $t0 =  length
# return value: ----------
# registers to be used: $s0, $t0, $t3, $t5, $t6, $t7, $t8, $0
#--------------------------------------------------------------------------
sumSmallerThanFirst:

la  $s0, array
beq $t3, $t0, main          # if count=length, goto main
lw  $t7, 0($s0)         # t7 = array[i]
sw  $t7, next           # t7 = next
slt $t8, $t7, $t6           # if t7<t6, t8=1
addi    $s0, $s0, 4         # array[i++]
addi    $t3, $t3, 1         # count+1
sw  $t3, count2         # store count2 to t3

beq $t8, $zero, sumSmallerThanFirst # if t8=0, goto top sum

add $t5, $t5, $t7           # t5=t5+t6 (sum = sum + array[i]) 
sw  $t5, sum            # store sum to t5

j sumSmallerThanFirst


#--------------------------------------------------------------------------
# Procedure printArray
# Description: Prints out the array of inputted integers.
# parameters: $s0 = address of array, $t0 =  length
# return value: -------------
# registers to be used: $v0, $t0, $t4, $t6, $s0
#--------------------------------------------------------------------------
printArray:

beq $t4, $t0, Exit          # if count=length, goto Exit
lw  $t7, 0($s0)         # t7 = array[i]
sw  $t7, next           # t7 = next
lw  $a0, next           # load array[i] to print
li  $v0, 1              # print array[i]
syscall
la  $a0, return         # load line to print
li  $v0, 4              # print return line
syscall
addi    $s0, $s0, 4         # array[i++]
addi    $t4, $t4, 1         # count+1
sw  $t4, count3         # store count3 to t4

j printArray

Exit:

jr $ra                  # return

1 个答案:

答案 0 :(得分:0)

与我在同一位置上我认为完全相同的任务。

关于数组......

如果你将数组定义为空格,它会给你一些奇怪的问题......如果你把它定义为单词。你不会遇到这样的问题。我想这是因为内核中空格和字的差异对齐,因为我注意到了内核级别的不同操作。

然而......关于总和......我不断得到荒谬的答案,主要是0,1或-1。