How to enable a program to be placed anywhere in memory and run correctly

  • Let's consider a simplified representation of the computer system:
     

  • I have omitted the (1) data bus, (2) control bus and (3) IO devices because they are not relevant in the discussion

How to enable a program to be placed anywhere in memory and run correctly

  • suppose the program is placed in memory starting starting at memory address 0:
     

  • The assembler has allocated memory for the variable x at address 4000
     

How to enable a program to be placed anywhere in memory and run correctly

  • The "traditional" computer system (using the trivial address mapping function) is able to execute this program placement:

  • The trivial address mapping function is: the memory address = program address
    I.e.: the MMU implements the "pass through" function

How to enable a program to be placed anywhere in memory and run correctly

  • (1) The CPU can fetch the program instruction from the correct memory location
    Example: the CPU can fetch the correct instruction at program address 2000:

  • When PC = 2000, the CPU sends out program address = 2000
    Result: MMU sends out memory address = 2000 and fetches program instruction @ 2000

How to enable a program to be placed anywhere in memory and run correctly

  • (2) The CPU can also fetch an operand (= variable) from the correct memory location
    Example: instruction ldr r0,[r0] will use the correct variable at program address 4000:

  • When the CPU executes ldr r0,[r0], the CPU sends out program address = 4000
    Result: MMU sends out memory address = 4000 and fetches the value @ 4000 (= x)

How to enable a program to be placed anywhere in memory and run correctly

  • Problem:   suppose a program is placed in memory starting at memory address 6000:
     

  • For clarity, I will also include the program address in the next figure....
     

How to enable a program to be placed anywhere in memory and run correctly

  • Problem:   can the CPU execute the program when it is placed at memory address 6000?
     

  • Notice that the program addresses and memory addresses are not equal to each other !
     

How to enable a program to be placed anywhere in memory and run correctly

  • The "traditional" computer system (using the address mapping memory address = program address) will not to execute the program correctly:

  • E.g.: the ldr r0, [r0] instruction uses the program address 4000 which maps to memory address 4000 --- BUT the correct memory address 10000

How to enable a program to be placed anywhere in memory and run correctly

  • Failure (1): the CPU will fetch program instructions from the wrong memory location
    Here's what happens when the CPU fetches the instruction at program address 2000:

  • When PC = 2000, the CPU sends out program address = 2000
    Result: MMU sends out memory address = 2000 but the correct memory address = 8000

How to enable a program to be placed anywhere in memory and run correctly

  • Failure (2): the CPU will fetch operands (= variables) from the wrong memory location
    Example: instruction ldr r0,[r0] will use the wrong variable for program address 4000:

  • When the CPU executes ldr r0,[r0], the CPU sends out program address = 4000 (=x)
    Result: MMU sends out memory address = 4000 but the correct memory address=10000

How to enable a program to be placed anywhere in memory and run correctly

  • In order to for the CPU to execute a program placed at a NON-ZERO memory location, we use a mapping function in the MMU:

  • The mapping function will transform each (input) program address to a memory address as: memory address = f(program address) = program address + 6000

How to enable a program to be placed anywhere in memory and run correctly

  • Suppose: we use an adder circuit in the MMU and create the mapping
    Memory Address = Program Address + 6000:

  • This MMU circuit will transform each (input) program address to a memory address as: memory address = program address + 6000

How to enable a program to be placed anywhere in memory and run correctly

  • (1) The CPU can now fetch the program instruction from the correct memory location
    Example: the CPU can fetch the correct instruction at program address 2000:

  • When PC = 2000, the CPU sends out program address = 2000
    Result: MMU sends out memory address = 8000 and fetches the correct instruction

How to enable a program to be placed anywhere in memory and run correctly

  • (2) The CPU can also fetch an operand (= variable) from the correct memory location
    Example: instruction ldr r0,[r0] will use the correct variable at program address 4000:

  • When the CPU executes ldr r0,[r0], the CPU sends out program address = 4000
    Result: MMU sends out memory address = 10000 and fetches the value @ 10000 (= x)

Weakness of this simple address mapping technique

  • The whole program must be placed in consecutive memory locations:
     

  • This requirement is not flexible !!!
    The "paging" technique (discussed next) is more powerful and more flexible