0
votes

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!

2
Use a compiler, then study the output.J...
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
@eva - follow the link to stackoverflow.com/questions/5666023/… 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
@ChrisStratton That link is pitched more specifically to Linux. Options for Windows (if OP is using Windows) : stackoverflow.com/a/12983016/327083J...

2 Answers

1
votes

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

-1
votes

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