NASM Forum > Example Code

How do I properly set up nasm to be the output of a compiler?


I am writing a compiler and I want the output to be in nasm. The tutorial I was learning from uses gas, and I want to convert it into nasm. I think the project would go further in nasm.

In code_gen.c, I am using fprintf functions, to print out nasm code into the file out.s, that can be used as the output to get assembled. I have been placing these functions, where I think they should be in the grammar. I know in normal circumstances I should be using an ast, but for now I am just manually doing it, to quickly test and get the language done step by step.

The issue I am having is setting nasm up itself. I haven't found many tutorials using nasm with compilers.

In my language, I have two kinds of functions or two classes of functions, one that acts like a class or module and the other is a normal function.

In this instance, Would I use a normal label for the first class function and a local label for the second class functions? I wanted to create a new scope called universal. This universal scope would be outside of any first class function, with global being inside the first class function, and local being inside second class functions. That is why my thought was that I might have to use a label with local labels.

 Is there a better way to approach this? Any tutorials I may have missed that would help answer some questions?

--- Code: ---
global _start

           push ebp
   mov ebp, esp
           sub esp, 32

   call  main
           call exit


   mov   ebx, i
   mov   eax, 6
           mov         ebx, eax

   mov   ecx, nam
   mov   edx, 5
           mov         ecx , edx

   call    main.entry

      mov    ebx, .entry
      push   ebx
      call main.hi

      mov    ebx, .hi
      push   ebx

           mov    ebx, 0
   mov    ebx, 1
   int    80h

--- End code ---

You might find it useful to read the docs, which you can find at

The docs will tell you everything you can do with nasm, from basic instructions through to macros and more. You will need to work out (possibly by looking at examples, such as, for example, gcc source code) how to write the basic functions, such as io functions and others. A guide to Linux syscalls (I'm assuming you are writing for Linux, but I can't help with that as only generally write code for DOS or OS independent code) or BIOS interrupts (if writing for DOS or OS independent, that you will find at will be helpful as well. If writing for Windows, someone else will have to direct you.

Frank Kotler:
Hi  lord_raven,

I haven't written a compiler, so I can't help you much.

Nasm doesn't really like your code. Throws a warning right on the first line. Not a good start, I'd say. We can configure Nasm to not emit the warning, but that's not what I think you should do. Your compiler ought to produce code that Nasm likes, I think. There are some things that Nasm doesn't complain about, but which I don't understand. Loading a register with a value and then loading the same register with a different value. You do this in a few places. I don't ;npw what your compiler intends... but then, I haven't written a compiler.

I wish you luck with your project. I hope you don't get too frustrated with it.



[0] Message Index

Go to full version