Sign in to follow this  

[C++] [GCC] - Inline Assembly

This topic is 3731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Me parece que o seguinte código é considerado ilegal no GCC, porquê?
movb 2(%1, %%ecx), (%0, %%ecx);
E eu tenho um código asm definido como volatile que executa um loop:
    __asm__ __volatile__ (
         "xorl %%ecx, %%ecx;"
         "bgrLoop:;"
         "movb  (%1, %%ecx), %%al;"
         "movb 2(%1, %%ecx), %%ah;"
         "movb %%al, 2(%0, %%ecx);"
         "movb %%ah,  (%0, %%ecx);"
         "addl $3, %%ecx;"
         "decl %2;"
         "jnz bgrLoop;" : "=r" (buffer) : "r" (buffer), "r" (size) : "ah", "al", "ecx");
Porquê quando eu compilo ele com -O3 o compilador me retorna dizendo que bgrLoop já foi definido? Felipe

Share this post


Link to post
Share on other sites
I don't speak Spanish so I don't know what your question is, however: is there a good reason why you're writing that loop in assembler? I'm pretty sure it won't improve your performance; I think the compiler should be able to generate better code than that (specifically using the PREFETCH instruction), so why not just write:


for (std::size_t i = 0; i < size; i += 3) {
char tmp = buffer[i];
buffer[i] = buffer[i + 2];
buffer[i + 2] = tmp;
}

Share this post


Link to post
Share on other sites
Ae mané, aqui tem que falar em inglês morô?! Se não ninguém vai te ajudar principalmente por que tem poucos brasileiros e portugueses por aqui. Bom, eu não manjo de assembly então não posso ajudar. Se liga :P hiaehiaehiaeiaheiaeai

Share this post


Link to post
Share on other sites
Quote:
Original post by ZQJ
I don't speak Spanish ...


I guess this is Portugues, he's from Brazil. ;)

If you try to use English, this will be more useful as nearly everybody here understand it and your post may have more replys and be read by many others so that they can benefit from it as well...

English is NOT my native language, but using it whaen I need it and when I can help others is a reasonable thing.

[Edited by - yehdev_cc on September 30, 2007 1:16:21 AM]

Share this post


Link to post
Share on other sites
Yes, this is portuguese. Ok ok, I'll translate that :P Here it goes:

"It seems that the following code is illegal in GCC, why?


movb 2(%1, %%ecx), (%0, %%ecx);


I have an asm code defined as volatile which executes a loop:


__asm__ __volatile__ (
"xorl %%ecx, %%ecx;"
"bgrLoop:;"
"movb (%1, %%ecx), %%al;"
"movb 2(%1, %%ecx), %%ah;"
"movb %%al, 2(%0, %%ecx);"
"movb %%ah, (%0, %%ecx);"
"addl $3, %%ecx;"
"decl %2;"
"jnz bgrLoop;" : "=r" (buffer) : "r" (buffer), "r" (size) : "ah", "al", "ecx");


Why when I compile that using -O3 the compiler returns me an error saying that bgrLoop was already defined?"

Thats it ._.

Share this post


Link to post
Share on other sites
Your problem with 'bgrLoop' loop is that you probably use that name as a label in more than one place, but that is a global label and can only be used once. Use local labels instead. Local labels are 0-9, and you can use f/b to refer to the label in front or behind the jump/branch.

Also your output/input/clobber section is wrong. The operand "buffer" is not an output because you never write to it. You write to memory that "buffer" points to, so you can use "memory" in your clobber section, but do not declare "buffer" as output, the compiler will emit incorrect assembly if you do.

See below for an example using 0 and 0b and with a correct output/input/clobber section.


__asm__ __volatile__ (

"xorl %%ecx, %%ecx;"

"0:;"

"movb (%0, %%ecx), %%al;"

"movb 2(%0, %%ecx), %%ah;"

"movb %%al, 2(%0, %%ecx);"

"movb %%ah, (%0, %%ecx);"

"addl $3, %%ecx;"

"decl %1;"

"jnz 0b;" : /* no output */ : "r" (buffer), "r" (size) : "ah", "al", "ecx", "memory");


Share this post


Link to post
Share on other sites

This topic is 3731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this