I was just wondering if a C++ compiler is taking the source code and creating a binary file out of it with a .exe extension on it or something or does it translate it to assembly first and than assemble it from there. If it is binary I have always wondered how it is structured i mean im pretty sure it isnt just a file that in notepad looks like 10100100010101011 but is it possibly to actually write to machine code using fstream or w.e. Thanks in advance

Recommended Answers

All 9 Replies

It depends on the compiler. Both methods have been done, as well as translating from C++ to C, which is then compiled however the C compiler handles it. However, you'll find that most compilers have a switch that give you assembly output whether the compiler itself has a separate assembly code step or not.

>is it possibly to actually write to machine code using fstream or w.e.
Yes, all you would be doing is writing bytes to a binary file in a format that the computer understands as executable. This isn't exactly trivial as you have to know the executable file format on top of the instructions that are to be executed, but it's certainly possible. Though personally, if I'm going to write machine code, I'd rather do it with my favorite assembly language than by whacking bytes directly. ;)

lol, so are you saying that with work its possible to write your own assembler that converts the source code right into machine code sort of like NASM. And is it possible to write straight to a Raw .bin file i no there is an option for that in nasm if ur not programming exe's for windows. When it says raw binary does that literally mean all 1's and 0's. Thanks in advance :)

The compiler creates a program in several steps before generating the *.exe file. How exactly that is accomplished is different for each compiler.

>>If it is binary I have always wondered how it is structured
Depends on the compiler. See this wikipedia article

>>is it possibly to actually write to machine code using fstream or w.e.
Yes, but it would be very tedious and error prone. That is best left up to a compiler.

>so are you saying that with work its possible to write your own assembler
>that converts the source code right into machine code sort of like NASM.
How do you think NASM was written? :icon_rolleyes:

>And is it possible to write straight to a Raw .bin file
Of course.

>When it says raw binary does that literally mean all 1's and 0's.
It means a flat binary file with no special formatting beyond what's in the code you compiled. An MS-DOS COM file, for example, is raw binary because it doesn't have headers or metadata or anything except program data and instructions.

Yes, you can write your own compiler and assembler if you want to. Many students have done that, and so have many companies. There are college courses in compiler design that you might want to take.

so could you recommend creating your very own assembler for your own assembly type language in C++ or C++ not good for writing an assembler.

you can write an assembler in most any language.

>so could you recommend creating your very own
>assembler for your own assembly type language in C++
I could. But I might not. Perhaps the object code generation would be well suited to C++, but for the parsing, I think I'd prefer a language that's a bit better at string handling.

Some remarks.
String handling per se is a smallest part of a scanner - the simplest component of any compiler. No problems to implement effective scanners in C or C++.

Syntax parsers and code generators (main parts of compilers) work with internal data structures and does not take trouble over string handling at all.

As usually a compiler generates object files (not exe). You can download MS Windows object (COFF) and executable (PE) file format specifications:
http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx
Introduction to Linux binaries file format (ELF):
http://www.linuxjournal.com/article/1060
Sun ELF description:
http://docs.sun.com/app/docs/doc/819-0690/chapter6-46512?l=ru&a=view

After that you will be ready to implement your own compiler... or assembler... or what else...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.