Asmutils Source Guide

by Konstantin Boldyshev, asmutils 0.03

Contents


1. Introduction

First of all please note that this is not nasm guide. I assume that you know nasm syntax; I will not explain any nasm-related things here.

Second, I expect that you will examine source code of asmutils. This document is not intended to replace the source, its goal is only to accompany asmutils source and explain some unclear moments. Again, examine ALL source code. Look how command line parsing is done, how conditional assembly for different kernel versions is done and so on -- I am not going to explain everything here.

Mostly this guide describes a set of macros I've developed to write fast and readable; they are hiding from you unneeded details, and also take care of optimization.

You may also want to read Startup state of Linux/i386 ELF binary and Linux/i386 system calls documents to get better general (not asmutils specific) understanding of asmutils source code.

2. Program structure

There are three macros that make section definition as simple as possible: CODESEG, DATASEG and UDATASEG (similar to TASM ideal mode syntax).

Program must have at least CODESEG (.text) section, other sections optional. CODESEG is read-only, DATASEG and UDATASEG are read-write; i.e. you can place data in CODESEG as long as you will not change it. You can also define your own sections if you want, but there's very rare need of doing so. Each section (even if it is empty) will enlarge your file.

START macro tells linker entry point. If you will not mark entry point -- linker will use start of CODESEG section.

So, typical code will look like:

%include "system.inc"

CODESEG

START:			;entry point

    your_code_here

DATASEG

    your_data_here

UDATASEG

    your_bss_here

3. Include files description

3.1 system.inc

system.inc file MUST be included into program code to do anything else; all vital constants and system call macros are here, as well as CODESEG, DATASEG and UDATASEG macros. Without this file you will have to write in usual boring way.

It also contains optimizing macro __setreg32, that does register assignment. You can use this macro instead of movs if you take care of size, it produces quiet good results (do not try to understand how it works :).

A lot of people asked me for some description of syscall macros, so here are general things to know about sys_xxx macros:

If some system call is missing -- you can add it to this file; it's simple, just look how others are done there; use sys_syscallname as macro name.

3.2 config.inc

This file stores configuration parameters. It is included from system.inc, so you have no need to include directly. Here you can set target kernel version (KERNEL) and optimization (__OPTIMIZE). Further other configuration parameters will be added.

3.3 kernel.inc

Kernel structures reside and should be added into kernel.inc file. So, if you work with kernel structures -- this file must be included.

3.4 types.inc

This file contains common types definitions (INT, LONG, etc.). This file is included internally from kernel.inc, but you can also use it without kernel.inc if you wish.

4. Size/speed optimization

In fact this must be done by assembler.. but.. optimizing assembler is a dream. So, I've took care of it. By default code is optimized for size, and you can get up to 20% smaller executable; speed optimization in fact is a fake, it is just absence of size optimization :), though theoretically you can gain something on pentium processors.. To enable speed optimization set __OPTIMIZE to __O_SPEED in config.inc. Optimization touches register assignment mechanism (__setreg32 macro) and section alignment (CODESEG, DATASEG macros). Optimization is a work in progress, so results may be better in future versions.

5. Tips and tricks

Section is not written yet, go examine source code :) Until you've got better algorithms use existing ones. Also, read Startup state of Linux/i386 ELF binary document, you can find some tricks there.


$Id: asmutils-source-guide.html,v 1.2 1999/07/04 10:17:26 konst Exp $