Thread: C code to assembly

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    C code to assembly

    Hi guys!
    can you help me please to convert this code :
    Code:
    int binom(n, k)
    {
    if (k == 0 || n == k)
    return 1;
    return binom(n-1, k-1) + binom(n-1, k)
    }
    to assembly code of MIPS ? I know that compiler can convert code c to assembly but it's converting to assembly language of I7 ! and I need assembly language of MIPS.

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please refer to the forum guidelines:
    4. Don't ask people to do all your work for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I know that compiler can convert code c to assembly but it's converting to assembly language of I7 ! and I need assembly language of MIPS.
    They're called cross compilers.

    A cross compiler runs on one architecture (say your windows machine, running on an I7), and it produces assembler (amongst other things) for another architecture (say MIPS).

    This will be a fun learning experience for you.
    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.

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    104
    Either you made a mistake or whoever gave you that algorithm is a troll. lol, I love them already
    Given the fact that:
    1. If k > n, this goes on forever.
    2. No matter what, if the function exists it will always return 2.
    we can easily rewrite this like:
    Code:
    int binom(int n, int k)
    {
        while (k > n)
            continue ;
        return (2);
    }
    Which is probably a lot simpler to write in assembly.

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    104
    Actually, your code eventually seg faults -due to stack overflow I imagine. So technically to mimic the orginal code correctly, replace the continue call with binom(n, k); That will do the trick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is object code the same as assembly source code?
    By c_weed in forum Tech Board
    Replies: 3
    Last Post: 01-05-2012, 07:25 PM
  2. Generate Assembly code and Binary code also
    By Hannibal2010 in forum C Programming
    Replies: 16
    Last Post: 07-07-2011, 05:43 AM
  3. Convert assembly>machine code, machine code>assembly
    By wenxinleong in forum C Programming
    Replies: 12
    Last Post: 06-23-2011, 10:42 PM
  4. GCC option to get assembly code AND C code
    By AntoineC in forum C Programming
    Replies: 2
    Last Post: 04-29-2010, 09:04 AM
  5. assembly code for release code?
    By George2 in forum Windows Programming
    Replies: 4
    Last Post: 07-09-2008, 11:17 AM

Tags for this Thread