0

I have the following C code, that I would like to translate into MIPS assembly.

int fib_iter(int n) {
  int i, f0, f1, f;
  f0 = 0;
  f1 = 1;
  if (n == 0) return f0;
  if (n == 1) return f1;
  for (i = 2; i <= n; i = i + 1) {
    f = f0 + f1;
    f0 = f1;
    f1 = 1;
  }
  return f;
}

I have not idea how to do it though, and I couldn't find a good tutoriol anywhere. I have read the wikipedia article about MIPS (https://en.wikipedia.org/wiki/MIPS_instruction_set) and found the table about all the assembly commands pretty helpful already, however actually translating is still unclear to me.. Could you maybe show me with the small example up there how to do it and then I can try myself? Thank you!

ec-m
  • 779
  • 1
  • 5
  • 15
  • The line at the end of the `for` loop: `f1 = 1;` looks like a typo - it should almost certainly be: `f1 = f;`. – Paul R May 13 '14 at 16:26
  • @thumbmunkeys http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated – Sedat Kapanoglu May 13 '14 at 16:28
  • 2
    Use a compiler, then study the output. – J... May 13 '14 at 16:33
  • @ssg I am asking because the "small example" seems to be the whole exercise – thumbmunkeys May 13 '14 at 16:34
  • 1
    actually - yes, the code is homework. i just wanted a small example of the code to be translated and explained, so i can do the rest. as i said in: "and then i can try myself". If you have a different example, that is fine too.. i just have no clue how to start. – ec-m May 13 '14 at 16:37
  • @J... what compiler should I use to do that? – ec-m May 13 '14 at 16:38
  • 1
    http://stackoverflow.com/questions/5666023/compile-code-in-c-to-mips-assembly – thumbmunkeys May 13 '14 at 16:39
  • @thumbmunkeys and why is asking for help with my homework bad? – ec-m May 13 '14 at 16:40
  • 1
    @eva - follow the link to http://stackoverflow.com/questions/5666023/compile-code-in-c-to-mips-assembly and follow the procedure there. The point of help with homework should be to obtain assistance in understanding how to solve the problem, not to be given the solution. – Chris Stratton May 13 '14 at 16:41
  • 1
    @ChrisStratton That link is pitched more specifically to Linux. Options for Windows (if OP is using Windows) : http://stackoverflow.com/a/12983016/327083 – J... May 13 '14 at 16:49
  • 1
    I am running suse linux, but still the link doesn't quite help me. First they direct C_Guy to cross-compilers which seem quite hard to use (see the comment "i am going nuts over those cross compilers" under the answer with 4 up votes) and then the link in the answer to that question "code sourcery" does not work. do you have a different link that might help me? or can you shortly explain how to use the cross-mips-binutils i found as a package in my zypper-library? thank you – ec-m May 13 '14 at 16:55
  • Your prof should have provided a program that assembles and runs. (If s/he didn't, drop the course. S/he's incompetent.) Assemble that program and run it. Now study how it works: How does it allocate variables? How does it use registers? How does conditional execution work? How does it implement function call and return? Now transfer that learning to your new problem and code it with a pencil. Step through it mentally with example data. Code it within your already-working example. Learn how to use a debugger to step through the code to verify it's doing what your pencil drill indicated. – Gene Aug 06 '14 at 23:18

2 Answers2

1

I think the idea of your homework is to try and get you to think about how a high level language like C is translated by a compiler into assembler. To think about registers, logical tests, jumps, stacks and so on.

I believe your little program will return the nth number in the Fibonacci sequence. If I were set this, I would expect to write a simple MIPS assembly fragment to do the same thing.

So, You have five numbers to think about in this little function, you could use five registers for these (or less if you are clever). There is some equality checking and a loop too.

So, you should look at the add, bne, beq and j instructions. Also look at labels (to use with bne and beq) and decide on which calling convention to use so as to know the return address and value registers to use with j.

Good luck!

See also - MIPS Architecture and Assembly Language Overview

IanNorton
  • 7,145
  • 2
  • 25
  • 28
-1

My gcc generated this assembly from your code.

    .file   1 "C2MIPS.c"

 # -G value = 8, Cpu = 3000, ISA = 1
 # GNU C version cygnus-2.7.2-970404 (mips-mips-ecoff) compiled by GNU C version cygnus-2.7.2-970404.
 # options passed:  -msoft-float
 # options enabled:  -fpeephole -ffunction-cse -fkeep-static-consts
 # -fpcc-struct-return -fcommon -fverbose-asm -fgnu-linker -msoft-float
 # -meb -mcpu=3000

gcc2_compiled.:
__gnu_compiled_c:
    .text
    .align  2
    .globl  fib_iter
    .ent    fib_iter
fib_iter:
    .frame  $fp,24,$31      # vars= 16, regs= 1/0, args= 0, extra= 0
    .mask   0x40000000,-8
    .fmask  0x00000000,0
    subu    $sp,$sp,24
    sw  $fp,16($sp)
    move    $fp,$sp
    sw  $4,24($fp)
    sw  $0,4($fp)
    li  $2,1            # 0x00000001
    sw  $2,8($fp)
    lw  $2,24($fp)
    bne $2,$0,$L2
    lw  $3,4($fp)
    move    $2,$3
    j   $L1
$L2:
    lw  $2,24($fp)
    li  $3,1            # 0x00000001
    bne $2,$3,$L3
    lw  $3,8($fp)
    move    $2,$3
    j   $L1
$L3:
    .set    noreorder
    nop
    .set    reorder
    li  $2,2            # 0x00000002
    sw  $2,0($fp)
$L4:
    lw  $2,0($fp)
    lw  $3,24($fp)
    slt $2,$3,$2
    beq $2,$0,$L7
    j   $L5
$L7:
    lw  $2,4($fp)
    lw  $3,8($fp)
    addu    $2,$2,$3
    sw  $2,12($fp)
    lw  $2,8($fp)
    sw  $2,4($fp)
    li  $2,1            # 0x00000001
    sw  $2,8($fp)
$L6:
    lw  $2,0($fp)
    addu    $3,$2,1
    sw  $3,0($fp)
    j   $L4
$L5:
    lw  $3,12($fp)
    move    $2,$3
    j   $L1
$L1:
    move    $sp,$fp         # sp not trusted here
    lw  $fp,16($sp)
    addu    $sp,$sp,24
    j   $31
    .end    fib_iter
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424