views:

121

answers:

2

I'm writing a compiler and I have gone through all the steps (tokenizing, parsing, syntax tree structures, etc.) that they show you in all the compiler books. (Please don't comment with the link to the "Resources for writing a compiler" question!).

I have chosen to use NASM together with alink as my backend.

Now my problem is: I just can't find any good resources for learning NASM and assembly in general.

The wikibook (german) on x86 assembly is horrible. They don't even explain the code they write there, I currently can't even get simple things like adding 1 to 2 and outputting the result working.

  • Where can I learn NASM x86 assembly?
+1  A: 

Well, if you will accept a book as a reference. My favorite author from the days I learned Pascal in the 80's was Jeff Duntemann. His latest assembly book covers NASM. http://www.duntemann.com/assembly.htm

Not sure what OS you are targetting, but the fact that the above book is targetting Linux should be not be a issue, the assembly constructs are what you are interested in.

To be honest, the actual assembly for code generation is not the hardest part, IMHO I think the register management is where the real learning is.

Goodk luck!

Chris Taylor
A: 

Unless you've checked it out, NASM manual is quite good resource for learning about NASM: http://www.nasm.us/doc/

Comparison of NASM and GAS also helps out a bit: http://www.ibm.com/developerworks/linux/library/l-gas-nasm.html

irc channel #asm at Freenode se(r)ver provides these links:

I also devoted some time to mash up a little hello world up for you:

bits 32
section .data
    greeting db "hello world", 10
section .text
global _start
_start:
    mov eax, 4 ; sys_write
    mov ebx, 1 ; stdout
    mov ecx, greeting
    mov edx, 12 ; greeting.length
    int 0x80 ; system call interrupt

    mov eax, 1 ; sys_exit
    mov ebx, 0
    int 0x80

Assemble this with:

nasm -f elf -o example.o example.asm
ld -o example example.o -melf_i386

I've myself written a small code generator in python. Though I left that thing in middle while ago. Recently I've written some bit different tool that might become useful for anyone tackling with assembly. Right now I'm also asking some help.. Except seems I have to do some self-help there: http://stackoverflow.com/questions/2855418/x86-instruction-encoding-tables

The old code generator piece I've got is in http://bitbucket.org/cheery/g386/ until I'll get my new table-based code generator up and running.

Cheery