绝对物理内存参考

时间:2017-11-01 03:52:54

标签: assembly operating-system

考虑使用绝对物理内存引用的程序

movl    ($0x100), %eax
movl    ($0x104), %ebx
movl    %eax,     ($0x104)
movl    %ebx,     ($0x100)

该程序在地址0x0加载时工作正常,但在地址0x1000加载时不能正常工作。为什么不呢?

重写上面的代码,以便程序在内存地址0x1000加载时有效。

1 个答案:

答案 0 :(得分:1)

这个答案属于操作系统设计的背景,可能会从书中Modern Operating System Design Edition 4讲授。在第3章中,我们将了解不同的架构设计以及操作系统如何处理诸如在没有内存抽象的情况下运行多个程序等问题。一种这样的机制是分区内存,每个程序在该分区内运行。一个例子可能是每个程序加载到4kb边界(0x0000,0x1000,0x2000)。

我相信这个问题就在这个背景下。

  

该程序在地址0x0加载时工作正常,但在地址0x1000加载时不能正常工作。为什么不呢?`

原因是提供的指令试图从内存地址(0x100和0x104)加载和存储。如果这些指令从0x1000开始加载然后执行,则可能会干扰从0x0000加载到0x1000的程序。为了解决这个问题,一个简单的操作系统设计可能会有一个程序加载器静态重定位所有绝对内存引用,使它们相对于它们被加载的内存分区的基地址。基数为0x1000在这种情况下,这将被添加到0x0100和0x0104。结果程序如下:

movl    ($0x1100), %eax
movl    ($0x1104), %ebx
movl    %eax,     ($0x1104)
movl    %ebx,     ($0x1100)

上面的语法似乎是用于虚构(或教育)汇编语言(基于x86 AT& T语法),其中($0x####)从绝对物理内存地址加载/存储。

我上面的答案是基于观察到的,这是标记操作系统,特别是位置0x1000和绝对内存引用的想法。这本身就让我假设(正确或错误),这可能是在没有使用内存抽象(如分页和分段)的机器的上下文中的内存分区问题。

我发现OPs问题似乎是课程的一部分。我注意到这个问题已经提出更充分:

The earliest memory systems managed physical memory directly, and typically 
the operating system could do very little to make programming easier.

In this studio, you will:

Consider statically partitioned physical memory
Consider dynamically partitioned memory with base and limit registers
Explore the limitations of physical memory based methods 

[剪断]

2. Consider a program that uses absolute physical memory references meaning that each 
reference refers to a specific physical memory location. One part of such a program 
is below:
    movl  ($0x100),   %eax
    movl  ($0x104),   %ebx
    movl  %eax,       ($0x104)
    movl  %ebx,       ($0x100)

This program works fine when it is loaded at address 0x0, but not when it is
loaded at address 0x1000. Why not?

3. Re-write the above code so the program works when it is loaded at memory
   address 0x1000.
4. Suppose you have a machine with four static parititons, each capable of holding a
   program with a length of 4096 bytes (0x1000 bytes). The first partition starts
   at address 0x0. Give the first and last address of each of the four partitions.
5. Suppose the first of the four programs executes the following line of code,
   with the operand located at byte 0x25, and the destination address located
   at bytes 0x26-0x2A:
       0x25:  jmp ($0x50)

   Suppose also another program executes the following line first.
       movl $0x1234,  ($0x26)

   What happens to the first progam upon executing the instruction at address 0x25?

course syllabus表示现代操作系统(第4版是最新的)正在用于该课程,并且最近有一项任务。教学大纲还表明, studio (练习)与本书第3章相关。

Oct 16    Memory management           MOS 3.1 Studio 15-a 
Oct 18    Address spaces and swapping MOS 3.2 Studio 15-b

考虑到这一点,我相信上面给出的答案可能是本书第3章的合理答案以及练习中提出的问题。