2012-09-17 136 views
0

这就是我到目前为止所得到的结果。我可以修正什么来获得这个权利?找到输入字符串的长度

.data 
msg1:.asciiz "Please insert text (max 20 characters): " 
msg2:.asciiz "\nThe length of the text is: " 

newline: .asciiz "\n" 

str1: .space 20 
.text 
.globl main 
main: 
addi $v0, $v0,4 
la $a0,msg1 
syscall #print msg1 
li $v0,8 
la $a0,str1 
addi $a1,$zero,20 
syscall #get string 1 

la $a0,str1 #pass address of str1 
jal len 

len: 
## your code here 
addi $t2, $zero, 0 # $t2 is what we want to return in the end -- the count of the length of the character array 
addi $s1, $zero, 0 # Index i for array traversing | init. to 0 | i = 0 

Loop: 

add $s1, $s1, $a0 # Adds the location of the index to the location of the base of the array | $t1 is now the location of: array[index] 
lw $t0, 0($s1) 

beq $t0, $zero, exit 
addi $t2, $t2, 1 # Count = Count + 1 
addi $s1, $s1, 1 # i = i + 1 
j Loop 

exit: 
la $a0,msg2 
li $v0,4 
syscall 
move $a0,$t0 #output the results 
li $v0,1 
syscall 

li $v0,10 
syscall 
+1

欢迎来到StackOverflow。请记住,您始终提供*所有*相关信息,我们需要您帮助解决您的问题。你的问题缺乏以下几个方面:你究竟想要做什么,它现在做了什么,你期望的是什么?请使用您帖子下方的修改按钮来提供更多信息。谢谢。 – phant0m

回答

2
add $s1, $s1, $a0 

在这里,你会覆盖$ S1,因此添加1〜$ S1不再被纠正。只需用字符串的地址(而不是0)初始化$ s1并删除这一行。

lw $t0, 0($s1) 

这应该是lb。您正在加载一个字节,而不是4个字节。