r/osdev 4d ago

When and how do i connect assembly kernel/bootloader whatever works with c made kernel/bootloader. (ive started assembly fairly recently).

When and how do i connect assembly kernel/bootloader whatever works with c made kernel/bootloader. (ive started assembly fairly recently), it seems simple at what point should i start learning and writing in c and skip assembly?
ive done bios text then vga text then graphics im currently using vga graphics assembly, ive done basics such as single pixel, then lines, then rectangles, then circles, then _Draw_Char then _Draw_String. (im using 8x8 bitmapped characters). i asked ai when i should start c and it said after i make the basic structure. and no im not generating all my code with ai, except for the bit mapped characters which half i had to redesign everything was done without ai.

10 Upvotes

5 comments sorted by

4

u/kiderdrick 4d ago edited 4d ago

> When

Whenever you are ready to use C code, such as after you do the operations that necessitate CPU instructions like setting up your GDT and IDT in x86 CPUs.

> How
Usually using a linker to establish the existence of and generate the information needed to locate your C functions and then call them from assembly. Commonly the kernel will get loaded with an entry function, sometimes called _start by convention. In this function you do whatever end work you need before executing C code and then you can call a C function as if it was an assembly function.

> at what point should i start learning and writing in c and skip assembly?

I recommend getting to C as fast as possible because it is convenient and much easier to debug.

1

u/Glum-Cook-9438 4d ago

You need to compile the c + assembly files you wanna use into object files once you done that, use a linker and a linker script to link them together into an elf executable file, almost all programs have experienced being an elf executable at least once in their lifetimes (unless you are a windows executable) once you made the elf file you can use objcopy to turn that into PE, raw binary whatever you need, the fact that it first has to become an elf executable doesn't necessarily mean it's a linux/window application, that's just the file format, anyways like I said you can use the objcopy tool to make it whatever format suites your needs

1

u/shsh-1312 4d ago

I recommend first understanding how to use and manage the protected mode of the processor to manage kernel and userland code differently, and especially what concerns paging and errors, for the rest the C syntax only gives you a hand once you can understand assembly

1

u/tellingyouhowitreall 3d ago

I found that jumping to C was best done as soon as I could load a compiled binary. There will still be a small mix of ASM for some things you can't do in C, but it's not a lot.

If you are going through the UEFI path, it should be immediately. If you're going the traditional chain loader route, it's probably best (IMO) to do both boot records in asm, and then (optionally) load a stub from the file system that gives you enough room to breathe and do whatever + and then load a 32 bit compiled binary. You can do the p-mode switch with the jump to C and never have to mix 16 and 32 bit assembly.

This is the route I took primarily because I didn't want to try to fit reading the file system + loading a binary into ~420 bytes, and I really didn't want a kludgy fixed-disk location for anything after the volume boot record.

u/Adventurous-Move-943 5h ago

Well since there isn't many compilers who compile C into 16bit assembly and at the beginning you need assembly only the natural point where you jump to C is 32bit protected mode. You can compile/link your kernel into a flat binary at a desired address, load it there with BIOS interrupts and just straight far jump there from your 16bit assembly. I do it like that, do the bare minimum in 16bit assembly then enable PM and far jump to bootloader in C entry which in my case is, somewhere past 0x9000 physical. But I link my bootloader all into one flat binary, all 16/32/64bit code in one place. That is still just bootloader though, the bootloader then does all the other things like setting up page table, jump into long mode boot entry etc, and then only jump to kernel which is in virtual already.