3

I wrote a simple c++ program that prints "Hello World!"

#include <iostream>
using namespace std;
int main(){
cout<<"Hello World!\n";
}

I then compiled it using g++

$ g++ Desktop/sample.cpp -o Desktop/hello

How would I be able to view the machine language, is it just

$ less Desktop/hello

?

I'm just curious to see what "machine language" looks like. Here's a sample from the above command

C>^D<A1><F4><9E>^D^H<83><F8><FF>t^S<BB><F4><9E>^D^Hf<90><83><EB>^D<FF>Ћ^C<83><F8><FF>u<F4><83><C4>^D[]Ð<90>U
<89><E5>S<83><EC>^D<E8>^@^@^@^@[<81>Ì^X^@^@<E8><<FE><FF><FF>Y[<C9><C3>^C^@^@^@^A^@^B^@Hello World!
^@^@^@^AESC^C; ^@^@^@^C^@^@^@<A4><FE><FF><FF>@^@^@^@<C8><FE><FF><FF>\^@^@^@^H<FF><FF><FF>x^@^
user784637
  • 15,392
  • 32
  • 93
  • 156

4 Answers4

9

If you compile with the -S option, it will spit out the assembler output and stop. Make sure you use the -o option to set the output file to sample.s:

g++ -S -o sample.s sample.cpp
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
5

You need to compile it this way:

  g++ -g -c -Wa,-alh Desktop/sample.cpp -o Desktop/hello

The look at the assembler code with:

 objdump -d Desktop/hello
tune2fs
  • 7,605
  • 5
  • 41
  • 57
1

Use xxd on the binary generated by g++ to view the machine code. Assembly is an abstract version of machine code and is not neccessarily what you are looking for.

Swiss
  • 5,556
  • 1
  • 28
  • 42
0

Use xxd -g 1 hello | more to view the binary content of the executable file 'hello' or any binary file you want to check.

pengw
  • 61
  • 5