Memory Detection

Home OS Home

                    Memory detection is a very important part in developing an Operating System. That is because of the importance of Memory Management in any system. If you are planning to write a memory management module, it is very important that you know how much memory the system has, to play with. Memory is never cheap and if you are unable to detect any portion of the memory, you are basically having a hole in memory. Although there are many ways to detect memory, the easiest way is to use BIOS 15H interrupt. In that interrupt itself, there are three different ways. The three methods are named the way they detect.

Extended Memory Detection

                    As the name suggest, it only tells you how much extended memory you have. This is a primitive level function because of the restriction it has. The limitation is that it can only detect memory up to 64 MB. For early system (in 70s and 80s [even in early 90s]) designers, this was more than enough because, nobody really had anything above 16MB. Since 1997, the need for memory grew and so did the way to detect that. There is another problem with this method which very few articles seems to say about. This method doesn't work in most of the systems. In those systems that it does work mostly returns a false value (mostly < 16MB). I do not know who designed this chip but whoever it was, didn't do a good job. Enough talk, let me show you how it works.

                    BIOS INTERRUPT:                    15H                                    Function Code        0x88 in 'AX'

                    RETURN VALUE:        Carry Flag (set = error, 0 = success!)        AX = Memory count in KBs (number of KBs)

                    So, in short the memory you have (if it detects it correctly) is 1024*AX+1MB

 

Getting Large Memory Size

                    This method works a little different. This is a reliable method since the source of error in detection is very very limited. It returns the memory in two different registers in two different units. Although this is a reliable method, it has it's own limitation. This method can return memory size up to 4GB (not that anybody has that much). I haven't tested this method in many systems, but so far it works in 100% of systems that I have tested. Of course, I never had or have any 4GB or above memory in my computer, so I do not know what happens then. Here is how it works

                    BIOS INTERRUPT:                15H                                        Function Code      0xE801 in 'AX'
 
                    RETURN VALUE:
 
                                 `CF'        Carry Flag                      Carry indicates ERROR and Non-Carry indicates SUCCESS
                                 `AX'        Extended 1                     Number of contiguous KB between 1MB and 16MB. [MAX = 0x3C00 = 15 MB].
                                 `BX'        Extended 2                     Number of contiguous 64KB blocks between 16MB and 4GB.

                                 `CX'        Configured 1                  Number of contiguous KB between 1MB and 16MB. [MAX = 0x3C00 = 15 MB].
                                 `DX'        Configured 2                  Number of contiguous 64KB blocks between 16 MB and 4 GB.
 

                    Like you, I do not know what the difference between Extended and Configured is. I have read many kernels and I never saw anybody using Configured. If you get to know it, email me. Below is a sample of code I wrote for my system.

Detect_Memory:
        push bp
        mov bp, sp

        xor eax, eax
        xor ebx, ebx
        xor ecx, ecx
        xor edx, edx

        mov ax, 0xE801                                         ; BIOS Function
        int 0x15                                                       ; BIOS System Service Interrupt
        jc Mem_Error                                             ; If there is an error, Exit

        mov WORD [Extended1], ax
        mov WORD [Extended2], bx

        mov cx, 1024
        mul cx                                                             ; DX:AX = {(ax in KB) * 1024}
        shl edx, 16
        or edx, eax                                                     ; EDX = Lower Memory in Bytes
        push edx

        xor eax, eax
        xor edx, edx
        mov ax, bx
        mov ecx, (64*1024)
        mul ecx                                                         ; EDX:EAX = {(EAX in 64K) * (64*1024)}

        pop ecx
        add eax, ecx                                                 ; Add the Extended1 and Extended2
        add eax, 1048576                                       ; Add the 1MB whole in Memory Detection
        mov DWORD [TotalMemory], eax

        mov sp, bp
        pop bp
        retn


        Mem_Error:
                mov DWORD [TotalMemory], 0
                mov sp, bp
                pop bp
                retn

 

Querying System Address Map

                    Like the name suggests, this method detects the memory by querying Memory Map. If you know a little about memory chips you will know about Memory Maps. I won't go into details about this system because everybody rarely uses this method. However, this guarantees actual memory without any limitation. Mapping is the method by which hardware knows what address represents what location in the memory chip. This is a little complicated than the prior two. The memory map is returned by making successive calls to the BIOS with a data structure in the register. I won't go in details because this is a tough method to explain in such detail. If I get time I will come back and fill that up.

 

OS Into Home System Theory Choosing Platform Bootstrap INIT or KERNEL