Stupid GCC inline assembler doesn't even support local variable
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Stupid GCC inline assembler doesn't even support local variable
Hi,
It's good that GCC support inline assembly Intel syntax, but it cannot even simply address local variables/parameters properly, making itself stupid and essentially useless, look at the following example:
Code:
// In MSVC, this compiles successfully
int main(int argc, char *argv[])
{
int x=1, f=2, fa=3;
__asm{
int 3
mov eax, [x+4]
movss xmm1,[f+4]
fld [fa+4]
}
return 0;
}
Code:
// In GCC AT&T syntax, this compiles successfully
int main(int argc, char *argv[])
{
int x=1, f=2, fa=3;
asm("int $0x3");
asm("mov 4%0,%%eax"::"m"(x));
asm("movss 4%0,%%xmm1"::"m"(f));
asm("fld 4%0"::"m"(fa));
return 0;
}
Code:
// In GCC Intel syntax, this would fail to compile
int main(int argc, char *argv[])
{
int x=1, f=2, fa=3;
asm(".intel_syntax noprefix\n"
"int 3\n"
"mov eax, [x+4]\n"
"movss xmm1,[f+4]\n"
"fld [fa+4]\n"
".att_syntax\n");
return 0;
}
The third piece of code would fail to compile under GCC, because although it supports intel syntax it cannot handle local variables nor function parameters. I hope future versions of GCC can support this feature better. Thanks!
Best regards,
Wang Xuancong
Last edited by xuancong; 07-05-2010 at 02:47 AM.
Reason: half problem solved
Personally, I think Intel's bass-ackwards syntax is "stupid".
I vastly prefer the consistency of ATT syntax. And I especially like being able to use the same general syntax across many different architectures (the Sun does NOT rise and set on Intel 80x86!).
Worse, there's a ton of Intel MASM based assembly code available on the web - most of it written for 16-bit *DOS*. Which, of course, tends to instill some very bad habits for anybody who wants to learn assembly on a modern, VMM-based OS.
I'd strongly encourage anybody interested in assembly to learn both ATT and Intel syntax. I'd even go so far as suggesting they learn ATT *first* (then decide which they prefer after becoming conversant in BOTH).
Hey Guys,
If you don't know how to solve this problem, don't post non-sense, ok? Apparently, AT&T syntax cannot achieve the same functionality as the Intel syntax in my case.
XC
Hey Guys,
If you don't know how to solve this problem, don't post non-sense, ok? Apparently, AT&T syntax cannot achieve the same functionality as the Intel syntax in my case.
XC
I didn't read your complete post, because you seem to be missing important tags to make your post readable. Furthermore, I don't have much experience with inline assembler, but just for the case you don't get any qualified answer in this forum, you could post your questions (possibly a bit shorten and more compact) to the GCC mailing list ...
Andi
P.s. I don't think that paulsm4's post is senseless!!!
The following quote as taken from GCC inline assembler but I'm fairly stupid and don't know assembler which is why I used a search engine.
Quote:
int main(void)
{
int foo = 10, bar = 15;
__asm__ __volatile__("addl %%ebx,%%eax"
:"=a"(foo)
:"a"(foo), "b"(bar)
);
printf("foo+bar=%d\n", foo);
return 0;
}
Here we instruct gcc to store foo in eax (constraint a) and bar in ebx (constraint b). We further declare that foo is only written (constraint modifier =). No registers are clobbered besides the declared output registers.
If you don't know how to solve this problem, don't post non-sense
I totally agree with you regarding paulsm4's post, but:
Quote:
Apparently, AT&T syntax cannot achieve the same functionality as the Intel syntax in my case.
That is also nonsense. AT&T syntax supports everything you're trying to do. Your lack of knowledge regarding both AT&T syntax and gcc inline assembler doesn't make either of those things stupid nor inadequate. It might make them the wrong tools for you.
You seem to want to write almost all of a function in assembler. GCC inline assembler is quite difficult to master. Writing a whole function in assembler (and assembling it separately with NASM) is far easier than writing most of a function in assembler.
You could change to a whole function in assembler. I know that could work and I think it would be easiest.
You could switch to AT&T syntax. I know that could work. I know AT&T syntax has full support for everything you're trying to do. But that doesn't make the translation easy.
You could stick to roughly the code you have now but modify it to correctly mix C and asm code according to gcc rules. I think that could work. I don't know Intel syntax gcc inline syntax well enough to be certain.
Quote:
Originally Posted by xuancong
it cannot even simply address local variables/parameters properly, making itself stupid and essentially useless
Certainly gcc inline asm can address local variables. It is a very powerful tool that can do far more than MS inline asm can do. But it doesn't have an "easy" mode.
The syntax for the asm instructions themselves is ordinary, but there is a whole extra language you need to know to make the connection between a chunk of asm code and the world outside that chunk of asm code (especially local variables in the same function).
That extra language is difficult and generally not worth it. It is usually better to write a whole function in ordinary asm rather than part of a function in inline asm.
Hey Guys,
If you don't know how to solve this problem, don't post non-sense, ok? Apparently, AT&T syntax cannot achieve the same functionality as the Intel syntax in my case.
XC
Hey xuancong,
try posting your code in "[ code ]" "[ /code ]" so it comes up nice and neat instead of smashed together. It looks like non-sense. If you don't want to have any manners or go through the effort of trying to post readable code, don't expect instant help. Expect to linger around here for a while and check on your thread over and over again or complain at everyone.
Last edited by pr_deltoid; 07-03-2010 at 06:49 AM.
#include <stdio>
#include <string.h>
char inlined[] = "this is a message from intel syntaxed inlined assembly code\n";
int len;
int main (void)
{
printf("this is a sample intel syntaxed and inlined c program\n");
len = strlen(inlined);
printf("%d\n",len);
asm(".intel_syntax noprefix\n");
asm("mov edx,len\n");
asm("mov ecx,offset inlined\n");
asm("mov ebx,1\n");
asm("mov eax,4\n");
asm("int 0x80\n");
printf("probably it is a success look in gdb\n");
return 1;
notice the first line :
asm(".intel_syntax noprefix\n");
This denotes that the folowing assembly snippet uses intel syntax to the
native assembler gas
Apparently it supports inline Intel syntax assembly, so what are you talking about? What are you having a problem with, specifically? Try posting your code in [ code ] [ /code ] (no spaces) like I said. It makes it much neater and no one has to go through any trouble to read it easily. Point out what, specifically, you're having trouble with. Anything.
If you want people to read your code and figure it out for you, make your code neat and easy to read.
Last edited by pr_deltoid; 07-03-2010 at 07:10 AM.
so what are you talking about? What are you having a problem with, specifically?
The main issue is that the OP wants to be able to declare a local variable in C code, such as
Code:
float vout[4];
then read and write the elements of that variable in the inline asm code.
I think I already gave the best answer to that question, which is: Don't do that, instead write the whole function in asm.
Another good answer would be a link to the tutorial (I've misplaced that link myself) that does a good job of explaining the obscure syntax gcc has for connecting local variables into inline asm.
I expect the OP would prefer if someone provided the exact syntax, fitting this example, for both declaring the use of vout[4] by the inline asm and actually reading and writing the elements within the asm code. I don't have the expertise to do that from memory, nor the patience to look up and then test the details, nor the belief that it is the best thing to give the OP.
I obviously think there is a fundamental difference between my non answer ("instead write the whole function in asm") and the other non answers posted here (such as Paulsm4's "learn ATT *first*"). Hopefully xuancong can see that difference, rather than just seeing non answers. (I also just thanked Pualsm4 for valuable help in an unrelated thread. I hope I won't be losing such help by my comments here.)
You don't walk in to somebody else's home as a guest and immediately start calling things "stupid". That's simply rude
It's also "stupid"
Q: Is there any specific thing you tried, but were unable to do with ATT syntax?
Thanx in advance .. PSM
PS:
You cut/pasted EXACTLY the same ("stupid") rant on other sites, and you cut/pasted the same ("ignorant", "nasty" - you choose ) response when people there legitimately tried to help you. For example:
Reading the rules and guidelines and adhering to them is not nonsense. Failing to do so will get you ignored or worse. I programmed asm exclusively for years but I now have zero interest in your problem. I WOULD be surprised if the choice of a set of mnemonics for the machine code was a show-stopper.
And again - GCC should *fully* support *anything* you want to do. Honest.
Hey guys,
I might have been rude since I searched throughout Google and manuals and tried all possible ways but still cannot find the solution. I'm getting so frustrated and started by calling things 'Stupid'. I apologize for that.
However, I don't blame things for no reasons, in fact if any one of you can translate the following 3 instructions into GCC inline assembly syntax, either AT&T or Intel, the entire problem is solved:
where x,f and fa are local variables or function parameters. I've already figured out how to translate the 1st instruction exactly as it is. Take note that you are not allowed to make use of any other registers since all of them are in use; and you are not allowed to pushing any registers onto stack and use them temporarily since that will slow down speed.
It compiles but it doesn't link, and if you turn on warnings you'll notice that it doesn't actually use the variables you named in the code, it assumes they are global symbols to be resolved at link time.
This links, can't really test it because it's just fragments:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.