### A gentle introduction to paging

• Problem description

• Problem description:

 How can you develop a computer system that can load a program in pieces (say chunks of 1 Kbytes) anywhere in memory and execute it ??? Furthermore, you do not need to load the entire program into the memory to execute the program !!!

This is the result if we have paging

• Program execution that you have learned so far....

• When you write an assembler program, you will no doubt has define variables and used them like this:

 ``` move.l x, D0 add.l #1, D0 move.l D0, x .... .... x: ds.l 1 ```

• After you assemble the program, the assembler would have replace the symbolic constant with a memory address

Suppose the variable

 ``` x: ds.l 1 ```

is stored at memory address 4000.

Then the label x is assigned the numeric value 4000

• The assembler program will be translated as:

 ``` Address What is stored in memory at this address ========================================================== 0: move.l 4000, D0 4: add.l #1, D0 8: move.l D0, 4000 .... .... 4000: 4 byte memory for variable x ```

(I assumed that one instruction occupies 4 bytes of memory)

• You were told that the whole program is stored in memory, starting at address 0:

We keep things simple so focus teaching assembler programming....

• The reality of program execution is very different (and strange)

• Execute a program loaded in a different location in memory

• Suppose we want to execute the same program but putting (= loading) the program in a different location in memory

Suppose we load the program at memory location 10000 and executed it:

• There will be a problem:

• The memory location for the variable X should now be:

 ``` 14000 ```

The program is still using the address 4000 !!!

• The most straightforward solution is to change all the address that reference the variable X to 14000:

 We have to change the program !!! (Actually, we must re-compile the assembler program after change the ORG primitive. Still, we need to do something)

• Question:

 Can we devise as solution where we do not need to change the program at all ???????

• Program address = an address used by an instruction in a program

Example:

• The instruction

 ``` move.l 4000, D0 ```

 Memory address = the address used to identify a specific location in memory

• Example:

• So far in your computer science career, you have only seen the following mapping:

 ``` memory address = program address ```

• In general, the memory address is a complex function of the program address:

 ``` memory address = f(program address) ```

• A better solution for the program movement problem

• Consider the following addressing technique:

• We add a base register inside the CPU

• Whenever the CPU sends an address to the memory, we add the value of the base register to the address:

• Consider now the execution of the instruction

 ``` move.l 4000, D0 ```

when we store the value 10000 in the base register:

The program will now work without having to change anything !!!

• In fact, in this example, you see that:

 ``` memory address used = program address + 10000 ```

This is the underlying mechanism of the paging technique !!!

• A generalized placement technique

• We can use this "trick" on pieces of a single program !!

• In paging, we cut a program up into pages of equal size

• Each page is stored in different location in memory

 We must define a mapping for each page of the program !!!

• Example:

• Suppose we have a program that has 2000 bytes:

• We partition the program into 2 (equal size) pieces:

• We store each fragment separately:

• We must provide 2 offsets:

 One for addresses that are between 0 - 999 One for addresses between 1000 - 2000

Example:

• The result is a very flexible system where:

 You can put a page of a program anywhere in memory The program does not need to have all its pages in memory simultaneously to run !!!