Discover the memory address where local variable `arg2` is stored in assembly using a step-by-step analysis of the code.
---
This video is based on the question https://stackoverflow.com/q/77589043/ asked by the user 'kornbot' ( https://stackoverflow.com/u/21795123/ ) and on the answer https://stackoverflow.com/a/77589072/ provided by the user 'harold' ( https://stackoverflow.com/u/555045/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, comments, revision history etc. For example, the original title of the Question was: At what memory address is local variable arg2 stored?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding Local Variable Memory Addresses in Assembly Language
When programming in C and delving into assembly, a common question that arises is the memory address of local variables once the function is called. In this guide, we will tackle the specific question: At what memory address is the local variable arg2 stored? Let's take a closer look at the code provided to understand how memory works within a function call.
The Code at Hand
To solve this problem, we have both C code and assembly code.
C Code
[[See Video to Reveal this Text or Code Snippet]]
Assembly Code
[[See Video to Reveal this Text or Code Snippet]]
Finding the Memory Address of arg2
In the assembly code, the relevant lines for determining where arg2 is stored are:
movl $1057, -8(%ebp) indicates that arg2 is stored at an offset of -8 from the base pointer, originally set by movl %esp, %ebp.
Step-by-Step Analysis
Stack Frame Setup:
The line movl %esp, %ebp updates the base pointer (%ebp) to the current stack pointer (%esp).
With a stack frame size adjustment via subl $24, it creates space for local variables.
Variable Allocation:
arg1 is stored at -4(%ebp), equating to address 0x800038.
arg2 is stored at -8(%ebp), which corresponds to address 0x800034.
Passing Arguments:
The subsequent assembly instructions prepare addresses for parameters to the swap_add function, with arg2's address being stored in the stack at 0x800028. However, this is merely a placeholder for the argument in the function call and does not denote the actual storage location of arg2.
Key Takeaway
The critical point to understand is that the address of arg2 is determined by the assembly instruction movl $1057, -8(%ebp), which confirms that arg2 is indeed located at 0x800034.
When the question shifts to where &arg2 (the address of arg2) is stored for function calls, that is represented by the entry at 0x800028 in our stack frame. Therefore, 0x800028 is where the address of arg2 is intended to be used, but its actual storage address is 0x800034.
Conclusion
In conclusion, understanding local variable memory addresses in assembly can be overwhelming at first, but with a systematic approach, we can clarify where each variable is stored. To reiterate, arg2 is stored at the memory address 0x800034, while its effective address is used at 0x800028 during function calls. This distinction is pivotal in ensuring you grasp how variables are managed in memory within assembly language. If you keep practicing, concepts like these will become second nature!
Информация по комментариям в разработке