There is one more thing we need to do before we switch to long mode, which is set video mode.
Printing on the screen is done by the bios service in real mode.
Once we switch to long mode, we are unable to call bios function. So we set up a video mode
and print characters on that mode.
There are a lot of video mode we can use. Our system choose text mode in which printing characters is really simple
.
All we need to do is specify the ascii code for the character we want to print
and the attribute for that character such as foreground color and background color. computer.
We write them on the specific address and the data will appear on the screen.
OK, let's see how to set up text mode.
To set up text mode, we use interrupt 10
and function code 0 in ah register which means we want to set video mode.
Then we need to choose video mode, text mode in this case, by copying 3 to al register.
So we copy
3 to ax to set it up.
Ok now we have set up text mode and we can print message on screen without calling BIOS service.
Let�s see how to print characters.
Remove the print function.
The base address for text mode is b8000. The size of screen we can print on is 80*25
.
So we have 25 lines and we can print 80 characters each line.
Every character takes up 2 bytes of space.
The first position on the screen corresponds to the two bytes at b8000.
The second position corresponds to the two bytes at b8002, and so on.
Within the 2 bytes of memory space, the first byte is for ascii code and the second byte is
attribute of the character.
The lower half are foreground color and the higher half are background color. The color index is listed here.
For example, we print text mode is set.
We change the message to text mode is set.
we move
si message
and di
0xb8000. What we do here is save the address of characters to regiser si and text mode address
b8000 to di.
Note that the value b8000 is too large for 16-bit register di to store.
So we save b800 to
es register
and zero di register.
When we reference the memory address b8000, we use es di which points to the same memory location
.
We also save the number of characters to cx.
Then we print characters one at a time.
move al si
and move es di,
al.
We copy the data in memory pointed to by si which is the first character of message at this point.
and copy the data to the memory addressed by di which is b8000. Next we specify
the attribute of character in the next byte,
we use color index a which is bright green.
Ok now we have printed the first character in green.
We continue the same process until all the characters have been printed.
So we add di,2 and si 1. Because the character takes up two bytes and
the character stored in message takes up 1 byte.
By adjusting di and si register, we just move to the next character. The last instruction is loop
which use cx register as a counter so that we can continue the process.
As you see, we have initialized the cx with the number of characters. So loop instruction will execute
this block of code the same number of times.
OK, let's build the project.
and the run the bochs.
As you see, we printed a message without using bios service.
This is a message we have printed in the real machine and in the next video, we will switch to protected mode
.
See you in the next video.