Writing applications for ELKS in assembler

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 as the file abc.s. Then you can compile it with these commands:

as86 -o abc.o abc.s
bcc -o abc abc.o

Instead of the command „bcc -o abc abc.o“ you can also invoke the ld86 linker and enter:

ld86 -i -o abc abc.o

You can reduce the memory requirements of a program if you modify the total program size (chmem) in the header with a hex editor. The abc.s program above is linked to require 32.752 bytes by bcc/ld86. If you set 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. The c program hello.c above is linked to require 33.520 bytes and can be reduced to 3.984 bytes instead. Just add the code, data and bss sections together and add 256 bytes for the stack. You will have to increase the stack size for a larger program.

Unlike gas the as86 assembler has Intel syntax like masm or tasm.

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

section .text                   ;section declaration
global _main                            ;ELKS needs main
_main:
                                ;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.s. Then you can compile it with these commands:

nasm -f as86 -o hello.o hello.s
bcc -o hello hello.o

Transfer the executable hello to ELKS as described above and see the message on the screen.

You cannot just bang any assembler program for NASM with the switch -f as86 to ELKS, you need to make a few modifications. See the e3-16.asm code for details.

The system calls implemented by ELKS are listed in 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

26th of March 2017 Georg Potthast