"In terms of this picture of mapping addresses from the address space onto the actual memory locations, a 4-KB machine without virtual memory simply has a fixed mapping between the addresses 0 to 4095 and 4096 words of memory. An interesting question is: "What happens if a program branches to an address between 8192 and 12287 ?" On machine that lacks virtual memory the program would cause an error trap that would print a suitably rude message,for example : "Nonexistent memory referenced" and terminate the program."

From the above paragraph this is what i understand , this is a 12 bit addressing system as 2^12 is 4096 and each word forms 1 byte (since 4KB X 1024 = 4096 Bytes = 4096 Words) . But when the author says "What happens if a program branches to an address between 8192 and 12287 ?" I don't understand what does that mean.From where do the addresses 8192 till 12287 come up ?(doesn't the processor have a 12 bit addressing scheme ?) And what does the statement "On machine that lacks virtual memory the program would cause an error trap that would print a suitably rude message" mean ? What has virtual to do with this ?

• I think the author got confused. – starblue Nov 18 '11 at 10:19

When designing a modern computer / operating system combination one of the things we want to do is run multiple programs at the same time. One of the problems that you would run into designing this system is that all your programs want to assume they have access to all the memory they want, and they don't coordinate what addresses they use.

The solution to the problem is a system called virtual memory. The virtual address space is the address space the operating system makes available for a program to use. When a program tries to access virtual memory at say, address 1024, they don't get to access the physical memory address (the addresses that go out on the wires to the ram chips) 1024. Instead there is a mapping system.

The operating system handles all the mappings, so that two different programs can both access what they consider address 1024, but process 1 might have its virtual address 1024 mapped to physical address 2048, while process 2 might have its virtual address 1024 mapped to physical address 4096.

In order to keep the mapping information manageable, the operating system maps memory in "chunks" called pages. 4096 bytes is a very common page size. In the example you site, a certain process has a single page, located at virtual addresses 4096, that is 4096 bytes in length (extending to virtual address 8191), mapped to the physical address 0 (since the page is 4096 bytes long, the mapping extends to physical address 4095)

The actual size of the virtual address space is not specified (it must be at least 14 bits wide because the address 12287 is mentioned), but that hardly matters. One thing for sure, it is not a 12 bit addressing system. That's just the size of a virtual memory page, the smallest chunk of memory the operating system will manage. The addresses 8192 through 12287 are just other virtual addresses a process could access.

The author asks the question "what happens if there is an access to memory that is not mapped?"

In a computer without a virtual memory mapping system, the hardware notices that accesses to addresses not connected to physical ram are errors. The hardware signals the operating system of the offense. This process is called an error trap. The operating system would then print the message "Nonexistent memory referenced" and terminate the process. That's the suitably rude message.

In a computer with a virtual memory mapping system almost the same thing happens. Since most programs don't use all the memory that they could possibly address, the operating system doesn't map all of a process' virtual memory to physical memory (also, most computers have more virtual address space available then total physical ram installed in them). So when a process tries to access a virtual address in unmapped memory, the hardware notices there is no physical memory mapped to the virtual address in question. The operating system is signaled, it prints a rude message, and terminates the process.

This mapping and error trapping system not only allows multiple processes to have their own views of the address space, it also allows the operating system to contain and protect the running processes from each other. Even though they may be using the same virtual addresses, the operating system keeps different processes mapped to different physical addresses. That way is isn't possible for a process to (accidentally or on purpose) access or overwrite the memory of any other process. This keeps buggy programs from taking out your whole computer when they crash.

• well explained ! But can you please give the significance of "chunks" called pages . I don't really get the line "In order to keep the mapping information manageable, the operating system maps memory in "chunks" called pages". What do you mean when you say "maps in chunks" ? What disadvantage will we get if the OS doesn't map the memory in chunks ? – Suhail Gupta Nov 18 '11 at 10:02
• The smaller the chunk, the greater their number and so the more tracking information that must be kept. 4K pages are a decent compromise at the moment. But mapping in single byte increments would require more memory to store the tracking information than to store the data itself! – Chris Stratton Nov 19 '11 at 18:43

First, the $12287$ is an odd value. Memory spaces are usually powers of 2, like 64k ($65536$) = $2^{16}$ for a typical 8-bitter. $12287$ as highest address means $12288$ addresses = $3 \cdot 2^{12}$. The reason for powers of 2 is that $12288$ addresses still need 14 address lines, just like $2^{14}$ or $16384$ would. You don't gain anything by disallowing the top 4k.

Microcontrollers without MMU (Memory Management Unit) don't know when you're addressing outside of physical memory, so you won't get the trap. Instead it will try to read at the non-existing address, which for many controllers will read as 0xFF, and execute this as an instruction. On some controllers this 0xFF will agree with a NOP instruction, which is a nice solution, because you'll be executing NOPs until you reach physical memory again, and more often than not this will be the reset vector. So executing code in non-existing memory will lead to a reset.

Controllers with an MMU will monitor each memory access and see if the access is in physical memory or virtual memory. If it's a virtual memory access it means that it's a valid address, but that the data is not available because it's on a hard disk for instance. The MMU will cause a trap (a kind of interrupt), and normal program execution is interrupted to allow the MMU reading the data from the hard disk. Once the data is loaded program execution continues. It goes without saying that you want as few as possible of these traps, as they seriously slow down the program. The OS will therefore have advanced algorithms to decide when which data is to be loaded.

• I'm quite surprised that the quoted author ignored the common case of a system with neither virtual memory nor memory protection, which is thus unable to recognize that an illegal address has been accessed. Besides reading FF or some undefined value, sometimes on such a system, the "unused" address lines may simply be ignored, meaning that the physical memory will "repeat" many times within the address space. In such a case the read would get value out of some memory location which has the same value as the requested address for all of the address lines which are decoded. – Chris Stratton Nov 19 '11 at 18:47