每当我运行以下代码时:
#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 $raQtSpim控制台打印"The length of The string: 0-“。我已经使用了我的print方法,但是我不确定问题出在哪里。那么,问题是:我如何修复我的打印输出?我应该在$t0中打印出信息,因为它是计数器。
提前感谢!
发布于 2013-12-11 16:27:36
我不完全确定修复打印输出是什么意思,但是有一个问题是,strlen函数中的计数寄存器是$t0,而print:中的第二个syscall是用参数$t1调用的
将该$t1更改为$t0并运行它将显示输出5。
https://stackoverflow.com/questions/20513857
复制相似问题