Writing applications for ELKS with the GNU as assembler

If you want to compile an assembler program with gnu as to use it with DLKS, here is a hello world example:

        .code16
        .text
        .global entry
entry:
        movw    $len,%dx
        movw    $msg,%cx
        movw    $1,%bx
        movw    $4,%ax
        int     $0x80

        movw    $0,%bx
        movw    $1,%ax
        int     $0x80
        .data
msg:
        .ascii  "Hello, world\n"
        len = . - msg

Make a new „testing“ directory and copy and save this code as 'hello.S' with a text editor in the elks/testing directory. Then get into this directory and compile it with these commands:

$ ia16-elf-gcc -melks-libc -mcmodel=small -c hello.S -o hello.o
$ ia16-elf-gcc -melks-libc -mcmodel=small -nostdlib hello.o -o hello

To see if this program will work with ELKS enter: „../elksemu/elksemu hello“ on the command line. The elksemu emulator will run the hello program then. Further alternatives to run the program are described in the „Writing applications for ELKS in C“ document.

The entry points can be defined as entry or _start, as handled in ELKS's C library linker scripts.

The system calls implemented by ELKS are listed in the file Documentation/function.lst. The system call numbers are identical to Linux. So you will find these numbers on this page: http://asm.sourceforge.net/syscall.html and further details here: https://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm

If an error number is returned you can look that up in the file 'elks\include\arch\errno.h'.

Writing applications for ELKS with the NASM assembler

It is also possible to use NASM with the -f elf switch to generate an object file for ELKS. Here is an example:

BITS 16
section .text                   ;section declaration
global _start                   
_start:                         ;write our string to stdout

    mov     dx,len              ;3rd arg: message length
    mov     cx,msg              ;2nd arg: pointer to message to write
    mov     bx,1                ;1st arg: file handle (stdout)
    mov     ax,4                ;system call number (sys_write)
    int     0x80                ;call kernel

                                ;exit now
    mov     bx,0                ;1st syscall arg: exit code
    mov     ax,1                ;system call number (sys_exit)
    int     0x80                ;call kernel

section .data                   ;section declaration

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

Save this code as the file hello.asm. Make sure the paths are set to find ia16-elf-gcc by running the script „. env.sh“ in an elks directory. Then you can compile the code with these commands:

nasm -f elf -o hello.o hello.asm
ia16-elf-gcc -melks-libc -mcmodel=small -nostdlib hello.o -o hello

Then enter: „../elksemu/elksemu hello“ on the command line and see the message on the screen.

Writing applications for ELKS with the as86 assembler

Unlike GNU as the as86 assembler has Intel syntax like masm or tasm. If you want to compile an assembler program with as86, here is an example:

.text
.global _main
_main:
push es
push ax
!write ABC at the left border of line 20
      mov ax,#0xb800
      mov es,ax
      seg es
      mov [3200],#0x41
      seg es
      mov [3201],#0x4B
      seg es
      mov [3202],#0x42
      seg es
      mov [3203],#0x2E
      seg es
      mov [3204],#0x43
      seg es
      mov [3205],#0x1f
pop ax      
pop es   

xor bx,bx
ret
.data
.bss

Save this code in the file abc.S. Then you can compile it with these commands:

as86 -o abc.o abc.S
ld86 -i -o abc abc.o

You can also call the ld86 linker via bcc like this instead:

bcc -o abc abc.o

You can reduce the memory requirements of a program if you modify the total program size (chmem) with the provided ld86 options. The abc.s program above is linked to require 32.752 bytes by bcc/ld86. If you reduce the size to 1.024 bytes instead it will run just as well. ELKS considers a memory requirement below 1.024 bytes invalid and will refuse to load the program with the message „out of space“. See the check in fs/exec.c. Add the code, data and bss sections and add e.g. 256 bytes for the stack.

14th of April 2020 Georg Potthast with help from tkchia