16 Bit Real Mode Example with C/Inline Asm

Опубликовано: 19 Январь 2025
на канале: Queso Fuego
477
39

*With inline assembly and linker scripts.
Showing a basic boot sector/2nd stage/"kernel" flat binary example to print a string. The boot sector/stage2 has 16 bit real mode code from -m16 and inline assembly.
I wanted to only need a C compiler and make to work with 16/32/64 bit code. It does likely still call out to a separate 'as' binary or other things anyway.
Also showing temporary files generated from -save-temps for extra info and example debugging with gdb & qemu.

Notes:
ld has LONG for 4 byte values, and QUAD for 8 byte values in linker scripts.
-fno-builtin prevents using/adding built-in functions that don't start with "__builtin_".
--nmagic or --omagic also prevents ld warnings for "relocation in read-only section" and "creating DT_TEXTREL in a PIE". Although I told it not to compile for pie, it still links for it, even though ld --help says -no-pie is the default... You can add -no-pie as a linker flag to explicitly have it position dependent, which may be needed depending on what you're trying to do.

Links:
Article that inspired this video, it didn't use -m16 actually, but did mention .code16gcc:
https://dc0d32.blogspot.com/2010/06/r...

https://wiki.osdev.org/Kernel_Debugging
https://sourceware.org/binutils/docs/...
https://sourceware.org/binutils/docs/...
https://en.wikipedia.org/wiki/Executa...
https://nullprogram.com/blog/2024/12/20/ (inline asm gotchas)
https://en.cppreference.com/w/c/23
https://gcc.gnu.org/onlinedocs/gcc/At...
https://gcc.gnu.org/onlinedocs/gcc/Co...
https://gcc.gnu.org/onlinedocs/gcc/x8...

Git Repo:
https://github.com/queso-fuego/c-real...
https://codeberg.org/queso_fuego/c-re...

Repo state as of this video:
git clone https://github.com/queso-fuego/c-real...
cd c-realmode-example
git checkout 5753bf3551abc92ca2bd13d7681d0bd29e19f97b

Next video:
Maybe starting a line editor like ed, maybe something else.

Join the Community Discord:   / discord  

Contact: https://queso_fuego.codeberg.page/contact.html

Support: https://queso_fuego.codeberg.page/support.html

Questions about setup/software/etc.? Check the FAQ or tell me to add it:
https://queso_fuego.codeberg.page/about.html

0:00:00 intro
0:01:20 boot.c/.ld
0:06:10 stage2.c/.ld
0:13:44 kernel.c/.ld
0:15:40 fix optimized label error, makefile
0:27:19 -save-temps and other intermediate files for info
0:34:21 debugging with gdb/qemu
0:40:47 outro

Misc. recording/editing notes:
Changed microphone placement, gain, and audio filters.
Tried to have it sound more natural and closer to my voice (well, a bit nicer, and less bassy/monotone), using a voice oriented EQ, lighter/less compression, and a multiband compressor for a de-esser.
It should also be less scratchy, but that's on me to drink more water.

Going to try at least spanish subtitles on future videos, davinci resolve worked up until 38 min, then failed or could not make correct timings and words. So future videos or series may need to be in ~30 min chunks for correct subtitles in different languages, not sure.

Music credits:
[Chiptune] Medieval: Minstrel Dance by RandomMind
Music: https://www.chosic.com/free-music/all/

#cprogramming #bootsector #realmode