summaryrefslogtreecommitdiff
path: root/miniemu/doc/lecture.html
blob: 4a7dfd7903b68f010dd2fb3a9c15b777525e6b4b (plain)
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <title>Instruction Format Design</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta http-equiv="Expires" content="Wed, 31 Jan 2007 23:59:59 GMT" />
    </head>
    <body>
    <p>
        CIS-77 Home
        <a href="http://www.c-jump.com/CIS77/CIS77syllabus.htm"><tt>http://www.c-jump.com/CIS77/CIS77syllabus.htm</tt></a>
    </p>
    <h3>
        Instruction Format Design
    </h3>
    <hr />
        <ol>
            <li><a href="#Y77_0010_instruction_format">Instruction Format Design</a></li>
            <li><a href="#Y77_0020_encoding_opcodes">Encoding The Opcodes</a></li>
            <li><a href="#Y77_0030_encoding_opcodes_cont">Encoding The Opcodes, Cont.</a></li>
            <li><a href="#Y77_0040_small_opcodes">The Goal to Keep Opcodes Small</a></li>
            <li><a href="#Y77_0050_opcode_length">Variable-length Opcodes</a></li>
            <li><a href="#Y77_0060_opcode_length_cont">Variable-length Opcodes, Cont.</a></li>
            <li><a href="#Y77_0070_choosing_opcodes_one">Example: One-byte Opcodes</a></li>
            <li><a href="#Y77_0080_choosing_opcodes_two">Example: Two-byte Opcodes</a></li>
            <li><a href="#Y77_0090_choosing_opcodes_three">Example: Three-byte Opcodes</a></li>
            <li><a href="#Y77_0100_trade_offs">Opcode Length Trade-offs</a></li>
            <li><a href="#Y77_0110_future_planning">Planning for the future</a></li>
            <li><a href="#Y77_0120_selecting">Selecting Instruction Set</a></li>
            <li><a href="#Y77_0130_instruction_groups">Instruction Groups</a></li>
            <li><a href="#Y77_0140_encoding_instructions">Encoding Instructions</a></li>
            <li><a href="#Y77_0150_design_trade_offs">Opcode Design Trade-offs</a></li>
            <li><a href="#Y77_0160_reducing_complexity">Reducing x86 ISA to a Simplified Version</a></li>
            <li><a href="#Y77_0170_mov_instruction">The MOV Instruction</a></li>
            <li><a href="#Y77_0180_arithmetic_n_logical">Arithmetic and Logical Instructions</a></li>
            <li><a href="#Y77_0190_simplified_encoding">Simplified Instruction Encoding (not x86!)</a></li>
            <li><a href="#Y77_0200_simplified_encoding_cont">Simplified Instruction Encoding, Cont. (not x86!)</a></li>
            <li><a href="#Y77_0210_simplified_encoding_example">Simplified Instruction Encoding Example (not x86!)</a></li>
            <li><a href="#Y77_0220_simplified_multibyte">Simplified Multibyte Instructions (not x86!)</a></li>
            <li><a href="#Y77_0230_simplified_multibyte_cont">Simplified Multibyte Instructions Cont. (not x86!)</a></li>
            <li><a href="#Y77_0240_simplified_special_opcode">Simplified Special Opcode Instructions (not x86!)</a></li>
            <li><a href="#Y77_0250_simplified_jump">Simplified Jump Instructions (not x86!)</a></li>
            <li><a href="#Y77_0260_simplified_conditional_jump">Simplified Conditional Jump Instructions (not x86!)</a></li>
            <li><a href="#Y77_0270_simplified_illegal">Simplified Instructions Reserved Opcode (not x86!)</a></li>
            <li><a href="#Y77_0280_simplified_zero_operand">Simplified Zero-Operand Instructions (not x86!)</a></li>
            <li><a href="#Y77_0290_extending">Extending the Simplified Instruction Set (not x86!)</a></li>
            <li><a href="#Y77_0300_problem_extending">Problem with Extending the Simplified Instruction Set (not x86!)</a></li>
            <li><a href="#Y77_0310_prefix_extending">Prefix-Extending the Simplified Instruction Set (not x86!)</a></li>
            <li><a href="#Y77_0320_prefix_extending_example">Prefix-Extending the Simplified Instruction Set Example (not x86!)</a></li>
        </ol>
<a id="Y77_0010_instruction_format"></a>


    <h3>
        1. Instruction Format Design
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    Since opcodes are submitted to the decoder circuits, encoding opcodes is quite more involved rather than just assigning numbers.
   </p>
  </li>
 </ul>
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul>
  <li>
   <p>
    Most important feature of instruction set design -
   </p>
   <ul style="list-style-type:none;">
    <li>
     <p>
       make opcodes easy to decode.
     </p>
    </li>
   </ul>
  </li>
  <li>
   <p>
    The easiest way to do this -
   </p>
   <ul style="list-style-type:none;">
    <li>
     <p>
       break up the opcode into several different bit fields.
     </p>
    </li>
   </ul>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Opcode fields:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/simple_opcode.png" alt="opcode byte" />
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul>
  <li>
   <p>
    Each field is contributing part of the information necessary to execute the full instruction.
   </p>
  </li>
  <li>
   <p>
    The smaller the bit fields, the easier it is for hardware to decode them.
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0020_encoding_opcodes"></a>


    <h3>
        2. Encoding The Opcodes
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    Suppose we decided to design a brand-new CPU with a set of 7-bit opcodes.
   </p>
   <ul>
    <li>
     <p>
      With an opcode of this size we could encode <strong>2<sup>7</sup> = 128</strong> different instructions.
     </p>
    </li>
    <li>
     <p>
      Decoding individual instructions requires a 7-line to 128-line decoder - an expensive piece of circuitry.
     </p>
    </li>
   </ul>
  </li>
  <li>
   <p>
    If you have 128 <em>truly unique</em> instructions, there's little you can do other than to decode each instruction individually.
   </p>
  </li>
  <li>
   <p>
    However, assuming our instructions contain <em>certain patterns</em>, we could reduce the hardware cost by replacing this large decoder with a few smaller decoders.
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0030_encoding_opcodes_cont"></a>


    <h3>
        3. Encoding The Opcodes, Cont.
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    For example, on the x86 CPUs the opcodes for
   </p>
<pre>    mov eax, ebx    ; copy data from <span style="color: blue">EBX</span> register to <span style="color: blue">EAX</span> register
</pre>
   <p>
    and
   </p>
<pre>    mov ecx, edx    ; copy data from <span style="color: blue">EDX</span> register to <span style="color: blue">ECX</span> register
</pre>
   <p>
    are different, but both instructions are <em>related</em>: they both move data from one register to another.
   </p>
  </li>
  <li>
   <p>
    The only difference between the two <span style="color: blue">MOV</span>s is the <em>source</em> and <em>destination</em> operands.
   </p>
  </li>
  <li>
   <p>
    This suggests that we could encode instructions like <span style="color: blue">MOV</span> with a <em>sub-opcode</em> and encode the operands using other bits within the opcode.
   </p>
   <p>
    &nbsp;
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0040_small_opcodes"></a>


    <h3>
        4. The Goal to Keep Opcodes Small
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    Another important criteria: keep instruction sizes within a reasonable range.
   </p>
  </li>
  <li>
   <p>
    CPU with unnecessarily long instructions will consume extra memory for programs in memory.
   </p>
  </li>
  <li>
   <p>
    Long instructions hurt overall CPU performance.
   </p>
  </li>
  <li>
   <p>
    Using encoding with <strong>n</strong>-bit size opcodes leaves us with <strong>2<sup>n</sup></strong> different instructions.
   </p>
  </li>
  <li>
   <p>
    With <strong>n</strong> bits, it seems like you can't do it with any fewer but <strong>2<sup>n</sup></strong> opcodes.
   </p>
   <p>
    &nbsp;
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0050_opcode_length"></a>


    <h3>
        5. Variable-length Opcodes
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    We can make some opcodes longer than <strong>n</strong> bits...
   </p>
   <ul style="list-style-type:none;">
    <li>
     <p>
       ...and that is the secret to reducing the size of a typical program on the CPU!
     </p>
    </li>
   </ul>
   <p>
    (This strategy is acceptable only for CISC processors; RISC<sup>(*)</sup> processors prefer uniform 32-bit or 64-bit instructions.)
   </p>
  </li>
  <li>
   <p>
    Assuming that CPU is capable of reading <strong>byte-sized</strong> quantities from memory, each opcode must be some even multiple of 8-bits long.
   </p>
  </li>
  <li>
   <p>
    Another point to consider is the space for instruction operands:
   </p>
   <ul>
    <li>
     <p>
      RISC designers include all operands in their opcode.
     </p>
    </li>
    <li>
     <p>
      CISC designers, including x86, place constants and address displacements (offsets) apart from the opcode.
     </p>
    </li>
   </ul>
  </li>
 </ul>
 <ul style="list-style-type:none;">
  <li>
   <p>
     ______________
   </p>
  </li>
  <li>
   <p>
    <sup>(*)</sup> CISC stands for
    <noindex><a href="http://en.wikipedia.org/wiki/CISC_processor" target="_blank">complex instruction set computer</a></noindex>
    design,
    <br />
    while RISC is a
    <noindex><a href="http://en.wikipedia.org/wiki/Reduced_instruction_set_computer" target="_blank">reduced instruction set computer.</a></noindex>
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0060_opcode_length_cont"></a>


    <h3>
        6. Variable-length Opcodes, Cont.
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    Most of processors predating the 8086 had 8-bit opcodes, allowing 256 different instructions.
   </p>
  </li>
  <li>
   <p>
    A two-byte opcode would allow 65,536 different instructions...
   </p>
   <ul style="list-style-type:none;">
    <li>
     <p>
       ...but from a practical standpoint,
     </p>
     <ul>
      <li>
       <p>
        <em>most-frequently-used</em> instructions continue to have 8-bit opcodes,
       </p>
      </li>
      <li>
       <p>
        <em>less-frequently-used</em> instructions have two-byte opcodes,
       </p>
      </li>
      <li>
       <p>
        three (or more) byte opcodes are mostly for the <em>rarely-used-instructions</em>.
       </p>
      </li>
     </ul>
    </li>
   </ul>
  </li>
  <li>
   <p>
    Such strategy makes a typical program significantly shorter, compared to a uniform two-byte opcode.
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0070_choosing_opcodes_one"></a>


    <h3>
        7. Example: One-byte Opcodes
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    Assume that two <em>high-order</em> bits of an imaginary opcode are<strong> <em>not</em> 00</strong>, and the opcode size is strictly <strong>one</strong> byte long.
   </p>
  </li>
  <li>
   <p>
    The 6-bit field marked <strong>xxxxxx</strong> provides <strong>2<sup>6</sup> = 64</strong> unique bit patterns.
   </p>
  </li>
  <li>
   <p>
    Together with three non-zero high-order combinations (<strong>01</strong>, <strong>10</strong>, and <strong>11</strong>), 192 different one-byte instructions can be encoded:
   </p>
   <ul style="list-style-type:none;">
    <li>
     <p>
       64 &times; 3 = 192
     </p>
    </li>
   </ul>
  </li>
 </ul>
 <ul style="list-style-type:none;">
  <li>
   <p>
     &nbsp
    <img src="http://www.c-jump.com/CIS77/images/opcode_size_management_one.png" alt="One-byte Opcodes" />
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0080_choosing_opcodes_two"></a>


    <h3>
        8. Example: Two-byte Opcodes
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    Assume that if three <em>high-order</em> bits of the opcode are equal <strong>001</strong>, it signals that the opcode size is <strong>two</strong> bytes.
   </p>
  </li>
  <li>
   <p>
    If so, the remaining 13 bits of the total 16-bit opcode let us encode
   </p>
   <ul style="list-style-type:none;">
    <li>
     <p>
       <strong>2<sup>13</sup> = 8192</strong>
     </p>
    </li>
   </ul>
   <p>
    different instructions.
   </p>
  </li>
 </ul>
 <ul style="list-style-type:none;">
  <li>
   <p>
     &nbsp
    <img src="http://www.c-jump.com/CIS77/images/opcode_size_management_two.png" alt="Two-byte Opcodes" />
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0090_choosing_opcodes_three"></a>


    <h3>
        9. Example: Three-byte Opcodes
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    If three <em>high-order</em> bits of the opcode are equal <strong>000</strong>, the imaginary opcode is <strong>three</strong> bytes long.
   </p>
  </li>
  <li>
   <p>
    If so, the remaining 21 bits of the total 24-bit opcode let us encode two million (<strong>2<sup>21</sup></strong>) different instructions.
   </p>
  </li>
 </ul>
 <ul style="list-style-type:none;">
  <li>
   <p>
     &nbsp
    <img src="http://www.c-jump.com/CIS77/images/opcode_size_management_three.png" alt="Three-byte Opcodes" />
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0100_trade_offs"></a>


    <h3>
        10. Opcode Length Trade-offs
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    Although we are able modify opcode sizes to have smaller programs, it comes at a price:
   </p>
   <ul>
    <li>
     <p>
      decoding the instructions is a bit more complicated.
     </p>
    </li>
    <li>
     <p>
      Before decoding opcode field, the CPU must first decode the instruction size.
     </p>
    </li>
    <li>
     <p>
      This extra step hurts the performance.
     </p>
    </li>
   </ul>
  </li>
  <li>
   <p>
    These are the reasons, along with some others, why most popular RISC architectures avoid variable-sized instructions.
   </p>
  </li>
  <li>
   <p>
    However, x86 uses the <em>variable-length opcodes</em>, since saving memory is such an admirable goal.
   </p>
   <p>
    &nbsp;
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0110_future_planning"></a>


    <h3>
        11. Planning for the future
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    There will be a need for new instructions in the future.
   </p>
  </li>
  <li>
   <p>
    Reserving some opcodes specifically for that purpose is a <em>really good idea</em>.
   </p>
  </li>
  <li>
   <p>
    For example, reserving a block of 64 <em>one-byte opcodes</em> may seem extravagant, but could also be a rewarding foresight!
   </p>
   <p>
    &nbsp;
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0120_selecting"></a>


    <h3>
        12. Selecting Instruction Set
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    <em>Keep in mind that it's much easier to add an instruction later than to remove it</em>.
   </p>
  </li>
  <li>
   <p>
    For starters, it's better to stick with simpler design rather than a more complex one.
   </p>
  </li>
  <li>
   <p>
    First step: let's choose some generic instruction types for a brand-new CPU.
   </p>
  </li>
  <li>
   <p>
    For example, most processors will have instructions like the following:
   </p>
   <ul>
    <li>
     <p>
      Data movement instructions (e.g., <span style="color: blue">MOV</span>)
     </p>
    </li>
    <li>
     <p>
      Arithmetic and logical instructions (e.g., <span style="color: blue">ADD</span>, <span style="color: blue">SUB</span>, <span style="color: blue">AND</span>, <span style="color: blue">OR</span>, <span style="color: blue">NOT</span>)
     </p>
    </li>
    <li>
     <p>
      Comparison instruction, <span style="color: blue">CMP</span>
     </p>
    </li>
    <li>
     <p>
      A set of conditional jump instructions <span style="color: blue">JE</span>, <span style="color: blue">JNE</span>, etc., generally used after the compare instructions.
     </p>
    </li>
    <li>
     <p>
      Input/Output instructions <span style="color: blue">GET</span> and <span style="color: blue">PUT</span>.
     </p>
    </li>
   </ul>
  </li>
  <li>
   <p>
    The bottom line: <em>allow programmers to efficiently write programs using as few instructions as possible</em>.
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0130_instruction_groups"></a>


    <h3>
        13. Instruction Groups
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    Once the initial instruction set is determined, next step is to assign opcodes for them.
   </p>
  </li>
  <li>
   <p>
    To do so, instructions are separated into groups with common characteristics:
   </p>
   <ul>
    <li>
     <p>
      For example, an <span style="color: blue">ADD</span> instruction supports exact same set of operands as the <span style="color: blue">SUB</span> instruction.
     </p>
    </li>
    <li>
     <p>
      <span style="color: blue">NOT</span> instruction requires a single operand, so does the <span style="color: blue">NEG</span> instruction.
     </p>
    </li>
    <li>
     <p>
      etc.
     </p>
    </li>
   </ul>
  </li>
  <li>
   <p>
    Once all the instructions are grouped by their respected categories, the next step is to encode the actual opcodes.
   </p>
   <p>
    &nbsp;
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0140_encoding_instructions"></a>


    <h3>
        14. Encoding Instructions
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul>
  <li>
   <p>
    Some bits are needed to identify:
   </p>
   <ul>
    <li>
     <p>
      instruction group
     </p>
    </li>
    <li>
     <p>
      instruction code
     </p>
    </li>
    <li>
     <p>
      operand types: <em>registers, memory locations, constants</em>.
     </p>
    </li>
   </ul>
  </li>
  <li>
   <p>
    All of the above has a direct impact on the instruction size.
   </p>
  </li>
  <li>
   <p>
    For example, 8-bit opcode could be split into
   </p>
   <ul>
    <li>
     <p>
      one 3-bit <strong>iii</strong> field to describe instruction and its group, and
     </p>
    </li>
    <li>
     <p>
      two fields, <strong>rr</strong> and <strong>mmm</strong>, (5 bits together) to specify where the instruction operands could be found.
     </p>
    </li>
   </ul>
   <p>
    &nbsp;
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     An opcode byte:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/simple_opcode.png" alt="opcode byte" />
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>


<a id="Y77_0150_design_trade_offs"></a>


    <h3>
        15. Opcode Design Trade-offs
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    Encoding operands is always a problem, because instructions have large number of operand combinations.
   </p>
  </li>
  <li>
   <p>
    For example, an x86 <span style="color: blue">MOV</span> instruction requires a two-byte opcode.
   </p>
  </li>
  <li>
   <p>
    However, Intel noticed that two instructions
   </p>
<pre>    mov memory, eax   ; store register in a variable
    mov eax, memory   ; load register from a variable
</pre>
   <p>
    occur <em>very</em> frequently. These instructions store <strong>EAX</strong> register into memory and load <strong>EAX</strong> from memory.
   </p>
  </li>
  <li>
   <p>
    As a result, x86 provided a special <strong>one</strong>-byte versions of dedicated <span style="color: blue">MOV</span> instructions to reduce program clutter.
   </p>
  </li>
  <li>
   <p>
    Note that Intel did not remove the <strong>two</strong>-byte version of these instructions, but compiler or assembler would always emit the shorter of the two instructions.
   </p>
  </li>
  <li>
   <p>
    By doing so, Intel has made an important <strong>trade-off</strong> with the <span style="color: blue">MOV</span> instruction encoding:
   </p>
   <ul style="list-style-type:none;">
    <li>
     <p>
       <em>giving up extra opcodes</em> in order to provide a shorter version of the <span style="color: blue">MOV</span> sub-family.
     </p>
    </li>
   </ul>
  </li>
  <li>
   <p>
    Intel used this trick all over the place to make decoding instructions shorter and easier.
   </p>
  </li>
  <li>
   <p>
    This decision dates back to 1978. Today's design could use extra bytes, but the cost of memory was high in 1978!
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0160_reducing_complexity"></a>


    <h3>
        16. Reducing x86 ISA to a Simplified Version
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    Advances in computer architecture technology since 1978 made encoding of x86 instructions quite complex and somewhat illogical.
   </p>
  </li>
  <li>
   <p>
    Despite the lack of simplicity, x86 ISA well deserves studying its design and encoding.
   </p>
  </li>
  <li>
   <p>
    To cope with x86 complexity, let's pretend that we deal with a <strong>simplified version of the CPU</strong>:
   </p>
   <ul>
    <li>
     <p>
      there are only four 16-bit registers: <strong>AX</strong>, <strong>BX</strong>, <strong>CX</strong>, and <strong>DX</strong>
     </p>
     <ul style="list-style-type:none;">
      <li>
       <p>
         (therefore, register operands can be encoded with just two bits.)
       </p>
      </li>
     </ul>
    </li>
    <li>
     <p>
      the <strong>address bus</strong> is 16-bit with a maximum of 65,536 bytes of addressable memory.
     </p>
    </li>
    <li>
     <p>
      there are only 20 instructions:
     </p>
     <ul style="list-style-type:none;">
      <li>
       <p>
         <span style="color: blue">MOV</span> (with two forms), <span style="color: blue">ADD</span>, <span style="color: blue">SUB</span>, <span style="color: blue">CMP</span>, <span style="color: blue">AND</span>, <span style="color: blue">OR</span>, <span style="color: blue">NOT</span>,
        <br />
        <span style="color: blue">JE</span>, <span style="color: blue">JNE</span>, <span style="color: blue">JB</span>, <span style="color: blue">JBE</span>, <span style="color: blue">JA</span>, <span style="color: blue">JAE</span>, <span style="color: blue">JMP</span>,
        <br />
        <span style="color: blue">BRK</span>, <span style="color: blue">IRET</span>, <span style="color: blue">HALT</span>, <span style="color: blue">GET</span>, and <span style="color: blue">PUT</span>.
       </p>
      </li>
     </ul>
    </li>
   </ul>
   <p>
    &nbsp;
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0170_mov_instruction"></a>


    <h3>
        17. The MOV Instruction
    </h3>
 <hr />
 <ul style="list-style-type:none;">
  <li>
   <p>
     The two forms of the simplified <span style="color: blue">MOV</span> instruction could have the following forms:
   </p>
<pre>        mov reg, reg/memory/constant    ; load register EAX
        mov memory, reg                 ; store register in memory
</pre>
   <p>
    where
   </p>
   <ul>
    <li>
     <p>
      <strong>mov</strong> is instruction mnemonic
     </p>
    </li>
    <li>
     <p>
      <strong>reg</strong> is any of <strong>AX</strong>, <strong>BX</strong>, <strong>CX</strong>, or <strong>DX</strong>,
     </p>
    </li>
    <li>
     <p>
      <strong>constant</strong> is a numeric constant (using hexadecimal notation),
     </p>
    </li>
    <li>
     <p>
      <strong>memory</strong> is an operand specifying a memory location.
     </p>
    </li>
   </ul>
   <p>
    &nbsp;
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0180_arithmetic_n_logical"></a>


    <h3>
        18. Arithmetic and Logical Instructions
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     The arithmetic and logical instructions could take the following forms:
   </p>
<pre>    add reg, reg/memory/constant

    sub reg, reg/memory/constant

    cmp reg, reg/memory/constant

    and reg, reg/memory/constant

    or  reg, reg/memory/constant

    not reg/memory
</pre>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     1-operand instructions modify its operand.
   </p>
  </li>
  <li>
   <p>
    2-operand instructions store the result in the destination operand:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/cpuinstructionformats.jpg" alt="Instruction formats" />
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul style="list-style-type:none;">
  <li>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0190_simplified_encoding"></a>


    <h3>
        19. Simplified Instruction Encoding (not x86!)
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Three high-order bit field, <strong>iii</strong>, defines the instruction and allows 8 unique bit combinations.
   </p>
  </li>
  <li>
   <p>
    (Since we decided to encode 20 different instructions, we cannot encode them with three bits, so we'll have to pull some tricks to handle all of the instructions.)
   </p>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Consider one-byte <em>opcode</em> with an optional <em>two-byte</em> constant value:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/simple_encoding.png" alt="simplified instruction encoding" />
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul style="list-style-type:none;">
  <li>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0200_simplified_encoding_cont"></a>


    <h3>
        20. Simplified Instruction Encoding, Cont. (not x86!)
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     There are three <strong>iii</strong> encoding groups:
   </p>
   <ol>
    <li>
     <p>
       Special instruction class <strong>iii=000</strong> is reserved for <em>instruction set expansion</em> in the future.
     </p>
    </li>
    <li>
     <p>
      Two forms of the <span style="color: blue">MOV</span> instruction include:
     </p>
     <ul>
      <li>
       <p>
        <strong>iii=110</strong> specifies <strong>rr</strong> field is the destination,
       </p>
      </li>
      <li>
       <p>
        <strong>iii=111</strong> specifies <strong>mmm</strong> field is the destination.
       </p>
      </li>
     </ul>
    </li>
    <li>
     <p>
      Remaining codes belong to <span style="color: blue">ADD</span>, <span style="color: blue">SUB</span>, <span style="color: blue">CMP</span>, <span style="color: blue">AND</span>, and <span style="color: blue">OR</span> instructions:
      <img src="http://www.c-jump.com/CIS77/images/simple_encoding.png" alt="simplified instruction encoding" />
     </p>
    </li>
   </ol>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Another opcode field, <strong>rr</strong>, contains the <em>destination register</em>,
   </p>
  </li>
  <li>
   <p>
    ...except for <span style="color: blue">MOV</span> whose <strong>iii = 111</strong>, in which case <strong>rr</strong> specifies the <em>source register</em>.
   </p>
  </li>
  <li>
   <p>
    Third bit field, <strong>mmm</strong>, encodes the <em>source operand</em> (again, except <span style="color: blue">MOV</span> whose <strong>iii = 111</strong>.)
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul style="list-style-type:none;">
  <li>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0210_simplified_encoding_example"></a>


    <h3>
        21. Simplified Instruction Encoding Example (not x86!)
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     For example, encoding of instruction
   </p>
<pre>        mov ax, bx
</pre>
   <p>
    consists bit fields
   </p>
   <ol>
    <li>
     <p>
       <strong>iii=110</strong> is the encoding for <span style="color: blue">MOV</span> <strong>REG, REG</strong>.
     </p>
    </li>
    <li>
     <p>
      <strong>rr=00</strong> specifies that <strong>AX</strong> is the destination operand.
     </p>
    </li>
    <li>
     <p>
      <strong>mmm=001</strong> specifies that <strong>BX</strong> is the source operand.
     </p>
    </li>
   </ol>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Simplified opcode structure:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/simple_encoding.png" alt="simplified instruction encoding" />
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 
 <ul style="list-style-type:none;">
  <li>
   <p>
     The encoding produces one-byte opcode <span style="background-color: yellow">110 00 001</span>, or <strong>0C1h</strong>.
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0220_simplified_multibyte"></a>


    <h3>
        22. Simplified Multibyte Instructions (not x86!)
    </h3>
 <hr />
 <ul>
  <li>
   <p>
    Instruction
   </p>
<pre>        mov ax, [1000h] ; load AX register from memory location 1000h
</pre>
   <p>
    loads the <strong>AX</strong> register from memory location <strong>1000h</strong>.
   </p>
  </li>
  <li>
   <p>
    The encoding for the opcode is <span style="background-color: yellow">110 00 110</span>, or <strong>0C6h</strong>.
   </p>
  </li>
  <li>
   <p>
    Another encoding,
   </p>
<pre>        mov ax, [2000h] ; load AX register from memory location 2000h
</pre>
   <p>
    is exact same <strong>0C6h</strong>, because none of the opcode fields store the memory address.
   </p>
  </li>
  <li>
   <p>
    To accommodate 16-bit address or a constant value, we must add two more bytes to the instruction opcode.
   </p>
   <p>
    &nbsp;
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0230_simplified_multibyte_cont"></a>


    <h3>
        23. Simplified Multibyte Instructions Cont. (not x86!)
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     To encode immediate constant values and address modes such as
   </p>
<pre>  0xxxxh          ; immediate mode
  [ 0xxxxh ]      ; direct mode
  [ 0xxxxh + bx ] ; fixed base + reg
</pre>
   <p>
    we add two bytes of <em>16-bit address</em> or <em>constant value</em> to the opcode:
   </p>
   <ul>
    <li>
     <p>
      low-order byte immediately follows the opcode byte in memory, and
     </p>
    </li>
    <li>
     <p>
      high-order byte comes after that.
     </p>
    </li>
   </ul>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Simplified multibyte instruction encoding:
    <img src="http://www.c-jump.com/CIS77/images/simple_encoding.png" alt="Simplified multibyte instruction encoding" />
   </p>
  </li>
  <li>
   <p>
    Three-byte encoding for <strong><tt><span style="color: blue">MOV</span> AX, [1000h]</tt></strong> instruction becomes
   </p>
<pre>    C6 00 10
</pre>
   <p>
    and the three-byte encoding for <strong><tt><span style="color: blue">MOV</span> AX, [2000h]</tt></strong> is
   </p>
<pre>    C6 00 20
</pre>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul style="list-style-type:none;">
  <li>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0240_simplified_special_opcode"></a>


    <h3>
        24. Simplified Special Opcode Instructions (not x86!)
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul>
  <li>
   <p>
    The special opcode<strong> [7:5]=000</strong> allows our imaginary CPU to <em>expand</em> the set of available instructions.
   </p>
  </li>
  <li>
   <p>
    This opcode handles several <strong>zero</strong>- and <strong>one</strong>-operand instructions.
   </p>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Imaginary Single-Operand Instruction Encoding:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/special_opcode.png" alt="Single Operand Instruction Encoding" />
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul>
  <li>
   <p>
    There are four possible one-operand instruction classes, specified by 2-bit <strong>ii[4:3] </strong>field:
   </p>
   <ol>
    <li>
     <p>
       The first encoding <strong>ii[4:3]=00</strong> further expands the instruction set with a set of <strong>zero</strong>-operand instructions
     </p>
    </li>
    <li>
     <p>
      The second opcode <strong>ii[4:3]=01</strong> is also an expansion opcode codifies all of the simplified <strong>jump</strong> instructions.
     </p>
    </li>
    <li>
     <p>
      The third opcode <strong>ii[4:3]=10</strong> is the <span style="color: blue">NOT</span> instruction, a <strong>bitwise logical not</strong> operation that inverts all the bits in the destination register or memory operand.
     </p>
    </li>
    <li>
     <p>
      The fourth opcode <strong>ii[4:3]=11</strong> is currently unassigned:
     </p>
     <ul style="list-style-type:none;">
      <li>
       <p>
         <em>Any attempt to execute unassigned opcode will halt the processor with an illegal instruction error</em>.
       </p>
      </li>
      <li>
       <p>
        CPU designers often reserve unassigned opcodes to extend the instruction set at a future date. Intel did so when moving from the 80286 processor to the 80386.
       </p>
      </li>
     </ul>
    </li>
   </ol>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0250_simplified_jump"></a>


    <h3>
        25. Simplified Jump Instructions (not x86!)
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     There are seven jump instructions in the simplified x86 instruction set. They all take the following form:
   </p>
<pre>        jxx address
</pre>
  </li>
  <li>
   <p>
    The <span style="color: blue">JMP</span> instruction copies the 16-bit value (address) following the opcode into the <strong>IP</strong> register.
   </p>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Imaginary <strong>jump</strong> instruction encodings:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/jump_encodings.png" alt="Jump Instruction Encodings" />
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul>
  <li>
   <p>
    Therefore, the CPU will fetch the next instruction from this new target address.
   </p>
  </li>
  <li>
   <p>
    Effectively, the program <em>jumps</em> from the point of the <span style="color: blue">JMP</span> instruction to the instruction at the target address.
   </p>
  </li>
  <li>
   <p>
    The <span style="color: blue">JMP</span> instruction is called an <em>unconditional jump</em> instruction, it always transfers control to the target address.
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0260_simplified_conditional_jump"></a>


    <h3>
        26. Simplified Conditional Jump Instructions (not x86!)
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     There are six conditional jump instructions:
   </p>
<pre>    JA  - jump <span style="color: blue">if</span> greater than (above)
    JAE - jump <span style="color: blue">if</span> greater than or equal
    JB  - jump <span style="color: blue">if</span> less than (below)
    JBE - jump <span style="color: blue">if</span> less than or equal
    JE  - jump <span style="color: blue">if</span> equality
    JNE - jump <span style="color: blue">if</span> inequality
</pre>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     You would normally execute <span style="color: blue">JE</span> or similar instruction <em>immediately after</em> a <span style="color: blue">CMP</span> instruction, since it
    sets the <strong>less than</strong> and <strong>equality</strong> flags in the CPU for conditional jump instructions to look at.
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul>
  <li>
   <p>
    Conditional jump instruction mechanics are:
   </p>
   <ol>
    <li>
     <p>
       <strong>Test</strong> some condition, and then <strong>jump</strong>, but only if the condition was <span style="color: blue">true</span>.
     </p>
    </li>
    <li>
     <p>
      <strong>Fall through</strong> to the next instruction if the condition was <span style="color: blue">false</span>.
     </p>
    </li>
   </ol>
  </li>
  <li>
   <p>
    Conditional jumps test the results of the preceeding <span style="color: blue">CMP</span> instruction. For example,
   </p>
<pre>            cmp     bx, 0           ; Is BX = 0?
            je      is_zero         ; Jump if so
            ...
    is_zero:
</pre>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0270_simplified_illegal"></a>


    <h3>
        27. Simplified Instructions Reserved Opcode (not x86!)
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Note that there are eight possible jump opcodes, but so far we needed only seven of them.
   </p>
  </li>
  <li>
   <p>
    The eighth opcode, <strong>mmm=111</strong> should then be another illegal opcode.
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Imaginary <strong>jump</strong> instruction encodings:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/jump_encodings.png" alt="Jump Instruction Encodings" />
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul style="list-style-type:none;">
  <li>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0280_simplified_zero_operand"></a>


    <h3>
        28. Simplified Zero-Operand Instructions (not x86!)
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul>
  <li>
   <p>
    The last group of instructions is the zero operand instructions.
   </p>
  </li>
  <li>
   <p>
    Three of these instructions are illegal instruction opcodes.
   </p>
  </li>
  <li>
   <p>
    The <span style="color: blue">BRK</span> (break) instruction pauses the CPU until the user manually restarts it.
   </p>
   <ul style="list-style-type:none;">
    <li>
     <p>
       This is useful for pausing a program during execution to observe results.
     </p>
    </li>
   </ul>
  </li>
  <li>
   <p>
    The <span style="color: blue">IRET</span> (interrupt return) instruction returns control from an interrupt service routine.
   </p>
   <ul style="list-style-type:none;">
    <li>
     <p>
       (We will discuss interrupt service routines later.)
     </p>
    </li>
   </ul>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Zero Operand Instruction Encodings:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/zero_operand_encodings.png" alt="Zero Operand Instruction Encodings" />
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul>
  <li>
   <p>
    The <span style="color: blue">HALT</span> program terminates program execution.
   </p>
  </li>
  <li>
   <p>
    The <span style="color: blue">GET</span> instruction reads a hexadecimal value from the keyboard and returns this value in the <strong>AX</strong> register.
   </p>
  </li>
  <li>
   <p>
    The <span style="color: blue">PUT</span> instruction prints the value in the <strong>AX</strong> register.
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0290_extending"></a>


    <h3>
        29. Extending the Simplified Instruction Set (not x86!)
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     The simplified CPU architecture design does provide the capability for expansion.
   </p>
  </li>
  <li>
   <p>
    The ability to accomplish this exists in the instruction set through undefined/reserved/illegal opcodes.
   </p>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Imaginary single-operand instruction encoding:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/special_opcode.png" alt="single operand instruction encoding" />
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul>
  <li>
   <p>
    The first method is to directly use the undefined opcodes to define new instructions
   </p>
   <ul style="list-style-type:none;">
    <li>
     <p>
       (this works best when there are undefined bit patterns within an opcode group and new instruction falls into that same group.)
     </p>
    </li>
   </ul>
  </li>
  <li>
   <p>
    For example, opcode "<strong>000 11 mmm</strong>" falls into the same group as the <span style="color: blue">NOT</span> instruction.
   </p>
  </li>
  <li>
   <p>
    If you decided to add <span style="color: blue">NEG</span> (negate, take the two's complement) instruction,
    <br />
    using opcode "<strong>000 11 mmm</strong>" makes a lot of sense.
   </p>
  </li>
  <li>
   <p>
    <span style="color: blue">NEG</span> instruction uses the same syntax and decoding, as the <span style="color: blue">NOT</span> instruction.
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0300_problem_extending"></a>


    <h3>
        30. Problem with Extending the Simplified Instruction Set (not x86!)
    </h3>
 <hr />
     <table border="0" cellspacing="0" cellpadding="2">
         <tr>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Unfortunately, the simplified CPU doesn't have that many illegal opcodes available.
   </p>
  </li>
  <li>
   <p>
    For example, to add new single-operand instructions
   </p>
<pre>    <span style="color: blue">SHL</span> - shift left
    <span style="color: blue">SHR</span> - shift right
    <span style="color: blue">ROL</span> - rotate left
    <span style="color: blue">ROR</span> - rotate right
</pre>
  </li>
 </ul>
             </td>
             <td valign="top">
 <ul style="list-style-type:none;">
  <li>
   <p>
     Imaginary single-operand instruction encoding:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/special_opcode.png" alt="single operand instruction encoding" />
   </p>
  </li>
 </ul>
             </td>
         </tr>
     </table>
 <ul>
  <li>
   <p>
    There is insufficient space in the single operand instruction opcodes.
   </p>
  </li>
  <li>
   <p>
    Currently there is only one open opcode: <strong>000 11 mmm</strong>.
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0310_prefix_extending"></a>


    <h3>
        31. Prefix-Extending the Simplified Instruction Set (not x86!)
    </h3>
 <hr />
 <ul style="list-style-type:none;">
  <li>
   <p>
     A common way to handle the opcode shortage (one the Intel designers have employed) is to use a <em>prefix opcode byte</em>:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/prefix_byte_extend.png" alt="Using a prefix byte to extend the instruction set" />
   </p>
  </li>
  <li>
   <p>
    Prefix opcode expansion scheme uses an <strong>opcode prefix byte</strong> as follows:
   </p>
   <ul>
    <li>
     <p>
      Decode prefix byte in memory.
     </p>
    </li>
    <li>
     <p>
      Read and decode the <strong>next byte</strong> in memory as the <strong>actual opcode</strong>.
     </p>
    </li>
    <li>
     <p>
      However, the second opcode byte uses a <em>completely different encoding scheme</em>.
     </p>
    </li>
   </ul>
  </li>
  <li>
   <p>
    Therefore, prefix lets you specify as many new instructions as you can encode in that byte (or bytes, if you prefer).
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


<a id="Y77_0320_prefix_extending_example"></a>


    <h3>
        32. Prefix-Extending the Simplified Instruction Set Example (not x86!)
    </h3>
 <hr />
 <ul style="list-style-type:none;">
  <li>
   <p>
     Using a prefix byte to extend the instruction set:
   </p>
   <p>
    &nbsp;
    <img src="http://www.c-jump.com/CIS77/images/prefix_byte_extend.png" alt="Using a prefix byte to extend the instruction set" />
   </p>
  </li>
  <li>
   <p>
    For example, the opcode <strong>0FFh</strong> is illegal, since it corresponds to a
   </p>
<pre>        mov <span style="color: blue">const</span>, dx ; error: attempt to modify immediate operand
</pre>
   <p>
    instruction. So, we could use <strong>0FFh</strong> as a special prefix byte to further expand the instruction set.
   </p>
   <p>
    &nbsp;
   </p>
   <p>
    &nbsp;
   </p>
  </li>
 </ul>


    </body>
</html>