Problem with a Function to list the Free Memory

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
ChristianF
Member
Member
Posts: 79
Joined: Sun Jun 10, 2007 11:36 am

Problem with a Function to list the Free Memory

Post by ChristianF »

Hi
I have written a little function which writes all the free memory addresses above 16MB in an Array.
When i want to put the address on screen i got nothing there.
Here is my code for listing the free memory and getting a free block:

Code: Select all

unsigned long free_blocks_index = 0;
unsigned long free_blocks[1024];

void make_free_blocks_list()
{
     unsigned int x;
     int i=0;
     for(x = 0x000000; x < (1024 * 1024 * 64); x += MEMORY_BLOCK)
     {
           // this is a page (considered a block)
           
           // if block is above 16MB.. then it should have a great chance of not being used? 
           if(x > 0x100000 + (1024 * 1024 * 16))
           {
                free_blocks[free_blocks_index] = x; 
                free_blocks_index = free_blocks_index + 1; 
           }
     }
}

unsigned long get_free_block()
{
         free_blocks_index = free_blocks_index - 1;
         return(free_blocks[free_blocks_index]);
}
And Then I have a second Problem: I have written a Function in ASM
which counts the memory, for example 512MB or so. When i use instead of the 64 in make_free_blocks_list the function
GetMemSize() then there is an endless loop.
This is the Sourcecode:

Code: Select all

; Function for Memorytesting
global _GetMemSize

_GetMemSize:
       push    ebp
       mov     ebp, esp
       push    eax
       push    edx
       mov     eax, 0                     
       mov     edx, 0x1EFFFC              
.1:
       inc     eax
       mov     DWORD [edx], 0xAAAAAAAA    
       mov     ebx, [edx]                 
       add     edx, 0x100000             
       cmp     ebx, 0xAAAAAAAA            
       je      .1
       pop     edx
       pop     eax
       mov     esp, ebp
       pop     ebp
       ret
I call this like this (puts() prints a string on the display):

Code: Select all

puts("There is a free block od memory at this address:");
puts(get_free_block());
And what I see on the Display is like this:

Code: Select all

There is a free block od memory at this address: 
What am I doing wrong? (My English isn't very good :( )
42
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

First: Does your number-printing function work? It doesn't look like you're using C++, so you can't have overloaded the puts function to take an integer parameter.

Code: Select all

puts("There is a free block od memory at this address:"); // Calling puts(char*)
puts(get_free_block()); // Calling puts(unsigned int)
I don't know how much C you know, but an integer can not be implicitly cast to a string. In fact, in C, there is no such thing as a string type. It's just a collection of characters ending in a zero. You have to implement your own 'puthex' or 'puti' functions. At the moment I would expect to see what you're seeing as output.

As for the second problem, that is not the correct way to query the memory size. Get it from the BIOS. Get into real mode and query it using a bios interrupt. Your address space is full of little bits of memory-mapped IO devices, like for example the VGA framebuffer. writing to that memory will, in some instances, destroy things. It's best never to do it.

On a side note, for future reference, you don't need to save the eax, ecx or edx registers when using the cdecl (default gcc) calling convention. They are assumed to be trampled by the caller function. You do, however, need to save ebx. (You have pushed eax, edx below but not ebx).
Plus, you only need to save the stack pointer into ebp and push ebp when you use the stack. Which you don't here.

JamesM
ChristianF
Member
Member
Posts: 79
Joined: Sun Jun 10, 2007 11:36 am

Post by ChristianF »

You do, however, need to save ebx. (You have pushed eax, edx below but not ebx).
After I have done this, I get something cryptic on the screen.

Now I have a question: Do somebody have a tutorial for implementing something like printf?

My Knowledge in C was better before 2 months.
Just when I started working, i programmed ASP Websites and no C, so my knowledge in C is at the moment not very good, but i work on it ;).
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

I wholeheartedly suggest that you program something smaller before trying an OS. Seriously, this is not the project to learn C with. It requires at least a sound, if not an inside-out knowledge of the language and compiler.

And check linux v0.01 for an implementation of vsprintf. writehex and writedec you can implement yourself if you think about it the right way.

And, by the by, if you can't implement functions to convert ascii to hex and ascii to decimal, you really should not be writing an operating system.

JamesM
ChristianF
Member
Member
Posts: 79
Joined: Sun Jun 10, 2007 11:36 am

Post by ChristianF »

I am programming C for about 5 years.
But if you are programming vbs so there are many differences between this and C.
I know the C language well.
And thank you for your reply.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

I know the C language well.
Yet you were trying to call a function taking a char* parameter with an integer argument and expecting it to work... :shock:
I am programming C for about 5 years.
And, this:

Code: Select all

free_blocks_index = free_blocks_index + 1;
Can be rewritten better as this:

Code: Select all

free_blocks_index ++;
Similarly:

Code: Select all

free_blocks_index = free_blocks_index - 1;
=>
free_blocks_index --;
You see why I questioned your C ability.
But if you are programming vbs so there are many differences between this and C.
I hope I am reading that incorrectly - it looks like you just said that visual basic script was similar to C......

JamesM
ChristianF
Member
Member
Posts: 79
Joined: Sun Jun 10, 2007 11:36 am

Post by ChristianF »

I know that i can write "free_blocks_index = free_blocks_index + 1;" as "free_blocks_index++;". But I think it is easier for people to understand which do not know the C language very well.
This was my reason to write it like this.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

You decided to write less readable code... so that people who don't know the language can understand it better?!

Seems a bit backwards.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post by inflater »

"free_blocks_index = free_blocks_index + 1;"
This is like from pascal syntax :) Of course even there you can use inc(free_blocks_index);, like from "free_blocks_index++".
You decided to write less readable code... so that people who don't know the language can understand it better?!
Well don't know, what's worse? :) If the code is very well commented and tabbed properly why not use the "pascalish" syntax (free_blocks_index = free_blocks_index + 1") ? ;)

Regards
inflater
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
Post Reply