CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2013
    Posts
    2

    if any1 know how to convert this c++ code into assembly please do help ......urgently

    for (a=0; a<=y; a++)
    for (b=0; b<=y/2; b++)
    for (c=0; c<=y/3; c++)
    if ((a + 2*b + 3*c) == y)
    count++;

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: if any1 know how to convert this c++ code into assembly please do help ......urge

    Write this in C and see Assembly code produced by compiler. You can see Assembly code by producing compiler Assembly listing, or in Debugger, by opening Disassembly window.

  3. #3
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Re: if any1 know how to convert this c++ code into assembly please do help ......urge

    I think I am safe to assume that all of the variables in your code block are integers, that y is set as an input parameter before your code block is executed, and that you meant to initialize count to 0 at the beginning of your code block.

    Now, in order to convert your code block to assembly, I need to know what processor you are using. Since you did not supply this information, I will assume you want the assembly for an x86 processor.

    Code:
    int a;
    int b;
    int c;
    int count;
    
    _asm
    {
    		MOV  count, 0
    		MOV  a, 0
    		JMP  Branch2
    Branch1:	MOV  EDX, a
    		ADD  EDX, 1
    		MOV  a, EDX
    Branch2:	MOV  EAX, a
    		CMP  EAX, y
    		JG   Branch7
    		MOV  b, 0
    		JMP  Branch4
    Branch3:	MOV  ECX, b
    		ADD  ECX, 1
    		MOV  b, ECX
    Branch4:	MOV  EAX, y
    		CDQ
    		SUB  EAX, EDX
    		SAR  EAX, 1
    		CMP  b, EAX
    		JG   Branch1
    		MOV  c, 0
    		JMP  Branch6
    Branch5:	MOV  EDX, c
    		ADD  EDX, 1
    		MOV  c, EDX
    Branch6:	MOV  EAX, y
    		CDQ
    		MOV  ECX, 3
    		IDIV ECX
    		CMP  c, EAX
    		JG   Branch3
    		MOV  EDX, b
    		MOV  EAX, a
    		LEA  ECX, [EAX + (2 * EDX)]
    		MOV  EDX, c
    		IMUL EDX, EDX, 3
    		ADD  ECX, EDX
    		CMP  ECX, y
    		JNE  Branch5
    		MOV  EAX, count
    		ADD  EAX, 1
    		MOV  count, EAX
    		JMP  Branch5
    Branch7:
    }
    I have to admit that I did not do this conversion myself. My compiler converted the C code to machine code and then I used a utility to convert the machine code to assembly. I then cleaned up the assembly so that it uses your variables and then re-inserted the assembly back into my C code and re-compiled it to make sure it works.

    The assembly is not the most efficient code. For one thing, the expressions y/2 and y/3 could be calculated just once and stored in temporary variables / registers. Also, if more of the available registers were used, the a, b, and c variables could be eliminated. Much of the assembly could be written better. Perhaps, there is even a mathematical function, count = f(y), that could be used, which would eliminate the need for any loops. I will let some one else figure that one out.

    I am curious as to why you need this done. Since my compiler generated the machine code, writing it in assembly produces the exact same machine code, so no speed advantage is gained unless you manually make the changes to the assembly as I have noted above. Was this a homework assignment that I just did for you?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: if any1 know how to convert this c++ code into assembly please do help ......urge

    Quote Originally Posted by Coder Dave View Post
    Was this a homework assignment that I just did for you?
    Anytime you see "urgent" in a post , always suspect it is a homework problem.

    1) If it's a self-assignment, then there is no need for "urgency". They can complete their self-assignment in any time frame, since there is no real teacher, no deadline, etc.

    2) No self-respecting company would hire a programmer and pay them money to do a job, and that programmer then goes on a programming forum asking for "urgent" help from complete strangers.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Feb 2013
    Posts
    2

    Re: if any1 know how to convert this c++ code into assembly please do help ......urge

    its not homework i just was trying it out n couldnt figure it out..anyway thanks...n what compiler r u using??

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: if any1 know how to convert this c++ code into assembly please do help ......urge

    Quote Originally Posted by falcan View Post
    its not homework i just was trying it out n couldnt figure it out..anyway
    Do not state "urgent" in the title of your thread. It gives the impression that you're trying to get others to look at your post first, as if it is more important than any other post.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: if any1 know how to convert this c++ code into assembly please do help ......urge

    there is absolutely zero point in writign C/C++ code then have the compiler generated code converted into assembly, and thinking you gained somethign. You haven't, in fact you lost the readability/maintability of the C/C++ code and gained harder to readassembly.

    It it's just for "seeing what the compiler did"... Then sure, there might be something to it.

    If the intent however is... "the compiler generates code that's too slow and I want optimized assembly", then starting with the compiler generated code is the wrong way to do things. The "magic" in assembly optimisation isn't in trying to beat the compiler by changing a few instructions here and there in the hope of gaining a couple clockcycles.
    The "magic" is in approaching the problem from a different perspective that can only be achieves by going "low level", and making best use of the CPU's capabilities in ways that compiler can never do.
    Compilers are tailored for generic solutions, optimising through assembly is all about discarding this generality and specializing.


    This example doesn't even make sense. It does not need assembly, it can be solved/calculated mathematically. (assuming the idea is to calculate count for a specific value of y).

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: if any1 know how to convert this c++ code into assembly please do help ......urge

    Quote Originally Posted by Coder Dave View Post
    Perhaps, there is even a mathematical function, count = f(y), that could be used, which would eliminate the need for any loops.
    Yes there is such a function. If you think about what this problem really tries to do, the answer is quite obvious.
    (not posting the answer as I assume this is indeed a homework question, albeit not a programming one but a math one, attempted to being solved by a brute force approach).

  9. #9
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Re: if any1 know how to convert this c++ code into assembly please do help ......urge

    The compiler I used is really of no importance, except that I will say that I did not have any optimizations turned on.

    I am new here and perhaps this really is a math homework problem. That is at least better than this being a prank and I was the newb who fell for it.

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: if any1 know how to convert this c++ code into assembly please do help ......urge

    No prank, and you did nothing wrong or fall into anything. Well.. maybe being too helpful and doign someone elses homework

    I see compiler generated assembly enough in my bug tracking/crash investigations. And I'm sometimes baffled. At times compilers do certain smart tricks that make it hard to match up the assembly code back to the C code. And soemtimes, seemingly "easy" bits of C code where you expect the compiler to pull all the tricks it has are compiled into straightforward (and less than optimal) assembly. So yes, you do learn something from looking at compiler generated code.

    But optimising with assembly is hard, and something you'll spend quite a bit of time at. You really want to avoid using it as much as possible and only do it where it really matters. But when you do go assembly, you basically throw away the method used in c/c++ and approach the problem from a different angle. And more likely than not, an angle that is not possible within the confines of the higher level language.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured