我想将我的C程序转换为MIPS指令

时间:2013-02-06 04:10:22

标签: gcc ubuntu mips

我想知道是否有可能在ubuntu 11.04中使用gcc将此代码转换为MIPS。 我试过lcc但是无法安装它。

#include<stdio.h>
int fibnacci(int c)
{
if(c==0)
return 0;
else if(c==1)
return 1;
else
return (fibnacci(c-1)+fibnacci(c-2));
}
int main(){
int i;
printf("Enter the fib number");
scanf("%d",&i);
int a;
a= fibnacci(i);
printf("The value is %d",a);
}

2 个答案:

答案 0 :(得分:2)

为了能够生成mips代码,必须将GCC配置为以mips作为目标体系结构的交叉编译器。所以正确答案 - 不,你不能在Ubuntu中使用常规gcc。你必须得到mips交叉编译器。要获得它,请从软件包管理器安装embedian软件包并使用gcc-4.4-mips-linux-gnu或从源代码安装mips交叉编译器。

答案 1 :(得分:0)

代码无法编译。使用MIPS模拟器在我的Ubuntu上轻微更改编译为MIPS。

#include<stdio.h>
int fibnacci(int c)
{
if(c==0)
return 0;
else if(c==1)
return 1;
else
return (fibnacci(c-1)+fibnacci(c-2));
}
int main(){
int i;
int a;
printf("Enter the fib number");
scanf("%d",&i);
a= fibnacci(i);
printf("The value is %d",a);
}

程序集

    .file   1 "mips-c.c"

 # -G value = 8, Cpu = 3000, ISA = 1
 # GNU C version cygnus-2.7.2-970404 (mips-mips-ecoff) compiled by GNU C version cygnus-2.7.2-970404.
 # options passed:  -msoft-float
 # options enabled:  -fpeephole -ffunction-cse -fkeep-static-consts
 # -fpcc-struct-return -fcommon -fverbose-asm -fgnu-linker -msoft-float
 # -meb -mcpu=3000

gcc2_compiled.:
__gnu_compiled_c:
    .text
    .align  2
    .globl  fibnacci
    .ent    fibnacci
fibnacci:
    .frame  $fp,32,$31      # vars= 0, regs= 3/0, args= 16, extra= 0
    .mask   0xc0010000,-8
    .fmask  0x00000000,0
    subu    $sp,$sp,32
    sw  $31,24($sp)
    sw  $fp,20($sp)
    sw  $16,16($sp)
    move    $fp,$sp
    sw  $4,32($fp)
    lw  $2,32($fp)
    bne $2,$0,$L2
    move    $2,$0
    j   $L1
    j   $L3
$L2:
    lw  $2,32($fp)
    li  $3,1            # 0x00000001
    bne $2,$3,$L4
    li  $2,1            # 0x00000001
    j   $L1
    j   $L5
$L4:
    lw  $3,32($fp)
    addu    $2,$3,-1
    move    $4,$2
    jal fibnacci
    move    $16,$2
    lw  $3,32($fp)
    addu    $2,$3,-2
    move    $4,$2
    jal fibnacci
    addu    $3,$16,$2
    move    $2,$3
    j   $L1
$L5:
$L3:
$L1:
    move    $sp,$fp         # sp not trusted here
    lw  $31,24($sp)
    lw  $fp,20($sp)
    lw  $16,16($sp)
    addu    $sp,$sp,32
    j   $31
    .end    fibnacci
    .rdata
    .align  2
$LC0:
    .ascii  "Enter the fib number\000"
    .sdata
    .align  2
$LC1:
    .ascii  "%d\000"
    .rdata
    .align  2
$LC2:
    .ascii  "The value is %d\000"
    .text
    .align  2
    .globl  main
    .ent    main
main:
    .frame  $fp,32,$31      # vars= 8, regs= 2/0, args= 16, extra= 0
    .mask   0xc0000000,-4
    .fmask  0x00000000,0
    subu    $sp,$sp,32
    sw  $31,28($sp)
    sw  $fp,24($sp)
    move    $fp,$sp
    jal __main
    la  $4,$LC0
    jal printf
    la  $4,$LC1
    addu    $5,$fp,16
    jal scanf
    lw  $4,16($fp)
    jal fibnacci
    sw  $2,20($fp)
    la  $4,$LC2
    lw  $5,20($fp)
    jal printf
$L6:
    move    $sp,$fp         # sp not trusted here
    lw  $31,28($sp)
    lw  $fp,24($sp)
    addu    $sp,$sp,32
    j   $31
    .end    main