Introduction to assembler
Back to index


Assembly language is the closest thing you come to machinde code. It's a low level language which means that isn't as intelligent as high level languages like C or pascal. It's harder to code in than those programs, but it runs much faster and you have total control of what you are doing. You shouldn't write programs all in assembly but use it as inline assembly in high level languages where you want it to run faster.

There are four 32 bit registers called eax, ebx, ecx and edx. These registers can also be used as 16 bit registers: ax, bx, cx, dx. The 16 bit registerscan be split up into two 8 bit registers which for ax are called ah and al. ah is the highest 8 bits in ax and al is the lowest 8 bits.
You can move constants into the registers and you can move from one register to another. You can also move to and from memory.

    mov ax,17
    mov dx,ax
    mov mem,dx

This will move 17 into ax, then move the contents of ax into dx and move dx into mem.

To add or subtract a constant or a register you can do this

    add ax,4
    add dx,ax
    sub dx,7
    sub ax,dx

If you want to add or subtract 1 use inc and dec

    inc ax
    dec ax

There are also 6 segment registers called cs, ds, es, fs, gs and ss. To access a memory you need both a segment and an offset. si and di are index registers and can be used as offsets. So when you want to access memory you can do it like this

    mov al,ds:[si]

cs is the code segment and should always point to this segment. The CPU executes instructions at cs:ip. ip is the instruction poiner. ds is the data segment. This is where your data are. es, fs and gs are extra segment registers.

You can use lodsb to load a byte from ds:si into al. lodsw loads a word into ax and lodsw loads a doubleword into eax. These instructions adds 1, 2 or 4 to si.
stosb stores al at es:di, stosw stores ax and stosd stores eax. These instructions adds 1, 2 or 4 to di.
If you want to store more than one byte use rep stosb. rep stosb stores al at es:di cx times.
This will store al at es:di 100 times.

    mov cx,100
    rep stosb

You can also use rep stosw and rep stosd to store a word or a doubleword.

To compare a register with another register, a constant or memory use the cmp instruction. After you've compared something you can use jumps to act on the compare.

    je somewhere
Jump to somewhere if equal

    jne somewhere
Jump to somewhere if not equal

    ja somewhere
Jump to somewhere if above

    jb somewhere
Jump to somewhere if below

somewhere is defined by typing somewhere: in the program.

Interrupts can be used to handle files or to change the screen or to put text on the screen. You call interrupts like this

    mov ax,0003h
    int 10h

This will put you in textmode. int 10h is the screeen interrupt. It would be a good idea to download a list of all interrupts and their functions.

This is the most common instructions. Download a full list of the instructions at www.intel.com


Jesper L. Poulsen