【问题标题】:string length for mips assemblymips 程序集的字符串长度
【发布时间】:2013-12-11 08:14:53
【问题描述】:

每当我运行以下代码时:

#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 中的信息,因为它是柜台。

提前致谢!

【问题讨论】:

    标签: assembly strlen qtspim


    【解决方案1】:

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

    将 $t1 更改为 $t0 并运行它会得到输出 5。

    【讨论】:

    • 我真的以为我已经尝试过了。谢谢。嗯,你知道为什么/如何去掉答案中出现的破折号吗?
    • 我正在使用 MARS,它输出的只是“字符串的长度:5”。稍后将尝试 QtSpim...
    猜你喜欢
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多