【问题标题】:Array strings in MIPS - an issue with a loopMIPS 中的数组字符串 - 循环问题
【发布时间】:2014-08-10 14:36:23
【问题描述】:

我正在编写一个代码来显示一个包含多个字符串的数组。因此我使用了一个循环。但是我的计数器无法正常工作,并且我收到一条错误消息和数组的第一个元素(字符串)。有人可以帮帮我吗?

'# $s0=count, $s1=total, $s3=data $s7=index

.data

Mssg1: .asciiz "你好\n"

第 1 行:.asciiz "aab\n"

第 2 行:.asciiz "ggdhj\n"

第 3 行:.asciiz "uio\n"

数组:.word Line1, Line2, Line3

.globl main
.text

主要: li $s1, 0 # 总计=0 li $s0, 3 # 计数=3 la $s7, 数组 # 索引

li $v0, 4       # print_str "Hello"
la $a0, Mssg1       
syscall

l1: lw $s3, 0($s7) # data=sum[index]

add $s1, $s1, $s3   # total=total+data

addi $s7, $s7, 4    # increment
addi $s0, $s0, -1   # decrement

bne $s0, $0, l1     # if not equal 0 go back onto the loop 


add $t0, $0, $s0    # t0=s0

li $v0, 4
move $a0, $t0
lw $a0, array       # print_str 
syscall'

【问题讨论】:

    标签: arrays string mips


    【解决方案1】:

    $t1 的值在循环中不会改变,所以 $t7 会增加,你会得到不存在的数组索引:

    lw $t2,0($t7)    # data=array[index]
    

    【讨论】:

    • 好的,我已经使用 s0 添加了减量操作(以前是 t2 - 抱歉,我有相同代码的 2 个不同版本,我不小心插入了不太透明的那个),但效果是还是一样。我做错了什么?
    • 现在你的代码没有在字符串 l1 之前定义寄存器 $t7 的内容: lw $s3, 0($s7) ...
    • 在进入循环之前声明为 la $s7, array # index 还不够吗?我应该在循环中定义它吗?所以我假设我应该将它分配给0?我说的对吗?
    猜你喜欢
    • 2020-02-06
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    相关资源
    最近更新 更多