GNU g++ inline assembly block like Apple g++/Visual C++?
I am currently following a course at my University in which, at this stage, we learn about the assembler code behind certain C/C++ constructs.
The workflow usually goes like this: the lab assistant briefly speaks about a topic, we figure out the quirks and then solve some totally random problem using inline assembly.
(For example: He briefly talks about how struct
(members) are stored in memory, we figure out the pattern and then we write the solution using inline assembly to a simple problem in which we use a struct
.)
The lab assistant (as well as the rest of the group) is using the Visual C++
compiler and debugger (for disassembly) for his demonstrations however I cannot use it due to ethical reasons and thus I opted for g++
and gdb
.
What I find awkward about g++'s inline assembly compared to Visual C++ is the fact that:
If I want to write a 'block' of inline assembly I have two options: Have a single asm("..")
construct in which each instruction is preceded by a nt
(leads to a lot of clutter). Or have each instruction in its own asm("..")
block (leads to a lot of typing).
If I want to reference a local variable in the inline assembly I have to either use the extended syntax or reference it by using offsets to esp
/ ebp
.
In respect to the two issues above I prefer the Visual C++'s inline assembly style in which in order to write an asm block all I have to do is __asm { .. }
and write each instruction on a new line and in order to reference a variable I just have to write its name.
Throughout my searches I have discovered that Apple's g++
supports the same syntax as Visual C++ with a switch (-fasm-blocks) however this does not seem to be the case for GNU g++
.
In the hopes that I might have missed something I am asking here if it is possible to compile Visual C++
like inline assembly blocks under GNU g++
.
The syntax you are referring to is not Microsoft specific. As you have found, Apple had it too (although Apple gave up on GCC and switched to Clang). AFAIK, Metrowerks supports the same syntax. GCC does not support it (probably because GCC guys believe that GCC is so good that nobody needs to write assembly anymore :-)). However, there is no need to type nt
all the time, you can replace it with ;
. For example:
void foo()
{
asm("xor %eax,%eax;"
"rep; nop;"
"nop;"
"sfence;"
"nop;");
}
Hope it helps. Good Luck!
链接地址: http://www.djcxy.com/p/59748.html上一篇: 内联汇编语言