-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathasm_tutorial_07.html
executable file
·642 lines (426 loc) · 21.4 KB
/
asm_tutorial_07.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<HTML>
<HEAD>
<TITLE>8086 assembler tutorial for beginners (part 7)</TITLE>
<META name="description" content="Program Flow Control - Tutorial for Beginners">
</HEAD>
<BODY>
<FONT FACE="Verdana" SIZE=3>
<FONT SIZE=+1>
<B>8086 assembler tutorial for beginners (part 7)</B>
</FONT>
<BR><BR>
<FONT SIZE=+2><B>program flow control</B></FONT>
<BR><BR>
controlling the program flow is a very important thing, this is where
your program can make decisions according to certain conditions.
<BR><BR>
<UL>
<LI><B>unconditional jumps</B><BR><BR>
The basic instruction that transfers control to another point in the program is <B>JMP</B>.
<BR><BR>
The basic syntax of <B>JMP</B> instruction:
<BLOCKQUOTE>
<FONT FACE="Fixedsys">
JMP <U>label</U>
</FONT>
</BLOCKQUOTE>
To declare a <I>label</I> in your program, just type its name and add "<B>:</B>"
to the end, label can be any character combination but it cannot start with
a number, for example here are 3 legal label definitions:
<BLOCKQUOTE>
<FONT FACE="Fixedsys">
label1:<BR>
label2:<BR>
a:
</FONT>
</BLOCKQUOTE>
Label can be declared on a separate line or before any other instruction, for example:
<BLOCKQUOTE>
<FONT FACE="Fixedsys">
x1:<BR>
MOV AX, 1<BR><BR>
x2: MOV AX, 2<BR>
</FONT>
</BLOCKQUOTE>
here's an example of <B>JMP</B> instruction:<BR><BR>
<!-- START AUTOMATIC ASM TO HTML EXPORT -->
<pre><font size=2 face="Terminal">
<font color=#000064>org</font> 100h
<font color=#0000FF>mov</font> <font color=#C80000>ax</font>, 5 <font color=#008000>; set ax to 5. </font>
<font color=#0000FF>mov</font> <font color=#C80000>bx</font>, 2 <font color=#008000>; set bx to 2. </font>
<font color=#0000FF>jmp</font> calc <font color=#008000>; go to 'calc'. </font>
back: <font color=#0000FF>jmp</font> stop <font color=#008000>; go to 'stop'. </font>
calc:
<font color=#0000FF>add</font> <font color=#C80000>ax</font>, <font color=#C80000>bx</font> <font color=#008000>; add bx to ax. </font>
<font color=#0000FF>jmp</font> back <font color=#008000>; go 'back'. </font>
stop:
<font color=#0000FF>ret</font> <font color=#008000>; return to operating system. </font>
</font></pre>
<!-- emu8086 version 3.27xo -->
<!-- STOP AUTOMATIC ASM TO HTML EXPORT -->
<BR>
Of course there is an easier way to calculate the some of two numbers,
but it's still a good example of <B>JMP</B> instruction.
<BR>
As you can see from this example <B>JMP</B> is able to transfer control both
forward and backward.
It can jump anywhere in current code segment (65,535 bytes).
<BR><BR><BR>
</LI>
<LI><B>Short Conditional Jumps</B><BR><BR>
Unlike <B>JMP</B> instruction that does an unconditional jump, there are instructions
that do a conditional jumps (jump only when some conditions are in act).
These instructions are divided in three groups, first group just test single flag, second
compares numbers as signed, and third compares numbers as unsigned.
<BR><BR>
<B>Jump instructions that test single flag</B>
<BR><BR>
<FONT FACE="Fixedsys">
<TABLE BORDER=1 CELLPADDING=7>
<TR>
<TD BGCOLOR=yellow>Instruction</TD> <TD BGCOLOR=yellow>Description</TD>
<TD BGCOLOR=yellow>Condition</TD>
<TD BGCOLOR=yellow>Opposite Instruction</TD>
</TR>
<TR>
<TD>JZ , JE</TD> <TD>Jump if Zero (Equal).</TD> <TD> ZF = 1</TD> <TD>JNZ, JNE</TD>
</TR>
<TR>
<TD><NOBR>JC , JB, JNAE</NOBR></TD> <TD>Jump if Carry (Below, Not Above Equal).</TD>
<TD> CF = 1</TD> <TD>JNC, JNB, JAE</TD>
</TR>
<TR>
<TD>JS</TD> <TD>Jump if Sign.</TD> <TD> SF = 1</TD> <TD>JNS</TD>
</TR>
<TR>
<TD>JO</TD> <TD>Jump if Overflow.</TD> <TD> OF = 1</TD> <TD>JNO</TD>
</TR>
<TR>
<TD>JPE, JP</TD> <TD>Jump if Parity Even.</TD> <TD> PF = 1</TD> <TD>JPO</TD>
</TR>
<TR>
<TD>JNZ , JNE</TD> <TD>Jump if Not Zero (Not Equal).</TD> <TD> ZF = 0</TD> <TD>JZ, JE</TD>
</TR>
<TR>
<TD><NOBR>JNC , JNB, JAE</NOBR></TD> <TD>Jump if Not Carry (Not Below, Above Equal).</TD>
<TD> CF = 0</TD> <TD>JC, JB, JNAE</TD>
</TR>
<TR>
<TD>JNS</TD> <TD>Jump if Not Sign.</TD> <TD> SF = 0</TD> <TD>JS</TD>
</TR>
<TR>
<TD>JNO</TD> <TD>Jump if Not Overflow.</TD> <TD> OF = 0</TD> <TD>JO</TD>
</TR>
<TR>
<TD>JPO, JNP</TD> <TD>Jump if Parity Odd (No Parity).</TD> <TD> PF = 0</TD> <TD>JPE, JP</TD>
</TR>
</TABLE>
</FONT>
<BR><BR>
as you may already notice there are some instructions that do that same thing, that's correct,
they even are assembled into the same machine code, so it's good to remember that
when you compile <B>JE</B> instruction - you will get it disassembled as: <B>JZ</B>, <B>JC</B> is assembled the same as <B>JB</B> etc...<BR>
different names are used to make programs easier to understand, to code and most importantly to remember. very offset dissembler has no clue what the original instruction was look like that's why it uses the most common name.
<BR><BR>
if you emulate this code you will see that all instructions are assembled into <B>JNB</B>, the operational code (opcode) for this instruction is <B>73h</B> this instruction has fixed length of two bytes, the second byte is number of bytes to add to the <B>IP</B> register if the condition is true. because the instruction has only 1 byte to keep the offset it is limited to pass control to -128 bytes back or 127 bytes forward, this value is always signed.
<!-- START AUTOMATIC ASM TO HTML EXPORT -->
<pre><font size=2 face="Terminal">
<font color=#0000FF>jnc</font> a
<font color=#0000FF>jnb</font> a
<font color=#0000FF>jae</font> a
<font color=#0000FF>mov</font> <font color=#C80000>ax</font>, 4
a: <font color=#0000FF>mov</font> <font color=#C80000>ax</font>, 5
<font color=#0000FF>ret</font>
</font></pre>
<!-- emu8086 version 3.27xo -->
<!-- STOP AUTOMATIC ASM TO HTML EXPORT -->
<BR><BR><BR>
<B>Jump instructions for signed numbers</B>
<BR><BR>
<FONT FACE="Fixedsys">
<TABLE BORDER=1 CELLPADDING=7>
<TR>
<TD BGCOLOR=yellow>Instruction</TD> <TD BGCOLOR=yellow>Description</TD>
<TD BGCOLOR=yellow>Condition</TD>
<TD BGCOLOR=yellow>Opposite Instruction</TD>
</TR>
<TR>
<TD>JE , JZ</TD> <TD>Jump if Equal (<FONT COLOR=red>=</FONT>).<BR>
Jump if Zero.</TD>
<TD ALIGN=Center>ZF = 1</TD> <TD>JNE, JNZ</TD>
</TR>
<TR>
<TD>JNE , JNZ</TD> <TD>Jump if Not Equal (<FONT COLOR=red><></FONT>).<BR>
Jump if Not Zero.</TD>
<TD ALIGN=Center>ZF = 0</TD> <TD>JE, JZ</TD>
</TR>
<TR>
<TD>JG , JNLE</TD> <TD>Jump if Greater (<FONT COLOR=red>></FONT>).<BR>
Jump if Not Less or Equal <NOBR>(<FONT COLOR=red>not <=</FONT>).</NOBR></TD>
<TD ALIGN=Center>ZF = 0<BR>and<BR>SF = OF</TD> <TD>JNG, JLE</TD>
</TR>
<TR>
<TD>JL , JNGE</TD> <TD>Jump if Less (<FONT COLOR=red><</FONT>).<BR>
Jump if Not Greater or Equal <NOBR>(<FONT COLOR=red>not >=</FONT>).</NOBR></TD>
<TD ALIGN=Center>SF <> OF</TD> <TD>JNL, JGE</TD>
</TR>
<TR>
<TD>JGE , JNL</TD> <TD><NOBR>Jump if Greater or Equal (<FONT COLOR=red>>=</FONT>).</NOBR><BR>
Jump if Not Less (<FONT COLOR=red>not <</FONT>).</TD>
<TD ALIGN=Center>SF = OF</TD> <TD>JNGE, JL</TD>
</TR>
<TR>
<TD>JLE , JNG</TD> <TD><NOBR>Jump if Less or Equal (<FONT COLOR=red><=</FONT>).</NOBR><BR>
Jump if Not Greater <NOBR>(<FONT COLOR=red>not ></FONT>).</NOBR></TD>
<TD ALIGN=Center>ZF = 1<BR>or<BR>SF <> OF</TD> <TD>JNLE, JG</TD>
</TR>
</TABLE>
</FONT>
<BR><BR>
<FONT FACE="Fixedsys"><></FONT> - sign means not equal.
<BR><BR><BR>
<B>Jump instructions for unsigned numbers</B>
<BR><BR>
<FONT FACE="Fixedsys">
<TABLE BORDER=1 CELLPADDING=7>
<TR>
<TD BGCOLOR=yellow>Instruction</TD> <TD BGCOLOR=yellow>Description</TD>
<TD BGCOLOR=yellow>Condition</TD>
<TD BGCOLOR=yellow>Opposite Instruction</TD>
</TR>
<TR>
<TD>JE , JZ</TD> <TD>Jump if Equal (<FONT COLOR=red>=</FONT>).<BR>
Jump if Zero.</TD>
<TD ALIGN=Center>ZF = 1</TD> <TD>JNE, JNZ</TD>
</TR>
<TR>
<TD>JNE , JNZ</TD> <TD>Jump if Not Equal (<FONT COLOR=red><></FONT>).<BR>
Jump if Not Zero.</TD>
<TD ALIGN=Center>ZF = 0</TD> <TD>JE, JZ</TD>
</TR>
<TR>
<TD>JA , JNBE</TD> <TD>Jump if Above (<FONT COLOR=red>></FONT>).<BR>
Jump if Not Below or Equal <NOBR>(<FONT COLOR=red>not <=</FONT>).</NOBR></TD>
<TD ALIGN=Center>CF = 0<BR>and<BR>ZF = 0</TD> <TD>JNA, JBE</TD>
</TR>
<TR>
<TD><NOBR>JB , JNAE, JC</NOBR></TD> <TD>Jump if Below (<FONT COLOR=red><</FONT>).<BR>
Jump if Not Above or Equal <NOBR>(<FONT COLOR=red>not >=</FONT>).</NOBR><BR>
Jump if Carry.</TD>
<TD ALIGN=Center>CF = 1</TD> <TD>JNB, JAE, JNC</TD>
</TR>
<TR>
<TD><NOBR>JAE , JNB, JNC</NOBR></TD> <TD>Jump if Above or Equal (<FONT COLOR=red>>=</FONT>).<BR>
Jump if Not Below <NOBR>(<FONT COLOR=red>not <</FONT>).</NOBR><BR>
Jump if Not Carry.</TD>
<TD ALIGN=Center>CF = 0</TD> <TD>JNAE, JB</TD>
</TR>
<TR>
<TD><NOBR>JBE , JNA</NOBR></TD> <TD>Jump if Below or Equal (<FONT COLOR=red><=</FONT>).<BR>
Jump if Not Above <NOBR>(<FONT COLOR=red>not ></FONT>).</NOBR></TD>
<TD ALIGN=Center>CF = 1<BR>or<BR>ZF = 1</TD> <TD>JNBE, JA</TD>
</TR>
</TABLE>
</FONT>
<BR><BR>
Generally, when it is required to compare numeric values <B>CMP</B> instruction
is used (it does the same as <B>SUB</B> (subtract) instruction, but does not keep
the result, just affects the flags).<BR><BR>
The logic is very simple, for example:<BR>
it's required to compare
<FONT FACE="Fixedsys">5</FONT> and <FONT FACE="Fixedsys">2</FONT>,<BR>
<FONT FACE="Fixedsys">5 - 2 = 3</FONT><BR>
the result is not zero (Zero Flag
is set to <FONT FACE="Fixedsys">0</FONT>).
<BR><BR>
Another example:<BR>
it's required to compare <FONT FACE="Fixedsys">7</FONT> and
<FONT FACE="Fixedsys">7</FONT>,<BR>
<FONT FACE="Fixedsys">7 - 7 = 0</FONT><BR>
the result is zero! (Zero Flag
is set to <FONT FACE="Fixedsys">1</FONT> and <B>JZ</B> or <B>JE</B> will do the jump).
<BR><BR>
here's an example of <B>CMP</B> instruction and conditional jump:<BR><BR>
<!-- START AUTOMATIC ASM TO HTML EXPORT -->
<pre><font size=2 face="Terminal">
<font color=#000064>include</font> <font color=#800080>"emu8086.inc"</font>
<font color=#000064>org</font> 100h
<font color=#0000FF>mov</font> <font color=#C80000>al</font>, 25 <font color=#008000>; set al to 25. </font>
<font color=#0000FF>mov</font> <font color=#C80000>bl</font>, 10 <font color=#008000>; set bl to 10. </font>
<font color=#0000FF>cmp</font> <font color=#C80000>al</font>, <font color=#C80000>bl</font> <font color=#008000>; compare al - bl. </font>
<font color=#0000FF>je</font> equal <font color=#008000>; jump if al = bl (zf = 1). </font>
putc <font color=#800080>'n'</font> <font color=#008000>; if it gets here, then al <> bl, </font>
<font color=#0000FF>jmp</font> stop <font color=#008000>; so print 'n', and jump to stop. </font>
equal: <font color=#008000>; if gets here, </font>
putc <font color=#800080>'y'</font> <font color=#008000>; then al = bl, so print 'y'. </font>
stop:
<font color=#0000FF>ret</font> <font color=#008000>; gets here no matter what. </font>
</font></pre>
<!-- emu8086 version 3.27xo -->
<!-- STOP AUTOMATIC ASM TO HTML EXPORT -->
<BR><BR>
try the above example with different numbers for <B>AL</B> and <B>BL</B>,
open flags by clicking on flags button, use single step
and see what happens. you can use <b>F5</b> hotkey to recompile and reload the program into the emulator.
<BR><BR>
<HR>
<BR>
<!-- 20 -->
<B>loops</B>
<BR><BR>
<FONT FACE="Fixedsys">
<TABLE BORDER=1 CELLPADDING=7>
<TR>
<TD BGCOLOR=yellow>instruction</TD>
<TD BGCOLOR=yellow align=center>operation and jump condition</TD>
<TD BGCOLOR=yellow>opposite instruction</TD>
</TR>
<TR>
<TD>LOOP</TD> <TD>decrease cx, jump to label if cx not zero. </TD> <TD>DEC CX <FONT SIZE=1 FACE=Verdana>and</FONT> JCXZ</TD>
</TR>
<TR>
<TD>LOOPE</TD> <TD>decrease cx, jump to label if cx not zero and equal (zf = 1).</TD> <TD>LOOPNE</TD>
</TR>
<TR>
<TD>LOOPNE</TD> <TD>decrease cx, jump to label if cx not zero and not equal (zf = 0). </TD> <TD>LOOPE</TD>
</TR>
<TR>
<TD>LOOPNZ</TD> <TD>decrease cx, jump to label if cx not zero and zf = 0.</TD> <TD>LOOPZ</TD>
</TR>
<TR>
<TD>LOOPZ</TD> <TD>decrease cx, jump to label if cx not zero and zf = 1. </TD> <TD>LOOPNZ</TD>
</TR>
<TR>
<TD>JCXZ</TD> <TD>jump to label if cx is zero. </TD> <TD>OR CX, CX <FONT SIZE=1 FACE=Verdana>and</FONT> JNZ</TD>
</TR>
</TABLE>
</FONT>
<BR><BR>
loops are basically the same jumps, it is possible to code loops without using the loop instruction, by just
using conditional jumps and compare, and this is just what loop does. all loop instructions use <B>CX</B> register to count steps, as you know CX register has 16 bits and the maximum value it can hold is 65535 or FFFF, however with some agility it is possible to put one loop into another, and another into another two, and three and etc... and receive a nice value of 65535 * 65535 * 65535 ....till infinity.... or the end of ram or stack memory. it is possible store original value of cx register using <B>push cx</B> instruction and return it to original when the internal loop ends with <B>pop cx</B>, for example:
<!-- START AUTOMATIC ASM TO HTML EXPORT -->
<pre><font size=2 face="Terminal">
<font color=#000064>org</font> 100h
<font color=#0000FF>mov</font> <font color=#C80000>bx</font>, 0 <font color=#008000>; total step counter. </font>
<font color=#0000FF>mov</font> <font color=#C80000>cx</font>, 5
k1: <font color=#0000FF>add</font> <font color=#C80000>bx</font>, 1
<font color=#0000FF>mov</font> <font color=#C80000>al</font>, <font color=#800080>'1'</font>
<font color=#0000FF>mov</font> <font color=#C80000>ah</font>, 0eh
<font color=#0000FF>int</font> 10h
<font color=#0000FF>push</font> <font color=#C80000>cx</font>
<font color=#0000FF>mov</font> <font color=#C80000>cx</font>, 5
k2: <font color=#0000FF>add</font> <font color=#C80000>bx</font>, 1
<font color=#0000FF>mov</font> <font color=#C80000>al</font>, <font color=#800080>'2'</font>
<font color=#0000FF>mov</font> <font color=#C80000>ah</font>, 0eh
<font color=#0000FF>int</font> 10h
<font color=#0000FF>push</font> <font color=#C80000>cx</font>
<font color=#0000FF>mov</font> <font color=#C80000>cx</font>, 5
k3: <font color=#0000FF>add</font> <font color=#C80000>bx</font>, 1
<font color=#0000FF>mov</font> <font color=#C80000>al</font>, <font color=#800080>'3'</font>
<font color=#0000FF>mov</font> <font color=#C80000>ah</font>, 0eh
<font color=#0000FF>int</font> 10h
<font color=#0000FF>loop</font> k3 <font color=#008000>; internal in internal loop. </font>
<font color=#0000FF>pop</font> <font color=#C80000>cx</font>
<font color=#0000FF>loop</font> k2 <font color=#008000>; internal loop. </font>
<font color=#0000FF>pop</font> <font color=#C80000>cx</font>
<font color=#0000FF>loop</font> k1 <font color=#008000>; external loop. </font>
<font color=#0000FF>ret</font>
</font></pre>
<!-- emu8086 version 3.27xo -->
<!-- STOP AUTOMATIC ASM TO HTML EXPORT -->
bx counts total number of steps, by default emulator shows values in hexadecimal, you can double click the register to see the value in all available bases.
<BR><BR>
just like all other conditional jumps loops have an opposite companion that can help to create workarounds, when the address of desired location is too far assemble automatically assembles reverse and long jump instruction, making total of 5 bytes instead of just 2, it can be seen in disassembler as well.
<BR><BR>
<BR>
for more detailed description and examples refer to <A HREF="8086_instruction_set.html"><B>complete 8086 instruction set</B></A>
<!-- 20e -->
<BR><BR>
<HR>
<BR>
All conditional jumps have one big limitation, unlike <B>JMP</B> instruction
they can only jump <B>127</B> bytes forward and <B>128</B> bytes backward (note that most
instructions are assembled into 3 or more bytes).
<BR><BR>
We can easily avoid this limitation using a cute trick:<BR><BR>
<UL>
<LI>Get an opposite conditional jump instruction from the table above,
make it jump to <I>label_x</I>.
<BR><BR></LI>
<LI>Use <B>JMP</B> instruction to jump to desired location.
<BR><BR></LI>
<LI>Define <I>label_x:</I> just after the <B>JMP</B> instruction.
<BR><BR></LI>
</UL>
<I>label_x:</I> - can be any valid label name, but there must not be two or more labels with the same name.
<BR><BR>
here's an example:<BR><BR>
<!-- START AUTOMATIC ASM TO HTML EXPORT -->
<pre><font size=2 face="Terminal">
<font color=#000064>include</font> <font color=#800080>"emu8086.inc"</font>
<font color=#000064>org</font> 100h
<font color=#0000FF>mov</font> <font color=#C80000>al</font>, 5
<font color=#0000FF>mov</font> <font color=#C80000>bl</font>, 5
<font color=#0000FF>cmp</font> <font color=#C80000>al</font>, <font color=#C80000>bl</font> <font color=#008000>; compare al - bl. </font>
<font color=#008000>; je equal ; there is only 1 byte </font>
<font color=#0000FF>jne</font> not_equal <font color=#008000>; jump if al <> bl (zf = 0). </font>
<font color=#0000FF>jmp</font> equal
not_equal:
<font color=#0000FF>add</font> <font color=#C80000>bl</font>, <font color=#C80000>al</font>
<font color=#0000FF>sub</font> <font color=#C80000>al</font>, 10
<font color=#0000FF>xor</font> <font color=#C80000>al</font>, <font color=#C80000>bl</font>
<font color=#0000FF>jmp</font> skip_data
<font color=#000064>db</font> 256 dup(0) <font color=#008000>; 256 bytes </font>
skip_data:
putc <font color=#800080>'n'</font> <font color=#008000>; if it gets here, then al <> bl, </font>
<font color=#0000FF>jmp</font> stop <font color=#008000>; so print 'n', and jump to stop. </font>
equal: <font color=#008000>; if gets here, </font>
putc <font color=#800080>'y'</font> <font color=#008000>; then al = bl, so print 'y'. </font>
stop:
<font color=#0000FF>ret</font>
</font></pre>
<!-- emu8086 version 3.27xo -->
<!-- STOP AUTOMATIC ASM TO HTML EXPORT -->
</UL>
<BR><BR>
<FONT COLOR=green>Note: the latest version of the integrated 8086 assembler automatically creates a workaround by replacing the conditional jump with the opposite, and adding big unconditional jump. To check if you have the latest version of emu8086 click <B>help-> check for an update</b> from the menu.</font>
<BR><BR>
<HR>
<BR>
Another, yet rarely used method is providing an immediate value instead
of label. When immediate value starts with $ relative jump
is performed, otherwise compiler calculates instruction that jumps directly
to given offset. For example:<BR><BR>
<TABLE BORDER=1 CELLPADDING=10 WIDTH=50%><TR><TD>
<!-- START AUTOMATIC ASM TO HTML EXPORT -->
<pre><font size=2 face="Terminal">
<font color=#000064>org</font> 100h
<font color=#008000>; unconditional jump forward: </font>
<font color=#008000>; skip over next 3 bytes + itself </font>
<font color=#008000>; the machine code of short jmp instruction takes 2 bytes. </font>
<font color=#0000FF>jmp</font> $3<font color=#0064C8>+</font>2
a <font color=#000064>db</font> 3 <font color=#008000>; 1 byte. </font>
b <font color=#000064>db</font> 4 <font color=#008000>; 1 byte. </font>
c <font color=#000064>db</font> 4 <font color=#008000>; 1 byte. </font>
<font color=#008000>; conditional jump back 5 bytes: </font>
<font color=#0000FF>mov</font> <font color=#C80000>bl</font>,9
<font color=#0000FF>dec</font> <font color=#C80000>bl</font> <font color=#008000>; 2 bytes. </font>
<font color=#0000FF>cmp</font> <font color=#C80000>bl</font>, 0 <font color=#008000>; 3 bytes. </font>
<font color=#0000FF>jne</font> $<font color=#0064C8>-</font>5 <font color=#008000>; jump 5 bytes back </font>
<font color=#0000FF>ret</font>
</font></pre>
<!-- emu8086 version 3.27xo -->
<!-- STOP AUTOMATIC ASM TO HTML EXPORT -->
</TD></TR></TABLE>
<BR><BR><BR>
<HR>
<CENTER>
<A HREF="asm_tutorial_06.html"><B> <<< previous part <<< </B></A>
<A HREF="asm_tutorial_08.html"><B> >>> Next Part >>> </B></A>
</CENTER>
<HR>
<BR>
</FONT>
</BODY>
</HTML>