我在使用此 MIPS 程序时遇到问题

发布于 2025-01-03 11:32:21 字数 511 浏览 0 评论 0原文

我的目标是将下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹉夏雨初晴づ 2025-01-10 11:32:21

好吧,首先,您没有从循环内的 myStr 加载字节。

您的 lbu 在循环开始之前将 s 字符加载到 $t1 中,但在循环内部,您将其与地址进行比较 $t0

您需要做的是每次通过循环lbu来自$t0的字节,并将那个$t1进行比较代码>.

我认为这将是解决方案,尽管我已经有一段时间没有完成任何 MIPS 了。

更改

loop:   beq    $t0, $t1, continue
        addi   $t0, $t0, 1
        j      loop

为:

loop:   lbu    $t2, 0($t0)            ; get character at current string location.
        beq    $t1, $t2, continue     ; exit loop is it matches.
        beq    $0, $t2, continue      ; exit loop if it's null.
        addi   $t0, $t0, 1            ; continue loop with next character.
        j      loop

如果您的目标只是将 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 the s 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:

loop:   beq    $t0, $t1, continue
        addi   $t0, $t0, 1
        j      loop

into:

loop:   lbu    $t2, 0($t0)            ; get character at current string location.
        beq    $t1, $t2, continue     ; exit loop is it matches.
        beq    $0, $t2, continue      ; exit loop if it's null.
        addi   $t0, $t0, 1            ; continue loop with next character.
        j      loop

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 through gcc -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.

与酒说心事 2025-01-10 11:32:21

您也不会在循环外部从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文