问题
I'm teaching my 12 y.o. 8086 assembly language and yesterday we were talking memory, addressing and segmentation. I showed him how segments can be visualized as a sequence of overlapping 64Kb blocks starting on 16 byte boundaries, with the offset being an 8080-style pointer within a segment. Then he asked a question I could not answer: why (what for, with what purpose) do they overlap?
Trying to research this question I found many copies of the 20 bit math, and a few vague mentions of some memory savings this scheme presumably allows. Can somebody elaborate on the memory saving part? Or any other ways to take practical advantage of the overlaps?
回答1:
The less overlap, the less choice you'll have over where a segment starts in physical memory. That creates gaps between segments that are not useful, thus wasting memory. The design choice never creates a gap larger than 15 bytes. That's overdoing it a bit perhaps but 8086 was designed in an era where 1 megabyte was enough for everybody. And buying 64 kilobytes put a rather large dent in your budget. Precious enough to not want to waste on gaps.
回答2:
Memory savings is entirely the wrong way to look at it IMO. That processor came out at a time when 16-bit address space was getting really tight. Other 16-bit address space processors were having bank switching methods tacked on after the fact. A 20 bit address space was a sizeable approvement (24, or 32-bit was just crazy talk at the time). Rather than tack on an external bank switching (which is a pain to program for), they used registers. Well, guess what, the processor is 16-bit internally, so might as well use 16-bit registers. Now, getting 20 bits out of a pair of 16-bit registers is an interesting problem. Using the segment as basically paragraph alignment gives you the full range (plus a little overrun, but that's an entirely different problem) in the most flexible way (remember the pain of bankswitching that I mentioned? It's primarily because of not being able to do it in a fine-grained manner).
回答3:
Suppose you own a valet parking service in a long, narrow alley, so the vehicles are all parked parallel-parking style. Some of your customers drive cars and use only 1 space. Others drive stretch limos and require 2 spaces. One way of parking the vehicles would be to assign 2 spaces to each vehicle, regardless of size. Your claim tickets would say "Vehicle is parked starting in space 0 (maximum vehicle size is 2 spaces)" or "starting in space 2", or "starting in space 4", etc.
This would work, but it wastes spaces because you are reserving 2 spaces for cars which need only 1. So instead, you pack the vehicles together, with cars taking only one space and limos taking two. Your employees know that car takes only one space, so if a ticket says "starting in space 1 (maximum vehicle size is 2 spaces)" they know that "The car is in space 1, and space 2 belongs to somebody else." The car's space 2 actually overlaps the limo parked in spaces 2 and 3, but it would be a bad idea to use it or you're going to dent the limo.
来源:https://stackoverflow.com/questions/8728220/how-does-x86-real-mode-segments-overlap-help-memory-saving