x86实模式段重叠如何帮助节省内存?
我正在教我 12 岁的 8086 汇编语言,昨天我们正在谈论内存、寻址和分段。我向他展示了如何将段可视化为从 16 字节边界开始的一系列重叠的 64Kb 块,偏移量是段内的 8080 样式指针。然后他问了一个我无法回答的问题:为什么(为什么,出于什么目的)它们重叠?
在尝试研究这个问题时,我发现了许多 20 位数学的副本,并且一些含糊的提及该方案可能允许节省内存。有人可以详细说明内存节省部分吗?或者还有其他方法可以实际利用重叠部分吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
重叠越少,您对段在物理内存中的起始位置的选择就越少。这会在无用的段之间产生间隙,从而浪费内存。设计选择绝不会产生大于 15 字节的间隙。这也许有点过头了,但 8086 是在 1 MB 足以满足每个人的时代设计的。购买 64 KB 会大幅削减您的预算。足够珍贵,不想浪费在差距上。
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.
在我看来,内存节省的看待方式是完全错误的。该处理器问世时,16 位地址空间正变得越来越紧张。其他 16 位地址空间处理器在事后添加了存储体切换方法。 20 位地址空间是一个相当大的认可(24 位或 32 位在当时只是疯狂的谈论)。他们没有使用外部存储体切换(编程很痛苦),而是使用寄存器。好吧,你猜怎么着,处理器内部是 16 位的,所以不妨使用 16 位寄存器。现在,从一对 16 位寄存器中获取 20 位是一个有趣的问题。使用段作为基本的段落对齐方式可以以最灵活的方式提供完整的范围(加上一点点超限,但这是一个完全不同的问题)(还记得我提到的银行切换的痛苦吗?这主要是因为无法做到这一点)以细粒度的方式)。
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).
假设您在一条又长又窄的小巷里拥有代客泊车服务,因此车辆都是平行停车方式。您的一些客户驾驶汽车并且仅使用 1 个停车位。其他人则驾驶加长豪华轿车,需要 2 个车位。停放车辆的一种方法是为每辆车分配 2 个停车位,无论车辆大小。您的索赔单上会显示“车辆从 0 号车位开始停放(最大车辆尺寸为 2 个车位)”或“从 2 号车位开始”或“从 4 号车位开始”等。
这可行,但会浪费空间,因为您为只需要 1 个车位的汽车预留 2 个车位。因此,您将车辆打包在一起,汽车只占用 1 个车位,豪华轿车则占用 2 个车位。您的员工知道汽车只占用一个车位,因此,如果票上写着“从车位 1 开始(车辆最大尺寸为 2 个车位)”,他们就知道“汽车在车位 1 中,车位 2 属于其他人”。汽车的 2 号车位实际上与停在 2 号车位和 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.