Help With 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
KieranFoot

Help With Memory

Post by KieranFoot »

Hi guys this might seem like a stupid question...

ok the bios data area 40:00 - right
how do u use segments in c, or do i need to say if im using a flat memory model???
User avatar
gaf
Member
Member
Posts: 349
Joined: Thu Oct 21, 2004 11:00 pm
Location: Munich, Germany

Re:Help With Memory

Post by gaf »

Hi,
to translate a linear real-mode address to a physical address you first have to multiply the segment by 16 and then add the offset.

Code: Select all

BIOS data area:  0x40:0x0000 (seg:offset)
physial address: 0x400+0x0000 = 0x400
cheers,
gaf
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:Help With Memory

Post by Pype.Clicker »

If you have sufficient knowledge of your environment, you can use the well-known base of the current data segment to access the linear address computed with real_segment*16+real_offset ...

Otherwise, you can simply write a small function in ASM that will access the requested byte/word/dword and return it.

Code: Select all

//! reads a 32bits word located in sel:off (check your Intel Manuals)
/*! \note this function temporarily change the "Extended Segment" register
 */
static __inline__ dword peek(word sel,void *off)
{
  dword ret;
  asm("push %%es;mov %1,%%es;"
      "mov %%es:(%2),%0;"
      "pop %%es":"=r"(ret):"g"(sel),"r"(off));
  return ret;
}

Just updated http://www.osdev.org/osfaq2/index.php/S ... eFunctions with far_peek and far_poke functions ...
Post Reply