每个asm 构造都是独立的,并且设置在一个中的值/寄存器与另一个无关。为了完成这项工作,您需要一个asm。此外,实际上不需要将值移动到 eax 中——这就是“a”输入约束所做的。所以你想要:
int val=15
asm volatile("int $0x80" : : "a"(val));
或者只是
asm volatile("int $0x80"::"a"(15));
编辑
各种约束字母的含义在the gcc documentation中,但基本上,对于x86它们是:
'r' -- any general register
'm' -- in memory addressable by an EA operand (base reg + index * scale + displacement)
'a' -- al/ax/eax/rax register (depending on the size of the operand)
'b' -- bl/bx/ebx/rbx register
'c' -- cl/cx/ecx/rcx register
'd' -- dl/dx/edx/rdx register
'A' -- edx:eax register pair (holding a 64-bit value)
'D' -- di/edi/rdi
'S' -- si/esi/rdi
'f' -- any 8087 fp register
't' -- ST(0) -- top of 8087 stack
'u' -- ST(1) -- second on 8087 stack
'y' -- any MMX register
'x' -- any XMM register
如果你想把多个东西放在特定的寄存器中,你需要多个输入,每个输入都有适当的约束。例如:
int read(int fd, void *buf, int size) {
int rv;
asm ("int $0x80" : "=a"(rv) : "a"(3), "b"(fd), "c"(buf), "d"(size) : "memory");
return rv;
}
直接进行“读取”系统调用。输入约束将各种参数放在eax/ebx/ecx/edx寄存器中,返回值最终在eax寄存器中。
对于与特定寄存器不对应的约束,您可以在 asm 字符串中使用 %n,它会被编译器选择的寄存器替换,但对于与特定寄存器对应的约束,则有无需直接提及。