MCs*_*uch 4 assembly strlen qtspim
每当我运行以下代码时:
#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
Run Code Online (Sandbox Code Playgroud)
QtSpim 控制台打印“字符串的长度:0-”。我对我的打印方法进行了一些尝试,但我不确定问题是什么。所以,问题是:如何修复我的打印输出?我应该打印出 $t0 中的信息,因为它是柜台。
提前致谢!
不完全确定修复打印输出是什么意思,但一个问题是 strlen 函数中的计数寄存器是$t0
在print:
使用参数调用第二个系统调用时$t1
将其更改$t1
为$t0
并运行它会得到输出 5。