Convert C code to Assembly MIPS?

Asked

Viewed 5,984 times

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;  
}
  • 1

    I think it’s not the purpose of the site to make a new code, that is to make everything for you. I would need a specific question at some point that is in trouble. I would not waste time on this. It takes a monster job to make code better than the C compiler will produce, especially if you try this code better in C. The gain in this type of code does not justify the conversion.

  • Use this code formatting option when it is code that can be executed in the frontend as Javascript, HTML, CSS and so on. Do not use for backend code, such as C that depends on a compiler, for backend code you can use the { } or select the code and press the keys ctrl+k. Here’s how to format your question.

  • 2

    @Maniero I found the question very relevant. I think with the proper tags the question becomes clearer.

  • I also found the question good since there are tools for this.

1 answer

2

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

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

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

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

Once with the 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 programme in C:

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

aphrodisiac.:

    .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?\000"
    .align  2
$LC1:
    .ascii  "%d\000"
    .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"
  • I wanted to know in a more didactic way because the way it is generated by the compiler is difficult to understand for me that I am learning... But thanks for the idea!

  • More didactic form? In Assembly there is no more didactic form, it is complicated to even understand. Therefore it has become an obsolete language.

  • Very interesting. Could you do the reverse way? From Assembly to C?

  • https://defuse.ca/online-x86-assembler.htm reverse path

  • 1

    @gfleck: Of assembly for C is another story... but it is possible to unmount a binary for your code in assembly equivalent by means of the utility objdump. For example: $objdump -d -Mintel /usr/bin/ls

Browser other questions tagged

You are not signed in. Login or sign up in order to post.