15

I read that , "When a program executes an instruction like : MOV REG,1000 , it does so to copy the contents of the memory address 1000 to REG. Address can be generated using indexing,base registers,segment registers and other ways.

These program generated address are called virtual address and form the virtual address space."

Can anyone please explain me,what does it (These program generated address are called virtual address) mean ?

cHao
  • 84,970
  • 20
  • 145
  • 172
program-o-steve
  • 2,630
  • 15
  • 48
  • 67

2 Answers2

39

Programs and data are stored as numbers in memory cells. Each memory cell has a unique number, called its address. The range of numbers representing valid addresses is called address space.

When programs run, the CPU reads data from memory and writes results back to memory. CPU communicates the desired location to the memory by specifying the address of the memory cell targeted by a read or a write operation.

There are multiple ways in which the CPU can come up with an address (remember, address is only a number). The number representing the address could be in a register, it could be stored at another memory location, it could be calculated by adding or subtracting an offset to a register, and so on. In all cases your compiled program instructs CPU on how to come up with (or generate) the address it needs to read or write.

Modern architectures let multiple programs execute as if they own the entire logical address space. In other words, several programs could write to memory location at the same address without stepping over each others' results. This is done by virtualizing the address space: let's say programs A and B generate a write to memory location at 0x1000. The CPU, aided by the operating system, could performs additional adjustments to the address, and map it to physical address 0x60001000 for program A, and to 0x5F001000 for program B. Both programs think that they wrote to the location at 0x1000, because they operate in a virtual address space. Their model of the memory is a contiguous block starting at 0 and continuing to 0x000100000000 (assuming that your system has 4GiB of memory available to processes). But this model works only because the CPU additionally translates their logical addresses to physical addresses, which are allocated and taken away as needed in the process of running the program.

Because the same number representing an address means different things to a program and to a CPU, the address space of the program is called virtual, and the address space of the CPU is called physical.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • In your example 0x1000 is a program generated address ? – program-o-steve Feb 23 '12 at 16:13
  • @program-o-steve Yes, it is a program generated address. Note that it does not matter *how* the program generated that address: it could be an address of a static variable, an element 0 of an array at the address 0x1000, an element 32 of an array at the address 0x0FE0, and so on. – Sergey Kalinichenko Feb 23 '12 at 16:19
  • but the statement in my question says the program generated address to be a virtual address. 0x1000 doesn't seem to be a virtual address – program-o-steve Feb 23 '12 at 16:26
  • 4
    @program-o-steve Of course it is virtual: it is 0x1000 only to the program. The physical (hardware) address of the virtual 0x1000 cell in my example is 0x60001000 for program A, and 0x5F001000 for program B. Your program *thinks* that it is writing to 0x1000, but the hardware writes to an entirely different address. – Sergey Kalinichenko Feb 23 '12 at 16:36
  • @dasblinkenlight "There are multiple ways in which the CPU can come up with an address". Isn't it the responsibility of the address bus to designate an address so that the data bus can read and write to that address? – JohnMerlino May 28 '13 at 04:07
  • @JohnMerlino Memory bus is a passive unit controlled by the CPU (in some architectures, the address bus can be controlled by other active devices that need to gain access to memory, such as peripheral I/O devices with direct memory access, but the CPU designates the times when these other devices can take control of the address bus). CPU decides which address it wants, puts it out onto the address bus, and waits for the memory to give it back the data at that address. – Sergey Kalinichenko May 28 '13 at 09:59
  • @dasblinkenlight Sorry it's a bit late. I understood what you said but then again what is the utility of using the virtual address space? Why can't program A directly say "write to 0x60001000" and program B directly say "write to 0x5F001000"? – HelloWorld123456789 Jul 10 '14 at 15:27
  • 1
    @RikayanBandyopadhyay Because the `0x60001000` and `0x5F001000` addresses are determined by the OS when it loads the executable, not something that can be possibly compiled into the program. – Sergey Kalinichenko Jul 10 '14 at 15:31
  • But aren't the addresses relocatable and are adjusted by the loader before executing? – HelloWorld123456789 Jul 10 '14 at 15:36
  • @RikayanBandyopadhyay On systems with no virtual memory support, yes. Besides, not all addresses can be corrected by the loader - for example, in situations when an address is computed dynamically, the loader may have no visibility into data manipulation to recognize that the result would be used as an address. – Sergey Kalinichenko Jul 10 '14 at 15:48
  • Thanks, it helped! These types of answers (and questions) should be upvoted more. – HelloWorld123456789 Jul 10 '14 at 16:26
  • I know it's an old answer. But I want to just ask if size of virtual memory is limited by size of secondary storage and size of virtual address space is same as RAM . In 2nd question I mean If main memory has 512 addresses and if there are 2 processes, then will both processes think that they have 512 addresses? – Zephyr Aug 27 '17 at 18:11
1

When a program accesses memory, it does not know or care where the physical memory backing the address is stored. It knows it is up to the operating system and hardware to work together to map locate the right physical address and thus provide access to the data it wants. Thus we term the address a program is using to access memory a virtual address. A virtual address consists of two parts; the page and an offset into that page.

Saikiran
  • 140
  • 14