对于MIPS汇编字符串的长度 [英] string length for mips assembly

查看:1339
本文介绍了对于MIPS汇编字符串的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我运行下面的code:

Whenever I run the following code:

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

The QtSpim console prints "The length of the string: 0-". I've played around a bit with my print method, but I'm not sure what the problem is. So, the question: How do I fix my print outs? I should be printing out the info in $t0 since it's the counter.

在此先感谢!

推荐答案

不完全知道你所说的修复打印出来的意思,但一个问题是,在strlen函数的计数寄存器是 $ T0 ,而在第二个系统调用打印被调用,参数 $ T1

Not entirely sure what you mean by fix print out, but one problem is that your count register in the strlen function is $t0 while your second syscall in print: is called with argument $t1

改变这种 $ T1 $ T0 并运行它给输出5。

Changing that $t1 to $t0 and running it gives output 5.

这篇关于对于MIPS汇编字符串的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆