Theory: RAM memory
- contains: code, data
- indexed linearly, using hexadecimal numbers
- measured in bytes and multiples (kilobytes, megabytes, gigabytes, terabytes etc)
Frequently used units of data measurement:
- bit = 1 or 0
- nibble = 4 bits (hex digit)
- byte = 8 bits
- word = 2 bytes = 16 bits
- double word (dword) = 4 bytes = 32 bits
- quadruple word (qword) = 8 bytes = 64 bits
- 'low' memory (under 1 mb)
- 'upper' memory (above 1 mb)
The separation appeared in the 16-bit era. At the beginning, 16-bit indexed memory could only reach 64kb in size (0xFFFF is the maximum number in 16 bits). Because engineers didn't imagine that anything would need more than 640kb of memory, they came up with the Segment:Offset memory addressing scheme, which addresses memory using 32 bits.
In the Segment:Offset addressing scheme, the address is specified using 2 words. The first one is the segment; every segment has 64kb in size, and a new segment starts every 16 bytes. This means that the segments overlap each other. The other word is self explanatory.
There are many ways to specify the same address using this memory scheme, for example:
- Linear address: 0xA0000 (this is where the video ram starts)
- Segment:offset address: A000:0000 or 9100:F000 or 9FF0:0100
Calculating the linear address, you need to multiply the segment with 0x10 (16 in hex) and add the offset.
Not all memory is available for use. The restricted areas include:
Low memory (< 1MB):
0x0 - 0x3FF: Interrupt vector table
0x400 - 0x4FF: Bios data area
0x7C00 - 0x7DFF: The boot sector of the kernel. You can overwrite this area if you don't need it any more.
0x9FC00 - 0x9FFFF: The extended bios data area (may start from 0x80000)
0xA0000 - 0xBFFFF: Video RAM. This is where you usually write graphics to the screen (in graphic modes), or text (in text mode). For the default text mode (0x03), the address to write to is 0xB8000.
0xC0000 - 0xFFFFF: Read only, mostly code area. Contains the Video BIOS, some mapped hardware, and the motherboard BIOS.
Upper memory (>1MB):
These areas, if exist, must be read from the multiboot information structure. More about it in the future. There is a standard memory hole between 15 and 16MB, but again, you must read the information from the multiboot information structure.
Practice: the screen
If you read carefully above, to write text to the screen you need to write to address 0xB8000. For each character, you output 2 bytes: the ASCII code, and the color.
For example, to write a colored 'A' in position (3, 10):
- write ASCII code to 2 * (10 * ConsoleWidth + 3)
- write color code to 2 * (10 * ConsoleWidth + 3) + 1 (high nibble is the background, low nibble is the foreground).
To clear the screen, write zeros to all the bytes. The screen size in the default text mode is 80 x 25 characters, so you need to write a total of 80 * 25 * 2 bytes. If you want to color the background when the screen is cleared, set the color property for every character to the desired color (high nibble for background, low for foreground).
All these routines are implemented and explained in the video tutorial. You can also download the project files here: http://dl.dropbox.com/u/24832466/OSDEV/MyOS-2.2.zip