4

每当我运行以下代码时:

#counts length of a string

.data
 .data
string: .asciiz "Hello"

printedMessage: .asciiz "The length of the string: "

    .text
main:
  la $a0, string             # Load address of string.
        jal strlen              # Call strlen procedure.
        jal print
        addi $a1, $a0, 0        # Move address of string to $a1
        addi $v1, $v0, 0        # Move length of string to $v1
        addi $v0, $0, 11        # System call code for message.
        la $a0, printedMessage            # Address of message.
        syscall
        addi $v0, $0, 10        # System call code for exit.
        syscall


strlen:
li $t0, 0 # initialize the count to zero
loop:
lb $t1, 0($a0) # load the next character into t1
beqz $t1, exit # check for the null character
addi $a0, $a0, 1 # increment the string pointer
addi $t0, $t0, 1 # increment the count
j loop # return to the top of the loop
exit:
jr $ra

print:
li $v0, 4
  la $a0, printedMessage
  syscall

  li $v0, 1
  move $a0, $t1
  syscall


jr $ra

QtSpim 控制台打印“字符串的长度:0-”。我用我的打印方法玩了一下,但我不确定问题是什么。所以,问题是:我如何修复我的打印输出?我应该打印出 $t0 中的信息,因为它是柜台。

提前致谢!

4

1 回答 1

6

不完全确定您所说的修复打印输出是什么意思,但一个问题是您在 strlen 函数中的计数寄存器是$t0在您的第二个系统调用中print:使用参数调用时$t1

将其更改$t1$t0并运行它会给出输出 5。

于 2013-12-11T08:27:36.423 回答