I consider this article to be an introduction on writing a boot loader in C and Assembly and I did not want to get into performance comparisons against code written in C and Assembly in terms of writing a boot loader. In this article, I will only try to brief you about how to boot a floppy image by writing your own code and inject into the boot sector of a device (a boot loader program). During this process, I will break down the article into various sections. It felt hard to explain about computers, bootable devices, and how to write code in a single article so I have tried to do my best to explain the most common aspects of learning about computers and what is meant by booting. I tried to generalize the meaning and importance of each stage so that it may be easy to understand and remember too. In case you require more detailed explanation, you can browse through many articles provided on the internet.
What is the scope of the article?
I will limit the scope of the article to how to write program code and how to copy it to the boot sector of a floppy disk image, and then how to test the floppy disk if it boots with your program code or not, using an x86 emulator like bochs on Linux.
What I have not explained in the article?
I have not explained why a boot loader cannot be written in other languages like assembly and the drawbacks in terms of writing a boot loader in one language when compared to another. As this is an introductory learning article on how to write boot code I don't want to bother you much about more advanced topics like speed, writing smaller code, etc.
How are the contents organized
As far I am concerned I want to introduce in the article some of the basics and then follow that with code. But as one article seems to be quite tough to mention all the details. So, here is the break down of the contents in the order I brief you about how to write a boot loader.
- Introduction to computers.
- Introduction to programming tools.
- Introduction to Number Systems.
- Introduction to Bootable Devices.
- Introduction to the development environment.
- Introduction to microprocessor.
- Writing code in an Assembler.
- Writing code in a Compiler.
- A mini-project to display rectangles.
- Floppy disk and bootloader
Note:
This article really helps you a lot if you have prior programming experience in any language. Though this article seems to be fairly introductory, writing programs in Assembly and C can be a daunting task during boot time. If you are new to computer programming then I would suggest you to read a few tutorials on introducing programming and computer fundamentals and then come back to this article.Throughout this article, I will be introducing you to various terminologies related to computers in the way of questions and answers. To be frank, I will write this article as if I am introducing this article to myself. So many question and answer kind of conversations has been put in to make sure that I will understand its importance and purpose in my every day life. Example: What do you mean by computers?, or Why do I need them because I am much smarter than them?
So, let us begin then....:)
Introduction to computers
First answer me. Do you like computers?
Yes, of course. Computers do not carry sex, age, or caste, they do not show dominance, that is why I like them, and they are very honest. Whatever I ask them to do, they do it for me and why should I ignore or hate them? In addition, I admire them as my best friend. It is an electronic age. You often hear this one-liner “I am addicted to computers”.
Why does anyone feel like the need to work with a computer in their lives?
Honestly, humans are much better than computers because we are humorous :). Also we have a problem here when it comes to performing repetitive tasks or saving costs. Not every one of us is brilliant in this world to perform any kind of job within no time on a given day. Sometimes we are worried about other things and we give priorities to other things, that’s why we can not concentrate on utilizing every single second of a minute in doing our jobs successfully.
Saving our own costs, remind me of a term called simulation, which is commonly used in computing industry a lot. Let me present you with an example. Simulated learning environment for students who can't afford costly and very fragile equipments. Not only for students these days all costly equipments are being replaced by simulated environments so as to save the costs. How many times do you break a real equipment which costs millions of dollars with your testing and development? What I mean here is computer professionals write programs or software, which could give you a feel of the real time environment by sticking to one machine and through the software you can actually learn the things and then apply your knowledge to achieve better. It saves a lot of hardware and money for almost many. Also computers are also mainly used to automate a lot of hand work done during earlier days to provide a lot of services to humans to make their job much easier and a lot more faster these days. I guess that is enough for now :)
When I think about computers I sometimes actually think about electronic devices. They are the smallest and the most smartest devices available on the planet now. So, let us start with them.
What is an electronic device and what do I feel about them?
Let me explain you in a very generic way.. After all humans believed that humans can help each others only to a certain extent but not to the maximum. We have physical limits that make us the use of devices in our daily life. What I meant was you could not have a person dedicated to you and thinking about you for almost 24 hours a day and 365 days a year and then take necessary actions to make you feel batter. This lead to the invention of computers. I want a machine that could listen to me and do what I want it to do. I do not know if I am accurate about this or not but I will leave it to your own as to why you need a machine to help you rather than a human being. Are humans not smarter than computers? Come on...we are but I do not know sometimes machines are much faster than us. May be they look much smaller than us and we can carry them with our own hands and then make them do what we want. Hey, and do not take it in other ways...I just wanted to make it much simpler to you that is it.
What is a computer and is it much smarter than we are?
I hear them quite a lot of times in everyday life. A computer is an electronic device, which helps human’s as well as other electronic devices that depend upon information storage, retrieval, processing of data, which leads in providing more services in terms of simulation and decision-makings.
What does a typical computer look like?
What does each part do?
Monitor: We use a monitor to display information in visual form (pixel representation).
Why do we require a monitor?
A monitor displays information in visual form. I mean that it uses pixels to represent an image or object.
What is a pixel?
A pixel is the smallest unit in a computer through which a graphical image can be represented.
What is the use of a pixel?
Through a group of pixels computer represents an image or object. It is like a point that you draw on a paper with pen. But if you combine 100's of point you will be to provide a meaningful way to represent objects like text and expressions.
Let me provide you a visual representation of what a pixel is.
Now you might have got an idea as to why pixels are used in computing and what is their purpose.
A monitor uses a millions of pixels to provide pictorial representations of various objects to give the user a better experience along with speed. The portion of the monitor that displays the information is called the screen. Like a television screen, a computer screen can show still or moving pictures. No matter what human beings are, we try to imitate nature a lot and we call it creativity. Yes, this is true. Human beings in order to react to an event use voice and expressions (actions) which may be considered as the visual representations (actions) of the outcome against a particular event. Since a human has designed a computer and not all humans cant understand electrical signals they designed a special instrument called a monitor which actually responds to a particular event or all the events through visual representations.
Mouse: Remember me saying about visual representation of the outcome of an event earlier. Yes, we also use to give instructions visually to a computer through an input device called as mouse. The movement of a mouse is mapped to the X and Y co-ordinates of the screen of the monitor. Each monitor has a particular number of maximum number of columns and rows up to which it can represent the Income/outcome of an event.
Keyboard: Not all events seem humorous or easy to be triggered by mouse graphically. In order to overcome this limitation, we have invented an another device called keyboard through which typing a text can be really easy than mapping a text through a mouse.
Storage: In order to store the events triggered by a user, we need a device which can store what happened before or whats going to happen next and some temporary information and so on.
But why we need many storage devices instead of having a single one?
Well!!! the answer to it lies in terms of evolution of computers and how flexible these devices evolved are.
Examples of storage devices are:
- Floppy disk
- Hard disk
- CD ROM and DVD
- USB Disk
Where is the CPU?
These days the CPU is much buried inside a computer that it is not visible anymore. However, I will explain you about it.
CPU: This is called the brain of the computer. It contains various other parts that make it work like a human brain.
What are the different parts of a CPU?
I will list out only the most important ones here.
- Microprocessor
- RAM
What is a Microprocessor and what does it look like?
It is capable of understanding all the instructions given by a user and to proceed further to get the job done. You can think of it as a place in a computer where the given instructions are further broken down and then understood by a computer and then gives the desired output required by the user.
What is RAM?
This is like a work room for a computer. It uses this place like a garage to carry its temporary activities like creating its own temporary data to make decisions and proceed further.
Why doe a computer require a brain when humans tell the computer what to do and what not to do?
Yes you are right!!! It is the human beings that give instructions to a computer to follow our instructions, but we are much cleverer to the computers in terms of letting it to decide which is the best decision to take at what time and also to come up with the most optimum solution.
In this regards, we had to come up with one more component called as the CPU where the instructions are consumed from the user and then logical and mathematical decisions are taken.
What is the difference between RAM and the other storage devices specified earlier?
RAM also falls under the category of storage devices but it is used to store the data for only a certain period of time. When the computer is shutdown or restarted the data that is on RAM is lost. But the devices that I have mentioned under storage devices are used to store data for longer periods of time. If you write some data onto those devices, even after you shutdown or restart your machine, the data still remains.
So where and when can I start?
Wow, that is exciting...So I want to save a lot of money for others and me as well and want to help this world to become better and of course become famous too. I cant answer this question in a simple way like “Okay you get a computer and start writing programs on it and then sell it to your preferred customers and all the best”. What......Is that a joke ????? No! Not at all. Prior to helping others you should first help yourself in understanding computers a lot much before than to start writing programs on your own and then become a professional selling your programs to others and making your dream come true. The easiest way to start is learn them first. You are reading my article as you have access to internet and there are millions of tutorials out there in this electronic world which only help you to learn starting from what is an electronic device and how to use it. However, as far as writing your own programs on a computer, I seriously consider you to join into a course in computers may be at least for an year long and this should help you understand and write programs on a computer.
Okay...I got it and what is next?
I almost forgot one thing to tell you that computers are not like another world for humans. What I mean is they can understand humans and we can understand them too. What...so I can talk to them and be friends to them? Great. Wait What I mean here is that you have to segregate your self into one of the two categories as mentioned below.
- User
- Programmer
Once you start gaining experience for yourself as a user then you can be a programmer too. You will also see that these two terms are almost overlapping. A User can be a programmer too and a programmer is a user too. However, for now, I want to make it as simple as possible.
Computer User: A person who uses computers to get his job done. He does not write programs but in fact he uses the programs written by others to get his job done in his Daily life. A person who is not into understanding the internal workings of a computer.
Computer programmer: A person who is a User and his main activities or interests falls under writing programs or software for other people or to the users including himself. So that they can be benefited by using them to get their job done.
I call a person to be technical in computing if a person writes programs on computers and has a desire to understand them in depth...other wise you can’t write programs.
Program: A set of instructions, which are obeyed by the computer to execute and give the desired result. Programmers write the instructions to get the desired output by the computer.
It is not easy to write those unless you grasp the internal workings of a computer at a very detailed level.
It is not easy to write those unless you grasp the internal workings of a computer at a very detailed level.
What are computer instructions?
In a very generic sense, an instruction is like a command to make a computer carry out an activity or an event. Let me explain you much further. Remember me about introducing the Central Processing Unit that it processes the instructions from user and gives us the desired output. Suppose say you want to add two numbers like 1 and 2 and the result you expect is 3 after adding.
Task1:
You want this job to get it done by a human. Human to human way of solving the task. Person1 is asking person2: add two numbers say 1 and 2 and give me the result. Person2 replies: it is three.
Task2:
You want this job to get it done by a computer. Human to computer interactions to perform this task. A user1 is asking a computer1: 1+2. computer1 responds: 3.
That’s it!!! Task2 is very simple. All you were doing was using mathematical expressions. I am a math’s major too and I know how to write mathematical instructions. Wait! So you mean that I can directly ask the computer what is 1+2 and I expect it to give me an answer. No. you cannot interact directly with a computer this way. You need a translator or a compiler.
Introduction to programming tools
Translator or compiler ... why two names?
Well a translator is a more generic word and a compiler is more inclined towards computers. In addition, there might be pure translators that do nothing but convert the instructions given by users into pure machine code. From my experience what I know about compilers are that they do not blindly convert the given instructions into machine code but instead they optimize it for better performance as well and much more.
What is a compiler?
Please remember that the computer only understands only machine code in binary format, which is a combination of 1’s or 0’s. However, when we say 1+2, a computer cannot understand what we mean by that. Therefore, we pass 1+2 to a compiler and the compiler will convert it into the architecture specific machine code.
What is machine code?
A code is only represented by 1’s and 0’s. In our case to add 1 and 2 the machine code might look like below:
11001000100101000100101010111111001000000
It requires a lot of time to analyze the above code as to figure what was happening. Suppose say we are solving a puzzle which might have thousands of addition, subtraction and division operations. When those instructions are converted to machine code, you will have a book of 1s and 0s code which is quite tedious to figure out as to what was happening with those numbers. The above code is not the exact code, but what my point is can you remember the above code to just add two numbers. It is really a pain. In addition, what is we have a thousand instructions to execute. Would you remember 100’s of pages of 1’s and 0’s combinations. That’s why all we need is a compiler to convert our high-level instructions to respective machine code, which is a combination of 1’s and 0’s. We type 1+2 and pass it to the compiler, it will generate 11001000100101000100101010111111001000000 and executes, and further we perform the desired operation.
Why do I require a compiler? Can I not write machine code directly?
Yes, you can but it is quite tedious in terms of maintenance and if you have enough time try to Google on web for machine coding tutorial and then come back. At least in this article further I am not going to talk about it at all.
What else should I learn to start programming?
Number Systems and what happens when a computer is powered on.
Introduction to Number Systems
What is a number system in computers?
Everything in a computer operation is identified by numbers and computer identifies the numbers following one of the below numbering Systems.
What is a number?
A number can be treated as a combination of symbols, these symbols can be treated as digits, and the value of a number can be determined by the following. Count of the number of digits in a number.
The position of the digit in a number
Total number digits available with respect to its numbering system.
What are the type of number systems?
- Decimal
- Binary
- Hexadecimal
- Octal
I will ignore explaining about octal number system as its application in this article is not required at all.
Let us see what they are now.
Decimal:
This is the most common number system we use in our day-to-day life. This is based on 10 digits and the digits range from 0 to 9. i.e., 0,1,2,3,4,5,6,7,8,9.
123.45 is represented as 1*10^2 + 2*10^1+ 3*10^0 + 4*10^-1 + 5*10^-2
Example:
123 = 1*10^2 + 2*10^1+ 3*10^0123.45 is represented as 1*10^2 + 2*10^1+ 3*10^0 + 4*10^-1 + 5*10^-2
Binary:
This number system use only 1s and 0s to represent numbers. The base is defined as two. B denotes binary prefix.
101112 to base 2 = 10111B = 1×24+0×23+1×22+1×21+1×20 = 16+4+2+1= 23
Example:
101012 to base 2 = 10101B = 1×24+0×23+1×22+0×21+1×20 = 16+4+1= 21101112 to base 2 = 10111B = 1×24+0×23+1×22+1×21+1×20 = 16+4+2+1= 23
Hexadecimal:
This system uses digits from 0..9 and A..F. 0x denotes hexadecimal prefix
3D to base 16 = 0x3D = 16*3 + 13 = 61
If you need more information about number systems, please browse the internet and Google for number systems. You will find a ton of materials.
Example:
43 to base 16 = 0x43 = 16*4 + 3 = 673D to base 16 = 0x3D = 16*3 + 13 = 61
If you need more information about number systems, please browse the internet and Google for number systems. You will find a ton of materials.
Please browse through the next parts for more information