The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: joemc on May 17, 2010, 07:30:43 PM

Title: How do pointers really work?
Post by: joemc on May 17, 2010, 07:30:43 PM
I understand how they worked until i learned more :'(

Using a flat memory model the OS is determining where the memory actually is, correct?  how does the CPU carry out a LEA operation? the cpu is not aware of it's actual address, is it? how is the kernel getting involved in the LEA operation?
Title: Re: How do pointers really work?
Post by: redskull on May 17, 2010, 08:20:36 PM
You've sort of got it backwards; for the most part, in a virtual memory system, it's the CPU which "knows" where the memory actually is, and the memory address translation happens transparently to both the application and the kernel.  The memory addressing modes and instructions happen on the "virtual" side of the fence; the LEA calculates the address the same as if it were in real-mode, and is none-the-wiser as to where the physical RAM is located

-r
Title: Re: How do pointers really work?
Post by: clive on May 17, 2010, 08:24:10 PM
LEA has no knowledge of how memory is virtualized (ie it does not look into the paging tables). It uses a simple linear computation, and does not touch memory.

The OS is familiar with the paging tables that it has built, and can navigate them. There is no reverse lookup in hardware, so again the OS would need to do this via hashing or whatever method it chooses. (Physical-to-Virtual - Reverse lookup is where you have a page, and you want to know where that page is used within the large paging table, and what virtual address, or addresses, it backs)

The CPU handles virtual-to-physical translation through a TLB (Translation Lookaside Buffer) which caches the most current traversals the CPU has made of the hierarchical page tables. Different processor families have more/less TLB entries.
Title: Re: How do pointers really work?
Post by: hutch-- on May 18, 2010, 06:07:19 PM
Joe,

Its a bit to do with understanding what a protected mode operating system is that can multitask. The protection is that an application can only address memory it has allocated and can only work in its own address space. In the old days in 16 bit Windows one app could access and therefore read and write another apps memory. This caused crashes that brought down the OS and one single app could do it.

With protected mode any app built can run in its own memory space and all PE files have the same format, start at the same address etc .... The OS controls the virtual address so that each app that runs "sees" the same address range. An address in one app (properly process) can be passed to another app but the address is not valid outside of an apps own address space.

In a very crude sense each application runs in its own virtual machine. The details are complicated on how its done and ths is what we pay for when we buy an operating system, Microsoft handle the task assignment, switching and memory allocation, you need only use it knowing that the same address range is valid in each running process. (Some apps are bigger than others but in win32 they have a theoretical 4 gig address range and a 2 gig memory allocation range.)
Title: Re: How do pointers really work?
Post by: joemc on May 19, 2010, 02:41:35 AM
in addition to your responses, i also found http://wiki.osdev.org/Memory_management to be very helpful if anyone else doesn't know and every wonders whats going on.  or in more detail at http://wiki.osdev.org/Paging