从gcc获取汇编代码的问题 - 尽我所能

时间:2011-04-14 17:27:54

标签: c assembly mips

我在linux下工作,在尝试使用* .c代码创建MIPS汇编文件时遇到一些问题。

我得到的只是一个* .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++;
        }
    }    

}

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

1 个答案:

答案 0 :(得分:2)

您是否有生成MIPS代码的GCC版本?看起来你正在将代码传递给x86编译器!可以在GCC documentation中找到MIPS的GCC选项列表。如果这些不起作用,您将需要自己构建一个与MIPS一起使用的编译器。可以在LinuxMIPS wiki找到一些预构建的Linux选项和构建自己的选项和说明。