Thread: Converting C into MIPS Assembly

  1. #1
    Registered User
    Join Date
    Oct 2009

    Converting C into MIPS Assembly

    Hi, I wanted to convert this code into assembly but I'm having trouble. Here's what I got :]

    int b = 0, x; 
    while (x != 0) { 
             b += ((x & 03) == 2); 
        x >>= 1;
    believe this is just a basic instruction to count the number of occurrences of the bit sequence 10 (one zero) in x.

    Now to convert this, here's what I got:
    L1: bne  $s1, $zero, DONE    # branch if ! ( x ==0j )
    addu $s0, $ra, $0 #b = 0 store ra
    add  $s1, $s1, $s2 #    b+=$s2
    beq $s2, $s2, 2 b += ((x & 03) == 2
    addi $s3, $s3, 1 #    x>>=1
    j    L1 # jump back to top of loop
    but I'm thinking that there are some logical issues with this ASM code. Is there a different way to approach this conversion? Thanks :]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Use a compiler, and study the generated assembler; patch up to match your processor.

    It's even easier if you just get a cross-compiler that targets MIPS - the job is as good as done
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    MSVC 2008 has the option to compile for MIPS.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Just a small "thing". You wouldn't be using the "s" registers, since they're designed for "long" use. You'd want to be using the "t" registers to store the temporary values. Doesn't make much sense you're adding to "b" before you evaluate the expression? :\

    Not sure why you have a branch-on-equal instruction (beq). I don't see where that would come into your loop, since you'll then skip "x >>= 1;". So yes, your logic is wrong.

    As others have said, generating the optimised MIPS assembly would be easier for learning, but don't get in the habit, especially if you have to do this in an exam. You may find the compiler might even be putting things on the stack if it's not optimised.
    Last edited by zacs7; 02-13-2010 at 06:20 PM.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    I would approach this problem by rewriting the C code as simply as possible. Any line that has more than one operator on it probably needs to be simplified (with a few exceptions). Get it to the point where you can rewrite every line of C as a line of MIPS. A few obvious problems that jump out are the fact that you use the << and & operators, but haven't used the corresponding instructions . Also, you're using bne, but the comment right next to it implies the opposite comparison.

    If you want to go the compiler route, MIPS-SDE is a cross-compiler available for free download from the MIPS website, but if you already have VS as mentioned above - that's certainly easier. Though I'd second the recommendation not to do so if you're doing this for a class. I don't think there's anything wrong with your approach - you just need to have a better understanding of how each operation in C breaks down into the operations done in Assembly.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    If you turn off all optimizations in the MSVS compiler and set it for MIPS you may be able to learn something via its output. I would not turn this code in for credit but it may give you a push in the right direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-02-2008, 10:00 PM
  2. A question of the MIPS assembly program
    By ok_good in forum Tech Board
    Replies: 0
    Last Post: 05-03-2006, 10:13 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. converting c/c++ code to assembly
    By moonwalker in forum A Brief History of
    Replies: 8
    Last Post: 07-25-2002, 02:15 PM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM