当前位置:网站首页>In C language, the main function calls another function, which is understood by assembly code
In C language, the main function calls another function, which is understood by assembly code
2022-06-12 14:17:00 【kk_ forward】
Stack understanding
Because the main function is in the process of execution , You need to jump from the main function to the called function , This involves saving the current function state , Enter the operation of the called function . This state saving operation requires a stack .
The stack structure is as follows :
There are several points to note about the understanding of stack :
- The stack grows from high address to low address
- rsp It's a stack register , Inside is the address of the pointer at the top of the stack
- The stack top pointer is only used when the stack size needs to be expanded or reduced , Will move to the lower address , Otherwise, the stack entry and exit are operated according to the offset of the stack top pointer , The stack top pointer does not need to be moved .
Sample code
The main function call_proc.c
#include"proc.h"
long call_proc()
{
long x1 = 1; int x2 = 2;
short x3 = 3; char x4 = 4;
proc(x1, &x1, x2, &x2, x3, &x3, x4, &x4);
return (x1+x2)*(x3-x4);
}
Modulated function proc.c
void proc(long a1, long *a1p,
int a2, int *a2p,
short a3, short *a3p,
char a4, char *a4p)
{
*a1p += a1;
*a2p += a2;
*a3p += a3;
*a4p += a4;
}
Assembly code
Use execution gcc -Og -S xxx.c Generate the assembly code of both call_proc.s and proc.s as follows ( Only the key parts are listed ):
call_proc.s
.file "call_proc.c"
.text
.globl call_proc
.type call_proc, @function
call_proc:
.LFB0:
.cfi_startproc
endbr64
pushq %rbx
.cfi_def_cfa_offset 16
.cfi_offset 3, -16
subq $32, %rsp
.cfi_def_cfa_offset 48
movl $40, %ebx
movq %fs:(%rbx), %rax
movq %rax, 24(%rsp)
xorl %eax, %eax
movq $1, 16(%rsp)
movl $2, 12(%rsp)
movw $3, 10(%rsp)
movb $4, 9(%rsp)
leaq 12(%rsp), %rcx
leaq 16(%rsp), %rsi
leaq 9(%rsp), %rax
pushq %rax
.cfi_def_cfa_offset 56
pushq $4
.cfi_def_cfa_offset 64
leaq 26(%rsp), %r9
movl $3, %r8d
movl $2, %edx
movl $1, %edi
call [email protected]
movslq 28(%rsp), %rcx
addq 32(%rsp), %rcx
movswl 26(%rsp), %edx
movsbl 25(%rsp), %eax
subl %eax, %edx
movslq %edx, %rax
imulq %rcx, %rax
addq $16, %rsp
.cfi_def_cfa_offset 48
movq 24(%rsp), %rdi
xorq %fs:(%rbx), %rdi
jne .L4
addq $32, %rsp
.cfi_remember_state
.cfi_def_cfa_offset 16
popq %rbx
.cfi_def_cfa_offset 8
ret
proc.s
.file "proc.c"
.text
.globl proc
.type proc, @function
proc:
.LFB0:
.cfi_startproc
endbr64
movq 16(%rsp), %rax
addq %rdi, (%rsi)
addl %edx, (%rcx)
addw %r8w, (%r9)
movl 8(%rsp), %edx
addb %dl, (%rax)
ret
.cfi_endproc
Assembly code analysis
First analysis call_proc.s Assembly code in
Change the stack size
subq $32, %rsp // take rsp Reduce 32 Bytes , Because the growth direction of stack is from large address to small address , It is equivalent to increasing the capacity of the stack
Save local variable
// Stack local variables ,rsp The preceding number is the offset of the stack top pointer
movq $1, 16(%rsp)
movl $2, 12(%rsp)
movw $3, 10(%rsp)
movb $4, 9(%rsp)
Finish the above operation , The schematic diagram of the stack is as follows :
Save the formal parameters of the called function
Because the called function has 8 Parameters , But at most 6 Formal parameters can be saved in registers , So two are saved on the stack , And are the last two values of the formal parameter .
// Save formal parameters in registers
leaq 12(%rsp), %rcx //load effective adress, Used to directly assign a memory address to the destination operand
leaq 16(%rsp), %rsi
// Save the formal parameters to the stack ,
leaq 9(%rsp), %rax
pushq %rax // Stack the number in the register
pushq $4 // Stack the immediate number
// Save formal parameters in registers
leaq 26(%rsp), %r9
movl $3, %r8d
movl $2, %edx
movl $1, %edi
The corresponding relationship between parameters and formal parameters is shown in the following figure :
Call the called function
call [email protected]
Before calling the called function , Will stack the next execution instruction of the calling code , It is convenient to return to the main function from the called function , It can be executed further .
return
... // A series of logical operations
ret
Called function assembly code
Logical operations
movq 16(%rsp), %rax
// Take the operand directly from the register
addq %rdi, (%rsi)
addl %edx, (%rcx)
addw %r8w, (%r9)
// Take the operand from the stack
movl 8(%rsp), %edx
addb %dl, (%rax)
return
ret
summary
- Nested function calls , You need a stack to hold local variables 、 Shape parameter 、 The instruction address at the time of return
- C Language parameters are pushed onto the stack from right to left
Reference resources
C The underlying implementation of language function calls
In assembly language mov and lea What are the differences ?
边栏推荐
- 拆改廣告機---業餘解壓
- Getting started alicloud haas510 open board DTU (version 2.0) --510-as
- Alibaba cloud development board haas510 parses serial port JSON data and sends attributes
- Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform
- English learning plan
- SystemC time
- SystemC common errors
- Running phase of SystemC
- [early knowledge of activities] list of recent activities of livevideostack
- 2022版Redis数据删除策略
猜你喜欢

Mémoire de l'examen d'entrée à l'université

Leetcode 2185. 统计包含给定前缀的字符串

公司运营中更注重转化的出价策略,如何实现? —Google sem

程序分析与优化 - 6 循环优化

Postgresql14 installation and use tutorial

Player practice 26 adding slider and window maximization

阿裏雲開發板HaaS510報送設備屬性

阿里云开发板HaaS510解析串口JSON数据并发送属性

Alibaba cloud development board haas510 responds to UART serial port instructions

Compile and install lamp architecture of WordPress and discuz for multi virtual hosts based on fastcgi mode
随机推荐
OAuth2学习中的一些高频问题的QA
After reading the question, you will point to offer 16 Integer power of numeric value
Tlm/systemc: TLM socket binding problem
Leetcode 2185. 统计包含给定前缀的字符串
Single bus temperature sensor 18B20 data on cloud (Alibaba cloud)
IAT hook hijacking process API call
动态搜索广告智能查找匹配关键字
Crack WinRAR to ad pop-up window
How to set, reset and reverse bit
Go language functions as parameters of functions
基于Profibus-DP协议的PLC智能从站设计
Introduction to functions (inline functions and function overloading)
Player practice 18 xresample
Use of pytorch (to be supplemented)
PostgreSQL14安装使用教程
Is Shell Scripting really a big technology?
Axi4 increase burst / wrap burst/ fix burst and narrow transfer
Dial up and Ethernet
2000. reverse word prefix
SystemC simulation scheduling mechanism