Aeon Emulator Blog

October 7, 2009

Segmented Addressing

Filed under: Aeon, x86 Quirks — Greg Divis @ 7:43 am

The first oddity of the x86 instruction set to trip me up was its real-mode addressing. Having done all of my assembly programming in the past in 32-bit protected-mode, I wasn’t really prepared for the weirdness of its segmented addressing. I knew from my past experience running DOS about the 640k of conventional memory, and I had heard about “segments” when writing 16-bit code with pointers, but I didn’t really have a good understanding of what it all meant.

Unfortunately, now I do.

A 16-bit pointer would only allow 65536 possible addresses, and unless you’re on a Commodore 64, that isn’t going to cut it. To extend the range of available addresses, x86 real-mode requires the use of segment registers. It’s a pretty simple concept – you just load a segment register with a base address, and then pointers act as offsets into the selected 64k segment. You might think that this would allow 4 GB of address space, and you would be right if the segments didn’t overlap:

address = (segment * 65536) + offset

Sadly, it isn’t that simple. The actual formula to calculate the physical address in real-mode is:

address = (segment * 16) + offset

This amounts to a grand total of 1 MB of address space. Everything above 640k is reserved (video RAM is also mapped to the area above 640k), leaving the familiar 640k limit for DOS.

If you’ve worked with real-mode before, I’m sure this all seems perfectly obvious, but it hadn’t even occurred to me that it was implemented this way until I did my research.

Let me just say that I’m sure it was much easier for me to emulate this in Aeon that it was for the people that actually wrote code in this environment. I’m glad I missed that period of PC development history.

Blog at WordPress.com.