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
|
; TODO actually enforce any of these *_SIZE constants :p
LOAD_ADDR equ 0x00010000 ; address this program is loaded at
TEST_ARENA_ADDR equ 0x00050000 ; address to run tests at
TEST_ARENA_SIZE equ 0x1000 ; maximum size tests can use
TOKEN_TABLE_ADDR equ 0x00060000 ; address the token table is loaded at
TOKEN_TABLE_SIZE equ 0x1000 ; max length of table
TOKEN_TABLE_ENTRY_SIZE equ 2 ; size of token table entry; things may break
; if this ever changes
OUTPUT_ADDR equ 0x00070000 ; address of outputed binary
OUTPUT_SIZE equ 0x1000 ; max length of outputed binary
STACK_ADDR equ 0x00060000 ; address to put the 64-bit stack at
UNRECOGNISED_TOKEN_ID equ 0xFFFF ; id of an unrecognised token
UNRECOGNISED_ID_TYPE equ 0x0F ; type of an unrecognised id
UNRECOGNISED_ID_METADATA equ 0xFF ; metadata of an unrecognised id
UNRECOGNISED_ID_OPCODE equ 0x90 ; opcode of an unrecognised id (NOP)
TEST_LINE_LENGTH equ 80 ; right border of test suite results
; flags for expected values in tokeniser
E_COMMENT equ 1 << 0
E_NEWLINE equ 1 << 1
E_WHITESPACE equ 1 << 2
E_COMMA equ 1 << 3
E_OPERATOR equ 1 << 4
E_OPERAND equ 1 << 5
[bits 64]
[org LOAD_ADDR]
[default abs] ; TODO see if I actually need to do this
; afaik absolute addressing is not harmful on bare metal
; reasoning: stops annoying warning =D
start:
mov rsp, STACK_ADDR ; we might need more stack space, let's just be safe
mov rsi, msg_welcome
call print
call run_tests
call clear_token_table
mov rdi, program ; -> program
mov rsi, [program.size] ; = size of program
call tokenise
; rax = number of tokens processed
mov rdi, rax
push rdi
call clear_output_arena
pop rdi
call assemble
jmp halt
; ------------------------------------------------------------------------------
; assembling
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; assemble
; TODO write tests
; TODO make it work :/ putting the cart before the horse
;
; description:
; assembles the program from tokens located at TOKEN_TABLE_ADDR into a flat
; binary located at OUTPUT_ADDR. It's probably desirable to clear the output
; arena before calling this function.
;
; parameters:
; rdi = number of tokens in the token table
; ------------------------------------------------------------------------------
assemble:
xor rax, rax ; number of tokens processed
.loop:
cmp rax, rdi ; check incrementer against the number of tokens in the token
jge .break ; table. If overflown, break
push rdi
xor edi, edi
mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; next tte
push rax
; di = next tte
call get_tte_type
; al = type of token
cmp al, 0x01 ; check if next tte's type is an operator
je .operator ; if so, handle case of operator
jne .continue_operator ; if not, jump past the case
.operator: ; if next tte's type is an operator:
push rax ; MUST be popped BEFORE returning to .continue_operator; it
; contains the type of token, which still needs to be used.
push rdi
; di = tte
call get_tte_typed_metadata
; al = tte typed metadata
pop rdi
and al, 11b ; mask for # operands
cmp al, 0 ; check if operator has no operands
je .operator_0 ; if so, handle case of no operands
jne .operator_with_args ; if not, jump to case of multiple operands
.operator_0:
push rdi
; di = next tte
call get_opcode
; al = opcode
call .output_byte
pop rdi
pop rax ; from start of label .operator
jmp .continue_operator
.operator_with_args:
mov [.pending_operator_num_args], al ; save # args fttb
push rdi
; di = next tte
call get_opcode
; al = opcode
mov [.pending_operator_opcode], al ; save opcode fttb
pop rdi
pop rax ; from start of label .operator
.continue_operator:
cmp al, 0x02 ; check if next tte's type is a register
je .register ; if so, handle case of register
jne .continue_register ; if not, jump past the case
.register: ; if next tte's type is a register:
call .dec_num_args ; because we've found an argument, we need 1 fewer noch
cmp byte [.pending_operator_num_args], 1 ; check if this is 1st of 2 args
je .register_one_of_two ; if so, jump to handler
cmp byte [.pending_operator_num_args], 0 ; check if this is the last arg
je .register_last ; if so, jump to handler
; note: not necessarily the last
; of 2 args, it could also be the
; last of 1
; otherwise, discard the token, reset things, and keep going :/
push rsi
mov rsi, .warn_unexpected_register
call print.warn
pop rsi
call .reset_state
jmp .continue_register
.register_one_of_two: ; if it's the first of 2 arguments:
mov [.first_argument], di ; ax = tte
jmp .continue_register
.register_last: ; if it's the last argument:
; swap so the first argument sits in .first_argument
push rax
mov ax, di
mov di, [.first_argument]
mov [.first_argument], ax
pop rax
cmp di, UNRECOGNISED_TOKEN_ID ; check if the second argument is defined
jne .operator_finalise_2 ; if so, there are 2 arguments
; if not, there is just 1
.operator_finalise_1:
mov di, 0x0000 ; id of rax. reg bits 000b
.operator_finalise_2:
; TODO avoid swapping earlier and now :/
mov cx, di
mov di, [.first_argument]
mov si, cx
call get_direct_addressing_ModRM
; al = ModR/M byte
push rax
mov al, [.pending_operator_opcode]
call .output_byte ; output operator's opcode
pop rax
call .output_byte ; output ModR/M byte
call .reset_state ; reset all the state parts of this function
jmp .continue_register
.continue_register:
pop rax ; incrementer
pop rdi ; total number of tokens
inc rax ; move to next token
jmp .loop
.break:
ret
; constants
.warn_unexpected_register db "ignoring unexpected register", 0x0A, 0x00
; procedures
; al = byte to write
.output_byte:
mov edx, [.next_output_byte] ; get output byte's address
mov [edx], al ; write byte to that address
inc edx ; increment address
mov [.next_output_byte], edx ; put output byte's address
ret
; runs dec on .pending_operator_num_args
.dec_num_args:
push rax
mov al, [.pending_operator_num_args]
dec al
mov [.pending_operator_num_args], al
pop rax
ret
.reset_state:
; I don't actually know if these `word` directives are needed
; TODO check that. I think they are, becasue Nasm doesn't record the size
; of labels?
mov word [.pending_operator_opcode], UNRECOGNISED_TOKEN_ID
mov [.pending_operator_num_args], 0x00
mov word [.first_argument], UNRECOGNISED_TOKEN_ID
ret
; state variables
.pending_operator_opcode db 0x00 ; the operator seeking args
.pending_operator_num_args db 0x00 ; # of args it takes
.first_argument dw UNRECOGNISED_TOKEN_ID ; first argument if there are two
.next_output_byte dd OUTPUT_ADDR ; next empty byte in output
; ------------------------------------------------------------------------------
; get_tte_type
;
; description:
; given a token table entry, returns the declared type in `tokens.by_id`. If
; there is no entry, returns UNRECOGNISED_ID_TYPE
;
; parameters:
; di = token table entry
;
; returned:
; al = type of token, or UNRECOGNISED_ID_TYPE. The upper 4 bits of al are
; zeroed; the rest of rax is zeroed.
; ------------------------------------------------------------------------------
get_tte_type:
and rdi, 0xFFFF ; mask input so it behaves as expected
xor eax, eax
.loop:
cmp rax, (tokens.by_id_end - tokens.by_id) / 4 ; make sure it's still in range
jg .not_found
mov cx, [tokens.by_id + rax * 4] ; next entry in tokens.by_id
cmp cx, di
je .found
inc rax
jmp .loop
.not_found:
mov al, UNRECOGNISED_ID_TYPE
and ax, 0xF ; mask as expected
ret
.found:
mov al, [2 + tokens.by_id + rax * 4]
and ax, 0xF ; mask as expected
ret
; ------------------------------------------------------------------------------
; get_tte_typed_metadata
;
; description:
; given a token table entry, returns the declared typed metadata in
; `tokens.by_id`. If there is no entry, returns UNRECOGNISED_ID_METADATA
;
; parameters:
; di = token table entry
;
; returned:
; al = typed metadata of token, or UNRECOGNISED_ID_METADATA; the rest of rax is
; zeroed.
; ------------------------------------------------------------------------------
get_tte_typed_metadata:
and rdi, 0xFFFF ; mask input so it behaves as expected
xor eax, eax
.loop:
cmp rax, (tokens.by_id_end - tokens.by_id) / 4 ; make sure it's still in range
jg .not_found
mov cx, [tokens.by_id + rax * 4] ; next entry in tokens.by_id
cmp cx, di
je .found
inc rax
jmp .loop
.not_found:
xor eax, eax
mov al, UNRECOGNISED_ID_METADATA
ret
.found:
mov al, [3 + tokens.by_id + rax * 4]
and rax, 0xFF
ret
; ------------------------------------------------------------------------------
; get_direct_addressing_ModRM
;
; description:
; given 2 register tokens, returns the ModR/M byte in direct addressing
; (mod = 11b) mode
;
; parameters:
; di = token table entry `reg`
; si = token table entry `R/M`
;
; returned:
; al = ModR/M byte; the rest of rax is zeroed
; ------------------------------------------------------------------------------
get_direct_addressing_ModRM:
mov dl, 11b
call get_ModRM
ret
; ------------------------------------------------------------------------------
; get_ModRM
;
; description:
; given 2 register tokens and the mod bits, returns the ModR/M byte
;
; parameters:
; di = token table entry `reg`
; si = token table entry `R/M`
; dl = lower 2 bits: mod bits. The rest is ignored
;
; returned:
; al = ModR/M byte; the rest of rax is zeroed
; ------------------------------------------------------------------------------
get_ModRM:
and dl, 11b ; mask for mod bits
shl dl, 6
; di = tte
call get_reg_bits
; al = reg bits
mov bl, al
shl bl, 3
mov rdi, rsi ; do the other one
; di = tte
call get_reg_bits
; al = reg bits
mov cl, al
xor eax, eax
or al, dl ; mod bits
or al, bl ; reg bits
or al, cl ; R/M bits
and rax, 0xFF ; mask for byte
ret
; ------------------------------------------------------------------------------
; get_opcode
;
; description:
; given an operator token, returns its opcode
;
; parameters:
; di = token table entry
;
; returned:
; al = opcode; the rest of rax is zeroed
; ------------------------------------------------------------------------------
get_opcode:
and rdi, 0xFFFF
xor eax, eax
.loop:
cmp rax, (opcodes.by_id_end - opcodes.by_id) / 4 ; make sure it's still in range
jg .not_found
mov cx, [opcodes.by_id + rax * 4] ; next entry in opcodes.by_id
cmp cx, di
je .found
inc rax
jmp .loop
.not_found:
xor eax, eax
mov al, UNRECOGNISED_ID_OPCODE
ret
.found:
mov al, [2 + opcodes.by_id + rax * 4]
and rax, 0xFF ; mask
ret
; ------------------------------------------------------------------------------
; get_reg_bits
;
; description:
; given a register token, returns its reg bits metadata
;
; parameters:
; di = token table entry
;
; returned:
; al = register token; the rest of rax, including the upper 5 bits of al, are
; zeroed.
; ------------------------------------------------------------------------------
get_reg_bits:
; di = tte
call get_tte_typed_metadata
; al = typed metadata
shr al, 2 ; discard type data
and al, 111b ; mask
ret
; ------------------------------------------------------------------------------
; tokenising
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; tokenise
; TODO write tests
;
; description:
; represents the program at the given address and puts it in the token table
; it's probably desirable to clear the token table before calling this function.
;
; parameters:
; rdi -> first byte of program
; rsi = size of program in bytes
;
; returned:
; rax = number of tokens processed
; ------------------------------------------------------------------------------
tokenise:
; rdi -> current byte of program
add rsi, rdi ; rsi -> last byte of program
xor eax, eax ; rax = number of tokens processed
xor edx, edx ; dl = current byte of program
.loop:
cmp rdi, rsi ; if current byte greater than last byte
jg .break ; then break
mov dl, [rdi] ; dl = current byte
cmp dl, ";" ; if current byte is the start of a comment
je .comment ; then handle the comment
cmp dl, 0x0A ; if current byte is the end of a line
je .newline_mk_flags ; then reset relevant flags
cmp dl, "," ; if current byte is a comma
je .comma ; then handle the comma
push rsi
push rdi
push rax
push rdx
; TODO probably should not ignore null bytes
mov rsi, whitespace_3 ; rsi -> list of whitespace (ignored) bytes
mov rdi, 3 ; rdi = size of list in bytes
; dl = current byte
call elemb
; al = 0 if not whitespace, 1 if whitespace
cmp al, 1 ; check if current byte is whitespace
pop rdx
pop rax
pop rdi
pop rsi
je .skip_byte_whitespace
test byte [.expecting], E_OPERATOR ; check if an operator is expected
jnz .operator ; if so, handle it
jmp .operand ; otherwise, handle as an operand
.comment:
push rsi
mov rsi, .found
call print.debug
mov rsi, .msg_comment
call print
pop rsi
test byte [.expecting], E_COMMENT ; make sure a comment is expected
jz .unexpected_comment ; if not, error
.comment_loop:
; TODO range check rdi
mov dl, [rdi] ; dl = current byte
cmp dl, 0x0A ; if current byte is a newline
je .comment_break ; then break
inc rdi ; point to next unread byte
jmp .comment_loop
.comment_break:
jmp .loop
.skip_byte_whitespace:
push rsi
mov rsi, .found
call print.debug
mov rsi, .msg_whitespace
call print
pop rsi
test byte [.expecting], E_WHITESPACE ; make sure a whitespace was expected
jz .unexpected_whitespace ; if not, error
inc rdi
jmp .loop ; else, loop
.comma: ; found comma
push rsi
mov rsi, .found
call print.debug
mov rsi, .msg_comma
call print
pop rsi
test byte [.expecting], E_COMMA ; make sure a comma was expected
jz .unexpected_comma ; if not, error
inc rdi
mov [.expecting], E_WHITESPACE | E_OPERAND ; else, make operand expected
jmp .loop ; and loop
.newline_mk_flags:
push rsi
mov rsi, .found
call print.debug
mov rsi, .msg_newline
call print
pop rsi
test byte [.expecting], E_NEWLINE ; make sure a newline was expected
jz .unexpected_newline ; if not, error
mov byte [.expecting], E_COMMENT | E_NEWLINE | E_WHITESPACE | E_OPERATOR
inc rdi
jmp .loop
.operator:
; debug message
push rsi
mov rsi, .found
call print.debug
mov rsi, .msg_operator
call print
pop rsi
push rax
xor eax, eax ; eax = number of bytes in operator
mov [.pending_operator], eax ; zero pending operator
.operator_loop:
; TODO give this its own error
; TODO make this pop rax
cmp eax, 4 ; check that operator is short enough
jg .unexpected_operator ; if not, error
mov dl, [rdi] ; next byte
; TODO have better check for operator end
cmp dl, " "
je .operator_break
cmp dl, 0x0A
je .operator_break
cmp dl, 0x00
je .operator_break
cmp dl, ";"
je .operator_break
mov [.pending_operator + eax], dl
inc rax ; inc byte counter
inc rdi ; inc byte pointer
jmp .operator_loop ; and loop
.operator_break:
; rax already pushed from .operator
push rdi
mov edi, [.pending_operator] ; edi = operator to be searched
call identify_operator
; ax = operator's token ID
mov cx, ax ; cx = operator's token ID for safe keeping
pop rdi ; rdi = byte counter
pop rax ; rax = tokens processed
mov [TOKEN_TABLE_ADDR + rax * TOKEN_TABLE_ENTRY_SIZE], cx
inc rax ; plus 1 token processed
mov byte [.expecting], E_COMMENT | E_NEWLINE | E_WHITESPACE | E_OPERAND
jmp .loop
.operand:
; debug message
push rsi
mov rsi, .found
call print.debug
mov rsi, .msg_operand
call print
pop rsi
test byte [.expecting], E_OPERAND ; make sure an operand was expected
jz .unexpected_operand ; if not, error
push rax
push rdi
xor eax, eax ; rax = length of operand
.operand_loop:
mov dl, [rdi]
cmp dl, ","
je .operand_break
cmp dl, 0x0A
je .operand_break
cmp dl, 0x00
je .operand_break
cmp dl, ";"
je .operand_break
inc rax ; inc length counter
inc rdi ; inc byte pointer
jmp .operand_loop
.operand_break:
pop rdi ; rdi -> first byte of operand
push rdi
push rsi
mov rsi, rax ; rsi = length of operand in bytes
mov cx, ax ; cx = length counter for safe keeping
call evaluate_operand
; dl = return code
; ax = register's token ID
pop rsi
pop rdi ; rdi = first byte of operand
add di, cx ; rdi = last byte of operand
mov rcx, rax ; rcx = evaluate_operand's return value
pop rax ; rax = number of tokens processed
; operand is a register
; cx = token ID
cmp dl, 0x00
je .operand_register
jmp .unexpected_operand
.operand_register:
mov [TOKEN_TABLE_ADDR + rax * TOKEN_TABLE_ENTRY_SIZE], cx
inc rax ; another token processed
jmp .operand_break_continue
.operand_break_continue:
mov byte [.expecting], E_COMMENT | E_NEWLINE | E_WHITESPACE | E_COMMA
jmp .loop
.break:
ret
; state
.expecting db E_COMMENT | E_NEWLINE | E_WHITESPACE | E_OPERATOR
.unexpected_whitespace:
mov rsi, .err_unexpected
call print.error
mov rsi, .msg_whitespace
call print
jmp halt
.unexpected_comment:
mov rsi, .err_unexpected
call print.error
mov rsi, .msg_comment
call print
jmp halt
.unexpected_newline:
mov rsi, .err_unexpected
call print.error
mov rsi, .msg_newline
call print
jmp halt
.unexpected_comma:
mov rsi, .err_unexpected
call print.error
mov rsi, .msg_comma
call print
jmp halt
.unexpected_operand:
mov rsi, .err_unexpected
call print.error
mov rsi, .msg_operand
call print
jmp halt
.unexpected_operator:
mov rsi, .err_unexpected
call print.error
mov rsi, .msg_operator
call print
jmp halt
.err_unexpected db "unexpected ", 0x00
.found db "found ", 0x00
.msg_whitespace db "whitespace.", 0x0A, 0x00
.msg_comment db "comment.", 0x0A, 0x00
.msg_newline db "newline.", 0x0A, 0x00
.msg_comma db "comma.", 0x0A, 0x00
.msg_operator db "operator.", 0x0A, 0x00
.msg_operand db "operand.", 0x0A, 0x00
.pending_operator dd 0 ; the operator token that is pending processing
; ------------------------------------------------------------------------------
; evaluate_operand
;
; description:
; takes the location and length of an operand and evaluates it into binary data
; and a return code to interpret the binary data.
;
; | code | rsi contents | notes |
; |------|----------------------|-------|
; | 0x00 | token ID of register | reg |
; | 0x10 | token ID of register | [reg] |
; | 0xFF | - | error |
;
; parameters:
; rdi -> first byte of operand
; rsi = size of operand in bytes
;
; returned:
; rax = binary data corresponding to the operand
; dl = return code
; ------------------------------------------------------------------------------
evaluate_operand:
push rdi
push rsi
mov rsi, rdi ; rsi -> start of operand
pop rdi ; rdi = size of operand
call trim_trailing_whitespace
pop rdi ; rdi -> first byte of operand
mov rsi, rax ; rsi = size of operand w/o trailing whitespace
cmp [rdi + 1], '['
je .address
jmp .register
.address:
jmp halt
.register:
cmp rsi, 4
jg .unrecognised
push rdi
mov edi, [rdi] ; edi = register to be searched
; TODO figure out how to mask elegantly :/
; mask edi for lower rsi bits
cmp rsi, 4
je .register4
cmp rsi, 3
je .register3
cmp rsi, 2
je .register2
cmp rsi, 1
je .register1
.register1:
and edi, 0xFF
push rsi
mov rsi, .msg
call print.debug
pop rsi
.register2:
and edi, 0xFFFF
.register3:
and edi, 0xFFFFFF
.register4:
call identify_register
; ax = register's token ID or UNRECOGNISED_TOKEN_ID
pop rdi
cmp ax, UNRECOGNISED_TOKEN_ID
je .unrecognised
mov dl, 0x00
ret
.unrecognised:
mov dl, 0xFF
ret
.msg db "evaluate_operand", 0x0A, 0x00
; ------------------------------------------------------------------------------
; evaluate_constant
;
; description:
; takes a constant and returns its hexidecimal representation. Currently the
; following constants are supported:
;
; | type | p. | description |
; |------|----|--------------|
; | 0x00 | 0x | hexidecimal |
; | 0x01 | 0q | octal |
; | 0x02 | 0b | binary |
; | 0x03 | " | char |
; | 0xFF | | unrecognised |
;
; where `p.` is the prefix or otherwise indicator
;
; parameters:
; rdi -> first byte of constant
; rsi = size of constant in bytes
;
; returned:
; rax = value of the constant in hexidecimal
; dl = type of constant; the rest of rdx is zeroed
; ------------------------------------------------------------------------------
evaluate_constant:
; rsi = number of bytes left
; rdi -> current byte of constant
xor eax, eax ; rax = value of constant
; each case pushes the return value of dl into `rcx`, which is popped into dl
; to return
mov dl, [rdi]
dec rsi ; one fewer byte left
inc rdi ; point to next byte
; all numeric prefixes further handled in .numeric
cmp dl, '0'
je .numeric
; chr case
mov rcx, 0x03
push rcx
xor ecx, ecx ; rcx = number of times right-rolled
cmp dl, '"'
je .chr
pop rcx
jmp .unrecognised
.numeric:
mov dl, [rdi]
dec rsi ; one fewer byte left
inc rdi ; point to next byte
; hex case
mov rcx, 0x00
push rcx
cmp dl, 'x'
je .hex_loop
pop rcx
; octal case
mov rcx, 0x01
push rcx
cmp dl, 'q'
je .oct_loop
pop rcx
; binary case
mov rcx, 0x02
push rcx
cmp dl, 'b'
je .bin_loop
pop rcx
jmp .unrecognised
.hex_loop:
cmp rsi, 0 ; make sure we're in range
je .break ; if not, break
shl rax, 4 ; make room for next hex digit
mov dl, [rdi] ; dl = next byte of constant
sub dl, '0' ; dl = if digit: digit; else :shrug:
cmp dl, 9 ; if !digit:
jg .hex_alpha ; letter
jmp .hex_continue ; else loop
.hex_alpha:
sub dl, 7 ; map [('A'-'0')..('F'-'0')] to [0xA..0xF]
cmp dl, 0xF ; if not in the range [0xA..0xF]
jg .unrecognised ; then unrecognised
.hex_continue:
and dl, 0x0F ; mask
or al, dl ; and add newest nibble
dec rsi ; one fewer byte left
inc rdi ; point to next byte
jmp .hex_loop ; and loop
.oct_loop:
cmp rsi, 0 ; make sure we're in range
je .break ; if not, break
shl rax, 3 ; make room for next octal digit
mov dl, [rdi] ; dl = next byte of constant
sub dl, '0'
cmp dl, 7
jg .unrecognised
and dl, 7 ; mask
or al, dl ; and add newest 3-bit group
dec rsi ; one fewer byte left
inc rdi ; point to next byte
jmp .oct_loop ; and loop
.bin_loop:
cmp rsi, 0 ; range check
je .break
shl rax, 1
mov dl, [rdi]
sub dl, '0'
cmp dl, 1
jg .unrecognised
and dl, 1 ; mask
or al, dl ; and newest bit
dec rsi
inc rdi
jmp .bin_loop
.chr:
cmp rcx, 4 ; ensure char is only 4 bytes long
jg .unrecognised
cmp rsi, 1 ; range check
je .chr_break
ror rax, 8
inc rcx
mov dl, [rdi]
; bound check byte as printable char
cmp dl, 0x20
jl .unrecognised
cmp dl, 0x7E
jg .unrecognised
or al, dl
dec rsi
inc rdi
jmp .chr
.chr_break:
cmp rcx, 1 ; for each [1..rcx]
jle .chr_break_for_good
rol rax, 8 ; roll left to make up for the roll right earlier
dec rcx
jmp .chr_break
.chr_break_for_good:
mov dl, [rdi] ; make sure the chr is closed
cmp dl, '"'
jne .unrecognised
jmp .break
.break:
pop rdx
ret
.unrecognised:
pop rdx
mov rdx, 0xFF ; unrecognised type
ret
; ------------------------------------------------------------------------------
; identify_register
;
; description:
; takes a register in ascii-encoded text and returns its token ID or
; UNRECOGNISED_TOKEN_ID if not recognised
;
; parameters:
; edi = register to be searched
;
; returned:
; ax = register's token ID or UNRECOGNISED_TOKEN_ID
; ------------------------------------------------------------------------------
identify_register:
xor eax, eax ; tokens.registers + eax -> entry in tokens.registers
.loop:
cmp eax, (tokens.registers_end - tokens.registers)
jge .not_found
cmp edi, [tokens.registers + eax]
je .found
add eax, 6
jmp .loop
.found:
mov ax, [tokens.registers + eax + 4]
ret
.not_found:
mov ax, UNRECOGNISED_TOKEN_ID
ret
; ------------------------------------------------------------------------------
; identify_operator
; TODO combine with identify_register
;
; description:
; takes an operator in ascii-encoded text and returns its token ID or
; UNRECOGNISED_TOKEN_ID if not recognised
;
; parameters:
; edi = operator to be searched
;
; returned:
; ax = operator's token ID or UNRECOGNISED_TOKEN_ID
; ------------------------------------------------------------------------------
identify_operator:
xor eax, eax ; tokens.operators + eax -> entry in tokens.operators
.loop:
cmp eax, (tokens.operators_end - tokens.operators)
jge .not_found
cmp edi, [tokens.operators + eax]
je .found
add eax, 6
jmp .loop
.found:
mov ax, [tokens.operators + eax + 4]
ret
.not_found:
mov ax, UNRECOGNISED_TOKEN_ID
ret
; ------------------------------------------------------------------------------
; utilities
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; print
;
; description:
; prints a null-terminated string
; probably doesn't change any registers for ease of debugging
;
; parameters:
; rsi -> start of null-terminated string
; ------------------------------------------------------------------------------
print:
push rdx
push rax
push rsi
mov edx, 0x3F8
.loop:
mov al, [rsi]
test al, al
jz .done
out dx, al
inc rsi
jmp .loop
.done:
pop rsi
pop rax
pop rdx
ret
.debug:
push rsi
mov rsi, .debug_msg
call print
pop rsi
call print
ret
.error:
push rsi
mov rsi, .error_msg
call print
pop rsi
call print
ret
.test:
push rsi
mov rsi, .test_msg
call print
pop rsi
call print
ret
.warn:
push rsi
mov rsi, .warn_msg
call print
pop rsi
call print
ret
.debug_msg db "[DEBUG]: ", 0x00
.error_msg db "[ERROR]: ", 0x00
.test_msg db "[TEST]: ", 0x00
.warn_msg db "[WARN]: ", 0x00
; ------------------------------------------------------------------------------
; halt
;
; description:
; halts the program, silly :)
; ------------------------------------------------------------------------------
halt:
mov rsi, msg_halt
call print
hlt
jmp halt
; ------------------------------------------------------------------------------
; elemb
;
; description:
; checks if given byte is element of the specified list.
;
; parameters:
; rdi = size of list
; rsi -> start of list
; dl = given byte
;
; returned:
; rax = 0: is not an element
; 1: is an element
; ------------------------------------------------------------------------------
elemb:
.loop:
cmp rdi, 0 ; check if remaining length 0
je .not_found ; if so, break; dl not an element of list
mov al, [rsi]
cmp al, dl ; check if current byte in list is the desired byte
je .found ; if so, break; dl an element of list
inc rsi ; move to next byte
dec rdi ; and reduce remaining length
jmp .loop
.not_found:
xor eax, eax ; return 0; dl not an element of list
ret
.found:
xor eax, eax
mov rax, 1 ; return 1; dl an element of list
ret
; ------------------------------------------------------------------------------
; trim_trailing_whitespace
;
; description:
; trims whitespace from the start and end of the given byte array.
;
; parameters:
; rdi = size of list
; rsi -> start of list
;
; returned:
; rax = new size of list
; ------------------------------------------------------------------------------
trim_trailing_whitespace:
cmp rdi, 0 ; list of length zero
je .done ; already trimmed
push rdi
push rsi
mov dl, [rsi + rdi - 1] ; last element of given list
mov rsi, whitespace_3 ; pointer of whitespace list
mov rdi, 3 ; length of whitespace list
call elemb
pop rsi ; rsi -> start of list
pop rdi ; rdi = size of list
cmp al, 0 ; if last element whitespace
je .done ; then break
.trim ; otherwise one shorter
dec rdi
call trim_trailing_whitespace
ret
.done
mov rax, rdi
ret
; ------------------------------------------------------------------------------
; clear_token_table
;
; description:
; clears the token table as specified by TOKEN_TABLE_SIZE and TOKEN_TABLE_ADDR
; ------------------------------------------------------------------------------
clear_token_table:
xor eax, eax ; value to write
mov rcx, TOKEN_TABLE_SIZE / 4 ; number of double words
mov rdi, TOKEN_TABLE_ADDR ; address to start
rep stosd
ret
; ------------------------------------------------------------------------------
; clear_test_arena
;
; description:
; clears the test arena as specified by TEST_ARENA_SIZE and TEST_ARENA_ADDR
; ------------------------------------------------------------------------------
clear_test_arena:
xor eax, eax ; value to write
mov rcx, TOKEN_TABLE_SIZE / 4 ; number of double words
mov rdi, TOKEN_TABLE_ADDR ; address to start
rep stosd
ret
; ------------------------------------------------------------------------------
; clear_output_arena
;
; description:
; clears the output arena as specified by OUTPUT_SIZE and OUTPUT_ADDR
; ------------------------------------------------------------------------------
clear_output_arena:
xor eax, eax ; value to write
mov rcx, OUTPUT_SIZE / 4 ; number of double words
mov rdi, OUTPUT_ADDR ; address to start
rep stosd
ret
%include "asm/tests.asm"
; ------------------------------------------------------------------------------
; data
; ------------------------------------------------------------------------------
tokens:
.by_id:
dw 0x0010 ; eax
db 0x02 ; type: register
db 00000010b ; reg: 000b
; width: 10b (32 bits)
dw 0x0000 ; rax
db 0x02 ; type: register
db 00000011b ; reg: 000b
; width: 11b (64 bits)
dw 0x0003 ; rdx
db 0x02 ; type: register
db 00001011b ; reg: 010b
; width: 11b (64 bits)
dw 0x0053 ; xor
db 0x01 ; type: operator
db 0x02 ; # operands
dw 0x0054 ; inc
db 0x01 ; type: operator
db 0x01 ; # operands
dw 0x0056 ; mov
db 0x01 ; type: operator
db 0x02 ; # operands
dw 0x004F ; hlt
db 0x01 ; type: operator
db 0x00 ; # operands
.by_id_end:
.operators:
dd "je"
dw 0x005C
dd "jg"
dw 0x005F
dd "jl"
dw 0x0061
dd "hlt"
dw 0x004F
dd "xor"
dw 0x0053
dd "inc"
dw 0x0054
dd "dec"
dw 0x0055
dd "mov"
dw 0x0056
dd "add"
dw 0x0057
dd "sub"
dw 0x0058
dd "ret"
dw 0x005A
dd "cmp"
dw 0x005B
dd "jne"
dw 0x005D
dd "jge"
dw 0x005E
dd "jle"
dw 0x0060
dd "int3"
dw 0x0050
dd "call"
dw 0x0059
.operators_end:
.registers:
dd "r8"
dw 0x0008
dd "r9"
dw 0x0009
dd "ax"
dw 0x0020
dd "bx"
dw 0x0021
dd "cx"
dw 0x0022
dd "dx"
dw 0x0023
dd "si"
dw 0x0024
dd "di"
dw 0x0025
dd "sp"
dw 0x0026
dd "bp"
dw 0x0027
dd "al"
dw 0x0030
dd "bl"
dw 0x0031
dd "cl"
dw 0x0032
dd "dl"
dw 0x0033
dd "ah"
dw 0x0040
dd "bh"
dw 0x0041
dd "ch"
dw 0x0042
dd "dh"
dw 0x0043
dd "cs"
dw 0x0044
dd "ds"
dw 0x0045
dd "es"
dw 0x0046
dd "fs"
dw 0x0047
dd "gs"
dw 0x0048
dd "ss"
dw 0x0049
dd "rax"
dw 0x0000
dd "rbx"
dw 0x0001
dd "rcx"
dw 0x0002
dd "rdx"
dw 0x0003
dd "rsi"
dw 0x0004
dd "rdi"
dw 0x0005
dd "rsp"
dw 0x0006
dd "rbp"
dw 0x0007
dd "r10"
dw 0x000A
dd "r11"
dw 0x000B
dd "r12"
dw 0x000C
dd "r13"
dw 0x000D
dd "r14"
dw 0x000E
dd "r15"
dw 0x000F
dd "eax"
dw 0x0010
dd "ebx"
dw 0x0011
dd "ecx"
dw 0x0012
dd "edx"
dw 0x0013
dd "esi"
dw 0x0014
dd "edi"
dw 0x0015
dd "esp"
dw 0x0016
dd "ebp"
dw 0x0017
dd "r8d"
dw 0x0018
dd "r9d"
dw 0x0019
dd "r8w"
dw 0x0028
dd "r9w"
dw 0x0029
dd "sil"
dw 0x0034
dd "dil"
dw 0x0035
dd "spl"
dw 0x0036
dd "bpl"
dw 0x0037
dd "r8b"
dw 0x0038
dd "r9b"
dw 0x0039
dd "cr0"
dw 0x004A
dd "cr2"
dw 0x004B
dd "cr3"
dw 0x004C
dd "cr4"
dw 0x004D
dd "cr8"
dw 0x004E
dd "r10d"
dw 0x001A
dd "r11d"
dw 0x001B
dd "r12d"
dw 0x001C
dd "r13d"
dw 0x001D
dd "r14d"
dw 0x001E
dd "r15d"
dw 0x001F
dd "r10w"
dw 0x002A
dd "r11w"
dw 0x002B
dd "r12w"
dw 0x002C
dd "r13w"
dw 0x002D
dd "r14w"
dw 0x002E
dd "r15w"
dw 0x002F
dd "r10b"
dw 0x003A
dd "r11b"
dw 0x003B
dd "r12b"
dw 0x003C
dd "r13b"
dw 0x003D
dd "r14b"
dw 0x003E
dd "r15b"
dw 0x003F
.registers_end:
opcodes:
.by_id:
dw 0x0053 ; xor
db 0x31
db 0x00 ; reserved
dw 0x0054 ; inc
db 0xFF
db 0x00 ; reserved
dw 0x0056 ; mov
db 0x89
db 0x00 ; reserved
dw 0x004F ; hlt
db 0xF4
db 0x00 ; reserved
.by_id_end:
msg_welcome db "Welcome to Twasm", 0x0A, 0x00
msg_halt db "halted.", 0x0A, 0x00
token_terminator_8 db 0x00, " ", 0x0A, 0x0D, ",", 0x00, 0x00, 0x00
whitespace_3 db " ", 0x0D, 0x00
; test program
program:
db "xor eax, eax", 0x0A
db "inc rax ; inline comment", 0x0A
db "; one line comment", 0x0A
db "mov [ rax ], rdx", 0x0A
db "hlt", 0x00 ; for the sake of being able to print it, I made it a string
.size db $ - program
|