是否已从相应的C循环正确转换了此MIPS? [英] Is this MIPS strlen correctly converted from the corresponding C loop?

查看:62
本文介绍了是否已从相应的C循环正确转换了此MIPS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于正在学习的Comp Sci类,我有一个简单的问题,我的任务是将函数转换为MIPS汇编语言.我相信我有一个正确的答案,但我想验证一下.

I have a simple question for a Comp Sci class I'm taking where my task is to convert a function into MIPS assembly language. I believe I have a correct answer but I want to verify it.

这是C函数

int strlen(char *s) {
     int len;

     len=0;
     while(*s != '\0') {
          len++;
          s++;
     }
     return len;
}

谢谢!

strlen: 
    add $v0, $zero, $zero     # len = 0
loop:                          # do{
    lbu $t0, 0($a0)               # tmp0 = load *s
    addi $a0, $a0, 1              # s++
    addi $v0, $v0, 1              # len++
    bne $t0, $zero, loop         # }while(tmp0 != 0)
s_end:
    addi $v0, $v0, -1        # undo counting of the terminating 0
    j $ra 

推荐答案

是的,您拥有正确的asm版本,我喜欢这样的事实:在测试t0的值之前,您需要做更多的工作以花更多的时间尽可能从内存中加载.

Yeah, you have a correct asm version, and I like the fact that you do as much work as possible before testing the value of t0 to give as much time as possible for loading from memory.

这篇关于是否已从相应的C循环正确转换了此MIPS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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