尝试在MIPS中创建和打印数组中的元素

时间:2015-03-08 05:11:02

标签: c arrays assembly mips

我似乎真的在使用mips,我已经阅读了多篇有关如何制作数组并访问它们的教程,但每当我尝试执行所列出的方法时,我的程序什么都不做。

目前我有这段代码:

.data
array:  .word  1:32       # array of 32 integers
line:   .asciiz "\n"
main:   
    li   $t0, 0          # $t0 is the loop induction variable
    li   $t1, 32     # $t1 is the sentinal value for the loop
    la   $t2, array      # $9 starts as the base address of the array
                  #   and is the address of each element
    li   $t3, 1        # $12 is the value 18, to be put in desired element
for:    
    bge  $t0, $t1, end_for
    sw   $t3, ($t2)
    li   $v0, 4
    la   $a0, line
    syscall
    li   $v0, 1
    move $a0, t3
    syscall
    add  $t2, $t2, 4      # get address of next array element
    add  $t0, $0, 1      # increment loop induction variable
    b    valfor        
valfor: 
    addi $t3, $t3, 1     
    b for

end_for:
    li $v0, 10
    syscall

尝试运行时,我的代码会立即完成。它没有列出任何错误或做任何有趣的事情,它只是说-- program is finished running (dropped off bottom) --

如果有人能指出我正确的方向,就创建,访问和打印整数数组而言,我会非常感激。

网站我认为我准确地解释了我的代码: http://pages.cs.wisc.edu/~cs354-2/onyourown/arrays.html http://courses.cs.vt.edu/~cs2505/fall2010/Notes/pdf/T23.MIPSArrays.pdf

1 个答案:

答案 0 :(得分:0)

您是否真的试图运行您在问题中输入的代码?它包含行move $a0, t3,由于缺少$符号(t3应为$t3)而无法在SPIM中汇编。

除此之外,我发现您的代码至少存在两个问题:

由于这一行,你有一个无限循环:

add  $t0, $0, 1      # increment loop induction variable

那只是$t0 = 1。它应该更改为add $t0, $t0, 1

另一个问题是您已将代码放入数据部分。代码应该在文本部分,即在您main:之前:

.text
.globl main
相关问题