[C++] [GCC] - Inline Assembly

Started by
4 comments, last by outRider 16 years, 6 months ago
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
Advertisement
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;  buffer = buffer;<br>  buffer = tmp;<br>}<br><br></pre></div><!–ENDSCRIPT–> 
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
.
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]
D:...You are a Muslim, and you don't understand our freedom...M:And you are a Western who doesn't know Muhammad ...
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 ._.
.
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");

This topic is closed to new replies.

Advertisement