MIPS阵列问题

时间:2013-12-07 12:45:19

标签: mips

我正在尝试编写一些代码作为输入整数和字符串,将它们存储到数组中然后按顺序打印它们(int-string,int-string等)。我最终会为整数添加一些排序代码,当我让它工作时。问题是我无法使代码正常工作,我无法设法正确输出。我假设问题在于阵列,因为我不确定它应该如何工作(MIPS绝对不是我的强项)。代码如下:

.data
 array:         .space  400 #array


 in_name:
   .asciiz "\nInsert name: "
 in_date:
   .asciiz "\nInsert date (mmdd): "
 appt:
   .asciiz "\nList: "
 spaz:      .asciiz " "

 .text
 main:      

    la  $s0, array      #load array in s0
    addi    $t0, $zero, 0       #t0=0 counter
    addi    $s1, $zero, 0       #s1=0 array size counter
            j    Input

 Input:
        li  $v0, 4           
    la  $a0, in_date
    syscall             #ask date 
    li  $v0, 5          
    syscall             #read date
    add     $t1, $zero, $t0                #offset in t1
    add     $t1, $t1, $t1           #t1*2
    add     $t1, $t1, $t1           #t1*4
    add     $s2, $t1, $s0           #array with offset in s2
    sw  $v0, 0($s2)     #save date
    addi    $t0, $t0, 1     #t0++
    addi    $s1, $s1, 1     #array size counter +1
    li      $v0, 4
    la      $a0, in_name          
    syscall                         #ask name
        li      $a0, 4                 
    li      $v0, 9
    syscall                         #space for new word (4bytes)
            la      $a0, array
            li      $a1, 4
    li      $v0, 8
    syscall                         #read name
    add     $t1, $zero, $t0                #offset in t1
    add     $t1, $t1, $t1           #t1*2
    add     $t1, $t1, $t1           #t1*4
    add     $s2, $t1, $s0           #array with offset in s2
    sw  $v0, 0($s2)     #save name
    addi    $s1, $s1, 1     #array size counter +1
    addi    $t0, $t0, 1     #t0++
    beq $s1, 10, print          #if array size=10 go to print
    j   Input               #start over until s1=10



  print:
    la  $a0, appt           
    li  $v0, 4          
    syscall             #print list
    addi    $t0, $zero, 0       #t0=0 counter

res:
    add     $t1, $zero, $t0                #offset in t1
    add     $t1, $t1, $t1           #t1*2
    add     $t1, $t1, $t1           #t1*4
    add     $s2, $t1, $s0           #array with offset in s2
    lw  $a0, 0($s2)     #load date
    li  $v0, 1          
    syscall             #print data
    addi    $t0, $t0, 1             #t0++
    la  $a0, spaz               #load space
    li  $v0, 4          
    syscall             #print space
    add     $t1, $zero, $t0                #offset in t1
    add     $t1, $t1, $t1           #t1*2
    add     $t1, $t1, $t1           #t1*4
    add     $s2, $t1, $s0           #array with offset in s2
    lw  $a0, 0($s2)     #load name
    li      $v0, 4
    syscall                         #print name
    addi    $t0, $t0, 1             #t0++
    la      $a0, spaz             
    syscall                         #print space
    addi    $t0, $t0, 1     #t0++ counter
    bne $t0, $s1, res           #start over until t0=s1
    j   end         
 end:
    li  $v0, 10         
    syscall             #the end

根据我用来测试它的程序,我得到一个错误或程序正确结束但输出错误(只是一些数字)。 有人可以向我指出我应该如何纠正它以使其正常工作? 感谢

1 个答案:

答案 0 :(得分:2)

我稍稍改变了程序。我为数字和字符串创建了2个数组。在我阅读日期和姓名时,我将它们分别保存到num_arraystr_array。这样可以更轻松地处理寻址,并且以后可能更容易处理。代码中的注释应该会有所帮助。

.data
    num_array:   .space 400 #array
    str_array:   .space 400
    in_name: .asciiz "\nInsert name: "
    in_date: .asciiz "\nInsert date (mmdd): "
    appt:    .asciiz "\nList: "
    spaz:    .asciiz " "
 .text
 main:      
    la $s0, num_array        #load array in s0
    la $s1, str_array
    addi $t0, $zero, 0       #t0=0 counter
    addi $s2, $zero, 0       #s1=0 array size counter
    j    Input

 Input:
    # prompt for date
    li  $v0, 4           
    la  $a0, in_date
    syscall

    # read date
    li  $v0, 5          
    syscall

    # store date in the num_array
    sw $v0, 0($s0)

    # increment counter and move to next position in the array
    addi $t0, $t0, 1
    addi $s2, $s2, 1
    addi $s0, $s0, 4

    # prompt for name
    li      $v0, 4
    la      $a0, in_name          
    syscall                         #ask name

    # store name (max 4 bytes) to str_array ($s1)  
    move $a0, $s1
    li      $a1, 4
    li      $v0, 8
    syscall                         #read name

    # move to the beginnig of next string in $s1
    # increment by 4 because length of each input word is 4
    addi $s1, $s1, 4

    beq $s2, 10, print          #if array size=10 go to print
    j   Input               #start over until s1=10



print:
    # print "List:"
    la  $a0, appt           
    li  $v0, 4          
    syscall             #print list

    addi    $t0, $zero, 0       #t0=0 counter    
    la $s0, num_array # address of num_array
    la $s1, str_array # address of str_array
res:
    # get number from num_array and print it
    lw $a0, 0($s0)
    li $v0, 1
    syscall

    # move to the next element, increment by 4 because numbers take 1 word = 4 bytes
    addi $s0, $s0, 4 

    # get string from str_array and print it
    la $a0, 0($s1)
    li $v0, 4
    syscall

    # print space
    la $a0, 32
    li $v0, 11
    syscall

    # move to the next element, increment by 4 because strs have 4 bytes length (in your case)
    addi $s1, $s1, 4

    # increment loop counter
    addi $t0, $t0, 1
    blt $t0, $s2, res # $s2 is size of the array
end:
    li  $v0, 10         
    syscall             #the end

这是输出:

Insert date (mmdd): 1201

Insert name: aaa
Insert date (mmdd): 1202

Insert name: bbb
Insert date (mmdd): 1203

Insert name: ccc
Insert date (mmdd): 1204

Insert name: ddd
Insert date (mmdd): 1205

Insert name: eee
Insert date (mmdd): 1206

Insert name: fff
Insert date (mmdd): 1207

Insert name: ggg
Insert date (mmdd): 1208

Insert name: hhh
Insert date (mmdd): 1209

Insert name: iii
Insert date (mmdd): 1210

Insert name: jjj
List: 1201aaa 1202bbb 1203ccc 1204ddd 1205eee 1206fff 1207ggg 1208hhh 1209iii 1210jjj 
-- program is finished running --