简体   繁体   中英

Why nasm says I have error in the assembly g++ created?

I have this c++ code:

#include <iostream>
using namespace std;

int main () {

  char chr[] = "111111111111";
  int pop = 9999;
  cout << chr << (pop+1) << endl;

}

when I do in the shell (64 bit linux) g++ -S hello.cpp I get assembly code :

when I use on it nasm hello.s it says it contains a lot of errors such as:

instruction needed
expression syntax error
symbol `popq' redefined

maybe it is because it is 64bit? how can I compile the .s I created with the g++?

The assembler generated by GCC is using what is known as AT&T syntax, which differs from the Intel-syntax used by nasm. You have to use the GCC assembler ( as ) to compile GCC generated assembler files.

See eg http://en.wikipedia.org/wiki/GNU_Assembler#Criticism .

For more information about the GNU assembler syntax, see http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax .

There are several assembler syntaxes for x86. In particular, nasm and gas (the assembler inside binutils ) have different syntaxes.

Very often, GCC is configured to emit assembly code using gas syntax. You could find out what GCC is doing with eg g++ -O -v -c yourcode.cc and you can learn how GCC was configured with gcc -v or g++ -v alone.

And you might invoke GCC as g++ -S -fverbose-asm yourcode.cc to get a more readable yourcode.s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM