将 C 程序转换为 MIPS [英] Converting a C program to MIPS

查看:100
本文介绍了将 C 程序转换为 MIPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将以下 C 函数转换为 MIPS:

int my_function(int x, int y){国际我,一;a = x+y;i = x-2;a = a+i;返回一个;}

<块引用>

假设变量 x 和 y 分别从参数寄存器 $a0 和 $a1 传递.返回值应存储在寄存器 $v0 中.请注意,如果在此过程中使用它们,则需要使用堆栈来存储任何其他寄存器.

因为我是 MIPS 的新手,所以我尝试参考

完成后,您可能会看到生成的代码如下:

$LFB0 = .my_function(int, int):$LVL0 = .加 2 美元、4 美元、5 美元$LVL1 = .另外 $4,$4,-2$LVL2 = .31 美元加 2 美元、4 美元、2 美元

请注意,编译器已经优化掉了原始 C 代码中的一些冗余操作.如果您想查看未优化的版本,请在编译器选项中指定-O0 并且您会看到效率低得多但更接近原始来源的内容:

$LFB0 = .my_function(int, int):添加 $sp,$sp,-16sw $fp,12($sp)移动 $fp,$sp瑞士法郎 $4,16($fp)瑞士法郎 $5,20($fp)体重 $3,16($fp)体重 $2,20($fp)加 2 美元、3 美元、2 美元sw $2,0($fp)体重 $2,16($fp)另外 $2,$2,-2sw $2,4($fp)lw $3,0($fp)lw $2,4($fp)加 2 美元、3 美元、2 美元sw $2,0($fp)lw $2,0($fp)移动 $sp,$fplw $fp,12($sp)addiu $sp,$sp,1631 美元没有

I have to convert the following C function to MIPS:

int my_function(int x, int y)
{
 int i,a;
 a = x+y;
 i = x-2;
 a = a+i;
 return a;
}

Assume that the variables x and y are passed from argument registers $a0 and $a1 respectively. The returned value should be stored in register $v0. Note that you need to use stack to store any other registers if you use them in this procedure.

Since I am new to MIPS I tried referring to a C to MIPS online convertor and I got this result:

my_function(int, int):
    push    rbp
    mov     rbp, rsp
    mov     DWORD PTR [rbp-20], edi
    mov     DWORD PTR [rbp-24], esi
    mov     edx, DWORD PTR [rbp-20]
    mov     eax, DWORD PTR [rbp-24]
    add     eax, edx
    mov     DWORD PTR [rbp-4], eax
    mov     eax, DWORD PTR [rbp-20]
    sub     eax, 2
    mov     DWORD PTR [rbp-8], eax
    mov     eax, DWORD PTR [rbp-8]
    add     DWORD PTR [rbp-4], eax
    mov     eax, DWORD PTR [rbp-4]
    pop     rbp
    ret

Can I get a better solution for this?

解决方案

Currently you're generating code for x86-64 - you need to select a MIPS compiler from the popup menu above the assembly pane:

After you've done that you'll probably see generated code like this:

$LFB0 = .
my_function(int, int):
$LVL0 = .
        addu    $2,$4,$5
$LVL1 = .
        addiu   $4,$4,-2
$LVL2 = .
        j       $31
        addu    $2,$4,$2

Note that the compiler has optimised away some of the redundant operations in the original C code. If you want to see an unoptimised version then specify -O0 in the compiler options and you'll see something much less efficient, but closer to the original source:

$LFB0 = .
my_function(int, int):
        addiu   $sp,$sp,-16
        sw      $fp,12($sp)
        move    $fp,$sp
        sw      $4,16($fp)
        sw      $5,20($fp)
        lw      $3,16($fp)
        lw      $2,20($fp)
        addu    $2,$3,$2
        sw      $2,0($fp)
        lw      $2,16($fp)
        addiu   $2,$2,-2
        sw      $2,4($fp)
        lw      $3,0($fp)
        lw      $2,4($fp)
        addu    $2,$3,$2
        sw      $2,0($fp)
        lw      $2,0($fp)
        move    $sp,$fp
        lw      $fp,12($sp)
        addiu   $sp,$sp,16
        j       $31
        nop

这篇关于将 C 程序转换为 MIPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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