Arrays 具有循环的Mips程序集中的数组
我的代码有问题。我正在创建一个程序,该程序将读取字符串,将其保存到数组中,然后输出字符串中每个字母的使用次数。因为现在我有一个问题,就是字符串返回到屏幕的输出。输出字符串,但循环从未退出,$t2值可能从未设置为值Arrays 具有循环的Mips程序集中的数组,arrays,loops,assembly,mips,Arrays,Loops,Assembly,Mips,我的代码有问题。我正在创建一个程序,该程序将读取字符串,将其保存到数组中,然后输出字符串中每个字母的使用次数。因为现在我有一个问题,就是字符串返回到屏幕的输出。输出字符串,但循环从未退出,$t2值可能从未设置为值 .data intro: .asciiz "Andrew Lofgren, Letter Checker Program" question: .asciiz "\nPlease enter a string for evaluation: " alphabet: .ascii "A
.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+个字符串,字符串中就没有字符了,因此我们将分支到结束:
。这将在下面的代码段中完成并进一步解释
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
...
打印:
li$t7,1024#加载字符串的总字节长度
添加$t7、$t7、$t2#将其添加到基址以获得结尾
slt$t6、$t0、$t7#检查当前索引是否<结束
beq$t6,$0,结束#如果我们到达了终点,从一个分支到另一个分支
...
有关MIPS说明的更多信息,请参见。您的退出条件是t0