Writing applications for ELKS in C

ELKS and application programs for ELKS are cross-compiled on a standard Linux system with GCC-IA16.

Test the programs using Qemu as described in the next chapter.

For a start let's compile a „hello world“ program and run that in ELKS. Here is the code:

#include<stdio.h>

int main() {
        printf("Hello World\n");
        return 0;
}

Copy and save this as hello.c with a text editor. Then enter

bcc -ansi hello.c -o hello

We will always use the -ansi switch because otherwise bcc will assume K&R style.

To run this program in ELKS there are three alternatives. You can load an existing ELKS image as a loop device on Linux and copy the new executable to this image. Then run the image using qemu. Or you make a directory in the ELKS source code, write a Makefile for that and compile ELKS including your program. Finally, you could set up an ftp server on your host and use the ftpget progam on ELKS to download the executable to ELKS and run it there.

It is described now, how to use the loop device to add the executable to the directory usr/bin in an existing ELKS image file named full3. For this you can make a script with the following lines:

set -x
cd ..
mount -o loop full3 floppy1
cp bccdev/hello floppy1/usr/bin
umount floppy1
cd bccdev

This assumes that you are developing in the directory „bccdev“ and that the full3 image file is in the directory above that. Also in the directory above there has to be the floppy1 directory which will be used for the loop device. You have to execute the script with „sudo“ !

You may wonder why Linux can access this floppy image since it is using the Minix file system which is the only file system ELKS supports. The reason is that Linux uses the Minix file system for floppy disks as well.

After running the script call qemu to run ELKS and execute your program:

qemu-system-i386 -fda ../full3

Log in as „root“ and type cd /usr/bin and execute the hello program in there. If you have to repeat this frequently while developing your program, you can put these commands into the „.profile“ script for root. Then your program will be run as soon as you log in as root.

bcc generates programs with either up to 64k or 128k in size. Currently ELKS can only load programs generated for 128k. In the case of 64k both the code and data are in the same segment while if you select 128k it will be up to 64k of code and up to 64k of data and stack in two separate segments. This is determined with the -i switch. If you do not use that, bcc will pass this to the linker as default and generate 128k programs. The programs that are included in ELKS therefore are 128k programs. However, if you enter „bcc -i“ then bcc will not pass „-i“ to the linker and thus generate 64k programs. There is also the -H switch which bcc will pass on to the linker. This shall determine the top of the heap address which is the initial stack address. You can enter this in hex as e.g. 0xF800.

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

The bcc compiler has been ported to DOS so you can also develop ELKS applications in DOS or a Windows 32bit DOS box: https://ftp.gwdg.de/pub/misc/freedos/files/devel/c/bcc/

This includes doselks.com to run the compiled programs.

Alfonso Martone found a way to convert DOS Turbo-C programs to enable these to run with ELKS: http://www.alfonsomartone.itb.it/fhlvnr.html For that he developed the exe2elks DOS utility program and a small libc without DOS system calls. The exe2elks program may work for DOS TASM assembler programs as well if you get TLINK to generate a map file.

26th of March 2017 Georg Potthast