Calculation of access to an object and structure and calculation of address offset

I am writing a simple virtual machine, and I have a question about using access to an object and structure.

Since the starting address of the program is arbitrary for each run, and then the address of each of its objects is also arbitrary.

Thus, the only way I can access an object or its member object is to access the offset from the "base" pointer, which means that there is an arithmetic operation necessary to access something in the program structure.

My question is whether this is done in professional compilers, because obviously this approach adds some overhead at runtime, and I myself can’t think about unloading this process from the runtime due to lack of warranty memory allocation consistency and its address?

+2
source share
1 answer

Most computers for many decades provide addressing modes that allow you to specify the address as a combination of base and offset, and the actual calculation is performed on hardware without additional costs in the processor clock cycles.

Later (last several decades) computers offer hardware for virtualization of the memory layout, which means that even with the physical address of the element in each run, they are different, its address in the virtual address space remains the same. Again, there is no additional cost to using the base address, since the calculations are performed implicitly and invisibly to the binary executable code of the program.

+2
source

Source: https://habr.com/ru/post/920534/


All Articles