introduction
This article briefly talks about how functions pass in parameters. We all know that when a function call uses a small number of parameters (less than or equal to 4 on ARM), the parameters are passed through registers (r0, r1, r2, r3 on ARM), and when there are more than 4 parameters, the extra parameters will be pushed into the stack for passing (in fact, the parameters passed by r0, r1, r2, r3 will also be pushed into the stack during the function call). Let's see how this is implemented.
Function Stack
First, we need to understand how the memory address space of a process in Linux is laid out. In Linux, the virtual address of 0~3G belongs to the process, and 3G~4G is used by the kernel. Each process has its own independent memory address space of 0~3G. When a process calls a function, we all know that the parameters passed to the called function are operated through the stack. Here we only need to briefly understand that the stack in the memory address space of Linux grows from top to bottom, that is, the bottom of the stack is at a high address and the top of the stack is at a low address.
OK, after a brief understanding of the stack in the memory address space, we also need to briefly understand the two registers EBP and ESP. EBP is used to save the low address of the stack, and ESP is used to save the top address of the stack. Each function call involves a stack frame.
Let's take an example to explain the characteristics of a function frame in detail, such as
1 /* B is called by A 2 * Parameters: data1, data2, data3 3 * Local variables: s1, s2, s3 */ 4 void B (int data1, int data2, int data3) 5 { 6 int b_s1; 7 int b_s2; 8 int b_s3; 9 } 10 11 /* A calls B function */ 12 void A (void) 13 { 14 int a_s1; 15 int a_s2; 16 int a_s3; 17 18 B (1, 2, 3); 19 printf ("1\n"); 20 }
In the above example, the stack frame is shown in the following figure
As can be seen from the legend, when function A does not call function B, the stack frame of function A only stores local variables, and EBP (stack bottom pointer) points to the function stack frame header of function A. When function A calls function B, function A will push the parameters required by function B from right to left into the stack (first push 3, then 2, and finally 1 in the example), and then push the first instruction that needs to be run after A calls B into the stack. At this time, a stack frame of B is established. The specific process is:
- Push the parameters required by function B onto the stack from right to left
- Push the address of the first instruction after executing function B
- Create B stack frame
- Push to the bottom of stack frame A
- Push into register protected by function B
- Push into the local variables of function B
summary
In fact, each processor architecture uses a different method. On ARM, the situations of several parameters and indefinite parameters are different when viewed through the assembly code. After deassembling and studying it thoroughly, I will publish a blog post specifically about this. Now, just treat this article as an introductory knowledge.
Previous article:ARM constant expressions
Next article:An attempt to develop a Hadoop server based on an ARM-based microcontroller
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- 16 Years of Taiwanese New Year
- 【Project Source Code】Digital Signal Processing Learning——Mixer
- Chapter 4: Use of Timers and PWM
- Ask an outrageous question, why do we need to use a resistor to form a discharge path for the capacitor to discharge?
- An annular solar eclipse is coming. Have those in the annular eclipse zone seen it?
- How to set differential traces for high-speed USB positive and negative in AD16 PCB
- 【Infineon XENSIV PAS CO2 sensor】Official data study
- Looking for a brushless gate driver design
- Voltage boost circuit and electrode detachment detection circuit
- HuaDa MCU M4 RTThread real-time operating system