0

Generally, there are 2 phases of the program - compiling and running.

Compile-time - compiler generates assembly of source program something like this -

MOV R1 R2

LOAD R2 1000

Run Time - program gets loaded to memory and execution starts.

Question: - How does the compiler know memory location such as 1000 in the above load instruction even though the program gets loaded in memory after the compilation phase?

  • Look into virtual memory. And understanding of the GDT and LDT wouldn't hurt either. Using a segment descriptor means a block of memory can be anywhere in physical memory space, yet the program will always be loaded to a specific point within said block.. – enhzflep Jul 25 '21 at 04:56
  • Can u post some gud resources which I can refer to for this. – Vasu_BHATT Jul 25 '21 at 05:04
  • 2
    another thing you can look up for programs like shared libraries is "relocation tables" where the executable code loader from the operating system replaces the addresses when loading the program into memory. – PeterT Jul 25 '21 at 05:28
  • 2
    @enhzflep: GDT and LDT are basically irrelevant in a flat memory model (which basically everything uses). Moreover, the code in the question clearly isn't x86 anyway; note the register names like R1, not RAX or EAX. – Peter Cordes Jul 25 '21 at 05:38
  • Despite this not being about x86, it's almost the same question as another one asked today, also about static addresses (for things in the `.data` section) and how those compile-time / link-time constant addresses can work at load time. If that's not what you were trying to ask, please clarify how it's different from the duplicate I picked ([.data section in x86 assembly](https://stackoverflow.com/q/68515099)) – Peter Cordes Jul 25 '21 at 05:44
  • @PeterCordes - cheers. I did gloss over the register names. :face-palm: – enhzflep Jul 25 '21 at 08:07
  • @PeterCordes Can u please provide me some gud resources to clear these topics, I mean is there any video or something to make this clear. – Vasu_BHATT Jul 25 '21 at 11:57
  • See the list of duplicate Q&As at the top of this page, especially [this answer](https://stackoverflow.com/a/33690144/224132) which has some practical examples of how a symbol name in asm turns into an absolute address in the executable. Note that compilers (and hand-written asm) *don't* use numeric addresses; that's for the linker to sort out, on normal systems. – Peter Cordes Jul 25 '21 at 18:33
  • Playing around with `readelf -a` / `objdump -drwC -Mintel` on a `.o` vs. a linked executable is a good way to see how the tools work. I don't have any videos to suggest; I learned this from experience with asm in general, and compilers/assemblers/linkers, and from the concept of position-independent code and how `gcc -fPIE` / `-fPIC` makes different asm. – Peter Cordes Jul 25 '21 at 20:35

0 Answers0