首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >mips程序集的字符串长度

mips程序集的字符串长度
EN

Stack Overflow用户
提问于 2013-12-11 16:14:53
回答 1查看 39K关注 0票数 4

每当我运行以下代码时:

代码语言:javascript
复制
#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控制台打印"The length of The string: 0-“。我已经使用了我的print方法,但是我不确定问题出在哪里。那么,问题是:我如何修复我的打印输出?我应该在$t0中打印出信息,因为它是计数器。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-11 16:27:36

我不完全确定修复打印输出是什么意思,但是有一个问题是,strlen函数中的计数寄存器是$t0,而print:中的第二个syscall是用参数$t1调用的

将该$t1更改为$t0并运行它将显示输出5。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20513857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档