What happens if a segment register plus offset overflows the 20-bit address space of the 8086? I assume it wraps around to 00000h, but want to confirm. For example, say DS is F001h and the offset is FFF0h. Would I then be reading 00000h?
- 2,299
- 2
- 17
- 31
1 Answers
On an 8086, yes, the address space wraps around. Thus a segment address of F001h and an offset of FFF0h, producing an address of F0010h + FFF0h = 100000h, wraps around to 00000h.
The 8088, 8086, 80188 and 80186 only have 20 address lines, so bits beyond that don’t correspond to anything and aren’t seen by the bus. Thus asking for 100000h in the CPU results in an address of 00000h on the bus, the top bit is lost.
On systems built around later CPUs, the behaviour depends on the A20 gate¹ (if there is one), which allows the 21st address line (A20) to be enabled or disabled. If the line is disabled, the behaviour in real mode ends up being the same as on the 8086. If it is enabled, addresses don’t “wrap around”; this can be used to provide access, in real mode, to the first few kilobytes of the second megabyte, known as the high memory area. A number of products could use this memory to reduce their footprint in conventional memory; perhaps most famously, DR DOS 5.0 and MS-DOS 5.0 and later could use it, which produced considerable memory savings (and the famous “Packed file corrupt” error message with some programs).
OS/2 Museum has a number of articles exploring this address wrap-around and the A20 gates: notably Who needs the address wraparound, anyway?, The A20-Gate: It Wasn’t WordStar, EXEPACK and the A20-Gate, and The A20-Gate Fallout.
¹ The 80286 and 80386 don’t provide control over their address lines; when IBM designed the PC AT, they added external hardware to control A20 so that backward compatibility with the original PC could be preserved when running DOS. The A20 gate was initially handled by the keyboard controller, and later by motherboard chipsets. Intel added A20 control to their CPUs starting with the 80486; this still required help from the chipset. CPUs from the last decade (Haswell and later) no longer have an A20 gate.
- 121,835
- 17
- 505
- 462
0xFFFFFFF0, at which address was the security ROM. We disabled A20, which caused the CPU to boot from0xFFEFFFF0instead, which was hacker-controlled. Now the hacker-controlled CPU code could re-enable A20 (by sending a message to hacker-added hardware) then read out the security ROM. – Myria Sep 28 '22 at 21:01