C++嵌入Assembler(汇编)汇编内出错?

问题描述:

.important { color:red; }

我在C++中插入了汇编代码,但汇编代码老报错,不知什么原因。求各位大神帮助!

代码:

#include <iostream>
using namespace std; 
int main(int argc,char *argv[]) {
 volatile int a[3] = {0,0,0};
 for (int i = 0;i < 3;i ++)
  cout << a[i] << ends;
 cout << endl;
 asm ("inc [esp + 2]");//Assembler:inc [esp + 2]   (++ a[1];)
 for (int i = 0;i < 3;i ++)
  cout << a[i] << ends;
 cout << endl;
 return 0;
}

报错信息:

(File(C:\Users\ADMINI~1\AppData\Local\Temp\ccR9LmJd.s)) Assembler messages:
(Line(42)) (File(C:\Users\ADMINI~1\AppData\Local\Temp\ccR9LmJd.s)) Error: invalid char '[' beginning operand 1 `[esp+2]'

之后我想是不是这个汇编器不支持Intel,就把那一行汇编改成了AT&T格式的:

asm ("inc [esp + 2]");//Assembler:inc [esp + 2]   (++ a[1];)

| |

\/

asm ("inc 2(%esp)");//Assembler:inc 2(%esp)   (++ a[1];)

但还是报错了:

(File(C:\Users\ADMINI~1\AppData\Local\Temp\ccEzueAV.s)) Assembler messages:
(Line(42)) (File(C:\Users\ADMINI~1\AppData\Local\Temp\ccEzueAV.s)) Error: no instruction mnemonic suffix given and no register operands; can't size instruction

本人对AT&T不熟悉,麻烦大家看看哪错了,致以感谢!