Saturday, September 3, 2011

OSDEV Series - Episode 2.2: Short recap

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

Memory map:
  • '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
There can be up to 4096 ways to specify the same address.

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 - 0x3FFInterrupt 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

10 comments:

  1. HI tibi i have found ur question on Stackoverflow website about hiding desktop icons programatically..........my situation is also the same....i also want to hide desktop icon programatically .....i have created a app which use all desktop data(files, folder, and EXE's etc) and display it on one form...i already read ur post here http://stackoverflow.com/questions/6402834/how-to-hide-desktop-icons-programatically

    but i cannot understand how to do it ...plz advise me step by step.

    plz contact me on
    i3ilal_qureshi@hotmail.com

    ReplyDelete
  2. how would you go about making the consolewritestring function end a line and print on the next line?

    ReplyDelete
    Replies
    1. I had planned to do that in the next episode, you know, to extend the functionality of these functions by making a printf-like function, and also add scrolling. I don't really know when I will make the next episode, but when I do, this is what I planned to cover.

      Delete
    2. Thanks!!! Can't wait to the next video! There amazing!!! Keep up the good work!!!!!

      Delete
  3. Where can we found basic input system and working with files tutorials? Can't wait for next video.

    ReplyDelete
    Replies
    1. You should check the brokenthorn tutorial, which helped me a lot: http://www.brokenthorn.com/Resources/OSDevIndex.html

      And also, the osdev.org website, where you will find a lot more information. I don't know whether I will continue the series or not, I have other priorities, problems etc.

      Delete
    2. Thanks. You are the only one who creates a normal OS tutorial in video format(there are some people who shows how to create "os" in batch script or VB -.-), so we hope you'll continue.
      By the way, how can we set a background color? You show how to set a character and it's color, but what about background color? You can just give us a right link to the info.

      Delete
    3. If you look closely to the tutorial, I said that for every character there are 2 bytes, one for the character, and one for the color. For the color byte, one of the nibbles is the foreground and one the background.

      To set the background of the entire screen, you need to set the background nibble for every character to that color.

      Delete
  4. I understood, thanks a lot :).
    Have a nice day.

    ReplyDelete
  5. This is a good post for this certain topic.
    The damage of files definitely directs an end user to some a sense of discouragement.
    A variety of recovery software programs are already developed by the experts, guaranteeing that an individual might easily retrieve his / her erased data.
    User have to be knowledgeable of possible information recovery tools to ensure that the guy can handle the info deletion condition in the correct way.
    repairing pst files

    ReplyDelete