Page 1 of 1

Detecting free RAM and freeing unused RAM?

Posted: Tue Nov 23, 2010 11:01 pm
by David2010
I can't seem to wrap my head around the concept of detecting free RAM when it uses multiple ranges of addresses. I thought about having a function to probe every single address within a certain range and then return if that address was null. However after reading around a little bit, it is not a good idea to probe the RAM.

I am making a 16-bit Real Mode OS in Nasm assembly. The whole OS is 1001 bytes large.

After working on the OS for a while It came to my attention that I was allocating data to RAM with no functions to show how much RAM was being used nor any functions to free the RAM.

I know that "0h00000500" to "0h00007BFF" can be used.

I also know that "0h00007E00" to "0h0007FFFF" can be used.

I also know that "0h00080000" to "0h0009FBFF" can be used.

This leads up to roughly 630 KB of RAM from these addresses however my memory detection function shows I have a total of 637 KB of RAM. Seven Kilobytes lost RAM really isn't that big of a deal to me but it does make me curious as to where it went.

Code I used to detect the amount of RAM:

Code: Select all


getmem:

	mov ax, 0
	mov si, 0

    xor ax, ax  
    int 0x12    
    jc .Error     
    test ax, ax    
    jz .Error     
    
	mov si, ax ;The OS uses the si register for printing text to the screen

.Error:
	ret

Are there any bios functions available to show the amount of RAM being used and to free any unused RAM?

If you want me to post the entire source code of the OS I can.

Re: Detecting free RAM and freeing unused RAM?

Posted: Tue Nov 23, 2010 11:37 pm
by Brendan
Hi,
David2010 wrote:This leads up to roughly 630 KB of RAM from these addresses however my memory detection function shows I have a total of 637 KB of RAM. Seven Kilobytes lost RAM really isn't that big of a deal to me but it does make me curious as to where it went.
The "int 0x12" BIOS function returns "amount of RAM" and not "amount of free RAM"; and includes RAM that the BIOS is using at 0x0000000 (e.g. for the real mode IVT and the BIOS data area). This adds up to a little more than 1 KiB.

0x00000000 to 0x000004FF = 1.25 KiB used by the BIOS
0x00000500 to 0x00007BFF = 29.75 KiB that is free
0x00007C00 to 0x00007DFF = 0.5 KiB that I assume is being used by your boot loader
0x00007E00 to 0x0007FFFF = 480.5 KiB that is free
0x00080000 to 0x0009FBFF = 127 KiB that is free

If you add all this up you get 639 KiB, which should be what the "int 0x12" BIOS function returns.
David2010 wrote:Are there any bios functions available to show the amount of RAM being used and to free any unused RAM?
The BIOS tells you what it considers to be "free RAM", and never knows/cares if you are using it or not. If you want to be able to allocate and free memory, then you need to implement some sort of memory manager yourself.
David2010 wrote:I am making a 16-bit Real Mode OS in Nasm assembly.
David2010 wrote:Seven Kilobytes lost RAM really isn't that big of a deal to me but it does make me curious as to where it went.
The computer I'm using to type this reply has 12 GiB of RAM. In real mode you can only use 640 KiB of that and about 11.99 GiB would be wasted. I can see how an additional 7 KiB isn't that big of a deal (it'd be like murdering an entire continent full of people, and then worrying about stepping on your cat)... ;)


Cheers,

Brendan

Re: Detecting free RAM and freeing unused RAM?

Posted: Thu Nov 25, 2010 11:58 pm
by Chandra
Brendan wrote:The computer I'm using to type this reply has 12 GiB of RAM. In real mode you can only use 640 KiB of that and about 11.99 GiB would be wasted. I can see how an additional 7 KiB isn't that big of a deal (it'd be like murdering an entire continent full of people, and then worrying about stepping on your cat)... ;)


Cheers,

Brendan
Cheers Brendan! :D :D :D

Re: Detecting free RAM and freeing unused RAM?

Posted: Sat Nov 27, 2010 1:55 pm
by WeirdCat
David2010 wrote:Are there any bios functions available to show the amount of RAM being used and to free any unused RAM?
To find the end of base memory you can also just look at 40h:13h. I'm using the word there to position the stack of my Bootstrap processor in the last 64kb of conventional memory there while loading the rest of the kernel:

Code: Select all

	; use the last 64kb of conventional memory as stack
	xor		ax, ax		; es -> 0000h
	mov		es, ax
	mov		ax, [es:413h]	; BASE MEMORY SIZE IN KBYTES
	sub		ax, 64
	shl		ax, 6
	mov		ss, ax
	xor		sp, sp