0
CASE2:
    la $t9, ARRAY       # Load base address of array into $t9
    li $t8, 36      # Initialize loop counter to 36

LOOP:   
    la $a0, Prompt2     # Load Prompt2 string into $a0.
    li $v0, 4       # system call to print string Prompt2.
    syscall

    li $v0, 12      # read character input. 
    syscall
    move $t8($t9), $v0  # move input character to an array element.

    la $a0, $t8($t9)    # load array element into $a0.
    li $v0, 11      # system call to print array element.
    syscall
    addi $t8, $t8, -4   # decrement loop counter.
    bgtz $t8, LOOP

    la $a0, MSG2        # load MSG2 string into $a0.
    li $v0, 4       # system call to print MSG2 string.
    syscall 
LOOP2:
    la $a0, $t8($t9)    # load element of array into $a0.
    li $v0, 11      # system call to print char.
    addi $t8, $t8, 4    # increment $t8.
    blt $t8, 36, LOOP2  # branch if $t8 is less than 36
    j EXIT          # when $t8 reaches 36 jump to EXIT.
    .data
Prompt2:.asciiz "\nEnter a character: "
ARRAY:  .space 10       # 10 bytes of storage to hold an array of 10 characters

我无法让这个数组工作,假设从输入中读取 10 个字符并在读取它们后立即打印它们,然后向后打印出数组。任何帮助,将不胜感激。

4

1 回答 1

2

一个错误立即显而易见:

move $t8($t9), $v0 

格式不正确。MIPS 不允许您使用寄存器作为偏移量。MIPS 也不允许在移动操作的目标寄存器上进行偏移。

代码应替换为以下内容:

addi $t8, $0, 36 # initialize the offset counter to 36
...
sll  $t8, $t8, 2 # multiply the offset counter by 4 to account for word indexing
add  $t9, $t8, $t9 # add the current offset to $t9
sw   $v0, 0($t9)   # store $v0 at the memory location held in $t9

移动操作是一个伪指令,它采用一个寄存器并将其中包含的值放入另一个目标寄存器。相反,使用存储字 (sw) 指令将寄存器的内容存储在提供的内存位置。您将不得不更改其余代码以使用上面的代码,但这应该足以让您朝着正确的方向开始。

于 2012-05-19T04:36:56.547 回答