2013-02-06 187 views
1

我想知道是否有可能在ubuntu 11.04中使用gcc將此代碼轉換爲MIPS。 我試過lcc但無法安裝。我想將我的C程序轉換爲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; 
printf("Enter the fib number"); 
scanf("%d",&i); 
int a; 
a= fibnacci(i); 
printf("The value is %d",a); 
} 
+1

發生了什麼?你的編譯命令是什麼?你能給你看到的錯誤嗎? – Floris

+1

使得LCC給出了這個錯誤: root @ ubuntu:/ home/lincon/Desktop/lcc#make all cc -g -c -o /host.o cc:致命錯誤:沒有輸入文件 編譯終止。 make:*** [/host.o]錯誤4 – Lincon

+1

可以用gcc編譯得到MIPS代碼嗎? – Lincon

回答

2

爲了能夠生成MIPS代碼,GCC必須被配置爲交叉編譯與mips作爲靶結構。所以正確的答案 - 不,你不能在Ubuntu中使用常規的gcc。你必須得到mips交叉編譯器。要獲得它,請從軟件包管理器安裝embedian軟件包,並使用gcc-4.4-mips-linux-gnu或從源安裝mips交叉編譯器。

+1

或者,MIPS在這裏指出幾個交叉編譯器:http://developer.mips.com/tools/compilers/。 –

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