繁体   English   中英

带循环的Mips组装中的数组

[英]Array in Mips Assembly with looping

我的代码有问题。 我正在创建一个程序,该程序将读取字符串,将其保存到数组中,然后输出字符串中每个字母的使用次数。 现在,我只将字符串输出返回屏幕时遇到问题。 输出字符串,但循环永不退出,$ t2值可能从未设置为值?

.data
intro: .asciiz "Andrew Lofgren, Letter Checker Program" 
question: .asciiz "\nPlease enter a string for evaluation: "
alphabet: .ascii "ABCDEFGHIJKLMONOPQRSTUVWXYZ"
results: .space 104
string: .space 1024

.text 

main:
jal setup
jal analyze
#jal results

li  $v0, 10
syscall 

setup:
li  $v0, 4  # outputing name and program information
la  $a0, intro
syscall

li  $v0, 4  # asksing for string input
la  $a0, question
syscall 

li  $v0, 8
la  $a0, string
li  $a1, 1024
syscall

jr  $ra     # return

analyze: 
la  $t0, string # taking string and saving into a tmp
move    $t2, $t0    # backup of orignal address
find:   
beq $t1, 0, print
addi    $t0, $t0, 1
j find

print:  
blt $t0, $t2, end   #PROBLEM HERE
li  $v0, 11
lb  $a0, 0($t0)
syscall
addi    $t0, $t0, 1
j print
end:
jr  $ra

$t0永远不会小于$t2 ,因为$t0不断增加,而$t2保持不变。 为了解决这个问题,您需要第三个寄存器$t7 ,该寄存器存储字符串的最终索引。 要计算最后一个索引,只需将字符串的基地址添加到您定义为1024的长度中。一旦$t0不再小于1024 +字符串,则字符串中就没有字符了,因此我们分支到end: 这样做并在下面的代码段中进一步说明。

print:
    li      $t7 , 1024            # Load total byte length of the string
    add     $t7 , $t7 , $t2     # add it to the base address to get the end
    slt     $t6 , $t0 , $t7     # check to see if cur index < end
    beq     $t6 , $0 , end     # if we hit the end, branch to end

...

有关MIPS说明的更多信息, 请访问此页面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM