Next Previous Contents

5. Optimization, tips and tricks

In fact optimization must be done by assembler.. but.. optimizing assembler is just a sweet dream yet. 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's just an absence of size optimization :), though theoretically you can gain something on pentium processors.. To enable speed optimization set OPTIMIZE to SPEED in Makefile. Optimization touches register assignment, addition and subtraction (_mov, _add, _sub macros), and section alignment (CODESEG, DATASEG macros). Optimization is a work in progress, so results may be better in future versions.

If you've gone crazy on binary size, you may want to use some of things described below.

First of all, try to keep your program in one CODESEG (.text) section. Remember, every new section (even if it is empty) increases size of executable file. Unless you have any read-write data, do not create DATASEG (.data section), keep your data in CODESEG. Even if you've got one/two variables with assigned initial values, first think of keeping them dynamically on the stack instead of creating DATASEG. And if your initial value is zero, place such variable in UDATASEG (.bss) section, it will be zeroed out by kernel.

Use _mov macro instead of mov instruction (if you do not assign one register to another), this will track several special cases and produce smaller code.

Avoid using 16bit registers (ax, bx, cx, etc) unless you know exactly what you're doing. Every 16bit instruction will take one more byte (0x66 prefix). For instance, inc ax will produce greater code than inc eax.

Here are some assembly examples you can use instead of cmp instruction to produce smaller code:


;if eax < 0 (signed compare)

        test    eax,eax
        js      is_less

;if eax == 0

        test    eax,eax
        jz      is_zero

;if eax == 0

        or      eax,eax
        jz      is_zero

;if eax == 1            (and you no more care of its value)

        dec     eax
        jz      is_one

;if eax == 2            (and you no more care of its value)

        dec     eax
        dec     eax
        jz      is_one


;if eax == -1           (and you no more care of its value)

        inc     eax
        jz      is_minus_one

;if eax == -2           (and you no more care of its value)

        inc     eax
        inc     eax
        jz      is_minus_one


;if -255 < value < 255, you can use

        cmp     eax,byte value ;or -value

;instead of
        cmp     eax,value

Seek, and you may find more..

$Id: Asmutils-HOWTO.sgml,v 1.1 2000/04/07 18:36:01 konst Exp $


Next Previous Contents