-3

I have to convert the following C function to MIPS:

int my_function(int x, int y)
{
 int i,a;
 a = x+y;
 i = x-2;
 a = a+i;
 return a;
}

Assume that the variables x and y are passed from argument registers $a0 and $a1 respectively. The returned value should be stored in register $v0. Note that you need to use stack to store any other registers if you use them in this procedure.

Since I am new to MIPS I tried referring to a C to MIPS online convertor and I got this result:

my_function(int, int):
    push    rbp
    mov     rbp, rsp
    mov     DWORD PTR [rbp-20], edi
    mov     DWORD PTR [rbp-24], esi
    mov     edx, DWORD PTR [rbp-20]
    mov     eax, DWORD PTR [rbp-24]
    add     eax, edx
    mov     DWORD PTR [rbp-4], eax
    mov     eax, DWORD PTR [rbp-20]
    sub     eax, 2
    mov     DWORD PTR [rbp-8], eax
    mov     eax, DWORD PTR [rbp-8]
    add     DWORD PTR [rbp-4], eax
    mov     eax, DWORD PTR [rbp-4]
    pop     rbp
    ret

Can I get a better solution for this?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Shubham R
  • 7,382
  • 18
  • 53
  • 119
  • 1
    Please define *better solution*. and what have you tried? – Raptor Mar 21 '17 at 08:31
  • @Raptor : i have no idea about the solution sir, i tried online compiling link, but i guess its incorrect. That's why i wanted a better solution – Shubham R Mar 21 '17 at 08:34
  • 1
    @S Ringne: This all rather depends on the specific operating environment from which you intend to call the compiled function, in particular whether it is 32 or 64-bit MIPS and the calling convention used. Compilation of a single function in isolation is a rather peculair thing to do so I think you could shed much of light on the question by describing more specifically what you need to code for and how it is to be integrated into the system. – doynax Mar 21 '17 at 08:51
  • [Is there a way to use gcc to convert C to MIPS?](https://stackoverflow.com/a/63386888) shows how to enable optimization and still get meaningful output, with gcc `-O3 -march=mips32r2 -Wall -fverbose-asm -fno-delayed-branch`. Or `-Og` might be useful for minimal optimization. – Peter Cordes Sep 25 '20 at 20:25

1 Answers1

3

Currently you're generating code for x86-64 - you need to select a MIPS compiler from the popup menu above the assembly pane:

enter image description here

After you've done that you'll probably see generated code like this:

$LFB0 = .
my_function(int, int):
$LVL0 = .
        addu    $2,$4,$5
$LVL1 = .
        addiu   $4,$4,-2
$LVL2 = .
        j       $31
        addu    $2,$4,$2

Note that the compiler has optimised away some of the redundant operations in the original C code. If you want to see an unoptimised version then specify -O0 in the compiler options and you'll see something much less efficient, but closer to the original source:

$LFB0 = .
my_function(int, int):
        addiu   $sp,$sp,-16
        sw      $fp,12($sp)
        move    $fp,$sp
        sw      $4,16($fp)
        sw      $5,20($fp)
        lw      $3,16($fp)
        lw      $2,20($fp)
        addu    $2,$3,$2
        sw      $2,0($fp)
        lw      $2,16($fp)
        addiu   $2,$2,-2
        sw      $2,4($fp)
        lw      $3,0($fp)
        lw      $2,4($fp)
        addu    $2,$3,$2
        sw      $2,0($fp)
        lw      $2,0($fp)
        move    $sp,$fp
        lw      $fp,12($sp)
        addiu   $sp,$sp,16
        j       $31
        nop
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Assume that the variables x and y are passed from argument registers $a0 and $a 1 respectively. The returned value should be stored in register $v0. Note that you need to use stack to store any other registers if you use them in this procedure. – Shubham R Mar 21 '17 at 08:44
  • This was the overall requirement of my question – Shubham R Mar 21 '17 at 08:45
  • @SRingne: sure - obviously you'll need to take care of the calling convention and other minor details in the assignment - using godbolt to generate the guts of the function is just a useful helping hand... – Paul R Mar 21 '17 at 08:46