问题描述
这看起来不太友好:
__asm("command 1"
"command 2"
"command 3");
我真的必须在每一行周围加上双引号吗?
Do I really have to put a doublequote around every line?
另外...由于多行字符串文字在 GCC 中不起作用,我也不能作弊.
Also... since multiline string literals do not work in GCC, I could not cheat with that either.
推荐答案
我总是在 Internet 上找到一些示例,该人手动插入制表符和换行符而不是 和,但是它不适用于我.不太确定您的示例是否可以编译..但我就是这样做的:
I always find some examples on Internet that the guy manually insert a tab and new-line instead of and, however it doesn't work for me. Not very sure if your example even compile.. but this is how I do:
asm volatile( // note the backslash line-continuation
"xor %eax,%eax
mov $0x7c802446, %ebx
mov $1000, %ax
push %eax
call *%ebx
add $4, %esp
"
: "=a"(retval) // output in EAX: function return value
:
: "ecx", "edx", "ebx" // tell compiler about clobbers
// Also x87 and XMM regs should be listed.
);
或者在每一行周围加上双引号,而不是使用 行继续.C 字符串文字仅由空格(包括换行符)单独连接成一个长字符串文字.(这就是为什么你需要
在它里面,所以当它被汇编程序看到时它是单独的行).
Or put double quotes around each line, instead of using line-continuation. C string literals separately only by whitespace (including a newline) are concatenated into one long string literal. (Which is why you need the
inside it, so it's separate lines when it's seen by the assembler).
这不那么难看,并且可以在每一行上放置 C 注释.
This is less ugly and makes it possible to put C comments on each line.
asm volatile(
"xor %eax,%eax
"
"mov $0x7c802446, %ebx
"
"mov $1000, %ax
"
"push %eax
" // function arg
"call *%ebx
"
"add $4, %esp
" // rebalance the stack: necessary for asm statements
: "=a"(retval)
:
: "ecx", "edx", "ebx" // clobbers. Function calls themselves kill EAX,ECX,EDX
// function calls also clobber all x87 and all XMM registers, omitted here
);
这篇关于如何在 GCC C++ 中编写多行内联汇编代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!