Page 1 of 1

Help With Memory

Posted: Mon Dec 06, 2004 7:37 am
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???

Re:Help With Memory

Posted: Mon Dec 06, 2004 8:18 am
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

Re:Help With Memory

Posted: Mon Dec 06, 2004 9:24 am
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 ...