-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccvm.html
More file actions
1109 lines (985 loc) · 37.4 KB
/
ccvm.html
File metadata and controls
1109 lines (985 loc) · 37.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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
<style>
* {
text-align: center;
font-size: 13px;
}
body {
margin: 0vh 20vw;
}
ul {
list-style: none;
padding-top: 0;
text-align: center;
}
li {
text-align: left;
}
.nested-list {
width: 40vw;
margin-left: 15vw;
}
h3 {
margin-bottom: 0;
margin-top: 40px;
font-size: 18px;
}
p {
text-align: left;
}
.codeblock {
padding: 8px;
border-left: 5px solid #777;
background: #eee;
font-family: Consolas, monaco, monospace;
font-size: 12px;
width: 100%;
}
.cb-title {
float: left;
width: 100%;
text-align: left;
font-weight: bold;
}
.cb-mark-green {
background: #86cf7e;
padding: 1px 0px;
font-size: 12px;
margin-bottom: -1px -0px;
}
.cb-mark-blue {
background: #b9abf7;
padding: 1px 0px;
font-size: 12px;
margin-bottom: -1px -0px;
}
.cb-mark-red {
background: #e3a8a8;
padding: 1px 0px;
font-size: 12px;
margin-bottom: -1px -0px;
}
th, td {
text-align: left;
}
.wide-table {
margin-left: -15vw;
width: calc(100% + 30vw);
}
</style>
<pre style="font-size: 20px">
_____ _____ _ _ __ ___
/ __ \ / __ \ | || | | \/ |
| / \/ | / \/ | || | | |
| | | | | || | | |\/| |
| \__/\ | \__/\ \ \/ / | | | |
\____/ \____/ \__/ \_| |_/
</pre>
<h3><b><pre>
---------------------------------
A peek under the hood of the CCVM
---------------------------------
</pre></b></h3>
<body></body>
<head></head>
content:
<ul class="nested-list">
<li>1 Bytecode format</li>
<li><ul>
<li><a href="#1.1">1.1 Hex encoded files</a></li>
<li><a href="#1.2">1.2 Sections</a></li>
<li><ul>
<li><a href="#1.2.1">1.2.1 Header section</a></li>
<li><a href="#1.2.2">1.2.2 Code section</a></li>
<li><ul>
<li><a href="#1.2.2A">1.2.2A Numbers</a></li>
<li><a href="#1.2.2B">1.2.2B Registers</a></li>
<li><a href="#1.2.2C">1.2.2C Addresses</a></li>
<li><a href="#1.2.2D">1.2.2D Opcodes</a></li>
</ul></li>
</ul></li>
<li>1.4 Instruction list</li>
<li>1.5 Examples</li>
</ul></li>
<li>2 VM internals</li>
<li>3 Bytecode interpreter</li>
<li>4 Assembly format</li>
<li>5 Assembler</li>
</ul>
<h3 id="1.1">1.1 Hex encoded files</h3>
<p>
The virtual machine is able to run programs that are stored in .ccb files. These files may look like normal files at first glance but when you put them in a text editor you may notice that its unreadable as if the file has been corrupted.
</p>
<p>
This is because the .ccb files are normally not meant to readed by humans, thuss are not formatted using ascii encoding.
</p>
<p>
To find the true meaning of this executable file we must translate each and every character to its ascii value using the following ascii convertion table:
</p>
<p>
<table cellspacing="1" cellpadding="5" border="1" align="center">
<tbody><tr>
<th width="50" align="center">Char.</th>
<th width="50" align="center">Dec.</th>
<th width="50" align="center">Hex.</th>
<th width="24"> </th>
<th width="50" align="center">Char.</th>
<th width="50" align="center">Dec.</th>
<th width="50" align="center">Hex.</th>
<th width="24"> </th>
<th width="50" align="center">Char.</th>
<th width="50" align="center">Dec.</th>
<th width="50" align="center">Hex.</th></tr>
<tr>
<td width="50" align="center">*space*</td>
<td width="50" align="center">32</td>
<td width="50" align="center">20</td>
<td width="24"> </td>
<td width="50" align="center">@</td>
<td width="50" align="center">64</td>
<td width="50" align="center">40</td>
<td width="24"> </td>
<td width="50" align="center">`</td>
<td width="50" align="center">96</td>
<td width="50" align="center">60</td></tr>
<tr>
<td width="50" align="center">!</td>
<td width="50" align="center">33</td>
<td width="50" align="center">21</td>
<td width="24"> </td>
<td width="50" align="center">A</td>
<td width="50" align="center">65</td>
<td width="50" align="center">41</td>
<td width="24"> </td>
<td width="50" align="center">a</td>
<td width="50" align="center">97</td>
<td width="50" align="center">61</td></tr>
<tr>
<td width="50" align="center">"</td>
<td width="50" align="center">34</td>
<td width="50" align="center">22</td>
<td width="24"> </td>
<td width="50" align="center">B</td>
<td width="50" align="center">66</td>
<td width="50" align="center">42</td>
<td width="24"> </td>
<td width="50" align="center">b</td>
<td width="50" align="center">98</td>
<td width="50" align="center">62</td></tr>
<tr>
<td width="50" align="center">#</td>
<td width="50" align="center">35</td>
<td width="50" align="center">23</td>
<td width="24"> </td>
<td width="50" align="center">C</td>
<td width="50" align="center">67</td>
<td width="50" align="center">43</td>
<td width="24"> </td>
<td width="50" align="center">c</td>
<td width="50" align="center">99</td>
<td width="50" align="center">63</td></tr>
<tr>
<td width="50" align="center">$</td>
<td width="50" align="center">36</td>
<td width="50" align="center">24</td>
<td width="24"> </td>
<td width="50" align="center">D</td>
<td width="50" align="center">68</td>
<td width="50" align="center">44</td>
<td width="24"> </td>
<td width="50" align="center">d</td>
<td width="50" align="center">100</td>
<td width="50" align="center">64</td></tr>
<tr>
<td width="50" align="center">%</td>
<td width="50" align="center">37</td>
<td width="50" align="center">25</td>
<td width="24"> </td>
<td width="50" align="center">E</td>
<td width="50" align="center">69</td>
<td width="50" align="center">45</td>
<td width="24"> </td>
<td width="50" align="center">e</td>
<td width="50" align="center">101</td>
<td width="50" align="center">65</td></tr>
<tr>
<td width="50" align="center">&</td>
<td width="50" align="center">38</td>
<td width="50" align="center">26</td>
<td width="24"> </td>
<td width="50" align="center">F</td>
<td width="50" align="center">70</td>
<td width="50" align="center">46</td>
<td width="24"> </td>
<td width="50" align="center">f</td>
<td width="50" align="center">102</td>
<td width="50" align="center">66</td></tr>
<tr>
<td width="50" align="center">'</td>
<td width="50" align="center">39</td>
<td width="50" align="center">27</td>
<td width="24"> </td>
<td width="50" align="center">G</td>
<td width="50" align="center">71</td>
<td width="50" align="center">47</td>
<td width="24"> </td>
<td width="50" align="center">g</td>
<td width="50" align="center">103</td>
<td width="50" align="center">67</td></tr>
<tr>
<td width="50" align="center">(</td>
<td width="50" align="center">40</td>
<td width="50" align="center">28</td>
<td width="24"> </td>
<td width="50" align="center">H</td>
<td width="50" align="center">72</td>
<td width="50" align="center">48</td>
<td width="24"> </td>
<td width="50" align="center">h</td>
<td width="50" align="center">104</td>
<td width="50" align="center">68</td></tr>
<tr>
<td width="50" align="center">)</td>
<td width="50" align="center">41</td>
<td width="50" align="center">29</td>
<td width="24"> </td>
<td width="50" align="center">I</td>
<td width="50" align="center">73</td>
<td width="50" align="center">49</td>
<td width="24"> </td>
<td width="50" align="center">i</td>
<td width="50" align="center">105</td>
<td width="50" align="center">69</td></tr>
<tr>
<td width="50" align="center">*</td>
<td width="50" align="center">42</td>
<td width="50" align="center">2A</td>
<td width="24"> </td>
<td width="50" align="center">J</td>
<td width="50" align="center">74</td>
<td width="50" align="center">4A</td>
<td width="24"> </td>
<td width="50" align="center">j</td>
<td width="50" align="center">106</td>
<td width="50" align="center">6A</td></tr>
<tr>
<td width="50" align="center">+</td>
<td width="50" align="center">43</td>
<td width="50" align="center">2B</td>
<td width="24"> </td>
<td width="50" align="center">K</td>
<td width="50" align="center">75</td>
<td width="50" align="center">4B</td>
<td width="24"> </td>
<td width="50" align="center">k</td>
<td width="50" align="center">107</td>
<td width="50" align="center">6B</td></tr>
<tr>
<td width="50" align="center">,</td>
<td width="50" align="center">44</td>
<td width="50" align="center">2C</td>
<td width="24"> </td>
<td width="50" align="center">L</td>
<td width="50" align="center">76</td>
<td width="50" align="center">4C</td>
<td width="24"> </td>
<td width="50" align="center">l</td>
<td width="50" align="center">108</td>
<td width="50" align="center">6C</td></tr>
<tr>
<td width="50" align="center">-</td>
<td width="50" align="center">45</td>
<td width="50" align="center">2D</td>
<td width="24"> </td>
<td width="50" align="center">M</td>
<td width="50" align="center">77</td>
<td width="50" align="center">4D</td>
<td width="24"> </td>
<td width="50" align="center">m</td>
<td width="50" align="center">109</td>
<td width="50" align="center">6D</td></tr>
<tr>
<td width="50" align="center">.</td>
<td width="50" align="center">46</td>
<td width="50" align="center">2E</td>
<td width="24"> </td>
<td width="50" align="center">N</td>
<td width="50" align="center">78</td>
<td width="50" align="center">4E</td>
<td width="24"> </td>
<td width="50" align="center">n</td>
<td width="50" align="center">110</td>
<td width="50" align="center">6E</td></tr>
<tr>
<td width="50" align="center">/</td>
<td width="50" align="center">47</td>
<td width="50" align="center">2F</td>
<td width="24"> </td>
<td width="50" align="center">O</td>
<td width="50" align="center">79</td>
<td width="50" align="center">4F</td>
<td width="24"> </td>
<td width="50" align="center">o</td>
<td width="50" align="center">111</td>
<td width="50" align="center">6F</td></tr>
<tr>
<td width="50" align="center">0</td>
<td width="50" align="center">48</td>
<td width="50" align="center">30</td>
<td width="24"> </td>
<td width="50" align="center">P</td>
<td width="50" align="center">80</td>
<td width="50" align="center">50</td>
<td width="24"> </td>
<td width="50" align="center">p</td>
<td width="50" align="center">112</td>
<td width="50" align="center">70</td></tr>
<tr>
<td width="50" align="center">1</td>
<td width="50" align="center">49</td>
<td width="50" align="center">31</td>
<td width="24"> </td>
<td width="50" align="center">Q</td>
<td width="50" align="center">81</td>
<td width="50" align="center">51</td>
<td width="24"> </td>
<td width="50" align="center">q</td>
<td width="50" align="center">113</td>
<td width="50" align="center">71</td></tr>
<tr>
<td width="50" align="center">2</td>
<td width="50" align="center">50</td>
<td width="50" align="center">32</td>
<td width="24"> </td>
<td width="50" align="center">R</td>
<td width="50" align="center">82</td>
<td width="50" align="center">52</td>
<td width="24"> </td>
<td width="50" align="center">r</td>
<td width="50" align="center">114</td>
<td width="50" align="center">72</td></tr>
<tr>
<td width="50" align="center">3</td>
<td width="50" align="center">51</td>
<td width="50" align="center">33</td>
<td width="24"> </td>
<td width="50" align="center">S</td>
<td width="50" align="center">83</td>
<td width="50" align="center">53</td>
<td width="24"> </td>
<td width="50" align="center">s</td>
<td width="50" align="center">115</td>
<td width="50" align="center">73</td></tr>
<tr>
<td width="50" align="center">4</td>
<td width="50" align="center">52</td>
<td width="50" align="center">34</td>
<td width="24"> </td>
<td width="50" align="center">T</td>
<td width="50" align="center">84</td>
<td width="50" align="center">54</td>
<td width="24"> </td>
<td width="50" align="center">t</td>
<td width="50" align="center">116</td>
<td width="50" align="center">74</td></tr>
<tr>
<td width="50" align="center">5</td>
<td width="50" align="center">53</td>
<td width="50" align="center">35</td>
<td width="24"> </td>
<td width="50" align="center">U</td>
<td width="50" align="center">85</td>
<td width="50" align="center">55</td>
<td width="24"> </td>
<td width="50" align="center">u</td>
<td width="50" align="center">117</td>
<td width="50" align="center">75</td></tr>
<tr>
<td width="50" align="center">6</td>
<td width="50" align="center">54</td>
<td width="50" align="center">36</td>
<td width="24"> </td>
<td width="50" align="center">V</td>
<td width="50" align="center">86</td>
<td width="50" align="center">56</td>
<td width="24"> </td>
<td width="50" align="center">v</td>
<td width="50" align="center">118</td>
<td width="50" align="center">76</td></tr>
<tr>
<td width="50" align="center">7</td>
<td width="50" align="center">55</td>
<td width="50" align="center">37</td>
<td width="24"> </td>
<td width="50" align="center">W</td>
<td width="50" align="center">87</td>
<td width="50" align="center">57</td>
<td width="24"> </td>
<td width="50" align="center">w</td>
<td width="50" align="center">119</td>
<td width="50" align="center">77</td></tr>
<tr>
<td width="50" align="center">8</td>
<td width="50" align="center">56</td>
<td width="50" align="center">38</td>
<td width="24"> </td>
<td width="50" align="center">X</td>
<td width="50" align="center">88</td>
<td width="50" align="center">58</td>
<td width="24"> </td>
<td width="50" align="center">x</td>
<td width="50" align="center">120</td>
<td width="50" align="center">78</td></tr>
<tr>
<td width="50" align="center">9</td>
<td width="50" align="center">57</td>
<td width="50" align="center">39</td>
<td width="24"> </td>
<td width="50" align="center">Y</td>
<td width="50" align="center">89</td>
<td width="50" align="center">59</td>
<td width="24"> </td>
<td width="50" align="center">y</td>
<td width="50" align="center">121</td>
<td width="50" align="center">79</td></tr>
<tr>
<td width="50" align="center">:</td>
<td width="50" align="center">58</td>
<td width="50" align="center">3A</td>
<td width="24"> </td>
<td width="50" align="center">Z</td>
<td width="50" align="center">90</td>
<td width="50" align="center">5A</td>
<td width="24"> </td>
<td width="50" align="center">z</td>
<td width="50" align="center">122</td>
<td width="50" align="center">7A</td></tr>
<tr>
<td width="50" align="center">;</td>
<td width="50" align="center">59</td>
<td width="50" align="center">3B</td>
<td width="24"> </td>
<td width="50" align="center">[</td>
<td width="50" align="center">91</td>
<td width="50" align="center">5B</td>
<td width="24"> </td>
<td width="50" align="center">{</td>
<td width="50" align="center">123</td>
<td width="50" align="center">7B</td></tr>
<tr>
<td width="50" align="center"><</td>
<td width="50" align="center">60</td>
<td width="50" align="center">3C</td>
<td width="24"> </td>
<td width="50" align="center">\</td>
<td width="50" align="center">92</td>
<td width="50" align="center">5C</td>
<td width="24"> </td>
<td width="50" align="center">|</td>
<td width="50" align="center">124</td>
<td width="50" align="center">7C</td></tr>
<tr>
<td width="50" align="center">=</td>
<td width="50" align="center">61</td>
<td width="50" align="center">3D</td>
<td width="24"> </td>
<td width="50" align="center">]</td>
<td width="50" align="center">93</td>
<td width="50" align="center">5D</td>
<td width="24"> </td>
<td width="50" align="center">}</td>
<td width="50" align="center">125</td>
<td width="50" align="center">7D</td></tr>
<tr>
<td width="50" align="center">></td>
<td width="50" align="center">62</td>
<td width="50" align="center">3E</td>
<td width="24"> </td>
<td width="50" align="center">^</td>
<td width="50" align="center">94</td>
<td width="50" align="center">5E</td>
<td width="24"> </td>
<td width="50" align="center">~</td>
<td width="50" align="center">126</td>
<td width="50" align="center">7E</td></tr>
<tr>
<td width="50" align="center">?</td>
<td width="50" align="center">63</td>
<td width="50" align="center">3F</td>
<td width="24"> </td>
<td width="50" align="center">_</td>
<td width="50" align="center">95</td>
<td width="50" align="center">5F</td>
<td width="24"> </td>
<td width="50" align="center">*nbsp*</td>
<td width="50" align="center">127</td>
<td width="50" align="center">7F</td></tr>
<tr>
<td colspan="11"><br><b>NOTE:</b> Decimal 0-31 (0x00-1F) and 127 (0x7F) are "non-printable characters"</td></tr>
</tbody></table>
</p>
<p>
Since its near infeasible to decode the entire program by hand, there are special editors that display the hexadecimal numeric value's of the file content instead of the ascii decoding of the file.
</p>
<p>
When we open a file in such an editor, we might be presented with a file content similair to the following:
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
0x73 0x70 0x61 0x6d 0x0a 0x1d 0x1d 0x1d 0x1d 0x06 0x00 0x00 0x00 0x00 0x00
0x06 0x03 0x00 0x00 0x00 0x00 0x06 0x01 0x00 0x00 0x00 0x00 0x06 0x02 0x00 0x00
0x00 0x05 0xff 0x06 0x00 0x00 0x00 0x00 0x03 0x09 0x00 0x00 0x00 0x64 0x03 0x08
0x01 0x00 0x00 0x00 0x64 0xff 0x06 0x00 0x00 0x00 0x00 0x00 0x06 0x01 0x00 0x00
0x00 0x00 0x06 0x02 0x00 0x00 0x00 0x01 0xff 0x40 0x31 0x03 0x00 0x00 0x00 0x64
0x01 0x00 0x00 0x00 0x01 0x02 0x03 0x11 0x03 0x03 0x34 0x00 0x00 0x00 0x0c 0x00
</p>
<p>
Notice: hexadecimal is just another way of describing numeric values, therefore you can translate between hexadecimal and our normal numeric system by using a converter like <a href="https://www.rapidtables.com/convert/number/hex-to-decimal.html" target="_blank">this</a>. you can use it to convert hex numbers to decimal, or reverse when you press the "swap button"
</p>
<p>
As you can see, each hexadecimal number starts with 0x (indicating this is a hex number) and then has 2 digits which are either 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e or f. since the 0x prefixes really disturb the view, we will leave them away from now on, you will beable to tell if the file is a hexadecimal file by looking at the title of the code block
</p>
<p>
Without the prefixes, a .ccb file might look like the following in a hex editor:
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
73 70 61 6d 0a 1d 1d 1d 1d 06 00 00 00 00 00
06 03 00 00 00 00 06 01 00 00 00 00 06 02 00 00
00 05 ff 06 00 00 00 00 03 09 00 00 00 64 03 08
01 00 00 00 64 ff 06 00 00 00 00 00 06 01 00 00
00 00 06 02 00 00 00 01 ff 40 31 03 00 00 00 64
01 00 00 00 01 02 03 11 03 03 34 00 00 00 0c 00
</p>
<h3 id="1.2">1.2 Sections</h3>
<p>
These CCVM binary files are split into 2 sections which all have a different purposes and are interpreted by the bytecode interpreter in different ways. To distinguish these 2 sections, we look for the first sequence of '0x1d 0x1d 0x1d 0x1d' this sequence is called the "section seperation sequence"
</p>
<p>
For example, when applied to the bytecode example from the previous chapter, the section seperation sequence is found right here marked in green:
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
73 70 61 6d 0a <span class="cb-mark-green">1d 1d 1d 1d</span> 06 00 00 00 00 00
06 03 00 00 00 00 06 01 00 00 00 00 06 02 00 00
00 05 ff 06 00 00 00 00 03 09 00 00 00 64 03 08
01 00 00 00 64 ff 06 00 00 00 00 00 06 01 00 00
00 00 06 02 00 00 00 01 ff 40 31 03 00 00 00 64
01 00 00 00 01 02 03 11 03 03 34 00 00 00 0c 00
</p>
<p>
Once we have located the section seperation sequence, we can identify the "header section" as all the bytes before the section seperation sequence. And the "code section" can be identified as all the bytes after the section seperation sequence
</p>
<p>
Here is the same example yet again, but the header section marked blue, and the code section marked red:
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
<span class="cb-mark-blue">73 70 61 6d 0a</span> <span class="cb-mark-green">1d 1d 1d 1d</span> <span class="cb-mark-red">06 00 00 00 00 00
06 03 00 00 00 00 06 01 00 00 00 00 06 02 00 00
00 05 ff 06 00 00 00 00 03 09 00 00 00 64 03 08
01 00 00 00 64 ff 06 00 00 00 00 00 06 01 00 00
00 00 06 02 00 00 00 01 ff 40 31 03 00 00 00 64
01 00 00 00 01 02 03 11 03 03 34 00 00 00 0c 00</span>
</p>
<h3 id="1.2.1">1.2.1 Header section</h3>
<p>
The header is the simplest section to grasp out of the 2. It's purpose is to store data, which could either be numerical or string data depending how the code in the code section handles it. In the case of our example, its string data.
</p>
<p>
If we use the ascii table from earlier to translate each hex value to its corresponding character, we will get the string "spam" and then a line break symbol.
</p>
<p>
When we run this program, the virtual machine will grab all this data in the header and throw it in memory at memory address 0. This data can then later be read and used by the code in the code section.
</p>
<p>
Where and how exactly this data i mounted in memory will be explained later.
</p>
<h3 id="1.2.2">1.2.2 Code section</h3>
<p>
The code section itself is split into various "instructions" which all can have an arbitrary length, but are at least 1 byte in length. This instruction can then also be split in various amount of meta data which discribes exactly what the instruction is meant to do.
</p>
<img src="instruction.png" style="width: 50vw">
<p>
These meta bytes exists in 4 different types:
<ul>
<li>- Opcodes (These are 1 byte wide)</li>
<li>- Registers (These are 1 byte wide)</li>
<li>- Number literals (These are 4 bytes wide)</li>
<li>- Addresses (These are 4 bytes wide)</li>
</ul>
</p>
<h3 id="1.2.2A">1.2.2A Numbers</h3>
<p>
One of the meta byte types is the number literal type, the number literal
stores a number in the code which could then be used as meta data for the
instruction.
</p>
<p>
Since these numbers literals take up 4 bytes in the byte code, they can only
store 4 * 8bit = 32bit numbers. These numbers are normally interpreted as
unsigned integer numbers. meaning they can only store positive and whole numbers
like 5 and 10
</p>
<p>
One example where numbers can be used is for example in an add instruction
where you want to add 10 to something, you would then want to have the following
bytes somewhere in the code section of the bytecode
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
00 00 00 0a
</p>
<p>
If this is a number literal, then we would take 4 bytes, and we get the hex
number 00 00 00 0a, which translated to decimal would be the number 10
</p>
<h3 id="1.2.2B">1.2.2B Registers</h3>
<p>
Registers take up 1 byte each in the code section and they are able to store
and manipulate numbers in the virtual machine. the CCVM has 4 registers that
we will call a, b, c and d. each of which can store an unsigned 32bit number
</p>
<p>
An example usage of registers is the following:<br>
- move the number 8 into register A, A is now 8<br>
- move the number 5 into register B, B is now 5<br>
- add register A and B together and store the result in A<br><br>
A will end up being 13 and B will end up being 5
</p>
<p>
In the bytecode, registers will be represented as 'register indexes' which
are 1 byte numbers that represent what register is targeted: <br>
- 00 is register A <br>
- 01 is register B <br>
- 02 is register C <br>
- 03 is register D <br>
<br>
for all the indexes after 3, the registers will loop around, so 04 is register A again.
tho it is possible to use post 03 indexes, it is highly disrecommended.
</p>
<p>
An example of a register represented in the codesection of a bytecode could be:
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
02
</p>
<p>
In this case, 02 is representing register C
</p>
<h3 id="1.2.2C">1.2.2C Addresses</h3>
<p>
Addresses are very similair to numbers, except they are interpreted in the virtual machine
in a different way
</p>
<p>
Since registers are extremely limited in terms of the amount they can store
(they can only hold 4 * 4bytes = 16 bytes) There needs to be another way to store data
which is the heap memory.
</p>
<p>
The CCVM virtual heap memory, could be seen as a huge list of 32bit unsigned integer numbers
at each element of this list could be any number, the CCVM has special instructions dedicated to
reading/writing to those elements.
</p>
<p>
Addresses are meant to tell the virtual machine what element in that list should be used by targeted
for a read/write.
</p>
<p>
Therefore, when we talk about "address N" we mean the N'th element from the virtual memory list.
</p>
<p>
An example of how to represent an address in the code section of the bytecode could be:
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
00 00 00 2a
</p>
<p>
In this example, if this would represent an addess, it would be address 42 (since 2a in hex is 42 in decimal)
</p>
<h3 id="1.2.2D">1.2.2D Opcodes</h3>
<p>
Opcodes are the meta data elements that bind addresses, register indexes
and numbers together to make up the instructions of the virtual machine.
</p>
<p>
Each opcode expects a certain set of other meta data elements, this is
dependant on what the meaning of the opcode is, which is arbetrairily defined.
</p>
<p>
Lets take the opcode "06" as example:
</p>
<p>
Opcode 06 means "to move a number, into a register". It expects an register (which is 1 byte wide),
and then a number literal (which is 4 bytes wide). Thuss the total length will be 6 bytes (1 byte for the opcode + 1 byte for the register index + 4 bytes for the number)
</p>
<p>
There is also a way to notate the structure of an instruction called "instruction structure syntax", for the instruction with opcode 06, the instruction structure syntax (ISS) looks like this:
</p>
<span class="codeblock cb-title">
ISS
</span>
<p class="codeblock hex">
06: [opcode(1) register(1) address(4)] 6
</p>
<p>
Lets give each of the important elements in this ISS a color:
</p>
<span class="codeblock cb-title">
ISS
</span>
<p class="codeblock hex">
<span class="cb-mark-blue">06</span>: [<span class="cb-mark-green">opcode(1)</span> <span class="cb-mark-green">register(1)</span> <span class="cb-mark-green">address(4)</span>] <span class="cb-mark-red">6</span>
</p>
<p>
The blue element describes the opcode that this instruction has, in this
case we are talking about the instruction with opcode 06.
</p>
<p>
The green elements describe all the meta data elements that this instruction
should have, in this case it needs an opcode, then a register and finally an
memory address. The numbers in the parenthesis discribe the width of that element
in bytes.
</p>
<p>
The red element describes the total width of the entire instruction, which you can
calculate by adding together all the width's of the meta data elements (1 + 1 + 4).
</p>
<p>
Lets take a look at an example of a bytecode:
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
1d 1d 1d 1d 06 01 00 00 00 2a 00
</p>
<p>
The first thing we should do is identify the section seperation sequence (1d 1d 1d 1d)
so that we know where the code section and the header sections are.
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
<span class="cb-mark-blue">1d 1d 1d 1d</span> <span class="cb-mark-green">06 01 00 00 00 2a 00</span>
</p>
<p>
As we can see, the section seperation sequence is right at the very start
of the bytecode (marked in blue), this means that the header will be completely
empty, and that we only have to worry about the code section (marked in green)
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
1d 1d 1d 1d <span class="cb-mark-red">06</span> 01 00 00 00 2a 00
</p>
<p>
As we can see above here marked in red, the first byte of the code section
(06) will be the first opcode, and the start of the first instruction
</p>
<p>
As we know from the ISS from before, 06 means that we want to move a number into
a register. we also know that the this instruction is 6 bytes wide, so the entire
instruction should be this:
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
1d 1d 1d 1d <span class="cb-mark-red">06</span> <span class="cb-mark-blue">01</span> <span class="cb-mark-green">00 00 00 2a</span> 00
</p>
<p>
The byte marked in blue tells us that we want to send the nummer to the register
with index 01 (which is register B). The green bytes tell us that the number we want
to put in register B will be 0000002a which is 42.
</p>
<p>
Once register B has been set to the number 42, the virtual machine will step forward to
the next instruction which starts right here marked in red:
</p>
<span class="codeblock cb-title">
Hexadecimal
</span>
<p class="codeblock hex">
1d 1d 1d 1d 06 01 00 00 00 2a <span class="cb-mark-red">00</span>
</p>
<p>
The next opcode will thuss be 00. opcode 00 has an ISS that looks like this:
</p>
<span class="codeblock cb-title">
ISS
</span>
<p class="codeblock hex">
00: [opcode(1)] 1
</p>
<p>
It does not expect any meta data elements and is only a single opcode.
</p>
<p>
Opcode 00 tells the virtual machine to stop running, and thuss after this instruction
is done, the program will stop.
</p>
<h3 id="1.3">1.3 Instruction list</h3>
<p>
We have already discussed the opcodes 00 and 06, however CCVM has many more.
Here is a list of all of them, including their short hand name (in red), a discription, and its ISS (in blue)
</p>
<span class="codeblock cb-title">
ISS + Descriptions list
</span>
<p class="codeblock hex">
-------------------------------------------<br><br>
<b class="cb-mark-red">STP</b>: <br>
emediatly stops the virtual machine from executing the code<br>
<span class="cb-mark-blue">00: [opcode(1)] 1</span>
<br><br>-------------------------------------------<br><br>
<b class="cb-mark-red">PSH (number)</b>: <br>
pushes a number to the stack<br>
<span class="cb-mark-blue">01: [opcode(1) number(4)] 5</span>
<br><br>-------------------------------------------<br><br>
<b class="cb-mark-red">PSH (register)</b>: <br>
pushes the value of a register to the stack<br>
<span class="cb-mark-blue">02: [opcode(1) register(1)] 2</span>
<br><br>-------------------------------------------<br><br>
<b class="cb-mark-red">POP (register)</b>: <br>
pops the top value on the stack and puts it in a register<br>
<span class="cb-mark-blue">03: [opcode(1) register(1)] 2</span>
<br><br>-------------------------------------------<br><br>
<b class="cb-mark-red">POP (address)</b>: <br>
pops the top value on the stack and puts it in the memory at some address<br>
<span class="cb-mark-blue">04: [opcode(1) address(4)] 5</span>
<br><br>-------------------------------------------<br><br>
<b class="cb-mark-red">DUP (register)</b>: <br>
duplicates the top value of the stack<br>
<span class="cb-mark-blue">05: [opcode(1)] 1</span>
<br><br>-------------------------------------------<br><br>
<b class="cb-mark-red">MOV (number -> register)</b>: <br>
moves a number into a register<br>
<span class="cb-mark-blue">06: [opcode(1) register(1) number(4)] 6</span>
<br><br>-------------------------------------------<br><br>
<b class="cb-mark-red">MOV (number -> address)</b>: <br>
moves a number into memory to some address<br>
<span class="cb-mark-blue">07: [opcode(1) address(4) number(4)] 9</span>
<br><br>-------------------------------------------<br><br>
<b class="cb-mark-red">MOV (address -> register)</b>: <br>
moves the value at some address to a register<br>
<span class="cb-mark-blue">08: [opcode(1) register(1) address(4)] 6</span>
<br><br>-------------------------------------------<br><br>
<b class="cb-mark-red">MOV (register -> address)</b>: <br>
moves the value of a register into memory at some address<br>
<span class="cb-mark-blue">09: [opcode(1) address(4) register(1)] 6</span>
<br><br>-------------------------------------------<br><br>
<b class="cb-mark-red">MOV (register -> register)</b>: <br>
moves the value of a register into another register<br>
<span class="cb-mark-blue">0a: [opcode(1) register(1) register(1)] 3</span>
<br><br>-------------------------------------------<br><br>
<b class="cb-mark-red">MOV (address -> address)</b>: <br>
moves the value at some address in memory to some other address in memory<br>
<span class="cb-mark-blue">0b: [opcode(1) address(4) address(4)] 9</span>
<br><br>-------------------------------------------<br><br>