convert c++ to machine code?

For a class assignment I'm supposed to note differences / similarities between a simple c++ program and its corresponding machine code. From what I understand machine code is all 1's and 0's, am I correct?

So far I seem to have successfully converted the C++ source to assembly using...
g++ -S main.cpp -o assem1.o
I can view the assembly code using the cat command...
cat assem1.o

My next challenge is to convert the assembly language into machine code. Is there a simple way do this in Linux? Thank you for your time.
Assembly language has a one to one relationship with machine code. One assembly command translates to one opcode. You'll need to find a lookup table for your given CPU architecture (I'd assume x86)
Yes x86_64 I guess would be my architecture. I'll give this a shot. Thank you for your response.
My next challenge is to convert the assembly language into machine code. Is there a simple way do this in Linux?

Yes, use gcc:

~$ g++ -S -o test.S test.cc
~$ g++ -o test test.S

(note the file extension has to be .S or .s, not .o)

You could also use as and ld directly, if you want to get into details, but if you're compiling a C++ source, not a hand-written assembly source, you will need to know where C++ runtime and libraries are. You can find out if you run g++ -v -o test test.S
Last edited on
I didn't even know that was possible. Shows I have missed quite a bit in my learning myself.
For a class assignment I'm supposed to note differences / similarities between a simple c++ program and its corresponding machine code. From what I understand machine code is all 1's and 0's, am I correct?


In my mind, I don't see the point in comparing machine code & C++. C++ & assembly: yes; Assembly & machine code: yes.

When I did a short course on asm 20 years ago, we were asked to write a hello world program in asm. As far as I remember the was an instruction to write 1 char on the screen, so it was easy. We also got it to loop so it printed 10 times. Then we had to write the same in C & use the compiler to produce asm and compare to what we had written earlier. There was quite a bit of difference because of the call to printf. Our code was much simpler & fairly obvious.

I am wondering exactly what your instructor wants you to do, because using the complier to produce asm, might not be that educational IMO. Also, it might be considered as cheating a bit, because it is very easy to use the compiler to translate. Have you guys been taught any asm?

Finally, the executable file IS THE machine code, once you turn it into 0's & 1's. I hope you didn't mean machine code split into opcodes and arguments!!

Alright I see the ridiculousness of my title now. =)
How do I view the 0's and 1's contained in my executable?
I used hexdump prog but I notice a bunch of what appear to be line numbers written in hex in the first column. Every time I edit the program, compile, and run hexdump on the binary executable, I get the same number of lines. This is really weird. Any ideas?
Topic archived. No new replies allowed.