1

I've been working with a lot of assembly, and reviewing virtual memory I've run into some new confusion.

Briefly, I don't understand how an address in assembly, the code that interfaces with the processor directly, could be converted from a virtual address to a physical address.

I was always told that the operating system handled mapping from virtual to physical memory, but assembly directly references an address without any system calls, how could the OS intervene if it isn't called directly?

Where does an address, (mov eax, [0xDEADBEEF]), get translated from the virtual address space to the physical address space using the page table in the OS without specifically calling the OS?

8
  • 5
    The OS sets up the mapping in the CPU beforehand. The CPU does the translation by itself, and raises an exception if there is some problem. That's how the OS can get control to e.g. load pages back from paging file.
    – Jester
    Commented Jul 10, 2019 at 22:50
  • x86 and x86-64 use paging: en.wikipedia.org/wiki/Page_table
    – Renat
    Commented Jul 10, 2019 at 22:52
  • 1
    Also osdev article on MMU and paging and obviously the official intel manuals.
    – Jester
    Commented Jul 10, 2019 at 22:53
  • the same way mail or a phone call gets forwarded. it leaves you with the address you know but along the way the address gets changed by someone who knows both address spaces. stack vs instruction fetches vs other non-stack data accesses have nothing to do with it. the mmu translates addresses, and one of the major selling points is the software doesnt know the physical nor does it care it is very happy with its virtual space.
    – old_timer
    Commented Jul 10, 2019 at 23:07
  • where is in the mmu which is usually between the processor core and the memory bus.
    – old_timer
    Commented Jul 10, 2019 at 23:08

2 Answers 2

2

Simply because the CPU supports that kind of translation directly, using page tables. OS sets up those page tables beforehand to tell CPU where to look when it references a memory address. That's how the translation happens transparently.

1
  • 1
    Thank you, I had forgotten the TLB and the MMU were part of the cpu, not the OS
    – solumnant
    Commented Jul 11, 2019 at 16:07
0

In assembly language you work with logical addresses. The operating system maps logical addresses to physical addresses using page tables. The CPU automatically translates the local address to a physical address.

It is possible that a logical address will not have a physical address mapped to it. When the CPU encounters that condition it invokes the operating system's page fault handler.

The operating system has to maintain a copy the process's address space on secondary storage. This is the "virtual" memory. When a page fault occurs, the operating system determines if the page being referenced exists in virtual memory. If it does, the page fault handler reads the page into physical memory, alters the page tables to so the logical address maps to the correct physical address, then restarts the instruction.

If the virtual page does not exist, the operating system raises an access violation exception.

1
  • I understood that assembly was logical addresses, I was just confused on how logical addresses got translated into physical addresses at the cpu level because I had forgotten that the TLB and MMU were part of the CPU, and that faulting those tables resulted in a OS lookup.
    – solumnant
    Commented Jul 11, 2019 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.