Physical Memory Layout

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.
mystran

Re:Physical Memory Layout

Post by mystran »

Solar wrote: Because that construct is correct.
Agreed.
But try to come up with a char* to 0xb8000 that doesn't throw a warning with -pedantic. The solution is

Code: Select all

char* ptr = 0;
ptr += 0xb8000;
and potentially non-portable because pointer arithmetics are only defined within the same array (and there is no char* array starting at 0 because 0 must not point to a valid object). Add to this that ptr is not required by the standard to actually point to address 0 (as we discussed in this thread)... you could, theoretically, end up just about anywhere.
The thing is, when writing portable C, you should never need to come up with a pointer to a specific address.

Anyhow, are you sure pointer arithmetics are not defined outside arrays.. I've never seen anything that would claim this to be true, and the point is, it makes malloc useless for things like strings... then again.. there's calloc().

Let's all from now on allocate our strings with calloc(len,sizeof(char)); We should also probably not rely on char being 8bits or int 32bits since char could just as well be 7bits and int 25bits, or pretty much anything else, as long as sizeof(char)<=sizeof(short)<=sizeof(int)<=sizeof(long).

This actually gave me an insight. At some point I'm supposed to have a uni class of C. I happen to know that the professor happens to want (supposedly) standard C99 so I better remember all the pain of using pointers portably then. He's gonna have fun with that calloc()..
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Physical Memory Layout

Post by Pype.Clicker »

hmm ...
maybe

Code: Select all

   asm volatile("movl $0xb8000, %0":"=g"(ptr));
would be the sole clean option ... Or have an extern char array Video[] which address would be set by the linker script to be 0xB8000 ...

However, the address of the video memory (or any fixed absolute address i can think of atm) is platform-specific, so what would a "portable" code mean in that context, anyway ? There's no portable way to set a pointer to video memory, nor to page directories or whatever ...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Physical Memory Layout

Post by Solar »

Both solutions are outside the scope of C/C++. ;-)
Pype.Clicker wrote: However, the address of the video memory (or any fixed absolute address i can think of atm) is platform-specific, so what would a "portable" code mean in that context, anyway ?
That's probably the reason why the committees didn't feel a portable way to set a pointer to X is necessary.
Every good solution is obvious once you've found it.
Post Reply