Splitting kernel into multiple assembly files and compiling & linking them together

I noticed in the FlingOS kernel, in FlingOS/Kernel/Kernel/ASM/PreReqs/, the assembly code for setting up the basic parts of the kernel (like in the video tutorials) are separated out. In the video tutorials everything is 1 large assembly file.

Since it was not explained in the tutorial videos, I am wondering how i can split out the assembly code from the getting started repo into smaller files, and how to compile/link them.

Cameron

Comments

  • Hi Cameron,

    Thanks for your question. You can split the large assembly code into multiple files by following these steps:
    1. Create new .asm files and copy the separate sections of code into them
    2. Your split-up code will now have two issues:
    > 1. Some files will use labels in one file that are declared in another
    > 2. Some files will declare labels that need to be used in other files

    To solve these issues you must use the GLOBAL and EXTERN pre-processor functions.

    For files which use labels that are defined in other files (case 1 above), use "EXTERN label_name" at the top of the file for each label that is used which is declared in a separate file.

    For files which declare labels used in other files (case 2 above), use "GLOBAL label_name:function" or "GLOBAL label_name:data" depending on whether the label is for a function or for data.

    3. To compile the assembly files to object files, pass each one through NASM separately with the appropriately named output file name option.
    4. To combine the object files into the final .bin file, pass the names(/paths) of all of them on the command line to the linker (Ld) The order in which you list them matters! The order they are listed will be the order in which the object files are concatenated.

    Please note, that in our experience a very odd bug can occur (which we have yet to find an explanation for) where the concatenated code doesn't work but an equivalent, identical, single-file version does. Our solution to this is to place "mov ecx, 0x0" as the first instruction of each separate assembly file. This strangely resolves the issue. (Using a large number of NOPs can also resolve it but mov ecx, 0x0 is more reliable.). You don't have to use ECX, any general purpose register will do.

    Hopefully this answers your question. Let us know if you have any issues,
    Ed

    Founder of FlingOS
Sign In or Register to comment.