I've recently started using the i586-kos32 cross-compiler (Thank you, Serge) to build applications in C for kolibriOS. Getting FASM libraries (especially DLLs) to work with C code has been problematic all this while and used to require some (at times, a lot) inline assembly. As there is a lot of code in C out there which can be ported to KolibriOS, it makes sense to simplify interfacing C with KolibriOS and it's libraries.
This is what I've thought of :
Step 1 : Write a wrapper in flat assembly : LoadSomelib.asm
Small piece of FASM code that calls the dll.Load procedure in dll.inc with the specified library at run time. This routine will be called from our C program to load the library at run time inside Kolibri. This wrapper contains public declarations for the procedures that we wish to make available to a C program. As I use linux, I prefer the COFF format instead of MS COFF.
Step 2 : Write a Header file in C : Somelib.h
Write a header file in C which lists all the functions along with the correct prototypes as the main SomeLib.obj that we are targetting. For example, if the SomeLib.asm file has a procedure :
Code: Select all
find_substring haystack, needle
; haystack -> ASCIIZ string in which to find substring
; needle -> substring to find in haystack
; return address in haystack where needle found or 0 on failure
Code: Select all
extern char* (*find_substring)(char *haystack, char *needle) __attribute__((__stdcall__));
It also makes sense to write another header file if you need more functionality (such as kolibri_debug.h for working with the Debug board).
Step 3 : Write a C Program : someProg.c
Now it's time to write a C program that will use SomeLib.obj. Make sure you include the header file(s) from Step 2 in this C program with the #include directive.
Step 4 : Put it all together
Code: Select all
fasm LoadSomelib.asm LoadSomelib.obj
i586-kos32-gcc someProg.c LoadSomelib.obj -o binaryfile
--
As an example, as well as to test the approach, I've written a wrapper around http.obj library (written in FASM) and calling HTTP functions from C. I've attached the files to this post as well as the final binary (to next post). I'm able to fetch the HTML page from http://kolibrios.org/en/ as a result.
Some notes on the cross compiler :
Download it from https://code.google.com/p/kolibri-pe/do ... z&can=2&q=
When compiling with i586-kos32-gcc, you might need to specify include and linker directories, for example, I invoke the cross compiler as :
Code: Select all
~/i586-kos32/kos32/bin/i586-kos32-gcc -I/home/username/kolibrios/contrib/sdk/sources/newlib/libc/include
Code: Select all
alias kgcc='~/i586-kos32/kos32/bin/i586-kos32-gcc -I/home/ashish/kolibrios/contrib/sdk/sources/newlib/libc/include'
---
Hopefully doing this for all other libraries (such as boxlib, libio, libini and others) will bring more HLL programmers and even more programs to KolibriOS. We can also have a C style API for KolibriOS then which HLL programmers can use directly.