Like Tree1Likes
  • 1 Post By dmh2000

Convert C into MIPS

This is a discussion on Convert C into MIPS within the Tech Board forums, part of the Community Boards category; I wanted to convert this C code into MIPS. C Code: f = A[B[h-g]] We assume that h > g ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18

    Convert C into MIPS

    I wanted to convert this C code into MIPS.

    C Code:

    f = A[B[h-g]]

    We assume that h > g & B[h-g] > 0. h,g,f are integers.

    Also assume that f is assigned to register $s0, g to $s1, h to $s2.

    Base addresses of A -> $s6 and B -> $s7

    Here is my attempt:

    sub $t0, $s2, $s1
    mult $t0, $t0, 4
    lw $t0, $t0($s7)
    mult $t0, $t0, 4
    sw $s0, $t0($s6)

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    63
    Why do you post in a C forum if you want to write MIPS assembly?

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18
    oh...then where should I post this??

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    I would compile with the -S option (or whatever it is for your compiler) to see what the compiler generates. at least as an example.
    stahta01 likes this.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    It should probably be posted in the Tech Board. If a mod wants to move it...

    You're almost there. I think that last line ought to be a lw as well. The way you have it written, you're storing $s0 in $t0($s6), i.e. reversing the assignment, putting f into A[B[h-g]]. You want to load A[B[h-g]] into $s0 (f). Other than that, it looks right, but it's been a while since I had anything to do with MIPS, so I wouldn't notice more subtle problems.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18
    @anduril462 Yes it should be lw. Thanks a lot guys!

    The corrected code is shown below:

    Code:
    sub $t0, $s2, $s1
    mult $t0, $t0, 4
    lw $t0, $t0($s7)
    mult $t0, $t0, 4
    lw $s0, $t0($s6)
    
    Last edited by stud91; 02-06-2012 at 11:25 AM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Just my 2c, but you might shave off some cycles by replacing the mult with sll:
    Code:
    sub  $t0,$s2,$s1
    sll  $t0,$t0,2
    lw   $t0,$t0($s7)
    sll  $t0,$t0,2
    lw   $s0,$t0($s6)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert c programm to mips assembly
    By mariakat in forum Tech Board
    Replies: 8
    Last Post: 02-14-2011, 07:39 PM
  2. help me convert c to mips
    By chicken in forum C Programming
    Replies: 3
    Last Post: 11-03-2010, 10:35 AM
  3. Need help with Mips
    By NoobieGecko in forum C Programming
    Replies: 11
    Last Post: 02-25-2008, 01:57 PM
  4. MIPS instructions
    By kokopo2 in forum C Programming
    Replies: 2
    Last Post: 08-24-2005, 01:02 AM
  5. Have you ever programmed for MIPS?
    By Maragato in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-05-2004, 08:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21