Thread: Converting C to MIPS?

  1. #1
    Registered User
    Join Date
    Mar 2010

    Converting C to MIPS?


    I've just started looking at MIPS assembly and one of the questions in my book asks to convert C to MIPS:

    for (i = 1; i <= 25; i += 2) {
             A[i] = A[i-1] - B[i-1];
    Where A and B are both arrays of integers, an addressed at $s2 and $s4 respectively.

    I'm not sure how to approach this as I've never really coded in C and very new to MIPS. Would I store i to a temporary register like say $t0? Also would I need to rewrite this as a while loop for MIPS? I'm not really sure how to write it as a For loop in MIPS. I would use a "Break" if it were an if/then loop, but like I said I'm not sure how to handle the for loop.

    Any info or starting tips would be greatly appreciated.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Yes, you'll need to store 'i' in some temporary register. As you said $t0 is a good place.

    Just keep looping, but branch when 'i' ($t0) is greater than 25. Your loop may look something like:
    addi $t0, $0, 1 
    addi $t1, $0, 25
       # i <= 25
       bgt $t0, $t1, end
       # ... stuff in the loop
       # i += 2
       addi $t0, $t0, 2
       # go to the next iteration
       j loop
       # ... stuff after the loop ...
    Don't get caught up in the while/do-while/for loops, they all can be written as for loops, while loops or do-while loops.
    Last edited by zacs7; 03-25-2010 at 05:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Funny Binary to Mips
    By clag in forum Windows Programming
    Replies: 0
    Last Post: 02-22-2010, 01:14 AM
  2. Converting C into MIPS Assembly
    By clag in forum C Programming
    Replies: 5
    Last Post: 02-13-2010, 07:48 PM
  3. Cross Compiling GDB for MIPS
    By vlrk in forum Linux Programming
    Replies: 4
    Last Post: 10-21-2009, 09:18 AM
  4. Replies: 1
    Last Post: 09-20-2009, 07:39 AM
  5. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM