Repository navigation
Expand file tree
/
Copy pathassembly-tutorial.html
More file actions
384 lines (362 loc) · 64.8 KB
/
Copy pathassembly-tutorial.html
File metadata and controls
384 lines (362 loc) · 64.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>汇编语言教程 · 会 C 语言就会汇编</title>
<style>
:root {
--bg: #f8f9fa;
--bg2: #ffffff;
--bg3: #f0f2f5;
--border: #e1e4e8;
--text: #24292e;
--text2: #586069;
--accent: #0366d6;
--accent2: #0550ae;
--green: #22863a;
--orange: #d18616;
--purple: #6f42c1;
--red: #cf222e;
--cyan: #1b7c83;
--shadow: rgba(0,0,0,.06);
--sidebar-w: 280px;
}
* { margin:0; padding:0; box-sizing:border-box; }
html { scroll-behavior:smooth; scroll-padding-top:24px; }
body {
font-family:'Noto Sans SC',-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;
background:var(--bg); color:var(--text); line-height:1.8;
display:flex; min-height:100vh;
}
::-webkit-scrollbar { width:8px; }
::-webkit-scrollbar-track { background:var(--bg); }
::-webkit-scrollbar-thumb { background:#c1c7cd; border-radius:4px; }
::-webkit-scrollbar-thumb:hover { background:#a0a7af; }
.sidebar {
position:fixed; top:0; left:0; width:var(--sidebar-w); height:100vh;
background:var(--bg2); border-right:1px solid var(--border);
overflow-y:auto; z-index:100; padding:24px 0;
box-shadow:1px 0 3px var(--shadow);
}
.sidebar::-webkit-scrollbar { width:4px; }
.sidebar-logo {
text-align:center; padding:0 20px 20px; border-bottom:1px solid var(--border); margin-bottom:8px;
}
.sidebar-logo h1 { font-size:20px; font-weight:700; color:var(--accent); }
.sidebar-logo p { font-size:12px; color:var(--text2); margin-top:2px; }
.sidebar nav { padding:0 12px; }
.sidebar nav a {
display:block; padding:7px 14px; margin:2px 0;
color:var(--text2); text-decoration:none; font-size:14px;
border-radius:6px; transition:.2s;
}
.sidebar nav a:hover, .sidebar nav a.active { color:var(--accent); background:var(--bg3); }
.sidebar nav .sec { padding-left:28px; font-size:13px; }
.sidebar nav .sec2 { padding-left:42px; font-size:12.5px; }
.sidebar nav .sec2:hover { color:var(--cyan); }
.sidebar nav .label { font-size:11px; color:var(--text2); padding:12px 14px 4px; font-weight:500; letter-spacing:1px; text-transform:uppercase; }
.main { margin-left:var(--sidebar-w); flex:1; max-width:100%; }
.content {
max-width:880px; margin:0 auto; padding:48px 48px 120px;
}
h1 { font-size:30px; font-weight:700; margin:44px 0 14px; padding-bottom:10px; border-bottom:2px solid var(--accent); color:var(--accent2); }
h1:first-of-type { margin-top:0; }
h2 { font-size:22px; font-weight:600; margin:32px 0 10px; color:var(--accent); }
h3 { font-size:17px; font-weight:600; margin:22px 0 8px; color:var(--text); }
h4 { font-size:15px; font-weight:600; margin:14px 0 6px; color:var(--orange); }
p { margin:8px 0; }
ul, ol { margin:6px 0 6px 20px; }
li { margin:3px 0; }
strong { color:var(--text); }
code {
font-family:'JetBrains Mono','Fira Code',monospace; font-size:13.5px;
background:#eff1f3; color:#d73a49; padding:2px 7px; border-radius:4px;
}
pre { margin:12px 0; border-radius:8px; overflow:hidden; position:relative; border:1px solid var(--border); }
pre code { background:none !important; color:inherit; padding:0; font-size:13px; }
.hljs { background:var(--bg3) !important; padding:16px 20px !important; }
blockquote {
border-left:4px solid var(--accent); background:var(--bg3);
padding:10px 16px; margin:12px 0; border-radius:0 6px 6px 0; color:var(--text2);
}
table { width:100%; border-collapse:collapse; margin:12px 0; font-size:14px; border-radius:6px; overflow:hidden; }
th, td { padding:8px 14px; border:1px solid var(--border); text-align:left; }
th { background:var(--bg3); color:var(--accent); font-weight:600; }
td { background:var(--bg2); }
hr { border:none; border-top:1px solid var(--border); margin:32px 0; }
.info { background:#ddf4ff; border:1px solid #b8e1ff; border-radius:8px; padding:12px 16px; margin:12px 0; color:var(--text); }
.warn { background:#fff8c5; border:1px solid #f0d87e; border-radius:8px; padding:12px 16px; margin:12px 0; color:var(--text); }
.tip { background:#dcffe4; border:1px solid #b4e5c4; border-radius:8px; padding:12px 16px; margin:12px 0; color:var(--text); }
.dng { color:var(--red); font-size:14px; }
.diagram {
background:var(--bg2); border:1px solid var(--border); border-radius:10px;
padding:20px; margin:16px 0; overflow-x:auto; text-align:center;
box-shadow:0 1px 4px var(--shadow);
}
.diagram svg { max-width:100%; height:auto; }
.diagram figcaption { font-size:13px; color:var(--text2); margin-top:8px; font-style:italic; }
@media(max-width:768px) {
.sidebar { display:none; }
.main { margin-left:0; }
.content { padding:24px 16px 80px; }
}
.hero {
text-align:center; padding:48px 0 36px;
}
.hero h1 { font-size:36px; border:none; margin:0; padding:0; }
.hero p { color:var(--text2); font-size:15px; margin-top:8px; }
.hero .badge {
display:inline-block; background:var(--bg2); border:1px solid var(--border);
padding:3px 14px; border-radius:20px; font-size:12px; color:var(--text2); margin:3px;
transition:.2s;
}
.hero .badge:hover { border-color:var(--accent); color:var(--accent); }
.toc { background:var(--bg2); border:1px solid var(--border); border-radius:10px; padding:16px 24px; margin:24px 0; box-shadow:0 1px 4px var(--shadow); }
.toc h3 { margin:0 0 10px; color:var(--accent); font-size:16px; }
.toc a { color:var(--text2); text-decoration:none; font-size:14px; display:block; padding:2px 0; }
.toc a:hover { color:var(--accent); }
.toc .l2 { padding-left:20px; font-size:13px; }
.toc .l3 { padding-left:40px; font-size:12.5px; }
.author-note {
background:linear-gradient(135deg,#ddf4ff,#e8f5e9); border:1px solid #b8e1ff;
border-radius:10px; padding:20px 24px; margin:24px 0;
}
.author-note strong { color:var(--accent); }
</style><style>pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}/*!
Theme: GitHub Dark
Description: Dark theme as seen on github.com
Author: github.com
Maintainer: @Hirse
Updated: 2021-05-15
Outdated base version: https://github.com/primer/github-syntax-dark
Current colors taken from GitHub's CSS
*/.hljs{color:#c9d1d9;background:#0d1117}.hljs-doctag,.hljs-keyword,.hljs-meta .hljs-keyword,.hljs-template-tag,.hljs-template-variable,.hljs-type,.hljs-variable.language_{color:#ff7b72}.hljs-title,.hljs-title.class_,.hljs-title.class_.inherited__,.hljs-title.function_{color:#d2a8ff}.hljs-attr,.hljs-attribute,.hljs-literal,.hljs-meta,.hljs-number,.hljs-operator,.hljs-selector-attr,.hljs-selector-class,.hljs-selector-id,.hljs-variable{color:#79c0ff}.hljs-meta .hljs-string,.hljs-regexp,.hljs-string{color:#a5d6ff}.hljs-built_in,.hljs-symbol{color:#ffa657}.hljs-code,.hljs-comment,.hljs-formula{color:#8b949e}.hljs-name,.hljs-quote,.hljs-selector-pseudo,.hljs-selector-tag{color:#7ee787}.hljs-subst{color:#c9d1d9}.hljs-section{color:#1f6feb;font-weight:700}.hljs-bullet{color:#f2cc60}.hljs-emphasis{color:#c9d1d9;font-style:italic}.hljs-strong{color:#c9d1d9;font-weight:700}.hljs-addition{color:#aff5b4;background-color:#033a16}.hljs-deletion{color:#ffdcd7;background-color:#67060c}</style>
<link rel="stylesheet" href="visual-system.css?v=20261007-2">
<link rel="stylesheet" href="tutorial-visual.css?v=20260930-2">
<link rel="stylesheet" href="type-system.css?v=20261005-3">
<link rel="stylesheet" href="responsive.css?v=20261007-1">
</head>
<body class="page-tutorial">
<aside class="sidebar">
<div class="sidebar-logo">
<h1>ASM Teach</h1>
<p>汇编语言入门</p>
</div>
<nav>
<div class="label">章节</div>
<a href="#1" class="active">一、寄存器</a>
<a href="#2" class="sec">1. 数据寄存器</a>
<a href="#3" class="sec">2. 栈寄存器</a>
<a href="#4" class="sec">3. 程序指针</a>
<a href="#5" class="sec">二、常见指令</a>
<a href="#6" class="sec2">1. MOV 赋值</a>
<a href="#7" class="sec2">2. LEA 取地址</a>
<a href="#8" class="sec2">3. 算术逻辑</a>
<a href="#9" class="sec">三、栈与函数调用</a>
<a href="#10" class="sec2">3.1 最简函数栈帧</a>
<a href="#12" class="sec2">3.2 带参数调用</a>
<a href="#15" class="sec2">3.3 函数标准头尾</a>
<a href="#16" class="sec">四、条件判断</a>
<a href="#17" class="sec2">4.1 CMP 比较</a>
<a href="#18" class="sec2">4.2 条件跳转</a>
<a href="#19" class="sec2">4.3 switch 跳转表</a>
<a href="#20" class="sec">五、汇编代码骨架</a>
<a href="#21" class="sec">六、栈帧与 PWN</a>
</nav>
</aside>
<div class="main">
<div class="content">
<div class="hero">
<h1>汇编语言教程</h1>
<p>会 C 语言就会汇编 — 变量、栈、条件跳转</p>
<div>
<span class="badge">寄存器</span>
<span class="badge">栈帧</span>
<span class="badge">CMP</span>
<span class="badge">条件跳转</span>
<span class="badge">函数调用</span>
</div>
</div>
<div class="author-note">
<strong>写在前面</strong>:本文以 32 位汇编为主,也会混着讲点 64 位(原理一样,只是名字前面多了个 R,如 EAX → RAX)。如果你学过 C 语言,知道指针、变量、函数调用、if-else、switch,那你完全具备了看懂汇编的底色。
</div>
<div class="toc">
<h3>目录</h3>
<a href="#1">一、寄存器:8 个预定义的"变量"</a>
<a href="#2" class="l2">1. 数据寄存器</a>
<a href="#3" class="l2">2. 栈相关寄存器</a>
<a href="#4" class="l2">3. 程序指针</a>
<a href="#5">二、最常见的指令</a>
<a href="#6" class="l2">1. MOV 赋值</a>
<a href="#7" class="l2">2. LEA 取地址</a>
<a href="#8" class="l2">3. 算术逻辑</a>
<a href="#9">三、栈与函数调用</a>
<a href="#10" class="l2">3.1 最简函数栈帧</a>
<a href="#12" class="l2">3.2 带参数调用</a>
<a href="#15" class="l2">3.3 函数标准头尾</a>
<a href="#16">四、条件判断</a>
<a href="#17" class="l2">4.1 CMP 比较</a>
<a href="#18" class="l2">4.2 条件跳转</a>
<a href="#19" class="l2">4.3 switch 跳转表</a>
<a href="#20">五、汇编代码骨架</a>
<a href="#21">六、栈帧与 PWN</a>
</div>
<section class="container mx-auto"><h1 class="h1" data-heading="true" id="0" _msttexthash="246609922" _msthash="42546">逆向与二进制基础|汇编:变量、栈、条件跳转,会 C 语言就会汇编</h1><blockquote class="blockquote"><p class="p"><font _mstmutation="1" _msttexthash="569831899" _msthash="42547">学过 C 语言再来看汇编,把寄存器当变量,栈还是你熟悉的那个栈, 比较、条件跳转选分支——入门真的很简单。</font><code class="codespan">cmp</code></p></blockquote><p class="p" _msttexthash="385442018" _msthash="42548">很多人一听到“汇编语言”,脑海里浮现的就是满屏的十六进制,还没开始学腿就先软了。</p><p class="p" _msttexthash="326091493" _msthash="42549">不过先别慌。 <strong class="strong" _istranslated="1">汇编不是逆向专家才能看的,是“懂一点点代码的人”都可以随便看的。 </strong></p><p class="p"><font _mstmutation="1" _msttexthash="1356362137" _msthash="42550">如果你学过 C 语言,知道指针、变量、函数调用、、,那你完全具备了看懂汇编的底色。 今天我们不管 CPU 怎么造出来的,不管内存颗粒的物理结构,甚至不用深究寄存器的硬件原理。</font><code class="codespan">if-else</code><code class="codespan">switch</code></p><p class="p"><strong class="strong" _msttexthash="30867135" _msthash="42551">我们只做三件事:</strong></p><ol class="ol"><li class="listitem" _msttexthash="37073023" _msthash="42552">1. 把寄存器当变量用。</li><li class="listitem" _msttexthash="229209370" _msthash="42553">2. 把栈直接当成你 C 语言里那个栈,看它在汇编里怎么长出来的。</li><li class="listitem"><font _mstmutation="1" _msttexthash="145679534" _msthash="42554">3. 把 当成 的条件比较,把条件跳转当成分支选择。</font><code class="codespan">cmp</code><code class="codespan">if</code></li></ol><blockquote class="blockquote"><p class="p"><font _mstmutation="1" _msttexthash="384855185" _msthash="42555">本文以 32 位汇编为主,也会混着讲点 64 位(原理一样,只是名字前面多了个 ,如 → )。</font><code class="codespan">R</code><code class="codespan">EAX</code><code class="codespan">RAX</code></p></blockquote><hr class="hr hr-dash"><h2 class="h2" data-heading="true" id="1" _msttexthash="70058066" _msthash="42556">一、寄存器:8 个预定义的“变量”</h2><p class="p" _msttexthash="65949299" _msthash="42557">在 C 语言里,你定义变量:</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-c"><span class="hljs-type">int</span> a =<span class="hljs-number"> 10</span>;<span class="hljs-type"><br>int</span> *p = &a;</code></pre><p class="p" _msttexthash="192956478" _msthash="42558">在汇编里,没法随便新建变量,CPU 只给你<strong class="strong">有限的几个“变量”</strong></p><p class="p" _msttexthash="119159105" _msthash="42559">我们把常用寄存器分成三组,这样方便理解。</p><h3 class="h3" data-heading="true" id="2" _msttexthash="108029883" _msthash="42560">1. 数据寄存器(存值、存地址、存指针)</h3><p class="p"><code class="codespan">EAX</code> <code class="codespan">EBX</code> <code class="codespan">ECX</code> <code class="codespan">EDX</code> <code class="codespan">ESI</code> <code class="codespan">EDI</code></p><p class="p"><strong class="strong" _msttexthash="49828506" _msthash="42561">你就当它们是六个全局变量。</strong></p><ul class="ul"><li class="listitem"><font _mstmutation="1" _msttexthash="48237254" _msthash="42562">• 经常兼职“返回值”也就是return</font><code class="codespan">EAX</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="32428019" _msthash="42563">• 经常兼职“计数器”</font><code class="codespan">ECX</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="51495210" _msthash="42564">• / 常用于字符串或数组操作</font><code class="codespan">ESI</code><code class="codespan">EDI</code></li></ul><h3 class="h3" data-heading="true" id="3" _msttexthash="109491642" _msthash="42565">2. 栈相关寄存器(管函数调用和栈帧)</h3><p class="p"><code class="codespan">ESP</code> <code class="codespan">EBP</code></p><p class="p" _msttexthash="237797547" _msthash="42566">由于栈的重要性,后面会用一整节来细讲,现在只需要记住:</p><ul class="ul"><li class="listitem"><font _mstmutation="1" _msttexthash="90583051" _msthash="42567">• :栈顶指针,永远指向当前栈顶。</font><code class="codespan">ESP</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="134988386" _msthash="42568">• :栈基址指针,用来固定一个函数的栈帧底部。</font><code class="codespan">EBP</code></li></ul><h3 class="h3" data-heading="true" id="4" _msttexthash="166256415" _msthash="42569">3. 程序指针寄存器(不能直接写,只能借指令改)</h3><p class="p"><code class="codespan">EIP</code><font _mstmutation="1" _msttexthash="21849191" _msthash="42570"> (64 位叫 )</font><code class="codespan">RIP</code></p><p class="p" _msttexthash="177638435" _msthash="42571">它永远指向下一条要执行的指令地址,像程序的行号光标。</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-" data-language-pending="" data-raw-code="地址: 0x401000 mov eax, 60 ; 执行这行时,EIP = 0x401005
地址: 0x401005 mov edi, 42 ; 执行这行时,EIP = 0x40100A" data-show-line-number="false">地址: 0x401000 mov eax, 60 ; 执行这行时,EIP = 0x401005<br>地址: 0x401005 mov edi, 42 ; 执行这行时,EIP = 0x40100A</code></pre><p class="p"><strong class="strong" _msttexthash="212995705" _msthash="42572"><code class="codespan">EIP</code> 不能直接 <code class="codespan">mov eip, 1234</code>,只能通过 <code class="codespan">jmp</code>、<code class="codespan">call</code>、<code class="codespan">ret</code> 等指令间接修改。</strong></p><hr class="hr hr-dash"><h2 class="h2" data-heading="true" id="5" _msttexthash="85568262" _msthash="42573">二、最常见的指令,其实就三个逻辑</h2><h3 class="h3" data-heading="true" id="6"><font _mstmutation="1" _msttexthash="84757517" _msthash="42574">1. —— 就是 C 语言里的 (赋值)</font><code class="codespan">MOV</code><code class="codespan">=</code></h3><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-c"><span class="hljs-type">int</span> a =<span class="hljs-number"> 10</span>;<span class="hljs-type"><br>int</span> b = a;</code></pre><p class="p" _msttexthash="13546767" _msthash="42575">汇编:</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">mov eax, 10 ; eax = 10<br>mov ebx, eax ; ebx = eax</code></pre><h3 class="h3" data-heading="true" id="7"><font _mstmutation="1" _msttexthash="43133441" _msthash="42576">2. —— 取地址,不是取值</font><code class="codespan">LEA</code></h3><p class="p" _msttexthash="91622076" _msthash="42577">mov知道了lea就不难了对比看一下:</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">mov eax, [0x400000] <br>把地址 0x400000里面存的值取出来,放进 eax<br>lea eax, [0x400000] <br>把 0x400000 这个数本身(地址常数)放进 eax</code></pre><p class="p" _msttexthash="45261489" _msthash="42578">这就跟 C 语言里的:</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-c"><span class="hljs-type">int</span> x = *p;<span class="hljs-comment"> // mov 取内容</span><span class="hljs-type"><br>int</span> *q = &x;<span class="hljs-comment"> // lea 取地址</span></code></pre><p class="p" _msttexthash="228137390" _msthash="42579"><strong class="strong" _istranslated="1">注:有中括号 <code class="codespan" _istranslated="1">[ ]</code> 基本代表“去指针指向的地方拿东西”。 </strong> 也就是解指针</p><h3 class="h3" data-heading="true" id="8" _msttexthash="45115954" _msthash="42580">3. 加减乘除、移位、逻辑</h3><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">add eax, 1 ; 加<br>sub eax, 1 ; 减<br>mul ecx ; 无符号乘<br>div ecx ; 无符号除<br>shl eax, 1 ; 左移<br>shr eax, 1 ; 逻辑右移<br>and eax, 0xFF ; 按位与<br>or eax, 1 ; 按位或<br>xor eax, eax ; 清空 eax 的经典写法<br>movzx eax, byte ptr [地址] ; 读单字节并零扩展,避免高位污染</code></pre><p class="p" _msttexthash="251438213" _msthash="42581">这些基本都是见名知意,碰到不认识的查一下即可,无需死记硬背。</p><hr class="hr hr-dash"><h2 class="h2" data-heading="true" id="9" _msttexthash="124741019" _msthash="42582">三、栈与函数调用:从 C 代码到汇编的完整拆解</h2><p class="p" _msttexthash="1169147941" _msthash="42583">栈,你学 C 语言时就知道:函数调用时,局部变量、返回地址、参数都放在栈上。 汇编里的栈就是同一个东西。 另外栈的结构和c语言中的stack结构是一样的(先进后出)</p><p class="p"><font _mstmutation="1" _msttexthash="111457060" _msthash="42584"><strong class="strong" _mstmutation="1" _istranslated="1">栈向低地址方向生长。 </strong> 数据时 减小, 时 增大。</font><code class="codespan">push</code><code class="codespan">ESP</code><code class="codespan">pop</code><code class="codespan">ESP</code></p><h3 class="h3" data-heading="true" id="10" _msttexthash="134240197" _msthash="42585">3.1 一个最简单的函数,看看栈帧怎么搭起来的</h3><p class="p" _msttexthash="14190514" _msthash="42586">C 代码:</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-c"><span class="hljs-type">void</span><span class="hljs-title function_"> test</span><span class="hljs-params">()</span> {<span class="hljs-type"><br> int</span> val =<span class="hljs-number"> 0</span>;<span class="hljs-comment"><br> // do something...</span><br>}<span class="hljs-type"><br><br>int</span><span class="hljs-title function_"> main</span><span class="hljs-params">()</span> {<br> test();<span class="hljs-keyword"><br> return</span><span class="hljs-number"> 0</span>;<br>}</code></pre><h4 class="h4" data-heading="true" id="11"><font _mstmutation="1" _msttexthash="79966068" _msthash="42587">汇编层面, 调用 时发生了什么?</font><code class="codespan">main</code><code class="codespan">test</code></h4><p class="p"><strong class="strong" _msttexthash="28328586" _msthash="42588">第一步:<code class="codespan">call test</code> 指令</strong></p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">call test</code></pre><p class="p"><code class="codespan">call</code><font _mstmutation="1" _msttexthash="21993803" _msthash="42589"> 做了两件事:</font></p><ol class="ol"><li class="listitem"><font _mstmutation="1" _msttexthash="128720774" _msthash="42590">1. 把<strong class="strong" _mstmutation="1">返回地址</strong>( 下一条指令的地址)压入栈中。</font><code class="codespan">call</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="63628786" _msthash="42591">2. 跳转到 函数的地址开始执行。</font><code class="codespan">test</code></li></ol><p class="p"><font _mstmutation="1" _msttexthash="72066462" _msthash="42592">此时栈上多了一层返回地址, 减 4。</font><code class="codespan">ESP</code></p><p class="p"><strong class="strong" _msttexthash="63657789" _msthash="42593">第二步:进入 <code class="codespan">test</code>,标准开头</strong></p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">push ebp ; 保存 main 函数的 EBP(栈基址)<br>mov ebp, esp ; 把当前栈顶设为 test 函数的栈基址<br>sub esp, 4 ; 为局部变量 val 分配 4 字节空间</code></pre><p class="p" _msttexthash="87319414" _msthash="42594">这一步是<strong class="strong">每个函数必做的栈帧搭建</strong>:</p><ul class="ul"><li class="listitem"><font _mstmutation="1" _msttexthash="99119917" _msthash="42595">• 把调用者的 存起来,以便返回时恢复。</font><code class="codespan">push ebp</code><code class="codespan">EBP</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="113835007" _msthash="42596">• 把 当前值赋给 ,从此 固定指向 栈帧的底部。</font><code class="codespan">mov ebp, esp</code><code class="codespan">ESP</code><code class="codespan">EBP</code><code class="codespan">EBP</code><code class="codespan">test</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="124288580" _msthash="42597">• 把 往下移 4 字节,空出的位置就是局部变量 。</font><code class="codespan">sub esp, 4</code><code class="codespan">ESP</code><code class="codespan">val</code></li></ul><p class="p" _msttexthash="111454187" _msthash="42598">此时栈的布局(从高地址到低地址):</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-" data-language-pending="" data-raw-code="[ebp+4] 返回地址
[ebp] 保存的 main 的 EBP
[ebp-4] 局部变量 val ← ESP 指向这里" data-show-line-number="false">[ebp+4] 返回地址<br>[ebp] 保存的 main 的 EBP<br>[ebp-4] 局部变量 val ← ESP 指向这里</code></pre><p class="p"><font _mstmutation="1" _msttexthash="237846843" _msthash="42599">函数内访问 就是 。 如果还有更多局部变量,就 ,然后用 、 等访问。</font><code class="codespan">val</code><code class="codespan">[ebp-4]</code><code class="codespan">sub esp, 更大的数</code><code class="codespan">[ebp-8]</code><code class="codespan">[ebp-12]</code></p><p class="p"><strong class="strong" _msttexthash="70303350" _msthash="42600">第三步:函数返回,标准结尾</strong></p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">mov esp, ebp ; 释放所有局部变量,ESP 指回保存的 EBP 处<br>pop ebp ; 恢复 main 的 EBP,ESP 自动指向返回地址<br>ret ; 弹出返回地址到 EIP,程序回到 main 继续执行</code></pre><p class="p" _msttexthash="145182817" _msthash="42601">这三步是<strong class="strong">函数收尾的标准动作</strong>,与开头严格对应:</p><ul class="ul"><li class="listitem"><font _mstmutation="1" _msttexthash="35354813" _msthash="42602">• 撤销 分配的空间。</font><code class="codespan">mov esp, ebp</code><code class="codespan">sub esp, N</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="27407185" _msthash="42603">• 恢复调用者的 。</font><code class="codespan">pop ebp</code><code class="codespan">EBP</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="70786989" _msthash="42604">• 弹出返回地址,跳回调用者。</font><code class="codespan">ret</code></li></ul><h3 class="h3" data-heading="true" id="12" _msttexthash="38360543" _msthash="42605">3.2 带参数的函数调用</h3><p class="p" _msttexthash="14190514" _msthash="42606">C 代码:</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-c"><span class="hljs-type">int</span><span class="hljs-title function_"> add</span><span class="hljs-params">(<span class="hljs-type">int</span> x,<span class="hljs-type"> int</span> y)</span> {<span class="hljs-type"><br> int</span> sum = x + y;<span class="hljs-keyword"><br> return</span> sum;<br>}<span class="hljs-type"><br><br>int</span><span class="hljs-title function_"> main</span><span class="hljs-params">()</span> {<span class="hljs-type"><br> int</span> result = add(<span class="hljs-number">3</span>,<span class="hljs-number"> 5</span>);<span class="hljs-keyword"><br> return</span><span class="hljs-number"> 0</span>;<br>}</code></pre><h4 class="h4" data-heading="true" id="13"><font _mstmutation="1" _msttexthash="97910618" _msthash="42607">调用者 的准备工作(cdecl 调用约定)</font><code class="codespan">main</code></h4><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">push 5 ; 参数 y 先入栈(参数从右向左压入)<br>push 3 ; 参数 x 入栈<br>call add ; 压入返回地址,跳转到 add<br>add esp, 8 ; 调用者负责清理栈上的 8 字节参数</code></pre><h4 class="h4" data-heading="true" id="14"><code class="codespan">add</code><font _mstmutation="1" _msttexthash="25080211" _msthash="42608"> 函数的汇编实现</font></h4><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">push ebp ; 保存 main 的 EBP<br>mov ebp, esp ; 建立 add 的栈帧<br>sub esp, 4 ; 为局部变量 sum 分配空间<br><br>mov eax, [ebp+8] ; 取出参数 x (3)<br>add eax, [ebp+12] ; 加上参数 y (5)<br>mov [ebp-4], eax ; 存入局部变量 sum<br><br>; 返回值通常放在 eax 中<br>mov eax, [ebp-4] ; sum 的值放入 eax<br><br>mov esp, ebp ; 释放 sum 的空间<br>pop ebp ; 恢复 main 的 EBP<br>ret ; 返回,main 的 add esp, 8 随后清理参数</code></pre><p class="p"><font _mstmutation="1" _msttexthash="35419735" _msthash="42609">进入 后的栈布局:</font><code class="codespan">add</code></p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-" data-language-pending="" data-raw-code="[ebp+12] 参数 y (5)
[ebp+8] 参数 x (3)
[ebp+4] 返回地址
[ebp] 保存的 main 的 EBP
[ebp-4] 局部变量 sum ← ESP 指向这里" data-show-line-number="false">[ebp+12] 参数 y (5)<br>[ebp+8] 参数 x (3)<br>[ebp+4] 返回地址<br>[ebp] 保存的 main 的 EBP<br>[ebp-4] 局部变量 sum ← ESP 指向这里</code></pre><p class="p"><strong class="strong" _msttexthash="16613220" _msthash="42610">栈平衡规则</strong></p><p class="p"><font _mstmutation="1" _msttexthash="996543808" _msthash="42611">函数用了多少栈空间,一定要释放多少。 和 要配对, 后必须有 或 来恢复。 参数要么由调用者清理(如 ),要么由被调用函数用 清理(stdcall 约定)。</font><code class="codespan">push</code><code class="codespan">pop</code><code class="codespan">sub esp, N</code><code class="codespan">mov esp, ebp</code><code class="codespan">add esp, N</code><code class="codespan">add esp, 8</code><code class="codespan">ret 8</code></p><h3 class="h3" data-heading="true" id="15" _msttexthash="73934913" _msthash="42612">3.3 函数标准头尾模板(32 位)</h3><p class="p" _msttexthash="244807147" _msthash="42613">任何一个函数在汇编里都有这个骨架,看到它就认出了函数边界:</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">push ebp<br>mov ebp, esp<br>sub esp, 0x20 ; 分配 32 字节局部变量空间<br>... 函数体 ...<br>mov esp, ebp<br>pop ebp<br>ret</code></pre><p class="p" _msttexthash="142094004" _msthash="42614">这就是栈帧的完整生命周期:<strong class="strong">搭台子 → 用台子 → 拆台子</strong></p><hr class="hr hr-dash"><h2 class="h2" data-heading="true" id="16"><font _mstmutation="1" _msttexthash="113890764" _msthash="42615">四、条件判断: 做比较,条件跳转选分支</font><code class="codespan">cmp</code></h2><p class="p"><font _mstmutation="1" _msttexthash="937292564" _msthash="42616">汇编没有 关键字,没有 关键字。 它的条件判断就两步:<strong class="strong" _mstmutation="1" _istranslated="1">先用 <code class="codespan" _istranslated="1">cmp</code> 做比较(相当于 <code class="codespan" _istranslated="1">if</code> 括号里的条件),然后根据结果用条件跳转指令选择走哪条路。 </strong></font><code class="codespan">if</code><code class="codespan">switch</code></p><h3 class="h3" data-heading="true" id="17"><font _mstmutation="1" _msttexthash="122019131" _msthash="42617">4.1 —— 做减法,但不保存结果,只设标志位</font><code class="codespan">CMP</code></h3><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">cmp eax, 10</code></pre><p class="p"><strong class="strong" _msttexthash="553987798" _msthash="42618"><code class="codespan">cmp eax, 10</code> 就是偷偷算了一下 <code class="codespan">eax - 10</code>,然后根据减出来的结果改变 CPU 内部的几个标志,结果本身直接丢掉。</strong></p><p class="p"><font _mstmutation="1" _msttexthash="176768644" _msthash="42619">这组标志存在 标志寄存器。 减法后会自动更新几个关键位:</font><code class="codespan">EFLAGS</code></p><ul class="ul"><li class="listitem" _msttexthash="241612085" _msthash="42620">• <strong class="strong" _istranslated="1">ZF(零标志)</strong>:减出来是 0,ZF 置为1。 (说明两个数相等)</li><li class="listitem"><font _mstmutation="1" _msttexthash="215131293" _msthash="42621">• <strong class="strong" _mstmutation="1" _istranslated="1">CF(进位/借位标志)</strong>:不够减,发生借位,CF 置为1。 比如 。</font><code class="codespan">cmp 2, 5</code></li><li class="listitem" _msttexthash="134840589" _msthash="42622">• <strong class="strong">SF(符号标志)</strong>:减出来是负数,SF 置为1。</li><li class="listitem" _msttexthash="178436635" _msthash="42623">• <strong class="strong">OF(溢出标志)</strong>:有符号数结果超出范围,OF 置为1。</li></ul><p class="p"><font _mstmutation="1" _msttexthash="1274854113" _msthash="42624"><strong class="strong" _mstmutation="1" _istranslated="1">乘法也会影响 CF 和 OF。 </strong> 比如 (有符号乘法),如果乘积的结果高位不是符号位的简单扩展,说明结果超出寄存器能表示的范围了,CF 和 OF 都会置为1。 这在检查乘法溢出时有用。</font><code class="codespan">imul</code></p><h3 class="h3" data-heading="true" id="18"><font _mstmutation="1" _msttexthash="85169253" _msthash="42625">4.2 后面跟条件跳转,就是完整的 </font><code class="codespan">cmp</code><code class="codespan">if</code></h3><p class="p"><code class="codespan">cmp</code><font _mstmutation="1" _msttexthash="316629079" _msthash="42626"> 把灯设好之后,后面一定跟一个条件跳转指令。 这些跳转指令就是看灯决定往哪走。</font></p><p class="p"><strong class="strong" _msttexthash="16354637" _msthash="42627">C 语言:</strong> <code class="codespan">if (a == b) { ... }</code></p><p class="p"><strong class="strong" _msttexthash="21406710" _msthash="42628">汇编就是:</strong></p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">cmp eax, ebx ; 相当于 if (eax == ebx) 的条件比较<br>je somewhere ; 如果 ZF 亮(相等),就跳到大括号里的代码</code></pre><p class="p" _msttexthash="165158448" _msthash="42629">常用跳转指令就是一张表,看过一遍有印象就行:</p>
<section style="max-width: 100%; overflow: auto; -webkit-overflow-scrolling: touch">
<table class="preview-table">
<thead><tr><th class="th" style="text-align: left" _msttexthash="4407325" _msthash="42630">指令</th><th class="th" style="text-align: left" _msttexthash="10024469" _msthash="42631">什么意思</th><th class="th" style="text-align: left" _msttexthash="11116937" _msthash="42632">看哪个灯</th><th class="th" style="text-align: left" _msttexthash="14858532" _msthash="42633">C 语言等价</th></tr></thead>
<tbody><tr class="tr"><td class="td" style="text-align: left"><code class="codespan">je</code></td><td class="td" style="text-align: left" _msttexthash="13539227" _msthash="42634">相等就跳</td><td class="td" style="text-align: left" _msttexthash="28977" _msthash="42635">ZF = 1</td><td class="td" style="text-align: left"><code class="codespan">if (a == b)</code></td></tr><tr class="tr"><td class="td" style="text-align: left"><code class="codespan">jne</code></td><td class="td" style="text-align: left" _msttexthash="12586002" _msthash="42636">不等就跳</td><td class="td" style="text-align: left" _msttexthash="28847" _msthash="42637">ZF = 0</td><td class="td" style="text-align: left"><code class="codespan">if (a != b)</code></td></tr><tr class="tr"><td class="td" style="text-align: left"><code class="codespan">ja</code></td><td class="td" style="text-align: left" _msttexthash="23832666" _msthash="42638">无符号大于就跳</td><td class="td" style="text-align: left" _msttexthash="2931370" _msthash="42639">CF=0 且 ZF=0</td><td class="td" style="text-align: left"><code class="codespan">if (a > b)</code></td></tr><tr class="tr"><td class="td" style="text-align: left"><code class="codespan">jb</code></td><td class="td" style="text-align: left" _msttexthash="23929386" _msthash="42640">无符号小于就跳</td><td class="td" style="text-align: left" _msttexthash="26884" _msthash="42641">CF=1</td><td class="td" style="text-align: left"><code class="codespan">if (a < b)</code></td></tr><tr class="tr"><td class="td" style="text-align: left"><code class="codespan">jae</code></td><td class="td" style="text-align: left" _msttexthash="22331725" _msthash="42642">无符号大于等于</td><td class="td" style="text-align: left" _msttexthash="26754" _msthash="42643">CF=0</td><td class="td" style="text-align: left"><code class="codespan">>=</code></td></tr><tr class="tr"><td class="td" style="text-align: left"><code class="codespan">jbe</code></td><td class="td" style="text-align: left" _msttexthash="22428445" _msthash="42644">无符号小于等于</td><td class="td" style="text-align: left" _msttexthash="3664141" _msthash="42645">CF=1 或 ZF=1</td><td class="td" style="text-align: left"><code class="codespan"><=</code></td></tr><tr class="tr"><td class="td" style="text-align: left"><code class="codespan">jg</code></td><td class="td" style="text-align: left" _msttexthash="23859693" _msthash="42646">有符号大于就跳</td><td class="td" style="text-align: left" _msttexthash="2952976" _msthash="42647">ZF=0 且 SF=OF</td><td class="td" style="text-align: left"><code class="codespan">if (a > b)</code></td></tr><tr class="tr"><td class="td" style="text-align: left"><code class="codespan">jl</code></td><td class="td" style="text-align: left" _msttexthash="23956413" _msthash="42648">有符号小于就跳</td><td class="td" style="text-align: left" _msttexthash="7682857" _msthash="42649">SF != OF</td><td class="td" style="text-align: left"><code class="codespan">if (a < b)</code></td></tr><tr class="tr"><td class="td" style="text-align: left"><code class="codespan">jge</code></td><td class="td" style="text-align: left" _msttexthash="22358752" _msthash="42650">有符号大于等于</td><td class="td" style="text-align: left" _msttexthash="42250" _msthash="42651">SF=OF</td><td class="td" style="text-align: left"><code class="codespan">>=</code></td></tr><tr class="tr"><td class="td" style="text-align: left"><code class="codespan">jle</code></td><td class="td" style="text-align: left" _msttexthash="22455472" _msthash="42652">有符号小于等于</td><td class="td" style="text-align: left" _msttexthash="3694288" _msthash="42653">ZF=1 或 SF!=OF</td><td class="td" style="text-align: left"><code class="codespan"><=</code></td></tr></tbody>
</table>
</section>
<p class="p"><strong class="strong" _msttexthash="37174956" _msthash="42654">重点区分一下 <code class="codespan">ja</code> 和 <code class="codespan">jg</code>:</strong></p><ul class="ul"><li class="listitem"><font _mstmutation="1" _msttexthash="183139749" _msthash="42655">• :把你比的两个数当<strong class="strong" _mstmutation="1">无符号数</strong>(全是正数,0 到 42 亿)。</font><code class="codespan">ja</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="150389161" _msthash="42656">• :把你比的两个数当<strong class="strong" _mstmutation="1">有符号数</strong>(可以有负数)。</font><code class="codespan">jg</code></li></ul><p class="p" _msttexthash="16115749" _msthash="42657">看例子:</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">mov eax, -1 ; 0xFFFFFFFF<br>cmp eax, 0<br>ja somewhere <br>0xFFFFFFFF 作为无符号数是 42 亿,大于 0,会跳!<br>jg somewhere ; 作为有符号数,-1 小于 0,不跳。</code></pre><p class="p" _msttexthash="322243337" _msthash="42658">逆向时看到条件跳转,要留个心眼:编译器到底在比有符号数还是无符号数?</p><h3 class="h3" data-heading="true" id="19"><font _mstmutation="1" _msttexthash="43007393" _msthash="42659">4.3 实战: 的跳转表</font><code class="codespan">switch-case</code></h3><p class="p" _msttexthash="22055735" _msthash="42660">C 语言里:</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-c"><span class="hljs-keyword">switch</span>(index) {<span class="hljs-keyword"><br> case</span><span class="hljs-number"> 0</span>: ...<span class="hljs-keyword"> break</span>;<span class="hljs-keyword"><br> case</span><span class="hljs-number"> 1</span>: ...<span class="hljs-keyword"> break</span>;<span class="hljs-keyword"><br> case</span><span class="hljs-number"> 2</span>: ...<span class="hljs-keyword"> break</span>;<span class="hljs-keyword"><br> case</span><span class="hljs-number"> 3</span>: ...<span class="hljs-keyword"> break</span>;<span class="hljs-keyword"><br> default</span>: ...<br>}</code></pre><p class="p"><font _mstmutation="1" _msttexthash="211280446" _msthash="42661">编译器往往不会翻译成一长串 ,而是用<strong class="strong" _mstmutation="1">跳转表</strong>,效率更高。</font><code class="codespan">cmp/je</code></p><p class="p" _msttexthash="49644257" _msthash="42662">来看这段经典 x64 汇编:</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">.intel_syntax noprefix<br>.global _start<br>_start:<br> cmp rdi, 3<br> ja default ; rdi > 3(无符号),直接去 default<br> jmp [rsi + rdi*8] ; rdi 在 0~3,查跳转表<br>default:<br> jmp [rsi + 32] ; 32 = 4*8,跳转表第 5 项</code></pre><p class="p"><strong class="strong" _msttexthash="25305449" _msthash="42663">逐行解释:</strong></p><ol class="ol"><li class="listitem"><font _mstmutation="1" _msttexthash="40806207" _msthash="42664">1. : 是 ,跟 3 比较。</font><code class="codespan">cmp rdi, 3</code><code class="codespan">rdi</code><code class="codespan">index</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="346724014" _msthash="42665">2. :如果 (无符号比较),直接跳到 。 跳转表只有 0~3 四项,超出下标必须保护。</font><code class="codespan">ja default</code><code class="codespan">rdi > 3</code><code class="codespan">default</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="552218173" _msthash="42666">3. :核心。 是跳转表基地址,每个目标地址占 8 字节。 就是索引乘以 8。 从表里取出 8 字节的目标地址,然后 过去。</font><code class="codespan">jmp [rsi + rdi*8]</code><code class="codespan">rsi</code><code class="codespan">rdi*8</code><code class="codespan">[rsi + rdi*8]</code><code class="codespan">jmp</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="172134664" _msthash="42667">4. 的地址在 (前 4 项各 8 字节 = 32),就是表的第 5 项。</font><code class="codespan">default</code><code class="codespan">rsi+32</code></li></ol><p class="p"><strong class="strong" _msttexthash="120123952" _msthash="42668">这就是 <code class="codespan">switch</code> 的高效实现,比一个个 <code class="codespan">if</code> 快得多。</strong></p><p class="p"><font _mstmutation="1" _msttexthash="232379823" _msthash="42669">下次在反汇编里看到 这类语句,马上反应过来:它在查跳转表。</font><code class="codespan">jmp [rax + rcx*8]</code></p><hr class="hr hr-dash"><h2 class="h2" data-heading="true" id="20" _msttexthash="106978781" _msthash="42670">五、画龙点睛:汇编代码的骨架长这样</h2><p class="p" _msttexthash="205742485" _msthash="42671">抛开所有细节,大多数汇编程序都是由以下积木搭起来的:</p><ol class="ol"><li class="listitem"><font _mstmutation="1" _msttexthash="53312467" _msthash="42672">1. <strong class="strong" _mstmutation="1">变量搬运</strong>:、、、 清零。</font><code class="codespan">mov</code><code class="codespan">lea</code><code class="codespan">movzx</code><code class="codespan">xor</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="39134238" _msthash="42673">2. <strong class="strong" _mstmutation="1">算术逻辑</strong>:、、、、...</font><code class="codespan">add</code><code class="codespan">sub</code><code class="codespan">imul</code><code class="codespan">and</code><code class="codespan">shl</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="78527384" _msthash="42674">3. <strong class="strong" _mstmutation="1">栈帧操作(函数调用)</strong>:</font><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-asm">push ebp<br>mov ebp, esp<br>sub esp, 0x20 ; 分配局部变量空间<br>...<br>mov esp, ebp<br>pop ebp<br>ret</code></pre></li><li class="listitem"><font _mstmutation="1" _msttexthash="168070175" _msthash="42675">4. <strong class="strong" _mstmutation="1">条件判断</strong>: 设灯,然后 、、、 等看灯跳转,就是 。</font><code class="codespan">cmp</code><code class="codespan">je</code><code class="codespan">jne</code><code class="codespan">ja</code><code class="codespan">jg</code><code class="codespan">if</code></li><li class="listitem"><font _mstmutation="1" _msttexthash="35966034" _msthash="42676">5. <strong class="strong" _mstmutation="1">跳转表</strong>: 实现 。</font><code class="codespan">jmp [base + index*scale]</code><code class="codespan">switch</code></li></ol><p class="p" _msttexthash="203648614" _msthash="42677">当你下次看到一大片十六进制,心里就能浮现出这些结构:</p><p class="p"><em class="em" _msttexthash="357379477" _msthash="42678">“这里在设标志位…… 那里在看灯跳转…… 这是个函数头,<code class="codespan" _istranslated="1">push ebp; mov ebp, esp</code> 建栈帧…… 它要 <code class="codespan" _istranslated="1">ret</code> 回家了。 ”</em></p><p class="p" _msttexthash="177995272" _msthash="42679">汇编不再是天书,而是由几个简单部件组装成的精密机器。</p><hr class="hr hr-dash"><h2 class="h2" data-heading="true" id="21" _msttexthash="57501886" _msthash="42680">六、栈帧结构为什么是 PWN 的基础</h2><p class="p" _msttexthash="513152159" _msthash="42681">如果你将来要学习二进制漏洞利用(PWN),尤其是<strong class="strong">栈溢出</strong>,那么今天讲的栈帧结构就是你绕不开的地基。</p><p class="p" _msttexthash="648702171" _msthash="42682">栈溢出的本质非常简单:局部变量的空间是有限的,但程序没有检查输入的长度,导致数据越界写到了栈帧的其它区域。</p><p class="p" _msttexthash="39685685" _msthash="42683">回顾一下栈帧布局:</p><pre class="hljs code__pre"><span class="mac-sign" style="padding: 10px 14px 0;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" width="45px" height="13px" viewBox="0 0 450 130">
<ellipse cx="50" cy="65" rx="50" ry="52" stroke="rgb(220,60,54)" stroke-width="2" fill="rgb(237,108,96)"></ellipse>
<ellipse cx="225" cy="65" rx="50" ry="52" stroke="rgb(218,151,33)" stroke-width="2" fill="rgb(247,193,81)"></ellipse>
<ellipse cx="400" cy="65" rx="50" ry="52" stroke="rgb(27,161,37)" stroke-width="2" fill="rgb(100,200,86)"></ellipse>
</svg></span><code class="language-" data-language-pending="" data-raw-code="[ebp+8] 参数
[ebp+4] 返回地址
[ebp] 保存的 EBP
[ebp-4] 局部变量 buf[8]
[ebp-12] ..." data-show-line-number="false">[ebp+8] 参数<br>[ebp+4] 返回地址<br>[ebp] 保存的 EBP<br>[ebp-4] 局部变量 buf[8]<br>[ebp-12] ...</code></pre><p class="p"><font _mstmutation="1" _msttexthash="1723436975" _msthash="42684">假设 只有 8 个字节,但你强行打了 20 个字节 的台子,多出的数据会一路向上覆盖掉 (保存的 EBP),再覆盖掉 (返回地址)。 当函数执行 时,就会把被篡改的值弹进 ,从而跳转到攻击者控制的地址上。</font><code class="codespan">buf</code><code class="codespan">[ebp]</code><code class="codespan">[ebp+4]</code><code class="codespan">ret</code><code class="codespan">EIP</code></p><p class="p"><font _mstmutation="1" _msttexthash="737159774" _msthash="42685">这就是经典的栈溢出劫持控制流。 你学到的每一个细节—— 怎么压返回地址、 怎么保存栈基址、局部变量在 的位置——都是漏洞利用的发力点。</font><code class="codespan">call</code><code class="codespan">push ebp</code><code class="codespan">[ebp-xx]</code></p><p class="p" _msttexthash="1685735181" _msthash="42686">很多 CTF 的 pwn 题,第一步就是 IDA 里看栈帧大小,找到缓冲区到返回地址的偏移,然后构造 payload 精准覆盖返回地址。 今天你在这里打下的栈帧基础,正是后面搞懂 ROP、ret2libc、canary 绕过等高阶技巧的基石。</p><p class="p"><strong class="strong" _msttexthash="1862288948" _msthash="42687">把今天的内容啃透,以后看到 <code class="codespan">sub esp, 0x28</code> 时,你不是在背指令,而是直接能想到:这函数向上搭了 40 字节的台子,buf 可能在 <code class="codespan">ebp-0x20</code> 处,返回地址就在 <code class="codespan">ebp+4</code>,我如果能控制输入,就能改变这个台子原本的作用。</strong></p><hr class="hr hr-dash"><h2 class="h2" data-heading="true" id="22" _msttexthash="12486825" _msthash="42688">下篇预告</h2><p class="p" _msttexthash="393203083" _msthash="42689">有了这些基础,我们就可以一起拆一个真正的程序,分析它的控制流,找出隐藏的逻辑。 </p><p class="p" _msttexthash="18010395" _msthash="42690">准备开始PWN了吗 !</p>
<style>
.preview-wrapper pre::before {
position: absolute;
top: 0;
right: 0;
color: #ccc;
text-align: center;
font-size: 0.8em;
padding: 5px 10px 0;
line-height: 15px;
height: 15px;
font-weight: 600;
}
</style>
<style>
.hljs.code__pre > .mac-sign {
display: flex;
}
</style>
<style>
h2 strong {
color: inherit !important;
}
</style>
</section>
</div> <!-- /content -->
</div> <!-- /main -->
<script>
const navLinks = document.querySelectorAll('.sidebar nav a');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
navLinks.forEach(l => l.classList.remove('active'));
const id = entry.target.getAttribute('id');
const link = document.querySelector(`.sidebar nav a[href="#${id}"]`);
if (link) link.classList.add('active');
}
});
}, { rootMargin: '-80px 0px -70% 0px' });
document.querySelectorAll('h1[id], h2[id]').forEach(h => observer.observe(h));
</script>
<script defer src="visual-system.js?v=20261007-3"></script>
</body>
</html>