Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing the right and learning assembler for compiler-writing

Tags:

x86

assembly

nasm

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?
like image 585
X A Avatar asked Apr 22 '10 21:04

X A


People also ask

Which is the best assembly language to learn?

The current most popular are ARM, MIPS, and x86. ARM is used on lots of cell phones and many embedded systems. MIPS is popular on IBM CPUs and is found on systems such as Macs, some video game consoles, and a few I'm sure I'm missing. x86 assembler is used on Intel PCs.

What are compilers and assemblers?

The difference between compiler and assembler is that a compiler is used to convert high-level programming language code into machine language code. On the other hand, an assembler converts assembly level language code into machine language code.

Why is it important to learn assembly?

Better Understanding of Architecture Issues Learning and spending some time working at the assembly language level provides a richer understanding of the underlying computer architecture. This includes the basic instruction set, processor registers, memory addressing, hardware interfacing, and Input/ Output.

Which is faster assembler or compiler?

On average the compiler will do far better than a human for a large project, but it is not hard in a decent sized project to find performance issues in the compiled code. Actually, the short answer is: Assembler is always faster or equal to the speed of C.


1 Answers

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:

  • Understanding Assembly: http://www.acm.uiuc.edu/sigmil/RevEng/ch06.html
  • 80386 HTML Reference: http://pdos.csail.mit.edu/6.828/2004/readings/i386/toc.htm
  • Linux/Win32 x86 tutorial w/nasm: http://www.drpaulcarter.com/pcasm/
  • Linux assembly book: http://nongnu.uib.no/pgubook/

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

like image 156
Cheery Avatar answered Nov 15 '22 00:11

Cheery