-
Notifications
You must be signed in to change notification settings - Fork 7
/
asm_tutorial_06.html
executable file
·340 lines (244 loc) · 8.55 KB
/
asm_tutorial_06.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
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
<HTML>
<HEAD>
<TITLE>8086 assembler tutorial for beginners (part 6)</TITLE>
<META name="description" content="Arithmetic and Logic Instructions">
</HEAD>
<BODY>
<FONT FACE="Verdana" SIZE=3>
<FONT SIZE=+1>
<B>8086 assembler tutorial for beginners (part 6)</B>
</FONT>
<BR><BR>
<FONT SIZE=+2><B>Arithmetic and Logic Instructions</B></FONT>
<BR><BR>
Most Arithmetic and Logic Instructions affect the processor status register
(or <B>Flags</B>)
<BR><BR>
<IMG SRC="img/flags.gif">
<BR><BR>
As you may see there are 16 bits in this register, each bit is called a <B>flag</B>
and can take a value of <B>1</B> or <B>0</B>.
<BR><BR>
<UL>
<LI><B>Carry Flag (CF)</B> - this flag is set to <B>1</B> when there is
an <B>unsigned overflow</B>.
For example when you add bytes <B>255 + 1</B> (result is not in range 0...255).
When there is no overflow this flag is set to <B>0</B>.
<BR><BR>
</LI>
<LI><B>Zero Flag (ZF)</B> - set to <B>1</B> when result is <B>zero</B>.
For none zero result this flag is set to <B>0</B>.
<BR><BR>
</LI>
<LI><B>Sign Flag (SF)</B> - set to <B>1</B> when result is <B>negative</B>.
When result is <B>positive</B> it is set to <B>0</B>. Actually this flag
take the value of the most significant bit.
<BR><BR>
</LI>
<LI><B>Overflow Flag (OF)</B> - set to <B>1</B> when there is a <B>signed overflow</B>.
For example, when you add bytes <B>100 + 50</B> (result is not in range -128...127).
<BR><BR>
</LI>
<LI><B>Parity Flag (PF)</B> - this flag is set to <B>1</B> when there is even number
of one bits in result, and to <B>0</B> when there is odd
number of one bits. Even if result is a word only 8 low
bits are analyzed!
<BR><BR>
</LI>
<LI><B>Auxiliary Flag (AF)</B> - set to <B>1</B> when there is an <B>unsigned overflow</B>
for low nibble (4 bits).
<BR><BR>
</LI>
<LI><B>Interrupt enable Flag (IF)</B> - when this flag is set to <B>1</B> CPU reacts to
interrupts from external devices.
<BR><BR>
</LI>
<LI><B>Direction Flag (DF)</B> - this flag is used by some instructions to process
data chains, when this flag is set to <B>0</B> - the processing is done forward,
when this flag is set to <B>1</B> the processing is done backward.
<BR><BR>
</LI>
</UL>
<HR>
<BR>
There are 3 groups of instructions.<BR><BR>
<HR>
<BR>
First group: <B>ADD</B>, <B>SUB</B>,<B>CMP</B>, <B>AND</B>, <B>TEST</B>,
<B>OR</B>, <B>XOR</B><BR><BR>
These types of operands are supported:<BR><BR>
<BLOCKQUOTE>
<FONT FACE="Fixedsys">
REG, memory<BR>
memory, REG<BR>
REG, REG<BR>
memory, immediate<BR>
REG, immediate
</FONT>
</BLOCKQUOTE>
<B>REG</B>:<FONT SIZE=-1> AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.</FONT><BR><BR>
<B>memory</B>: [BX], [BX+SI+7], variable, etc...<BR><BR>
<B>immediate</B>: 5, -24, 3Fh, 10001101b, etc...<BR><BR>
After operation between operands, result is always stored in first operand.
<B>CMP</B> and <B>TEST</B> instructions affect flags only and do not store a
result (these instruction are used to make decisions during program execution).
<BR><BR>
These instructions affect these flags only:<BR>
<B>CF</B>, <B>ZF</B>,
<B>SF</B>, <B>OF</B>, <B>PF</B>, <B>AF</B>.<BR><BR>
<UL>
<LI><B>ADD</B> - add second operand to first.
<BR><BR></LI>
<LI><B>SUB</B> - Subtract second operand to first.
<BR><BR></LI>
<LI><B>CMP</B> - Subtract second operand from first <B>for flags only</B>.
<BR><BR></LI>
<LI><B>AND</B> - Logical AND between all bits of two operands. These rules
apply:<BR>
<BLOCKQUOTE>
<FONT FACE="Fixedsys">
1 AND 1 = 1<BR>
1 AND 0 = 0<BR>
0 AND 1 = 0<BR>
0 AND 0 = 0<BR>
</FONT>
</BLOCKQUOTE>
As you see we get <B>1</B> only when both bits are <B>1</B>.
<BR><BR></LI>
<LI><B>TEST</B> - The same as <B>AND</B> but <B>for flags only</B>.
<BR><BR></LI>
<LI><B>OR</B> - Logical OR between all bits of two operands. These rules
apply:<BR>
<BLOCKQUOTE>
<FONT FACE="Fixedsys">
1 OR 1 = 1<BR>
1 OR 0 = 1<BR>
0 OR 1 = 1<BR>
0 OR 0 = 0<BR>
</FONT>
</BLOCKQUOTE>
As you see we get <B>1</B> every time when at least
one of the bits is <B>1</B>.
<BR><BR></LI>
<LI><B>XOR</B> - Logical XOR (exclusive OR) between all bits of two operands.
These rules apply:<BR>
<BLOCKQUOTE>
<FONT FACE="Fixedsys">
1 XOR 1 = 0<BR>
1 XOR 0 = 1<BR>
0 XOR 1 = 1<BR>
0 XOR 0 = 0<BR>
</FONT>
</BLOCKQUOTE>
As you see we get <B>1</B> every time when bits
are different from each other.
<BR><BR></LI>
</UL>
<HR>
<BR>
Second group: <B>MUL</B>, <B>IMUL</B>, <B>DIV</B>, <B>IDIV</B><BR><BR>
These types of operands are supported:<BR><BR>
<BLOCKQUOTE>
<FONT FACE="Fixedsys">
REG<BR>
memory<BR>
</FONT>
</BLOCKQUOTE>
<B>REG</B>:<FONT SIZE=-1> AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.</FONT><BR><BR>
<B>memory</B>: [BX], [BX+SI+7], variable, etc...<BR><BR>
<B>MUL</B> and <B>IMUL</B> instructions affect these flags only:<BR>
<B>CF</B>, <B>OF</B><BR>
When result is over operand size these flags are set to <B>1</B>, when result
fits in operand size these flags are set to <B>0</B>.
<BR><BR>
For <B>DIV</B> and <B>IDIV</B> flags are undefined.<BR><BR>
<UL>
<LI><B>MUL</B> - Unsigned multiply:
<BLOCKQUOTE>
when operand is a <B>byte</B>:<BR>
<FONT FACE="Fixedsys">AX = AL * operand</FONT>.
</BLOCKQUOTE>
<BLOCKQUOTE>
when operand is a <B>word</B>:<BR>
<FONT FACE="Fixedsys">(DX AX) = AX * operand</FONT>.
</BLOCKQUOTE>
</LI>
<LI><B>IMUL</B> - Signed multiply:
<BLOCKQUOTE>
when operand is a <B>byte</B>:<BR>
<FONT FACE="Fixedsys">AX = AL * operand</FONT>.
</BLOCKQUOTE>
<BLOCKQUOTE>
when operand is a <B>word</B>:<BR>
<FONT FACE="Fixedsys">(DX AX) = AX * operand</FONT>.
</BLOCKQUOTE>
</LI>
<LI><B>DIV</B> - Unsigned divide:
<BLOCKQUOTE>
when operand is a <B>byte</B>:<BR>
<FONT FACE="Fixedsys">AL = AX / operand<BR>
AH = remainder (modulus).
</FONT>.
</BLOCKQUOTE>
<BLOCKQUOTE>
when operand is a <B>word</B>:<BR>
<FONT FACE="Fixedsys">AX = (DX AX) / operand<BR>
DX = remainder (modulus).
</FONT>.
</BLOCKQUOTE>
</LI>
<LI><B>IDIV</B> - Signed divide:
<BLOCKQUOTE>
when operand is a <B>byte</B>:<BR>
<FONT FACE="Fixedsys">AL = AX / operand<BR>
AH = remainder (modulus).
</FONT>.
</BLOCKQUOTE>
<BLOCKQUOTE>
when operand is a <B>word</B>:<BR>
<FONT FACE="Fixedsys">AX = (DX AX) / operand<BR>
DX = remainder (modulus).
</FONT>.
</BLOCKQUOTE>
</LI>
</UL>
<HR>
<BR>
Third group: <B>INC</B>, <B>DEC</B>, <B>NOT</B>, <B>NEG</B><BR><BR>
These types of operands are supported:<BR><BR>
<BLOCKQUOTE>
<FONT FACE="Fixedsys">
REG<BR>
memory<BR>
</FONT>
</BLOCKQUOTE>
<B>REG</B>:<FONT SIZE=-1> AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.</FONT><BR><BR>
<B>memory</B>: [BX], [BX+SI+7], variable, etc...<BR><BR>
<B>INC</B>, <B>DEC</B> instructions affect these flags only:<BR>
<B>ZF</B>, <B>SF</B>, <B>OF</B>, <B>PF</B>, <B>AF</B>.<BR><BR>
<B>NOT</B> instruction does not affect any flags!<BR><BR>
<B>NEG</B> instruction affects these flags only:<BR>
<B>CF</B>, <B>ZF</B>, <B>SF</B>, <B>OF</B>, <B>PF</B>, <B>AF</B>.<BR><BR>
<UL>
<LI><B>NOT</B> - Reverse each bit of operand.
<BR><BR>
</LI>
<LI><B>NEG</B> - Make operand negative (two's complement).
Actually it reverses each bit of operand and then
adds 1 to it. For example 5 will become -5, and -2 will
become 2.
</LI>
</UL>
<HR>
<BR><BR><BR>
<HR>
<CENTER>
<A HREF="asm_tutorial_05.html"><B> <<< previous part <<< </B></A>
<A HREF="asm_tutorial_07.html"><B> >>> Next Part >>> </B></A>
</CENTER>
<HR>
<BR>
</FONT>
</BODY>
</HTML>