-3

Consider a program that uses absolute physical memory references

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?

Rewrite the above code so the program works when it is loaded at memory address 0x1000.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • movl ($0x1000), %eax movl ($0x1004), %ebx movl %eax, ($0x1004) movl %ebx, ($0x1000) – undrcmpsci Nov 01 '17 at 03:53
  • You can edit your question. Edit link is below the tags – Michael Petch Nov 01 '17 at 03:55
  • Is this a homework question? If so, please indicate that in your question and show the work you have already done to solve it. – Tom Aranda Nov 01 '17 at 03:56
  • So this runs with paging disabled? Otherwise the question makes no sense because those would be virtual addresses and you haven't said what's in the page tables. It still makes no sense because you haven't given the value of the `%ds` segment base (which is implicit for absolute addressing modes.) Or did you not mean to say **physical** address? – Peter Cordes Nov 01 '17 at 04:00
  • Voted to close as lacking a [mcve] because you don't show what goes wrong when you run it from `0x1000`. Single step it with a debugger and show what happens. – Peter Cordes Nov 01 '17 at 04:03
  • Even if for simplicity let's say: the code is at 0x1000, and loads/stores value at address 0x1000, and that's "rwx" accessible memory; it's not clear why it would NOT work fine? It would just overwrite itself and mixup instruction opcodes, so it would execute differently second time (and I'm too lazy to actually try out what instructions would emerge from that swap, maybe it would be still "fine" to execute), but first time it's perfectly fine for me, I don't see any problem with it (except it's pointless code). As a demonstration of self-modify/memory-overwrite problems it's poorly designed. – Ped7g Nov 01 '17 at 07:07
  • Consider for example: `incl ($0x1000)` `jnz $-6` ... now this one is hard to justify as intentionally self-modify code with reasonable meaning, when loaded at address 0x0 it will loop some-many-times (depending on initial value at address 0x1000), but when loaded at address 0x1000 it would ... hmm.. start modifying some other memory in second iteration and keep doing that for about ~1-256 many times... again not that spectacularly crash-wrong/explosion stuff, I can even imagine to use this in production for anti-debug/anti-tamper trap for noob RE people to confuse them. .... – Ped7g Nov 01 '17 at 07:16
  • This is likely part of a memory partitioning and physical memory exercise. – Michael Petch Nov 01 '17 at 07:24
  • By any chance are you taking a course that is using the book Modern Operating Systems by Tanenbaum? – Michael Petch Nov 01 '17 at 19:40
  • 1
    @PeterCordes : I have rolled back your change (It is very rare I do this, and never done it on you). You may have made the change because of the first comment by the OP. I do't believe his first comment was to alter the actual code in the question but was to give his *try* at solving it. The question as is actually makes a lot of sense if we know context. I've done OS and Compiler writing tutoring over the years and I've seen this kind of question before. The problem is the question itself lack context to answer the question, but I believe the original code posted is correct. – Michael Petch Nov 01 '17 at 23:16
  • If the OP can answer whether his course is based on Tanenbaum's Modern OS book I can almost guarantee this question and others the OP will be getting are based on the logical progression of memory management, and memory abstraction. Tanenbaum goes from no memory management to multi tiered page directories. My guess the OP is at the early stages of being taught the material on memory management, but has failed to provide SO the context of what he's learning to make the current question useful. I'm pretty sure my guess at context is likely correct. – Michael Petch Nov 01 '17 at 23:19
  • If this question did have proper context I believe it would also be better asked on [Computer Science Stack Exchange](https://cs.stackexchange.com/) – Michael Petch Nov 01 '17 at 23:32
  • 1
    @MichaelPetch: Thanks, that interpretation makes sense too, and makes the question less trivial. Agreed on the rollback; reapplied my downvote because it's a bad / incomplete question instead of just a trivial question. – Peter Cordes Nov 02 '17 at 02:02

1 Answers1

1

This answer is within the context of Operating System design as might be taught from the book Modern Operating System Design Edition 4 . In chapter 3 one learns about different architectural designs and how an operating system may deal with issues like running multiple programs without a memory abstraction. One such mechanism is to partition memory and each program runs within that partition. An example may be that each program is loaded on a 4kb boundary (0x0000, 0x1000, 0x2000).

I believe this question is in that context.

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

The reason would be that the instructions provided attempt to load and store from memory addresses (0x100 and 0x104). If these instructions are loaded starting at 0x1000 and then executed they would likely interfere with the program loaded from 0x0000 to 0x1000. To get around this a simple OS design could have a program loader that statically relocates all absolute memory references to make them relative to the base address of the memory partition they are being loaded in. The base would be 0x1000 in this case so that would be added to 0x0100 and 0x0104. The resulting program would look like:

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

The syntax above seems to be for a fictional (or educational) assembly language (based on x86 AT&T syntax) where ($0x####) does loads/stores from an absolute physical memory address.


My answer above was made based on observations that this was tagged operating system and in particular the location 0x1000 and the idea of absolute memory references. This in itself made me assume (right or wrong) that likely this was a memory partitioning question in the context of a machine that was using no memory abstractions (like paging and segmentation).

I discovered that the OPs question appears to be part of a course. I noticed that this question has been posed more fully as:

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 

[snip]

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?

The course syllabus says that Modern Operating System (Edition 4 being the latest) is being used for the course and that there is an assignment was due recently. The syllabus also suggests this studio (exercise) is associated with Chapter 3 of the book.

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

With this in mind I believe the answer I gave above is probably a reasonable answer given Chapter 3 of the book and what is being asked in the exercise.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198