-2

我在这里做错了什么?

int val = 15;
asm ("movl %1, %%eax"::"r"(val):"%eax" );
__asm__ volatile ("int $0x80");

我想在 eax 中移动 15 然后调用中断

“错误”:“asm”:操作数超出范围

4

2 回答 2

6

每个asm构造都是独立的,并且在一个中设置的值/寄存器与另一个无关。为了完成这项工作,您需要一个asm. 此外,实际上不需要将值移动到 eax 中——这就是“a”输入约束所做的事情。所以你要:

int val=15
asm volatile("int $0x80" : : "a"(val));

要不就

asm volatile("int $0x80"::"a"(15));

编辑

各种约束字母的含义在gcc 文档中,但基本上,对于 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 寄存器中。

对于不对应特定寄存器的约束,你会%n在asm字符串中使用,它会被编译器选择的寄存器替换,但对于特定寄存器对应的约束,不需要直接提及.

于 2013-02-05T02:35:04.710 回答
0

看起来他在下面的代码示例中明确地将 x 放入 EAX:

GCC 内联汇编语言中的 r() 和 double percent %% 是什么?

我用 g++ 编译了该代码并通过 gdb 运行它。
在 gdb 中,info register eax显示正确的数字

您使用的是什么编译器、操作系统和架构?如果您使用的是 64 位 MSVS,则无法进行内联汇编。

#include<iostream>
using namespace std;

int main(int argc, char* argv[])
{

cout << "Compile via: g++ -O0 -ggdb -o asm.out asm.cpp" << endl;
cout << "Want to put 15 into register EAX and then call an interrupt." << endl;
cout << "gdb ./asm.out  --> stepi --> info register eaain." << endl;

int eaxin = 15, eaxout=0, ebxin=33, ebxout=0;
cout << "Before calling assembly routines:" << endl;
cout << "eaxin=" << eaxin << "\t" << "eaxout=" << eaxout << "\t" ;
cout << "ebxin=" << ebxin << "\t" << "ebxout=" << ebxout << endl;

asm ("movl %1, %%eax;"
     "movl %%eax, %0;"
    :"=r"(eaxout)   /* 1  eaxout is output operand 1 */
    :"r"(eaxin)     /* 0  eaxin is input operand 0*/
    :"%eax"
    );   /* %eax is clobbered register.  Why not two percent signs here? */

//Lets play in the ebx register.
asm ("movl %1, %%ebx;"
     "inc %%ebx;"
     "inc %%ebx;"
     "movl %%ebx, %0;"
    :"=r"(ebxout) 
    :"r"(ebxin) /* "+" is supposed to allow input and output but did not work */
    :"%ebx"
    ); 


//Call the system interrupt 128.
asm volatile ("int $0x80");

cout << "After calling assembly routines. Value of eaxout should have changed." << endl;
cout << "eaxin=" << eaxin << "  " << "eaxout=" << eaxout << "\t" ;
cout << "ebxin=" << ebxin << "\t" << "ebxout=" << ebxout << endl;
cout << endl;
cout << "https://stackoverflow.com/questions/3589157/need-some-help-understanding-gcc-inline-assembly-language?rq=1" << endl;
cout << "Note: inline assembly CANNOT be done with MS VisualStudio 64bit." << endl;
cout << "   option1:   Use a separate file for the assembler code." << endl;
cout << "   option2:   Use instrinsics." << endl;
cout << " Look for intrn.h at http://software.intel.com/en-us    /node/181178?wapkw=ain86_64+assembly "  << endl;
return 0;
}

(gdb) info register eax ebx
eax 0xf 15
ebx 0x0 0
(gdb) step
35 asm volatile ("int $0x80");
(gdb) 信息寄存器 eax ebx
eax 0x23 35
ebx 0x23 35

于 2013-02-05T03:31:13.987 回答