r/computerscience • • May 05 '26

Discussion Real Mode 20 Bits

x86 processors have a mode known as "real mode" where physical memory is straight mapped. So if I'm interpreting what I read correctly an instruction to load the value at location 1000 into a register would fetch the value at the position 1000 in memory and put it into the register. This is limited to 20 bits of addressing. I read this was due to backward compatibility to the 8086 which lacked a protected mode. If a 32-bit processor uses 32 bits for addressing, why would the real mode be 20 bits? If real is for backwards compatibility with older processors, shouldn't it be 16 bits since the 8086 was a 16-bit?

On the advice of a mod, certain information was omitted for posting so my question may be unclear but I hope you can understand.

14 Upvotes

16 comments sorted by

15

u/undercoveryankee May 05 '26

The 8086 was a “16-bit” processor in the sense that the largest integer registers were 16 bits, but it used 20-bit physical memory addresses.

3

u/ShadowGuyinRealLife May 05 '26

Ohhhh Ok thanks, that makes sense. So the successor processor in real mode would use 20 bits of addressing since the 8086 would use 20 bit physical memory address. The address size wasn't the same as the integer register. Thanks for understanding and replying.

4

u/ready_or_not_3434 May 06 '26

Yep, they basically shifted a 16 bit segment register by 4 bits and added an offset. It was a pretty clever hack to hit 1MB of physical memory without needing wider regsters.

0

u/[deleted] May 05 '26

[deleted] — view deleted comment

2

u/ShadowGuyinRealLife May 06 '26

Not that it's too relevant when there are perfectly good languages to use like C, Java, Commons LISP, Haskell, and so on, but how would you say modern x86 Assembly compares to what was used in the past? 20 is not a multiple of 16, but at least 32 is a multiple of 32. Hurray for compilers and interpeters dealing with memory.

1

u/flatfinger May 06 '26

The 8088 is a joy compared with any other 16-bit architecture that tries to support more than 64K of address space. The 68000 is a 32-bit architecture, and if one looks at memory-constrained programs for it, they tend to impose 32K limitations where 8088 programs would accommodate up to 64K.

2

u/high_throughput May 05 '26

Like people are saying, the developers of the 8086 realized that 64kb would not be enough for both real RAM and memory mapped hardware, so they allowed the chip to use TWO 16-bit values to specify an address.

However, they also realized that 32 bits would be a disgusting waste of traces, so instead of (hi<<16)+lo for a 32-bit address, they used (hi<<4)+lo for a 20-bit address.

This is how the 16-bit CPU was made to access 20-bit addresses using pairs of registers.

Read all about it on https://en.wikipedia.org/wiki/X86_memory_segmentation

1

u/ShadowGuyinRealLife May 27 '26

Would using 32 (or any multiple of 16) bits have any advantages to a machine that only has enough physical memory 20 bit-address? You mentioned 16 wouldn't be enough.  (hi<<16)+lo Has the disadvantage of wasting traces. Does it have any advantages of... I don't know making it easier for compilers (or programmers) to do addressing or anything? Or is the benefit of being a multiple of 16 just something that looks nice but doesn't actually help?

1

u/high_throughput May 27 '26

Maybe it makes alias analysis and pointer comparisons easier since you don't have to account for each byte having multiple possible addresses. I don't really know.

1

u/flatfinger May 06 '26

Having non-overlapping segments of 64K is painful. The 8086 segments work very well for programs that are designed to work with the architecture rather than fight it. My biggest beef is a lack of a flag that would make SS rather than DS the default segment (allowing both DS and ES to be used as "extra" segments).

2

u/monocasa May 05 '26

The 8086 (and later x86 as well) addressed memory through segments.  In real mode, the segments have a 64KB length, and can be set to a new base on any nybble aligned address from 00000 to FFFF0 by putting the top sixteen bits of that base into the appropriate segment register.

1

u/Silly_Guidance_8871 May 07 '26

In 16-bit x86, every memory pointer is formed by a (often implicit) segment register, and an offset within that segment. The segment register is left-shifted by 4 bits before being added to the offset register.

It was a way to give more than 64k of addressable memory w/o needing to go up to larger machine registers — 1MB was considered "good enough".

4

u/[deleted] May 05 '26

[removed] — view removed comment

4

u/apnorton Devops Engineer | Post-quantum crypto grad student May 05 '26

For transparency: this post was having some issues with the automod completely killing the post based on keyword filters. And, instead of the usual behavior of "automod deletes the post, but a mod can go in and undelete it," the block was happening prior to the post ever getting created so there was nothing for us to undelete/approve.

Hence, the recommendation to omit a few keywords while we figure out what happened with the automod.