What it's like programming without Operating System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you ever written a hello world program it probably looks like this but it only works because of all the abstractions your operating system and your programming language provides right have you ever wondered how can you run this hello world program without your programming language without your operating system on Bare Bones on bare metal how does that work well today we are going to be exploring that so when you start your computer what does your CPU exactly do right what does your computer do how does it like load the operating system how what's the process right so I'm mostly covering for the x86 architecture by the way before I move forward now so the way it works is a CPU starts a program called bias or bios I don't know how to say that it stands for basic input output software and as you can tell it is a basic software for input and output and some other things so it starts that does it also does some health checks and stuff and then it has to start the it has to hand over the uh control to the operating system right so how does it know where the operating system lies because if it were to hand over to the wrong drive and we execute those random garbage values we may end up formatting all our hard drives right because they may be garbage values but who knows what instruction they might need so what to do what to do so the way that happens is we look for the boot sector so in a hard drive so hard drive is like a area of bytes essentially right we're gonna visualize it to be now at the first 512 bytes is what we are gonna want to be the boot sector and it is a valid boot sector if the last two bytes hold the magic number so there's the hex notation aa55 so if the last two bytes in the first 512 bytes of one of the hard drive have that value right have this magic number then it's a valid boot sector and the bias can load that so what we do is we write the boot loader here right and then so what it does it may be like does some bookkeeping and some like some stuff that it needs to do maybe move on from 16 bit to 32-bit so this bias and this starts with 16 bit even in your 64-bit it starts with 16-bit why for backwards compatibility so it might want to move on to 32-bit or 64-bit that would be the long mode so 64-bit right anyhow it does some stuff and then it loads up the current because in 512 bytes you cannot have an operating system so it jumps to some other instruction that is at some other memory right memory address and that is our kernel so it loads the kernel and then the operating system takes over and we boot up with that whatever theme you have okay so that's what happens so that means if you want to run a program without any operating system without any uh language right language runtime what we need to do is write a bootloader so we're going to write a bootload and then we're gonna compile it to Binary and then we're gonna use a emulator to run that or we can you know burn it into an ISO and then uh and then you can put it into a real computer or you can use a VM but we're gonna use an emulator because that's easy so I'm gonna be using qmu so you can see how you can download it for your device it's pretty simple and since we're gonna be programming in assembly I'm gonna be using nasm so this is netwide assembler now you can download some other tool but make sure you know that uh a syntax okay so I'm gonna be using the network syntax I I think it's called Intel syntax whatever whatever okay so let's create our hello assembly okay and now so first of all let's write a simple program that does nothing okay that does nothing so that would be jump dollar so what this would do it's a while true Loop okay so this is a assembly program it's valid but we cannot really we cannot really load it as a bootloader because it is not a valid boot sector we need 510 bytes after this I mean some bytes till 500 10th byte right and then we want the magic number so that would be by 5A I mean aa55 so to do this we have a simple declare word uh instruction so this will put this word 0x a55 in the binary okay and but to Pat this what we can do is we can copy paste this line so what this does is in 510th byte we are declaring a byte called zero just padding is nothing okay so yeah and this way we will get a valid boot effect so let's try to compile this so um and now if you open this file as you can see we have the first instruction jump dollar and then we have nothing and then in the 512th byte we have the magic number aa55 now if you're wondering why is it the opposite it's because it is little Indian so it is little endian has like the least significant bit first and then the most significant and then there's another way to write in binary that is bigger NBL most significantly significant so we are using little Indian now so we have this and now let's uh provide our Q emulator with the binary so like again I said you can run it on a VM as well or or like real Hardware but we're gonna use an emulator because that's easy so let's run that and we see something but we don't see really anything it's just all the booting from this right it solves what the bias does so what we want to do is we want to print something to create something you need to print you need to interact with the device right the hard the monitor so and if you remember we are in 16 bit where we have bias and bias is basic input output software so it provides some functionality to input output stuff so the way you tell buyers to output something is that you specify uh so what you're gonna do is you specify to the bias that you want a tele type instructor you want to do a teletype instruction where you put something okay so that console right where you type stuff so that's the teddy type device so you so you specify that by moving a some value right which the hardware developer so whoever decided so that is 0x0e and then you tell it what is the web what is the character you want to print so let's say h right and then you do an interrupt interrupt number 10 so on interrupt number 10 when we the bias will be invoked and then buyers will see the value of ax registered so if you know ax is a 16 bit resistor the first eight bits are the ah and then the lower a 8 Bits are Al so it will check that register and it will see that okay we want to output on into Telly type device and we want to Output H okay so it will see that and then it will output because it is biased it makes life easy so let's run I mean compile and now run and we should see nothing why Can you spot the difference do the Explorer of course we never came out of the true while loop right while true so yeah with that let's run it and okay I forgot to compile let's compile and run and we have H printed so we are pretty close now all we have to do is just copy this one two three four five and yeah so the next letter is e the next is uh L the Nexus L then o and then add an X let's add an excellent exclamation Because uh recorded in assembly right so let's compile and run and we have Hello World printed would you look at that though we have wrote a hello world program without the operating system without your language runtime obviously without operating system you don't have the language runtime but we have done it on bare metal so it is but we we heavily used uh our bias but when you move from 16 bit to 32-bit then you don't even have that and then it's even more interesting the way it works is that uh you you write this hello the hello in some specific memory layout and then because this Telly type is a memory map device it will like print that so uh I just mentioned that if you were interested in how that does if you didn't understand that's fine but there we go we have a Hello World program on bare metal done hope you like this video it was like a pretty random video I know uh but uh and probably not very useful but hopefully interesting if you like leave a like subscribe and I'll see you next time
Info
Channel: codemastercpp
Views: 2,554
Rating: undefined out of 5
Keywords:
Id: 2UqmrIPz1HY
Channel Id: undefined
Length: 10min 46sec (646 seconds)
Published: Sun Nov 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.