我在使用此 MIPS 程序时遇到问题
我的目标是将下面的 C 代码转换为 MIPS 汇编。我觉得我的代码中缺少一个关键部分。有人可以解释我做错了什么以及我需要做什么来解决问题吗?
这是 C 代码:
char str[] = "hello, class";
int len = 0;
char *ptr = str;
while (*ptr && *ptr != ’s’)
++ptr;
len = ptr - str;
这是到目前为止我的代码:
.data
myStr: .asciiz "hello, class"
s: .asciiz "s"
main:
la $t0, myStr
la $t1, s
lbu $t1, 0($t1)
loop:
beq $t0, $t1, continue
addi $t0, $t0, 1
j loop
continue:
sub $v0, $t0, $t1
My goal is to translate the C code below to MIPS assembly. I feel like I am missing a crucial part in my code. Can someone explain what I am doing wrong and what I need to do to fix the problem please?
Here is the C code:
char str[] = "hello, class";
int len = 0;
char *ptr = str;
while (*ptr && *ptr != ’s’)
++ptr;
len = ptr - str;
Here is my code so far:
.data
myStr: .asciiz "hello, class"
s: .asciiz "s"
main:
la $t0, myStr
la $t1, s
lbu $t1, 0($t1)
loop:
beq $t0, $t1, continue
addi $t0, $t0, 1
j loop
continue:
sub $v0, $t0, $t1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,首先,您没有从循环内的
myStr
加载字节。您的
lbu
在循环开始之前将s
字符加载到$t1
中,但在循环内部,您将其与地址进行比较$t0
。您需要做的是每次通过循环
lbu
来自$t0
的字节,并将那个与$t1
进行比较代码>.我认为这将是解决方案,尽管我已经有一段时间没有完成任何 MIPS 了。
更改
为:
如果您的目标只是将 C 代码转换为 MIPS,您只需获取 MIPS
gcc
编译器的副本并通过gcc -S
运行它,可能在-O0
处,这样您就可以理解输出:-)这可能是最快的方法。当然,如果您的目的是学习如何手动完成,您可能想忽略这个建议,尽管在我看来它仍然对学习很方便。
Well, for a start, you're not loading the byte from
myStr
inside the loop.Your
lbu
loads up thes
character into$t1
before the loop starts but, inside the loop, you compare that with the address$t0
.What you need to do is to
lbu
the byte from$t0
each time through the loop and compare that with$t1
.I would think that this would be the fix though it's been a while since I've done any MIPS.
Change:
into:
If your goal is to simply get C code converted to MIPS, you could just get a copy of a MIPS
gcc
compiler and run it throughgcc -S
and possibly at-O0
so you can understand the output :-)That's probably the fastest way. Of course, if your intent is to learn how to do it by hand, you may want to ignore this advice, although it would still be handy for learning in my opinion.
您也不会在循环外部从 A/myStr 加载字节 - 您加载地址,并在循环中递增它,但将该地址与字符 's' 进行比较,而不是与该地址处的字符进行比较。
你也不会将该字符与 0 进行比较。
You're not loading bytes from A/myStr outside the loop, either -- you load the address, and increment it in the loop, but compare the address to the character 's' as opposed to the character at that address.
Nor do you ever compare that character to 0.