Answer the question
In order to leave comments, you need to log in
How does the low-level code that uses the stack know where it starts from?
When the operating system starts a process, it allocates stack space for it, but how does low-level code know the starting address of the stack?
If we take this scheme as a basis, it becomes clear how the process code can find out the addresses of text, initialized data, uninitialized data, and the heap (just add the amount of memory allocated using system calls to the end of the BSS). However, it is not clear how the stack address and command line arguments are known.
If you think a little about the brains, then the most logical answer to this question is that when the process starts, the OS itself sets up (I think that in this example it doesn’t matter which OS, but I’m pushing away from the linux example) the stack registers to the desired values, but I have no experience assembly language programming (only theory), and all the sources that I read do not explicitly indicate this, so I cannot be sure of this assumption. Well, if you follow this assumption, then the addresses of the command line arguments can be found by counting them from the stack address.
If all my guesses are correct, then I would like to get an affirmative answer from people who know this for sure, but if another method is used, then I would like to get a detailed description of it.
Answer the question
In order to leave comments, you need to log in
If we take this scheme as a basis, it becomes clear how the process code can find out the addresses of text, initialized data, uninitialized data
It is strange that you have a question about the stack, not just how the process starts its work exactly from the right address, and not from where? A lot of these settings are stored in the headers of executable files: both the stack size and the address of the entry point, and which libraries and where to load them. And all these preparations are handled by the bootloader in the OS.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question