Writing applications for ELKS in C with gcc-ia16

You can develop application programs for ELKS by cross-compiling them on a standard Linux system with the gcc-ia16 compiler. Because this compiler is also used to compile ELKS, the gcc-ia16 compiler is usually installed in the „cross“ directory of the ELKS directory after you downloaded and compiled ELKS.

For a test 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;
}

Make a new „testing“ directory and copy and save this with a text editor as 'hello.c' in e.g. an elks/testing directory. Then get into this directory and enter:

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

This will compile the hello executable in the testing directory. The „-melks-libc“ specifies to use the libc in the elks directory and „-mcmodel=small“ specifies to compile the program using the small model. Currently ELKS can only load programs generated for 128k using the small model. This will be up to 64k of code and up to 64k of data and stack in two separate segments.

To see if this program works with ELKS enter: „../elksemu/elksemu hello“ on the command line. The elksemu emulator will run the hello program then.

If you want to run the hello executable with ELKS you have to put it e.g. into the directory 'usr/bin' on the ELKS disk image, e.g. the 'fd1440.bin' image file. You can load an existing ELKS image as a loop device on Linux and then copy the new executable to this image. To do this you can use a script with the following lines:

set -x
mount -o loop image/fd1440.bin floppy1
cp testing/hello floppy1/bin
umount floppy1

Execute this script with 'sudo' ! This script assumes that you are in the 'elks' directory and that the 'fd1440.bin' image file is in the image directory. Also there has to be a 'floppy1' directory within the 'elks' directory which will be used for the loop device.

You may wonder why Linux can access this floppy image since it is using the Minix file system. The reason is that Linux can use the Minix file system for floppy disks as well. Newer Linux distributions often blacklist this file system. Enter „modprobe -v minix“ to load this file system or add it to the „etc/modprobe.d“ file to have it available after a reboot.

Then call qemu to run ELKS and call your program. Either run the „./qemu.sh“ script in the elks directory or enter:

qemu-system-i386 -fda image/fd1440.bin

After ELKS has booted, log in as „root“ and type cd ../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 which is in the /root directory. Then your program will be run as soon as you log in as root.

Another alternative is to make a directory in the ELKS source code, write a Makefile for your application program and then compile ELKS including your program.

Finally, you could set up an ftp server on your Linux host and use the ftpget progam on ELKS to download the executable to ELKS and run it there. There is another document that describes how to set up a tcp/ip connection between ELKS running in Qemu and your Linux system.

Writing applications for ELKS in C with the bcc compiler

ELKS and application programs for ELKS can be made by cross-compiling them on a standard Linux system with the dev86 package. This contains the bcc C compiler, the ld86 linker, the as86 assembler and the ar86 archiver. You can download the dev86 package from the repository of your Linux distribution or download it here: https://github.com/jbruchon/dev86. Then compile and install it from this source code. Enter make in the main directory for that and „quit“ when the configuration menu is displayed.

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 always use the -ansi switch here because otherwise bcc will assume K&R style.

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. Or use one of the alternatives described above for the gcc-ia16 compiler.
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.

Writing applications for ELKS with the Turbo-C compiler

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 this he developed the exe2elks DOS utility program and a small libc without DOS system calls.

You compile your C program with this small libc library instead of the standard Turbo-C libc. Then you use the exe2elks utility to convert the generated executable in the DOS EXE format to a 16bit OMAGIC aout file as required by ELKS.

The exe2elks program may work for DOS TASM assembler programs as well if you get TLINK to generate a map file.

14th of April 2020 Georg Potthast