Convert C code to MIPS assembly?

2

How can I generate the Assembly code corresponding to the code below:

#include <stdio.h> 
#define LINHA  4
#define COLUNA 5

int tab [LINHA][COLUNA] = {
    2016,2012,2008,2004,2000,
    1916,1911,1908,1904,1900,
    1861,1857,1852,1846,1844,
    1728,1727,1726,1725,1723};

int cnt = 0;

int resultado[COLUNA] = {0,0,0,0,0};


int main() {
  int l, i, tmp; 

  do{
      printf("Qual linha deseja verificar?");
        scanf("%d", &l);
  }while(l >= LINHA);

  for(i = 0; i < COLUNA; i++){
        tmp = f_bissexto(l, i);  # esse metodo consegui fzer f_bissexto
        if(tmp){
            resultado[cnt] = tmp; 
            cnt++;          
        }
    }

    imprimir();

    return 0;  
}
    
asked by anonymous 03.10.2017 / 21:54

1 answer

2

If you are on a machine with a native MIPS architecture, you can use the GCC compiler by passing the -S argument so that it generates the code in assembly equivalent to the program written in C, for example:

$ gcc -S bissexto.c -o bissexto.asm 

Poreḿ, if you are not in a MIPS architecture, you will need a cross-compiler .

In this case, I suggest the Codescape MIPS SDK that can be obtained here .

Once with Codescape MIPS SDK installed:

$ pwd
/opt/imgtec/Toolchains/mips-mti-linux-gnu/2016.05-03/bin

$ ls -al
total 136060
drwxrwxr-x 2 root root     4096 Oct  3 20:49 .
drwxrwxr-x 9 root root     4096 Oct  3 20:46 ..
-rwxr-xr-x 1 root root  5151584 Jun 26  2016 mips-mti-linux-gnu-addr2line
-rwxr-xr-x 1 root root  5320040 Jun 26  2016 mips-mti-linux-gnu-ar
-rwxr-xr-x 1 root root  7876560 Jun 26  2016 mips-mti-linux-gnu-as
-rwxr-xr-x 1 root root  3351704 Jun 26  2016 mips-mti-linux-gnu-c++
-rwxr-xr-x 1 root root  5106592 Jun 26  2016 mips-mti-linux-gnu-c++filt
-rwxr-xr-x 1 root root  3347032 Jun 26  2016 mips-mti-linux-gnu-cpp
-rwxr-xr-x 1 root root   100104 Jun 26  2016 mips-mti-linux-gnu-elfedit
-rwxr-xr-x 1 root root  3351704 Jun 26  2016 mips-mti-linux-gnu-g++
-rwxr-xr-x 1 root root  3343240 Jun 26  2016 mips-mti-linux-gnu-gcc
-rwxr-xr-x 1 root root  3343240 Jun 26  2016 mips-mti-linux-gnu-gcc-4.9.2
-rwxr-xr-x 1 root root   145264 Jun 26  2016 mips-mti-linux-gnu-gcc-ar
-rwxr-xr-x 1 root root   145056 Jun 26  2016 mips-mti-linux-gnu-gcc-nm
-rwxr-xr-x 1 root root   145064 Jun 26  2016 mips-mti-linux-gnu-gcc-ranlib
-rwxr-xr-x 1 root root  2313536 Jun 26  2016 mips-mti-linux-gnu-gcov
-rwxr-xr-x 1 root root 30093240 Jun 26  2016 mips-mti-linux-gnu-gdb
-rwxr-xr-x 1 root root  3352168 Jun 26  2016 mips-mti-linux-gnu-gfortran
-rwxr-xr-x 1 root root  5681544 Jun 26  2016 mips-mti-linux-gnu-gprof
-rwxr-xr-x 1 root root  7715008 Jun 26  2016 mips-mti-linux-gnu-ld
-rwxr-xr-x 1 root root  7715008 Jun 26  2016 mips-mti-linux-gnu-ld.bfd
-rwxr-xr-x 1 root root  5188192 Jun 26  2016 mips-mti-linux-gnu-nm
-rwxr-xr-x 1 root root  6138528 Jun 26  2016 mips-mti-linux-gnu-objcopy
-rwxr-xr-x 1 root root  7251864 Jun 26  2016 mips-mti-linux-gnu-objdump
-rwxr-xr-x 1 root root  5320040 Jun 26  2016 mips-mti-linux-gnu-ranlib
-rwxr-xr-x 1 root root  1345744 Jun 26  2016 mips-mti-linux-gnu-readelf
-rwxr-xr-x 1 root root  5142248 Jun 26  2016 mips-mti-linux-gnu-size
-rwxr-xr-x 1 root root  5141808 Jun 26  2016 mips-mti-linux-gnu-strings
-rwxr-xr-x 1 root root  6138584 Jun 26  2016 mips-mti-linux-gnu-strip

You can use the compiler mips-mti-linux-gnu-gcc to generate the code assembly equivalent to the program in C:

$ ./mips-mti-linux-gnu-gcc -S bissexto.c -o bissexto.asm

Bissexto.asm:

    .file   1 "bissexto.c"
    .section .mdebug.abi32
    .previous
    .nan    legacy
    .module fp=xx
    .module nooddspreg
    .abicalls
    .option pic0
    .globl  tab
    .data
    .align  2
    .type   tab, @object
    .size   tab, 80
tab:
    .word   2016
    .word   2012
    .word   2008
    .word   2004
    .word   2000
    .word   1916
    .word   1911
    .word   1908
    .word   1904
    .word   1900
    .word   1861
    .word   1857
    .word   1852
    .word   1846
    .word   1844
    .word   1728
    .word   1727
    .word   1726
    .word   1725
    .word   1723
    .globl  cnt
    .section    .bss,"aw",@nobits
    .align  2
    .type   cnt, @object
    .size   cnt, 4
cnt:
    .space  4
    .globl  resultado
    .align  2
    .type   resultado, @object
    .size   resultado, 20
resultado:
    .space  20
    .rdata
    .align  2
$LC0:
    .ascii  "Qual linha deseja verificar?
$ gcc -S bissexto.c -o bissexto.asm 
0" .align 2 $LC1: .ascii "%d
$ pwd
/opt/imgtec/Toolchains/mips-mti-linux-gnu/2016.05-03/bin

$ ls -al
total 136060
drwxrwxr-x 2 root root     4096 Oct  3 20:49 .
drwxrwxr-x 9 root root     4096 Oct  3 20:46 ..
-rwxr-xr-x 1 root root  5151584 Jun 26  2016 mips-mti-linux-gnu-addr2line
-rwxr-xr-x 1 root root  5320040 Jun 26  2016 mips-mti-linux-gnu-ar
-rwxr-xr-x 1 root root  7876560 Jun 26  2016 mips-mti-linux-gnu-as
-rwxr-xr-x 1 root root  3351704 Jun 26  2016 mips-mti-linux-gnu-c++
-rwxr-xr-x 1 root root  5106592 Jun 26  2016 mips-mti-linux-gnu-c++filt
-rwxr-xr-x 1 root root  3347032 Jun 26  2016 mips-mti-linux-gnu-cpp
-rwxr-xr-x 1 root root   100104 Jun 26  2016 mips-mti-linux-gnu-elfedit
-rwxr-xr-x 1 root root  3351704 Jun 26  2016 mips-mti-linux-gnu-g++
-rwxr-xr-x 1 root root  3343240 Jun 26  2016 mips-mti-linux-gnu-gcc
-rwxr-xr-x 1 root root  3343240 Jun 26  2016 mips-mti-linux-gnu-gcc-4.9.2
-rwxr-xr-x 1 root root   145264 Jun 26  2016 mips-mti-linux-gnu-gcc-ar
-rwxr-xr-x 1 root root   145056 Jun 26  2016 mips-mti-linux-gnu-gcc-nm
-rwxr-xr-x 1 root root   145064 Jun 26  2016 mips-mti-linux-gnu-gcc-ranlib
-rwxr-xr-x 1 root root  2313536 Jun 26  2016 mips-mti-linux-gnu-gcov
-rwxr-xr-x 1 root root 30093240 Jun 26  2016 mips-mti-linux-gnu-gdb
-rwxr-xr-x 1 root root  3352168 Jun 26  2016 mips-mti-linux-gnu-gfortran
-rwxr-xr-x 1 root root  5681544 Jun 26  2016 mips-mti-linux-gnu-gprof
-rwxr-xr-x 1 root root  7715008 Jun 26  2016 mips-mti-linux-gnu-ld
-rwxr-xr-x 1 root root  7715008 Jun 26  2016 mips-mti-linux-gnu-ld.bfd
-rwxr-xr-x 1 root root  5188192 Jun 26  2016 mips-mti-linux-gnu-nm
-rwxr-xr-x 1 root root  6138528 Jun 26  2016 mips-mti-linux-gnu-objcopy
-rwxr-xr-x 1 root root  7251864 Jun 26  2016 mips-mti-linux-gnu-objdump
-rwxr-xr-x 1 root root  5320040 Jun 26  2016 mips-mti-linux-gnu-ranlib
-rwxr-xr-x 1 root root  1345744 Jun 26  2016 mips-mti-linux-gnu-readelf
-rwxr-xr-x 1 root root  5142248 Jun 26  2016 mips-mti-linux-gnu-size
-rwxr-xr-x 1 root root  5141808 Jun 26  2016 mips-mti-linux-gnu-strings
-rwxr-xr-x 1 root root  6138584 Jun 26  2016 mips-mti-linux-gnu-strip
0" .text .align 2 .globl main .set nomips16 .set nomicromips .ent main .type main, @function main: .frame $fp,48,$31 # vars= 16, regs= 2/0, args= 16, gp= 8 .mask 0xc0000000,-4 .fmask 0x00000000,0 .set noreorder .set nomacro addiu $sp,$sp,-48 sw $31,44($sp) sw $fp,40($sp) move $fp,$sp $L2: lui $2,%hi($LC0) addiu $4,$2,%lo($LC0) jal printf nop addiu $3,$fp,32 lui $2,%hi($LC1) addiu $4,$2,%lo($LC1) move $5,$3 jal __isoc99_scanf nop lw $2,32($fp) slt $2,$2,4 beq $2,$0,$L2 nop sw $0,24($fp) b $L3 nop $L5: lw $2,32($fp) move $4,$2 lw $5,24($fp) jal f_bissexto nop sw $2,28($fp) lw $2,28($fp) beq $2,$0,$L4 nop lui $2,%hi(cnt) lw $3,%lo(cnt)($2) lui $2,%hi(resultado) sll $3,$3,2 addiu $2,$2,%lo(resultado) addu $2,$3,$2 lw $3,28($fp) sw $3,0($2) lui $2,%hi(cnt) lw $2,%lo(cnt)($2) addiu $3,$2,1 lui $2,%hi(cnt) sw $3,%lo(cnt)($2) $L4: lw $2,24($fp) addiu $2,$2,1 sw $2,24($fp) $L3: lw $2,24($fp) slt $2,$2,5 bne $2,$0,$L5 nop jal imprimir nop move $2,$0 move $sp,$fp lw $31,44($sp) lw $fp,40($sp) addiu $sp,$sp,48 jr $31 nop .set macro .set reorder .end main .size main, .-main .ident "GCC: (Codescape GNU Tools 2016.05-03 for MIPS MTI Linux) 4.9.2"
    
04.10.2017 / 01:16