Solved

Porting assembly from MinGW to Visual Studio .NET 2005

Posted on 2007-03-22
8
864 Views
Last Modified: 2008-01-09
Hello Experts,

I got some C code that compiles perfectly under MinGW. I tried to port the code to compile under Visual Studio .NET 2005 and was able to do that easily until I encountered some assembly code which Visual Studio .NET 2005 did not understand the way it was written. Please see an example of the assembly code i'm referring to:

static void avg_pixels8_mmx(uint8_t *block, const uint8_t *pixels, int line_size, int h)
{
    MOVQ_BFE(mm6);
    JUMPALIGN();

    do {
        __asm __volatile(
            "movq  %0, %%mm0        \n\t"
            "movq  %1, %%mm1        \n\t"
            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
            "movq  %%mm2, %0        \n\t"
            :"+m"(*block)
            :"m"(*pixels)
            :"memory");

        pixels += line_size;
        block += line_size;
    }

    while (--h);
}

I know that the answer to this question is not made of few words. I'm OK if someone can direct me to a good tutorial that addresses porting assembly code from MinGW to MS Visual Studio compiler family. This is not the first time I deal with assembly code inside C, actually I have written some my self, but my experience in this area is all based on MS VS IDE.

Thank you,
Khalid.
0
Comment
Question by:komar
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 18775052
I dont think you can insert MMX code  easily into a .NET program.  

Also it's quite involved to call asm code from inside a .NET program.  All the arguments have to be properly marshalled, memory has to be locked down, and plenty more.

For starters I'd rewrite it in C.  It doesnt look very comploex, just some pixel averaging.



0
 
LVL 3

Author Comment

by:komar
ID: 18775341
Thank you grg99 for your answer. Maybe my question wasn't clear from the begining because i'm not trying to port to .NET program. All i'm trying to do is to compile the same C source under MS VS .NET IDE. The project I created for this purpose was Win32 Console application which contains only .C and .H files. I'm not trying to use the .NET framework by any means.

My problem is that if I compile the sample function shown in my question the compiles will generate many errors because it does not understand assembly the way it is written now (it is understood perfectly by MinGW as I said). for the sake of an example, __volatile is not understood by VS 2005. Similarly, "movq  %0, %%mm0" is not a valid assembly instruction from the point view of MS VS 2005, etc...
0
 
LVL 24

Expert Comment

by:fridom
ID: 18777942
VS 2005 does understand inline assembler also, as you can see here:
http://msdn2.microsoft.com/en-us/library/4ks26t93(VS.80).aspx

and you could use masm to write an external assembler routine which you could link to the program
http://msdn2.microsoft.com/en-gb/library/afzk3475(VS.80).aspx

You know have "just" to figure out where the differences between the assemblers are... and what the notion
%0 means. I'm not fluent in any assembler, but from the cd I'd assume that %0 means the first parameter, and it seems the order for the assembler instruction is
from-> to

So the first measn moving the content of the first parameter into the fist mmx register?

Regards
Friedrich
0
 
LVL 5

Accepted Solution

by:
mzvika earned 500 total points
ID: 18785674
actually, that's not true.
inline assembly is supported only for x86 platform, but not AMD64/x64/EM64T (pick a name). a compiler "feature".

the problem with your code is that it uses the AT&T syntax, as opposed to INTEL syntax which microsoft's compiler understands...
check these links:
http://vmlinux.org/crash/mirror/www.objsw.com/docs/as_196.html
http://www.w00w00.org/files/articles/att-vs-intel.txt
http://www.freepascal.org/docs-html/prog/progsu101.html
http://www.freepascal.org/docs-html/prog/progsu102.html
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 24

Expert Comment

by:fridom
ID: 18790783
I was quite aware about where inline assembler is supported, therefor I suggested to write  a separate Assembler file with the implementation of this function. But the Assembler is for x86 so inline assembler will do the job. So it's very well true that inline Assembler is supported.

Regards
Friedrich
0
 
LVL 3

Author Comment

by:komar
ID: 18792607
I was hoping for an answer that will help me do the porting in 5 minutes :-) apparently this is too much to ask for.

mzvika, you provided me with the piece of information that I was looking for which is "AT&T syntax" vs. "INTEL syntax". Now I have to learn how to port assembly between the two syntax manually.

Thank you everyone.
Khalid.
0
 
LVL 5

Expert Comment

by:mzvika
ID: 18798539
there are tools for automatic translation, although i haven't used any of them.
try googling for them.

here is one i found: http://membres.lycos.fr/placr/a2i.html
again, i'm not recommending any of them since i'm not familiar with them.
0
 
LVL 3

Author Comment

by:komar
ID: 18798670
I did google for translation tools and found some (including the one you listed) but non of them support translation of inline assembly. I'm now convinced that I have to do the translation manually.

Thanks again.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now