0

i would like to try converting a C program into MIPS assembly coding

here is the C language program:

int x=2;

int index;

for(index = 0;index<4;index++){

     x=x+index;

}

Heres my attempt on the MIPS assembly coding:

      li $8,4   # the limit
      li $9,2   #x = 2
      li $10,0  #index, starts at 0

forLoop:
      slt $11,$10,$8   #if index <4 then $11 = true =1
      beq $11,$0,Exit  #if $11 = 0 = false means reached 4, then exit
      add $9,$9,$10    #adding the index with the value in x
      addi $10,1       # add 1 to the index if didnt reach the limit
      j forLoop        # repeat the loop
Exit:
      nop              #end 

need to ask you all if this is correct as i dont have a mips simulator, not sure how to end the program, is nop the valid exit plan?

Liquified
  • 93
  • 2
  • 3
  • 11

2 Answers2

1

Here you go, a simple version that translates your C code into MIPS:

Note: I am using SPIM for this.

main:
    li $t0, 2           # $t0 = x = 2
    li $t1, 0           # $t1 = index = 0
    li $t2, 4           # $t2 = indexLimit = 4
    jal forLoop         # jump and link the forLoop label
    move $a0, $t0       # move the result into $a0 for printing
    li $v0, 1           # load print integer code
    syscall             # tell system to do it
    li $v0, 10          # load exit code
    syscall             # clean exit

forLoop:
    bge $t1, $t2, exit  # if index >= 4 goto exit label
    add $t0, $t0, $t1   # x = x + index
    addi $t1, $t1, 1    # index++
    j forLoop           # continue loop by jumping back up

exit:
    jr $ra              # jump and return the return address

To answer your question: nop does nothing at all. You can use it for timing purposes etc. Here is a Wikipedia link for further reading http://en.wikipedia.org/wiki/NOP. Note to end a MIPS program load 10 into $v0 and then syscall.

EDIT:

In response to your comment: you are on the right track but don't forget to add a main label, then jump from the main label to the forLoop and then have your Exit label terminate the program (print the integer first if so desired).

These are two useful links for programming in MIPS: http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm

wazy
  • 1,065
  • 1
  • 10
  • 23
  • not trying to sound ignorant here, but is my code correct at all? i know yours is right but i would like to see if my code is in the right direction first, excluding the nop at the exit, what do you think of the code? – Liquified Jun 19 '13 at 05:40
  • Yea you are on right track. I am using SPIM and thus addressing the registers as $t0, $t1, etc. Check the register figure on this page http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm. Please do not forget to put a main: label! In your code you can simply print the integer then terminate in your Exit label since you are not linking anywhere. Updated question. – wazy Jun 19 '13 at 05:58
  • Thanks for the swift reply! i shall check out that website u suggested, thanks again – Liquified Jun 19 '13 at 06:06
  • Glad, I could help. If you have future MIPS questions don't hesitate to ask and feel free to contact me with them. – wazy Jun 19 '13 at 06:13
0

Perhaps you need to look into this How do you get assembler output from C/C++ source in gcc? and use your cross compiler to produce assembly files.

Community
  • 1
  • 1
ila
  • 58
  • 1
  • 1
  • 9
  • He would have to set up a cross-compiler to obtain MIPS assembly. – wazy Jun 19 '13 at 06:02
  • 1
    @wazy: It's not that hard to find a prebuilt MIPS toolchain for Windows/Linux/OSX hosts. For example, one could download the Android NDK. – Michael Jun 19 '13 at 06:06
  • @Micheal I never said it was hard =). Just seems OP is learning the basics of MIPS at the moment. – wazy Jun 19 '13 at 06:12