2

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.

Cam
  • 14,930
  • 16
  • 77
  • 128

3 Answers3

1

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.

iksemyonov
  • 4,106
  • 1
  • 22
  • 42
  • Thanks. What about on windows? – Cam Jun 19 '10 at 16:11
  • 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. – iksemyonov Jun 19 '10 at 16:25
  • Cool, thanks. I actually hadn't mentioned windows in my question - probably should have :) – Cam Jun 19 '10 at 16:34
0

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
  • 34,983
  • 11
  • 83
  • 114
0

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).

How Do Assemblers Map x86 Instruction Mnemonics to Binary Machine Instructions?

Community
  • 1
  • 1
danatel
  • 4,844
  • 11
  • 48
  • 62