From: Luke D. <cod...@ho...> - 2002-11-08 02:35:35
|
I haven't really used inline asm but from what I understand this code is far from correct, and in this tiny example it only works by chance. My guess at a corrected version is: asm ( "movl $12, %ebx;" "movl %ebx, %0;" "movl %ebx, %1;" : "=m" (a), "=m" (b) /* outputs */ : /* no inputs */ : "ebx" /* clobber */ ); First of all, read the sections of the GCC 3.2 manual about inline assembler, and remember that unlike Windows compilers such as MSVC, GCC is portable so it cannot understand assembly language. The variables "a" and "b" are referred to as %0 and %1 because of their order in the list of outputs, and according to the documtation ebx should be referred to as %%ebx, but it seems to work as %ebx. The "=m" means that the these arguments should be memory addresses, but I'm sure it would work with "=g" or something. One reason why you must tell the compiler what the outputs are is because optimisation could change the order of statements so the program could become incorrect otherwise. It is also very important to tell the compiler that "ebx" changed because it could be using the register for some other purpose. In fact on win32 it is a rule that certain registers (including ebx) must not change across a function call, so if you look at the generated assembler (gcc -S) you will see that GCC now adds code to save and restore ebx. You could also specify "12" as an input to the asm statement for greater flexibility (e.g. using the value of a preprocessor macro). Luke Dunstan ----- Original Message ----- From: "Greg Chicares" <chi...@ya...> To: <min...@li...> Sent: Thursday, November 07, 2002 10:19 PM Subject: Re: [Mingw-users] I'm confused when using the inline asm in g++ > Hanzac Chen wrote: > > > > Today I found a problem about the inline asm in g++, > that is : > > > > When I want to change the value of a & b, but the > result of running is that > > the value of a is still 20, and b is changed to 12. > > I compiled and ran your example and got > 12 > 12 > so both a and b were changed. Here's the exact code > I used, with minimal changes to make it compile and > run with g++-3.2 ; I used the command 'g++' with no > options. > > #include <iostream> > > int a, b; > > void f1(void) > { > a = 20; > b = 30; > > asm > ( > "movl $12, %ebx;" > "movl %ebx, _a;" > "movl %ebx, _b;" > ); > > // cout<<""; > > std::cout<<a<<std::endl; > std::cout<<b<<std::endl; > } > > int main(){f1();} > > > Then I tried to and "cout<<"";" , the result is > right. > > So I think the code in asm{} don't change the value > at once. > > That is it must be actived by some code outside the > asm{}. > > Perhaps you compiled with optimization and then > inspected values with a debugger. In that case, > the optimizer may change the order in which > statements are executed. > > > __________________________________________________ > Do you Yahoo!? > U2 on LAUNCH - Exclusive greatest hits videos > http://launch.yahoo.com/u2 > > > ------------------------------------------------------- > This sf.net email is sponsored by: See the NEW Palm > Tungsten T handheld. Power & Color in a compact size! > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0001en > _______________________________________________ > MinGW-users mailing list > Min...@li... > > You may change your MinGW Account Options or unsubscribe at: > https://lists.sourceforge.net/lists/listinfo/mingw-users > |