2011-04-14 44 views
0

我正在linux下工作,並在嘗試從* .c代碼創建MIPS彙編文件時遇到一些問題。從gcc獲取彙編代碼的問題 - 盡了我最大的努力

所有我得到的是一個代碼* .s文件看起來像這樣(例如):

.file "1.c" 
    .section .rodata 
.LC0: 
    .string "Please enter your string: \n" 
.LC1: 
    .string "Please enter char to find: " 
.LC2: 
    .string "The char %c appears %d times\n" 
    .text 
.globl main 
    .type main, @function 
main: 
    pushl %ebp 
    movl %esp, %ebp 
    andl $-16, %esp 
    subl $160, %esp 
    movl %gs:20, %eax 
    movl %eax, 156(%esp) 
    xorl %eax, %eax 
    movl $0, 16(%esp) 
    movl $.LC0, (%esp) 
    call puts 
    movl stdin, %eax 
    movl %eax, 8(%esp) 
    movl $128, 4(%esp) 
    leal 28(%esp), %eax 
    movl %eax, (%esp) 
    call fgets 
    movl $.LC1, %eax 
    movl %eax, (%esp) 
    call printf 
    call getchar 
    movb %al, 27(%esp) 
    movl $0, 20(%esp) 
    jmp .L2 
.L4: 
    movl 20(%esp), %eax 
    movzbl 28(%esp,%eax), %eax 
    cmpb 27(%esp), %al 
    jne .L3 
    addl $1, 16(%esp) 
.L3: 
    addl $1, 20(%esp) 
.L2: 
    movl 20(%esp), %eax 
    movzbl 28(%esp,%eax), %eax 
    testb %al, %al 
    jne .L4 
    movsbl 27(%esp),%edx 
    movl $.LC2, %eax 
    movl 16(%esp), %ecx 
    movl %ecx, 8(%esp) 
    movl %edx, 4(%esp) 
    movl %eax, (%esp) 
    call printf 
    movl $0, %eax 
    movl 156(%esp), %edx 
    xorl %gs:20, %edx 
    je .L6 
    call __stack_chk_fail 
.L6: 
    leave 
    ret 
    .size main, .-main 
    .ident "GCC: (Ubuntu 4.4.1-4ubuntu8) 4.4.1" 
    .section .note.GNU-stack,"",@progbits 

我所做的就是寫GCC -S 1.C和文件被創建..但我需要它是一個MIPS代碼而不是808x彙編代碼...我該怎麼辦? 我盡我所能去了解交叉編譯器的神祕面紗,但沒有....我不明白什麼...

如果有人這樣做認爲,交叉編譯器可以爲我做以下2碼,我會很樂意......

第一個代碼是:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define SIZE 128 

int main() 

{ 
    char mychar , string [SIZE]; 
    int i; 
    int count =0 ; 

    printf ("Please enter your string: \n\n"); 
    fgets (string, SIZE, stdin); 

    printf ("Please enter char to find: "); 
    mychar = getchar(); 

    for (i=0 ; string[i] != '\0' ; i++) 
     if (string[i] == mychar) 
      count++; 

    printf ("The char %c appears %d times\n" ,mychar ,count); 


    return 0; 
} 

和第二代碼是:

#include <stdio.h> 
#include <string.h> 

void SIFT(int x_arr[ ], int y_arr[]); 

int main() 
{ 
    int x[20] = {0} , y[20] = {0}; 
    int m=0,temp=0,curr=0,i=0,j=0; 

    printf("Please enter your numbers now:\n\n"); 

    /*enter numbers one by one. if x[i+1] value < x[i] value, err msg. 
     when user want to end the series he must enter '0' which means end of string (it wont  included in x[]) */ 
    while ((scanf("%d",&temp)) != 5) 
    { 
     if (temp >= curr) 
     { 
      x[i] = temp; 
      curr = temp; 
      i++; 
     } 
     else 
     { 
      printf("The numbers are not at the right order !\n\nProgram will now terminate...\n\n"); 
     } 
    } 

    SIFT(x,y); 

    for (i=0 ; y[i]=='0' ; i++) /*strlen(y) without ('0')'s includes*/ 
     m++; 

    /*Prints m , y's organs*/ 
    printf("\n\nm = %d",m); 
    printf("Y = "); 
    while (y[j]!='0') 
    { 
     printf ("%d ,",y[j]); 
     j++; 
    } 

return 0; 
} 

void SIFT(int x_arr[ ], int y_arr[]) 
{ 
    int i=0,j=0; 

    while (x_arr[i] != '0') 
    { 
     if (x_arr[i] == x_arr[i+1]) /*if current val. equals next val. -> jump dbl at x_arr*/ 
     { 
      y_arr[j] = x_arr[i]; 
      i+=2; 
      j++; 
     } 
     else 
     { 
      y_arr[j]=x_arr[i]; 
      i++; 
      j++; 
     } 
    }  

} 

我真的很感謝所有的努力!

+0

如果你在學校爲MIPS課做這件事,你會很失望地發現GCC可能不一定現在如何根據你的教授可能想要你的方式來翻譯printf等函數。當我參加MIPS課程時,教授希望將printf調用轉換爲系統調用,而GCC可能只是向printf插入調用。 – Tyler 2014-06-11 04:14:08

回答

2

您是否構建了可生成MIPS代碼的GCC?它看起來像你將代碼傳遞給x86編譯器! MIPS的GCC選項列表可在GCC documentation中找到。如果這些不起作用,你將需要建立一個能夠與MIPS協同工作的編譯器。一些預建的Linux選項和說明可以在LinuxMIPS wiki找到。

+0

我不認爲我有,但我嘗試了所有這些手冊,他們只是把我和一個漫長而複雜的解釋混淆在一起...... – 2011-04-14 17:31:09

+2

@C_guy - 如果你沒有MIPS編譯器,你不會去獲取MIPS代碼。這就是關於它的一切。 – 2011-04-14 17:33:34

+0

我試過了: > gcc -mips1 1.c我得到了「無法識別的命令行」。但是當我做到:> gcc -march = 1.c我得到「缺少參數-march =」。什麼似乎是解決方案\問題? – 2011-04-14 17:40:29