A beginners guide to OS Dev: Part 2
Last time we got an OS that just booted up and did nothing and thats great, but most operating systems do more than just nothing. And im sure you don't want to be doing the entire thing in just assembly!
So we are going to be adding C to our OS.
C requires a stack and right now we don't have one. We have to fix that
Great! We have a stack and now we can call C, which we must compile separately. We don't have any standard library or protections so we need a lot of compiler flags
Here is the new compiling script
Now lets make our C entry point and call it from the OS
And to call it
Now we have an OS that calls a C function
Writing text to the screen
Have you ever done
console.log("Hello World") or maybe even called print a few times? Im sure you never paid much attention to what goes on far behind the scenes. Unfortunately we don't get the convenience of a print function until we make one, we are going to be using the VGA Text Mode Framebuffer as it is extremely easy to use.
Once compiled and ran this should output
Hi to the screen!
Unfortunately we can't do all of an operating system with just C.
I'm trying to convert my OS to C instead of all Assembly now so I can make the second part of my tutorial and the second version of my OS, but for some reason, string literals of over 63 characters are causing boot failures. Do you have any suggestions?
Whenever I use pointers (or more specifically, strings) I get this error:
Could not open option rom 'linuxboot_dma.bin': No such file or directory
Do you know what would cause that? (My project is a little different than yours, you can look at it here: https://repl.it/@fuzzyastrocat/OS)
Even just doing
char* mystr = "A string"; in main() causes this error.
On Bochs, the byte 0xF0 sets the background color to 15, fg to 0, and sets the blink byte. The byte 0x70 disables blink,but I can't get it to be white on black. Thanks!
@CodeLongAndPros @CSharpIsGud For the colors, the binary representation helps the most in my opinion. For 0xF0, it is the same as 1 111 0000. However, for white on black, you would need 0 000 1111 which is 0x0F. The first bit is the blink byte. The next three are the background. And, the last four are the foreground. When it comes to converting it to hexadecimal, the second digit is always the foreground while the first is the background, but it only ranges from 0 to 7 since there is also the blink bit.
mov esp, cstack + CSTACK_SIZE
Could you try and explain this line? I don’t really get it sorry I don’t really do assembly I’ve been holding out until you got to the point where c is a thing lol.
Also why do you add 0xF0 after each character?
@Highwayman it moves
cstack + CSTACK_SIZE into the esp register
think of it like
esp = cstack +CSTACK_SIZE, except cstack in this case is an address to where the stack should be. 0xF0 is the color byte, which you write after every color. there are only 15 colors, 0 black to F white
so a white foreground on black background is 0xF0