-
Notifications
You must be signed in to change notification settings - Fork 4
/
x86.html
209 lines (171 loc) · 11 KB
/
x86.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>CS 4440</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/jpswalsh/academicons/css/academicons.min.css">
<link rel="stylesheet" href="css/main.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.1/styles/default.min.css">
<script src="https://kit.fontawesome.com/982c2a20d7.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.1/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<nav class="navbar navbar-inverse">
<a class="navbar-brand" href="http://cs.utah.edu">
<img class="button" width="185px" src="../../../img/ksoclogo_white.png" style="margin-top:1.3%">
</a>
<div class="ml-auto">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav" style="margin-top: 1px;">
<li><a href="../">Syllabus</a></li>
<li><a href="../schedule">Schedule</a></li>
<li><a href="../assignments">Assignments</a></li>
<li><a href="../wiki">Wiki</a></li>
<li><a href="../redirect_piazza">Piazza</a></li>
<li><a href="../redirect_canvas">Canvas</a></li>
<li><a href="../redirect_pollev">PollEv</a></li>
<li><a href="../redirect_taqueue">TA Queue</a></li>
</ul>
</div>
</div>
</nav>
<!-- ################################################################################### -->
<div class="container main-container">
<div class="col-md-10">
<h1>CS 4440 Wiki: <br class="on-narrow"><b style="color:dodgerblue">x86 Cheat Sheet</b></h1><br>
<!-- ################################################################################### -->
<p>This page offers a brief cheat sheet of x86 Assembly fundamentals relevant to Project 2.</p>
<p><b>This page is by no means comprehensive—we encourage you to bookmark and familiarize yourself with one of the many in-depth x86 tutorials on the web.</b> Some great examples are:</p>
<ul>
<li><a href="https://flint.cs.yale.edu/cs421/papers/x86-asm/asm.html">Guide to x86 Assembly</a></li>
<li><a href="https://www.cs.uaf.edu/2005/fall/cs301/support/x86/index.html">UAF's x86 Assembly Quick Reference</a></li>
<li><a href="https://pages.cs.wisc.edu/~remzi/Classes/354/Fall2012/Handouts/Handout-x86-cheat-sheet.pdf">UW's x86 Cheat Sheet</a></li>
</ul>
<!-- ################################################################################### -->
<hr><h2 id=registers>Registers</h2>
<h3 id=registers#general>General-purpose Registers</h3>
<p>The following are <b>general-purpose registers</b>: quickly-accessible locations that the CPU utilizes any time data is being operated on (e.g., for storing or reading data from).</p>
<pre><code class="language-assembly">%eax
%ecx
%edx
%ebx
%esi
%edi
</code></pre>
<h3 id=registers#special>Special Registers</h3>
<p>Below are several "special" registers critical to program execution:</p>
<pre><code class="language-assembly">%eip // Instruction pointer (always points to the address of the next instruction).
%ebp // Frame pointer (points to current stack frame's base); adjusted on calls/rets.
%esp // Stack pointer (points to the top of the stack); adjusted on pushes/pops.
</code></pre>
<!-- ################################################################################### -->
<hr><h2 id=instructions>Instructions</h2>
<h3 id=instructions#arithmetic>Arithmetic</h3>
<p>Below describes several common arithmetic instructions that you will likely come across:</p>
<pre><code class="language-assembly">add src,dst // dst = dst + src
sub src,dst // dst = dst - src
mul src,dst // dst = dst * src
inc dst // dst = dst + 1
dec dst // dst = dst - 1
neg dst // dst = -dst
not dst // dst = ~dst
</code></pre>
<h3 id=instructions#copying>Copying Data</h3>
<p>The following data copying operations are shown in <b>AT&T</b> syntax (GDB's default syntax). In <b>Intel</b> syntax, the <code>src</code> and <code>dst</code> operands are simply swapped.</p>
<p>Operands <code>src</code> or <code>dst</code> can be <b>immediates</b> (e.g., <code>$0x10</code>), <b>registers</b> (e.g., <code>eax</code>), or memory <b>addresses</b>.</p>
<pre><code class="language-assembly">mov src, dst // Copies the contents of src into dst
lea src, dst // Copies the address of src into dst
</code></pre>
<h3 id=instructions#jumps>Jumps</h3>
<p>Aside from function calls and returns, <b>jumps</b> are the other way of transferring control from one program location to another. Below discusses two types of jumps: <b>unconditional</b> (equivalent to "just go there") and <b>conditional</b> (equivalent to "jump there if condition X is met").</p>
<pre><code class="language-assembly">jmp label // Unconditional jump to label
jmp *reg // Unconditional jump to contents of register reg
cmp arg1,arg2 // Compare arg1 to arg2; must precede a conditional jump.
je label // Conditional jump to label if arg1 == arg2.
jne label // Conditional jump to label if arg1 != arg2.
jg label // Conditional jump to label if arg2 > arg1.
jge label // Conditional jump to label if arg2 >= arg1.
jl label // Conditional jump to label if arg2 < arg1.
jle label // Conditional jump to label if arg2 <= arg1.
</code></pre>
<h3 id=instructions#misc>Miscellaneous</h3>
<p>Here are some other common instructions that you will likely encounter:</p>
<pre><code class="language-assembly">nop // Short for No Op; skips-over to the next instruction.
push item // Push item (e.g., constant, register) to stack.
pop reg // Pop item from stack into register reg.
</code></pre>
<!-- ################################################################################### -->
<hr><h2 id=functions>Functions</h2>
<h3 id=functions#calls>Calling</h3>
<p>Function <b>calls</b> transfer control from the call-<b><i>er</i></b> (the current active function) to a call-<b><i>ee</i></b> (the function being invoked by the caller). When the callee returns, execution transfers back to the caller.</p>
<pre><code class="language-assembly">cal​l label // Transfer control to the callee fun​ction named label.
</code></pre>
<h3 id=functions#prologues>Prologue Routines</h3>
<p>The following is a common function <b>prologue</b>—the initial set of callee instructions that happen immediately after a <code>call</code> instruction transfers control from the caller.</p>
<pre><code class="language-assembly">push ebp // Save the previous (i.e., the caller's) value of ebp.
// When callee returns, ebp will res​et to the caller ebp.
mov esp, ebp // U​pdate ebp to now point to the top of the stack.
sub X, esp // Allocate X bytes of stack space for local variables.
</code></pre>
<h3 id=functions#epilogues>Epilogue Routines</h3>
<p>The following is a common function <b>epilogue</b>—the final set of callee instructions that cleans-up its stack frame (i.e., de-allocates the space used by local variables) immediately before a <code>ret</code> transfers control back to the caller. Often, this epilogue procedure is represented as the single instruction <code>leave</code>.</p>
<pre><code class="language-assembly">mov ebp, esp // Reset the stack pointer to the top of the callee's frame.
// In other words, esp is now back to its pre-callee (caller) state.
pop ebp // Pop frame pointer off the stack, restoring to the caller's saved ebp.
// In other words, ebp is now back to its pre-callee (caller) state.
</code></pre>
<h3 id=functions#returns>Returning</h3>
<p>Following the epilogue, a <b>return</b> instruction is executed, transferring control from the callee back to the caller. Execution resumes in the caller at the instructiom immediately following the preceding <code>call</code>.</p>
<pre><code class="language-assembly">ret // Transfer control back to the caller's next (post-call) instruction.
</code></pre>
<!-- ################################################################################### -->
</div>
<div class="col-md-2 text-left scrollable" style="position:static;">
<div class="col-md-12 text-left scrollable" style="position:fixed;">
<h4><b>Table of Contents:</b></h4>
<ul>
<li><a href="#registers">Registers</a></li>
<ul>
<li><a href="#registers#general">General Registers</a></li>
<li><a href="#registers#special">Special Registers</a></li>
</ul>
<li style="margin-top:1ex"><a href="#instructions">Instructions</a></li>
<ul>
<li><a href="#instructions#arithmetic">Arithmetic</a></li>
<li><a href="#instructions#copying">Copying</a></li>
<li><a href="#instructions#jumps">Jumps</a></li>
<li><a href="#instructions#misc">Miscellaneous</a></li>
</ul>
<li style="margin-top:1ex"><a href="#functions">Functions</a></li>
<ul>
<li><a href="#functions#calls">Calling</a></li>
<li><a href="#functions#prologues">Prologues</a></li>
<li><a href="#functions#epilogues">Epilogues</a></li>
<li><a href="#functions#returns">Returns</a></li>
</ul>
</ul>
</h4>
</div>
</div>
</div>
<!-- ################################################################################### -->
<footer class="bg-light text-center text-lg-start">
<div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.2); font-size: 0.75em;">
Copyright © Stefan Nagy. All rights reserved.
</div>
</footer>
<!-- ################################################################################### -->
</body>
</html>