当前位置:网站首页>Assembly: stack frame analysis of complete function flow
Assembly: stack frame analysis of complete function flow
2022-06-10 03:48:00 【Melon seeds 300g】
Stack frame (Stack Frame Layout): The specific process of function call , Is a function execution environment , It includes parameters 、 local variable 、 Return address and other information .
List of articles
1、 Assembly code of the complete function call process
assume cs:code, ds:data, ss:stack
; ------- Stack segment -------
stack segment
db 100 dup(0)
stack ends
; ------- Data segment -------
data segment
db 100 dup(0)
data ends
; ------- Code segment -------
code segment
start:
; ------- Manual settings ds、ss Value
mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
mov si, 1
mov di, 2
mov bx, 3
mov bp, 4
; ------- Business logic
push 1
push 2
call sum
add sp, 4 ; Stack elimination
; ------- sign out
mov ax, 4c00h
int 21h
; ------- function
; Return value ax register
; Pass on 2 Parameters ( Put in stack )
sum:
; Protect bp
push bp
; preservation sp Previous value : Point to bp Previous value
mov bp, sp
; reserve 10 Bytes of space for local variables
sub sp, 10
; Protect registers that may be used
push si
push di
push bx
; Fill the local variable space int 3(CCCC): Breakpoint interrupt instruction
; stosw The role of : take ax Copy the value of to es:di in , meanwhile di It's worth it +2
mov ax, 0cccch
; Give Way es be equal to ss
mov bx, ss
mov es, bx
; Give Way di be equal to bp-10( The area with the smallest local variable address )
mov di, bp
sub di, 10
; cx To determine the stosw Number of executions
mov cx, 5
rep stosw
; rep The role of : Repeat an instruction ( The number of executions is determined by cx decision )
; -------- Business logic - begin
; Definition 2 Local variables
mov word ptr ss:[bp-2], 3
mov word ptr ss:[bp-4], 4
mov ax, ss:[bp-2]
add ax, ss:[bp-4]
mov ss:[bp-6], ax
; Accessing parameters in the stack
mov ax, ss:[bp+4]
add ax, ss:[bp+6]
add ax, ss:[bp-6]
; -------- Business logic - end
; Recover the value of the register
pop bx
pop di
pop si
; recovery sp
mov sp, bp
; recovery bp
pop bp
ret
code ends
end start
2、 Document description of the complete function call process
1、push Parameters
2、push The return address of the function
3、push bp ( Retain bp Previous value , Convenient for later recovery )
4、mov bp, sp ( Retain sp Previous value , Convenient for later recovery )
5、sub sp, The size ( Allocate space to local variables )
6、 Protect possible registers
7、 Use CC(int 3) Fill the space of local variables8.-------- Execute business logic --------
9、 Restore the value before the register
10、mov sp, bp ( recovery sp Previous value )
11、pop bp ( recovery bp Previous value )
12、ret ( Take the return address of the function out of the stack , Execute the next command )
13、 Restore stack balance (add sp, Space occupied by parameters )
3、 The function completely calls the gif chart

4、 Enter the sub function stack frame to execute the logic

5、 The subfunction is released from the processing logic

边栏推荐
- C#封装FluentValidation,用了之后通篇还是AbstractValidator,真的看不下去了
- Informatics Aosai yibentong 1260 [example 9.4] interceptor missile (noip1999) | Luogu p1020 [noip1999 popularization group] missile interception
- 反欺诈体系与设备指纹
- [mysql] database - View
- JVM内存结构分析(通俗易懂)
- Keywords such as do while for
- 戒烟日志_04 (day_09)
- 在MATLAB中使用tensorflow
- [pytorch pre training model modification, addition and deletion of specific layers]
- Storage concept of tree
猜你喜欢

【PyTorch预训练模型修改、增删特定层】
![[L1, L2 and smooth L1 loss functions]](/img/c6/27eab1175766b77d4f030b691670c0.png)
[L1, L2 and smooth L1 loss functions]

机器学习 && 内容安全 && 海外风控公司

vulnhub之doubletrouble: 1

【主流Nivida显卡深度学习/强化学习/AI算力汇总】

Monotone queue optimization DP example

Basic data types and sizeof understanding

135. distribute candy
![[calculation method]](/img/59/7488d25f72ffa642de76d9cf743196.png)
[calculation method]
![[mainstream nivida graphics card deep learning / intensive learning /ai computing power summary]](/img/1a/dd7453bc5afc6458334ea08aed7998.png)
[mainstream nivida graphics card deep learning / intensive learning /ai computing power summary]
随机推荐
【比特熊故事汇】X Microsoft Build 2022——微软专家+MVP,技术亮点全解析
Using tensorflow in MATLAB
SSTI(模板注入) ——(8)
Keywords register and static
A general es aggregation query method
Qt窗口、视口、逻辑坐标、物理坐标
QT window, viewport, logical coordinates, physical coordinates
【PyTorch模型剪枝实例教程3(多参数与全局剪枝)】
ACL 2022 | NLP领域最新热门研究,你一定不能错过!
How to open an account for stocks? Is it safe to open an account online?
RPC practice and core principles - Advanced notes
在MATLAB中使用tensorflow
【论文笔记|深读】struc2vec: Learning Node Representations from Structural Identity
Implementation scheme of shared file
JVM memory structure analysis (easy to understand)
C language question brushing series (II)
[pytorch model pruning example tutorial 2 (structured pruning)]
Redis 核心技术与实战-实践篇读书笔记 20~终结
Business card wechat applet error version 2
汇编:关于函数完整流程的栈桢解析