Search code examples
clinuxcompiler-constructionpipenasm

Integrate NASM to a compiler written in C


I have a C program that generates nasm assembly. How can I assemble it with nasm and then link it with ld to generate the final executable?

The easiest thing would be to just pipe the compiler's output into nasm, but I wanted the process to be simple for the user: just type compiler myfile and get an executable. Even if I would go through that easy route, nasm does not seem to accept files from stdin; you have to specify them as arguments.

So what I have so far is: output the assembly to stdout, create a pipe between the main process' stdout and a child process' stdin, and this process would be a call to nasm /dev/stdin. What am I doing wrong here?


Solution

  • In fact, your suggested procedure will not work, because nasm reads its input file twice (unless you suppress the preprocessor pass with the -a flag).

    So it would be necessary that /dev/stdin be an ordinary file, not a pipe. And if it were going to be an ordinary file, it might as well have a name.

    So just write your output to a temporary file, call nasm on that file, and then delete it.