Introduction to UNIX assembly programming
Konstantin Boldyshev
 Linux Assembly (http://linuxassembly.org/) 
           konst@linuxassembly.org
        
This document is intended to be a tutorial, showing how to write a simple assembly program in several UNIX operating systems on the IA-32 (i386) platform. Included material may or may not be applicable to other hardware and/or software platforms.
This document explains program layout, system call convention, and the build process.
It accompanies the Linux Assembly HOWTO (http://linuxassembly.org/howto.html), which may also be of interest, though it is more Linux specific.
Table of Contents
1. Introduction	000
1.1. Legal blurb	000
1.2. Obtaining this document	000
1.3. Tools you need	000
2. Hello, world!	000
2.1. System calls	000
2.2. Program layout	000
2.3. Linux	000
2.4. FreeBSD	000
2.5. BeOS	000
2.6. Building an executable	000
3. References	000
A. History	000
B. Acknowledgements	000
C. Endorsements	000

1. Introduction
1.1. Legal blurb
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License (http://www.gnu.org/copyleft/fdl.html) Version 1.1; with no Invariant Sections, with no Front-Cover Texts, and no Back-Cover texts.
1.2. Obtaining this document
The latest version of this document is available from http://linuxassembly.org/intro.html. If you are reading a few-months-old copy, please check the url above for a new version.
1.3. Tools you need
You will need several tools to play with programs included in this tutorial.
First of all you need the assembler (compiler). As a rule modern UNIX distributions include as (or gas), but all of the examples here use another assembler -- nasm (Netwide Assembler). It comes with full source code, and you can download it from the nasm page (http://sourceforge.net/projects/nasm/), or install it from the ports (or package) system. Compile it, or try to find precompiled binary for your OS; note that several distributions (at least Linux ones) already have nasm, check first.
Second, you need a linker -- ld, since assembler produces only object code. All distributions with the compilation tools installed will have ld.
If you're going to dig in, you should also install include files for your OS, and if possible, kernel source.
Now you should be ready to start, welcome.. 
2. Hello, world!
Now we will write our program, the old classic "Hello, world" (hello.asm). You can download its source and binaries here (http://linuxassembly.org/intro/hello.tgz). But before you do, let me explain several basics.
2.1. System calls
Unless a program is just implementing some math algorithms in assembly, it will deal with such things as getting input, producing output, and exiting. For this, it will need to call on OS services. In fact, programming in assembly language is quite the same in different OSes, unless OS services are touched.
There are two common ways of performing a system call in UNIX OS: through the C library (libc) wrapper, or directly.
Using or not using libc in assembly programming is more a question of taste/belief than something practical. Libc wrappers are made to protect programs from possible system call convention changes, and to provide POSIX compatible interface if the kernel lacks it for some call. However, the UNIX kernel is usually more-or-less POSIX compliant -- this means that the syntax of most libc "system calls" exactly matches the syntax of real kernel system calls (and vice versa). But the main drawback of throwing libc away is that one loses several functions that are not just syscall wrappers, like printf(), malloc() and similar.
This tutorial will show how to use direct kernel calls, since this is the fastest way to call kernel service; our code is not linked to any library, does not use ELF interpreter, it communicates with kernel directly.
Things that differ in different UNIX kernels are set of system calls and system call convention (however as they strive for POSIX compliance, there's a lot of common between them).
Note: (Former) DOS programmers might be wondering, "What is a system call?" If you ever wrote a DOS assembly program (and most IA-32 assembly programmers did), you may remember DOS services int 0x21, int 0x25, int 0x26 etc.. These are analogous to the UNIX system call. However, the actual implementation is absolutely different, and system calls are not necessarily done via some interrupt. Also, quite often DOS programmers mix OS services with BIOS services like int 0x10 or int 0x16 and are very surprised when they fail to perform them in UNIX, since these are not OS services).
2.2. Program layout
As a rule, modern IA-32 UNIXes are 32bit (*grin*), run in protected mode, have a flat memory model, and use the ELF format for binaries.
A program can be divided into sections: .text for your code (read-only), .data for your data (read-write), .bss for uninitialized data (read-write); there can actually be a few other standard sections, as well as some user-defined sections, but there's rare need to use them and they are out of our interest here. A program must have at least .text section.
Ok, now we'll dive into OS specific details.
2.3. Linux
System calls in Linux are done through int 0x80. (actually there's a kernel patch allowing system calls to be done via the syscall (sysenter) instruction on newer CPUs, but this thing is still experimental).
Linux differs from the usual UNIX calling convention, and features a "fastcall" convention for system calls (it resembles DOS). The system function number is passed in eax, and arguments are passed through registers, not the stack. There can be up to six arguments in ebx, ecx, edx, esi, edi, ebp consequently. If there are more arguments, they are simply passed though the structure as first argument. The result is returned in eax, and the stack is not touched at all.
System call function numbers are in sys/syscall.h, but actually in asm/unistd.h. Documentation on the actual system calls is in section 2 of the manual pages some documentation is in the 2nd section of manual (for example to find info on write system call, issue the command man 2 write).
There have been several attempts to write an up-to-date documentation of the Linux system calls, examine URLs in the References section below.
So, our Linux program will look like:
section     .text
    global _start                       ;must be declared for linker (ld)

_start:                                 ;tell linker entry point

        mov     edx,len ;message length
        mov     ecx,msg ;message to write
        mov     ebx,1   ;file descriptor (stdout)
        mov     eax,4   ;system call number (sys_write)
        int     0x80    ;call kernel

        mov     eax,1   ;system call number (sys_exit)
        int     0x80    ;call kernel

section .data

msg     db      'Hello, world!',0xa     ;our dear string
len     equ     $ - msg                 ;length of our dear string

Kernel source references: 
•	arch/i386/kernel/entry.S (file:///usr/src/linux/arch/i386/kernel/entry.S)
•	include/asm-i386/unistd.h (file:///usr/src/linux/include/asm-i386/unistd.h)
•	include/linux/sys.h (file:///usr/src/linux/include/linux/sys.h)
2.4. FreeBSD
Note: most of this section should apply to other BSD systems (OpenBSD, NetBSD) as well, however the source references may be different.
FreeBSD has the more "usual" calling convention, where the syscall number is in eax, and the parameters are on the stack (the first argument is pushed last). A system call should be done performed through a function call to a function containing int 0x80 and ret, not just int 0x80 itself (kernel expects to find extra 4 bytes on the stack before int 0x80 is issued). The caller must clean up the stack after the call is complete. The result is returned as usual in eax.
There's an alternate way of using call 7:0 gate instead of int 0x80. The end-result is the same, but the call 7:0 method will increase the program size since you will also need to do an extra push eax before, and these two instructions occupy more bytes.
System call function numbers are listed in sys/syscall.h, and the documentation on the system calls is in section 2 of the man pages.
Ok, I think the source will explain this better:
section     .text
    global _start                       ;must be declared for linker (ld)

_syscall:               
        int     0x80            ;system call
        ret

_start:                         ;tell linker entry point

        push    dword len       ;message length
        push    dword msg       ;message to write
        push    dword 1         ;file descriptor (stdout)
        mov     eax,0x4         ;system call number (sys_write)
        call    _syscall        ;call kernel

                                ;the alternate way to call kernel:
                                ;push   eax
                                ;call   7:0

        add     esp,12          ;clean stack (3 arguments * 4)

        push    dword 0         ;exit code
        mov     eax,0x1         ;system call number (sys_exit)
        call    _syscall        ;call kernel

                                ;we do not return from sys_exit,
                                ;there's no need to clean stack
section .data

msg     db      "Hello, world!",0xa     ;our dear string
len     equ     $ - msg                 ;length of our dear string

Kernel source references: 
•	i386/i386/exception.s (file:///usr/src/sys/i386/i386/exception.s)
•	i386/i386/trap.c (file:///usr/src/sys/i386/i386/trap.c)
•	sys/syscall.h (file:///usr/src/sys/sys/syscall.h)
2.5. BeOS
Note: if you are building nasm version 0.98 from the source on BeOS, you need to insert #include "nasm.h" into float.h, and #include <stdio.h> into nasm.h.
The BeOS kernel also uses the "usual" UNIX calling convention. The difference from the FreeBSD example is that you call int 0x25.
For information where to find system call function numbers and other interesting details, examine asmutils, especially the os_beos.inc file.
section  .text
    global _start                       ;must be declared for linker (ld)

_syscall:                       ;system call
        int     0x25
        ret

_start:                         ;tell linker entry point

        push    dword len       ;message length
        push    dword msg       ;message to write
        push    dword 1         ;file descriptor (stdout)
        mov     eax,0x3         ;system call number (sys_write)
        call    _syscall        ;call kernel
        add     esp,12          ;clean stack (3 * 4)

        push    dword 0         ;exit code
        mov     eax,0x3f        ;system call number (sys_exit)
        call    _syscall        ;call kernel
                                ;no need to clean stack
section .data

msg     db      "Hello, world!",0xa     ;our dear string
len     equ     $ - msg                 ;length of our dear string

2.6. Building an executable
Building an executable is the usual two-step process of compiling and then linking. To make an executable out of our hello.asm we must do the following:
$ nasm -f elf hello.asm              # this will produce hello.o ELF object file
$ ld -s -o hello hello.o        # this will produce hello executable
Note: OpenBSD and NetBSD users should issue the following sequence instead (because of a.out executable format):
$ nasm -f aoutb hello.asm   # this will produce hello.o a.out object file
$ ld -e _start -o hello hello.o # this will produce hello executable
That's it. Simple. Now you can launch the hello program by entering ./hello. Look at the binary size -- surprised?
3. References
I hope you enjoyed this journey. If you get interested in assembly programming for UNIX, I strongly encourage you to visit Linux Assembly (http://linuxassembly.org) site for more information, and download the asmutils (http://linuxassembly.org/asmutils.html) package, it contains a lot of sample code. For a comprehensive overview of Linux/UNIX assembly programming refer to the Linux Assembly HOWTO (http://linuxassembly.org/howto.html).
Thank you for your interest!
A. History
Revision History
Revision 0.9	11 Feb 2006	Revised by: konst	
Linux example correction for 2.6 kernels, updated NASM URL	
Revision 0.8	10 Nov 2000	Revised by: konst	
rewritten using DocBook DTD, minor corrections	
Revision 0.7	23 Aug 2000	Revised by: konst	

Revision 0.6	11 Jul 2000	Revised by: konst	

Revision 0.5	01 Jun 2000	Revised by: konst	
merged Daemonnews corrections	
Revision 0.3	09 Apr 2000	Revised by: konst	
rewritten using linuxdoc dtd	
Revision 0.2	26 Jan 2000	Revised by: konst	
rearrangement	
Revision 0.1	xx Jun 1999	Revised by: konst	
first version	

B. Acknowledgements
I would like to thank: 
•	DaemonNews (http://daemonnews.org) people for proofreading and corrections they kindly submitted to me
•	Eugene Tsyrklevich (mailto:eugene@securityarchitects.com) for note on NetBSD compile process
C. Endorsements
This version of the document is endorsed by Konstantin Boldyshev.
Modifications (including translations) must remove this appendix according to the license agreement.
$Id: Assembly-Intro.sgml,v 1.9 2006/02/11 08:10:41 konst Exp $