views:

80

answers:

3

I've been learning compiler theory and assembly and have managed to create a compiler that generates x86 assembly code.

How can I take this assembly code and turn it into a .exe? Is there some magical API or tool I have to interact with? Or is it simpler than I think?

I'm not really sure what's in a .exe, or how much abstraction lies between assembly code and the .exe itself.

My 'compiler' was written in Java, but I'd like to know how to do this in C++ as well.

Note that if I take the generated assembly, it compiles to a .exe just fine for example with vc++.


Edit: To be more precise, I already know how to compile assembly code using a compiler. What I'm wanting is to have my program to basically output a .exe.

A: 

Normally you use an assembler and a linker to create an exe file. There is no magic involved. The different parts are assembled, a header and other boilerplate is added so the OS knows where the bootstrap code is located for the program and to organize the memory.

The VC++ compiler does that under the hood. You might consider playing a bit on Linux as you can better see the machinery working on Unix platforms. It is fundamentally the same, but it s just difficult to look through to UI on windows.

Peter Tillemans
+1  A: 

It looks like you need to spawn an assembler process and a linker process. On UNIX, it's as simple as invoking the fork() function, which would create a new process, and the exec() function, specifying the assembler and the linker executable names as the function's parameter, with suitable arguments to those executables, which would be the names of your generated assembly and object files. That's all you'd need to do on a UNIX system.

Semen Semenych
Thanks. What about on windows?
Cam
Sorry, I must've missed the point about Windows. It should be pretty much the same, there's the CreateProcess() function, and the assembler and linker executables are installed as part of the VS distribution (al.exe and link.exe) I'd recommend you to consult the MSDN documentation for CreateProcess() as well.
Semen Semenych
Cool, thanks. I actually hadn't mentioned windows in my question - probably should have :)
Cam
A: 

In principle, each assembly line corresponds to a machine instruction which is just a few bytes in the exe file. You can find them in the specs for the processor. So you can write your own asembeler if you know the codes and the exe format (header, relocation info etc).

http://stackoverflow.com/questions/2760226/how-do-assemblers-map-x86-instruction-mnemonics-to-binary-machine-instructions

danatel