Talk about the function stack under arm

Publisher:雅逸之风Latest update time:2016-05-09 Source: eefocusKeywords:arm Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Talk about the function stack under arm

Talk about the function stack under arm

 

  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.

Talk about the function stack under arm

 

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

Talk about the function stack under arm

 

  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.

Keywords:arm Reference address:Talk about the function stack under arm

Previous article:ARM constant expressions
Next article:An attempt to develop a Hadoop server based on an ARM-based microcontroller

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号