-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path700_compiler.asm
2009 lines (1746 loc) · 58.5 KB
/
700_compiler.asm
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
;此文件为编译器,用于将一段字符串编译成可运行程序
[bits 32]
SECTION compiler_code vstart=0
program_length dd program_end
jmp start
program_linear_address dd 0x0000_0000 ;程序的起始线性地址
common_service_entry dd 0x0000_0000
dw 0x0000 ;公共服务选择子
start:
;------------------------------------------------------------
;显示例程的入口函数。系统通过此函数指定功能号完成显示例程的功能调用。
;@params: di,指定功能号
; si,参数传递
;@return:di 如果是有数据返回的函数,则返回在这里
;使用方式:远程调用
;注意,在使用过程中,不一定需要传递参数。如果需要参数的,si直接给出参数的准确位置。
;这就要求si要跳过所有的空格字符直到下一个可打印字符。不同的函数将对si做不同的解析。
funtions_entry:
;第一步,比较di的值,如果有该功能标号,则进行调用。否则退出。
cmp edi,0x01 ;1号功能,显示字符功能。最常用的可以放在前面,加快运行速度
je initialize
cmp edi,0x02
je compileCommandLine ;编译命令行与编译文本文档采用不同的风格。
cmp edi,0x03
je compileSourceCode ;编译文本文件,并在缓冲区中生成相关的应用程序。
.exit:
xor di,di
retf
;-------------------------------------------------------
initialize:
push ebx
mov dword ebx,[cs:program_linear_address]
mov word [ds:ebx+common_service_entry+0x04],ax
pop ebx
retf
;-------------------------------------------------------
;此函数用于编译一个命令行命令。编译到0处即结束。、
;@params:
; eax:字符串段起始偏移位置。
;@return:
; eax:保存了编译后的起始线性地址
;使用方式:远程调用
;注意事项:无
compileCommandLine:
push ebx
push ecx
push edx
push edi
push esi
mov esi,eax
call seperate_word_by_space
cmp ecx,0x00
je .exit
;此处应该开始获取表格内容,也就是get_word_location
call get_command_word_location
xor eax,eax
cmp edi,0x00
je .exit
;接下来应该是编译命令,并送到相关的命令运行器运行了。
xor eax,eax
mov word ax,[cs:edi] ;获取操作数
call generate_command_line_binary_data
xor eax,eax
cmp edi,0x00
je .exit
mov eax,[cs:program_linear_address]
add eax,compile_buffer_start
.exit:
pop esi
pop edi
pop edx
pop ecx
pop ebx
retf
;-------------------------------------------------------
;此函数用于编译一段程序文件。
;@params:
; eax:字符串起始偏移位置。
;@return:
; eax:保存了编译后的起始线性地址。
;使用方式:远程调用
;注意事项:无
compileSourceCode:
push ebx
push ecx
push edx
push esi
push edi
mov esi,eax
call syntax_check
cmp edi,STRING_TYPE_END
jne .exit_error
;词法和语法检查无误后,开始进入程序生成阶段。
call clear_compile_buffer ;清空编译缓存
call set_generate_message ;重新复位行号,设置起始数据位置。
xor eax,eax ;如果开始就是空的,那么返回结果肯定也是0、
.start_generate:
call seperate_word_with_type
cmp edi,STRING_TYPE_END
je .compile_finished ;程序为空,自然就不能编译通过啦。
cmp edi,STRING_TYPE_LINE_END
jne .continue_generate
add esi,ecx
call update_line_number
jmp .start_generate
.continue_generate:
cmp edi,STRING_TYPE_LABEL ;如果是标号,则可以指向下一行数据了,因为词法和语法检查阶段已经生成了相关的标号和地址。
je .generate_next
call get_command_word_location ;不是标号开头的情况,就只能是命令行开头的情况了。词法分析和语法分析也做了。
cmp edi,0x00
je .exit_error
xor eax,eax
mov word ax,[cs:edi] ;获取命令操作数
call add_code_data_to_compile_buffer ;先将操作数添加至编译缓存中。
call generate_source_code_data
cmp eax,0x00
je .exit
.generate_next: ;指向下一行
add esi,ecx
jmp .start_generate
.compile_finished:
mov dword eax,[cs:program_linear_address]
add eax,compile_buffer
.exit:
;更新编译缓存长度
mov dword edi,[cs:program_linear_address]
mov dword ebx,[ds:edi+argument_address]
mov dword [ds:edi+compile_buffer_pointer],ebx
;call show_compile_buffer
;call show_args_table
pop edi
pop esi
pop edx
pop ecx
pop ebx
retf
.exit_error:
xor eax,eax
;push esi
;mov esi,string_message_unknown_command
;call show_settings_message
;pop esi
jmp .exit
;--------------------------------------------------------
;此函数用于根据命令操作数生成相应的二进制代码。
;@params:
; eax:
; ecx:
; esi:
; ds:
;@return:
; esi:
; ecx:
; ds:
; eax:返回编译结果,如果为0则表示编译出错了。
;使用方式:近程调用
;注意事项:编译完成后不需要再手动添加下一个字符串指向。
generate_source_code_data:
push ebx
push edx
push edi
cmp eax,0x01 ;prints 命令
je generate_one_input_params_length_1 ;不限类型的单个参数的命令
cmp eax,0x07 ;stayPrints 命令
je generate_one_input_params_length_1 ;不限类型的单个参数的命令
cmp eax,0x0c
je generate_one_input_params_length_4
cmp eax,0x0d ;copy命令
je generate_two_input_params_length_0 ;两个输入参数的命令。
cmp eax,0x0e ;inc 自增指令
je generate_one_input_params_length_4 ;只允许长度为4的数据类型
cmp eax,0x0f ;add
je generate_two_input_params_length_4
cmp eax,0x10 ;dec
je generate_one_input_params_length_4
cmp eax,0x11 ;sub
je generate_two_input_params_length_4
cmp eax,0x32 ;jump
je generate_one_input_params_length_0 ;只允许长度为0的地址标号类型。
cmp eax,0x33 ;repeat
je generate_two_input_params_length_0_4 ;只允许长度为0的标号类型和数字类型。
cmp eax,0x13 ;sleep
je generate_one_intput_param_length_4_constant
cmp eax,0x68 ;getTimeString
je generate_one_output_params_length_64
cmp eax,0x69 ;getDateString
je generate_one_output_params_length_64
.else:
call show_line_message
push esi
mov esi,string_message_unknown_command
call show_settings_message
pop esi
xor eax,eax
pop edi
pop edx
pop ebx
ret
;--------------------编译单个参数的命令,示例:prints------------------------------
generate_one_input_params_length_1:
add esi,ecx
call seperate_word_with_type
mov ebx,edi
call get_label_from_args_table
cmp edx,0x00
je .arg_not_defined ;数据长度为0说明是标号类型,直接提示参数不存在。
cmp edi,0x00
jne .continue_arg_existed ;参数已经存在了,就直接添加到数据表中即可。
.arg_not_defined:
;其余的是参数不存在的情况。
cmp ebx,STRING_TYPE_NORMAL
je common_exit_arg_not_defined ;如果是常规类型,则提示参数不存在。
cmp ebx,STRING_TYPE_STRING
je .constant_type_string
mov edx,0x0c ;数字类型的数据长度
jmp .add_constant_type
.constant_type_string: ;常量类型为字符串
mov edx,72
.add_constant_type:
call add_constant_type_to_arg_table
mov dword eax,[cs:argument_address]
call update_data_segment_length
.continue_arg_existed: ;添加该变量的数据地址至程序段中。
call add_code_data_to_compile_buffer ;eax表示地址
jmp compile_exit
;--------------------编译两个参数的命令,示例:copy------------------------------
generate_two_input_params_length_0:
add esi,ecx
push esi
call seperate_word_with_type
add esi,ecx
call seperate_word_with_type ;获得第二个参数的类型
mov ebx,edi
call get_label_from_args_table
cmp edi,0x00
je .arg2_not_defined ;第二个参数不存在的情况,直接新建。
cmp edx,0x00
je common_exit_arg2_wrong_type ;如果存在,那么edx数据长度又为0,说明这是一个标号类型,直接提示不合法。
;其他情况就是arg2存在的情况了。
jmp .continue_arg2_existed
.arg2_not_defined:
cmp ebx,STRING_TYPE_NORMAL
je common_exit_arg2_not_defined ;如果是常规类型,则提示参数不存在。
cmp ebx,STRING_TYPE_STRING
je .constant_type_string
mov edx,0x04 ;数字类型的数据长度
jmp .add_constant_type
.constant_type_string: ;常量类型为字符串
mov edx,64
.add_constant_type:
call add_constant_type_to_arg_table
mov dword eax,[cs:argument_address]
call update_data_segment_length
.continue_arg2_existed: ;添加该变量的数据地址至程序段中。
mov ebx,edx ;先保存参数2的数据长度。
call add_code_data_to_compile_buffer
;此时edx中保存了参数2的长度,eax保存了参数2的数据地址,开始处理参数1
pop esi
call seperate_word_with_type ;得到第一个参数名称
cmp edi,STRING_TYPE_NORMAL ;参数1只能是标准的参数类型。
jne compile_exit_argument_wrong_type
call get_label_from_args_table
cmp edi,0x00
je .create_new_arg1
cmp edx,0x00 ;后面跟了标号类型,直接表示错误
je compile_exit_argument_wrong_type
;如果参数1存在,且参数2也存在,则需要对比两者的数据长度是否一致,只有数据长度一致,才能够赋值。
cmp edx,ebx
jne compile_exit_argument_wrong_type
call add_code_data_to_compile_buffer ;写入参数1的数据地址。
add esi,ecx
call seperate_word_with_type
jmp compile_exit
.create_new_arg1:
mov edx,ebx
call add_label_to_args_table
mov dword eax,[cs:argument_address]
call update_data_segment_length ;更新数据长度edx
call add_code_data_to_compile_buffer
add esi,ecx
call seperate_word_with_type
jmp compile_exit
;--------------------编译单个参数的命令,只允许数字类型-------------------------
generate_one_input_params_length_4:
add esi,ecx
call seperate_word_with_type
cmp edi,STRING_TYPE_NORMAL
jne compile_exit_argument_wrong_type
call get_label_from_args_table
cmp edx,0x00
je common_exit_arg_not_defined
cmp edx,0x04 ;判断数据长度是否为4,如果不为4,表示参数长度错误
jne compile_exit_argument_wrong_type
call add_code_data_to_compile_buffer
jmp compile_exit
;--------------------编译两个参数的命令,只允许两个都是数字类型,而且要求都存在-------------------
generate_two_input_params_length_4:
add esi,ecx
call seperate_word_with_type
cmp edi,STRING_TYPE_NORMAL
jne compile_exit_argument_wrong_type
call get_label_from_args_table
cmp edx,0x00
je common_exit_arg_not_defined
cmp edx,0x04 ;判断数据长度是否为4,如果不为4,表示参数长度错误
jne compile_exit_argument_wrong_type
call add_code_data_to_compile_buffer
;开始处理第二个参数
add esi,ecx
call seperate_word_with_type
mov ebx,edi
call get_label_from_args_table
cmp edx,0x04
je .arg2_existed
;cmp ebx,STRING_TYPE_NORMAL
;je compile_exit_argument_wrong_type
cmp ebx,STRING_TYPE_DECIMAL
jne compile_exit_argument_wrong_type
call add_constant_type_to_arg_table
mov edx,0x0004
mov dword eax,[cs:argument_address]
call update_data_segment_length
.arg2_existed: ;参数2存在的情况,直接添加地址即可
call add_code_data_to_compile_buffer
jmp compile_exit
;--------------------编译一个参数的命令,只允许标号类型,示例:jump--------------------------
generate_one_input_params_length_0:
add esi,ecx
call seperate_word_with_type
cmp edi,STRING_TYPE_NORMAL
jne compile_exit_argument_wrong_type
call get_label_from_args_table
cmp edi,0x00
je common_exit_arg_not_defined
cmp edx,0x00
jne compile_exit_argument_wrong_type
call add_code_data_to_compile_buffer
jmp compile_exit
;--------------------编译两个参数的命令,只允许标号类型和数字类型,示例:repeat--------------------------
generate_two_input_params_length_0_4:
add esi,ecx
call seperate_word_with_type
cmp edi,STRING_TYPE_NORMAL
jne compile_exit_argument_wrong_type
call get_label_from_args_table
cmp edi,0x00
je common_exit_arg_not_defined
cmp edx,0x00
jne compile_exit_argument_wrong_type
call add_code_data_to_compile_buffer
add esi,ecx
call seperate_word_with_type
mov ebx,edi
call get_label_from_args_table
cmp edx,0x04
je .arg2_existed
;cmp ebx,STRING_TYPE_NORMAL
;je common_exit_arg_not_defined
cmp ebx,STRING_TYPE_DECIMAL
jne compile_exit_argument_wrong_type
call add_constant_type_to_arg_table
mov edx,0x0004
mov dword eax,[cs:argument_address]
call update_data_segment_length
.arg2_existed: ;参数2存在的情况,直接添加地址即可
call add_code_data_to_compile_buffer
jmp compile_exit
;--------------------编译单个参数的命令,只允许字符串类型,示例:getTimeString-------------------------
generate_one_output_params_length_64:
add esi,ecx
call seperate_word_with_type
cmp edi,STRING_TYPE_NORMAL
jne compile_exit_argument_wrong_type
call get_label_from_args_table
cmp edi,0x00
je .arg_not_defind
cmp edx,0x00
je common_exit_arg_not_defined
cmp edx,64 ;判断数据长度是否为64,如果不为4,表示参数长度错误
jne compile_exit_argument_wrong_type
call add_code_data_to_compile_buffer
jmp compile_exit
.arg_not_defind: ;参数未定义的情况,就定义参数类型
mov edx,64
call add_label_to_args_table
mov dword eax,[cs:argument_address]
call update_data_segment_length ;更新数据长度edx
call add_code_data_to_compile_buffer ;更新代码段长度
jmp compile_exit
;--------------------编译单个参数的命令,只允许数字类型,示例:sleep--------------------
generate_one_intput_param_length_4_constant:
add esi,ecx
call seperate_word_with_type
mov ebx,edi
call get_label_from_args_table
cmp edx,0x04
je .arg_existed
cmp ebx,STRING_TYPE_DECIMAL
jne compile_exit_argument_wrong_type
call add_constant_type_to_arg_table
mov edx,0x0004
mov dword eax,[cs:argument_address]
call update_data_segment_length
.arg_existed: ;参数2存在的情况,直接添加地址即可
call add_code_data_to_compile_buffer
jmp compile_exit
;-------------公共错误提示区-------------------------
common_exit_arg_not_defined:
call show_line_message
push esi
mov esi,message_argument_not_defined
call show_settings_message
pop esi
xor eax,eax
jmp compile_exit
common_exit_arg2_not_defined:
call show_line_message
push esi
mov esi,message_argument_not_defined
call show_settings_message
pop esi
pop esi
xor eax,eax
jmp compile_exit
common_exit_data_length_wrong:
xor eax,eax
push esi
mov esi,illegle_argument_length_wrong
call show_settings_message
pop esi
jmp compile_exit
compile_exit_wrong:
call show_line_message
xor eax,eax
push esi
mov esi,message_argument_not_defined
call show_settings_message
pop esi
jmp compile_exit
exit_wrong_label_not_exists:
call show_line_message
push esi
mov esi,message_label_not_exists
call show_settings_message
pop esi
xor eax,eax
jmp compile_exit
common_exit_arg2_wrong_type:
pop esi
compile_exit_argument_wrong_type:
xor eax,eax
call show_line_message
push esi
mov esi,illegle_argument_wrong_type
call show_settings_message
pop esi
compile_exit:
pop edi
pop edx
pop ebx
ret
compile_inc_bin:
compile_jump_bin:
compile_repeat_bin:
compile_copy_bin:
;--------------------------------------------------------
;此函数用于编译阶段向数据表中添加一个常量类型。
;@params:
; ebx:需要添加的常量类型,会自动设置为4和字符串长度。
; esi:
; ecx:
; ds:
;@return:
; edi:0x00表示添加失败了。
;使用方式:近程调用
;注意事项:仅添加常量类型,数据段和程序段长度需要手动更新。
add_constant_type_to_arg_table:
push eax
push ebx
push ecx
push esi
call add_label_to_args_table ;只添加数据地址和数据长度,不添加数据内容。
mov dword edi,[cs:program_linear_address]
add edi,compile_buffer
mov dword eax,[cs:argument_address]
add eax,0x04
cmp ebx,STRING_TYPE_NORMAL
je .exit_error ;如果是常规类型,则提示参数不存在。
cmp ebx,STRING_TYPE_STRING
je .constant_type_string
.constant_type_number:
mov dword [ds:edi+eax],0x04 ;数字的数据长度为4字节
add eax,0x04 ;指向内容位置。
push edi
call convert_string_to_dec
cmp edi,0x00
je .exit_error
pop edi
mov dword [ds:edi+eax],ebx
jmp .exit
.constant_type_string:
sub ecx,0x02
mov dword [ds:edi+eax],64 ;写入数据长度
add eax,0x04
.copy_string: ;写入字符串内容。
inc esi
mov byte bl,[ds:esi]
mov byte [ds:edi+eax],bl
inc eax
loop .copy_string
.exit:
pop esi
pop ecx
pop ebx
pop eax
ret
.exit_error:
pop edi
xor edi,edi
jmp .exit
;--------------------------------------------------------
;此函数用于转换一个字符串数字类型至16进制格式
;@params:
; esi:
; ecx:
; ds:
;@return:
; ebx:返回转换后的结果
; edi:表示数据是否有效,0x00表示数据无效,转换失败了。
;使用方式:近程调用
;注意事项:暂无
convert_string_to_dec:
push eax
push edx
push ecx
push esi
mov edi,ecx
xor eax,eax
xor edx,edx
xor ebx,ebx
.start:
mov byte bl,[ds:esi]
cmp ebx,0x2f ;大于30,可以继续
jg .continue
;小于字符0的情况,直接退出
jmp .finish
.continue:
cmp ebx,0x39 ;大于9的情况,不是可转换字符
jg .finish
sub ebx,0x30 ;这就是在0-9之间的情况了
add eax,ebx
cmp ecx,0x01
je .convert_finished_normally
mov ebx,0x0a
mul ebx
inc esi
loop .start
.convert_finished_normally:
mov ebx,eax ;bx返回数据长度
.exit:
pop esi
pop ecx
pop edx
pop eax
ret
.finish:
xor ebx,ebx
xor edi,edi
jmp .exit
;--------------------------------------------------------
;此函数用于添加指令数据到编译缓存中。
;@params:
; eax;保存了需要添加的指令数据
;@return:
; NONE
;使用方式:近程调用
;注意事项:暂无
add_code_data_to_compile_buffer:
push ebx
push edi
mov dword edi,[cs:program_linear_address]
mov dword ebx,[cs:compile_buffer_pointer]
mov dword [ds:edi+ebx+compile_buffer],eax
add ebx,0x04
mov dword [ds:edi+compile_buffer_pointer],ebx
pop edi
pop ebx
ret
;--------------------------------------------------------
;此函数用于添加指令数据到编译缓存中。
;@params:
; eax;保存了需要添加的指令数据
;@return:
; NONE
;使用方式:近程调用
;注意事项:暂无
clear_compile_buffer:
push ecx
push edi
mov dword edi,[cs:program_linear_address]
add edi,compile_buffer
mov dword [ds:edi],0x04 ;重置指针
mov dword ecx,511 ;2044个字节的编译缓存大
.clear:
add edi,0x04
mov dword [ds:edi],0x0000
loop .clear
.exit:
pop edi
pop ecx
ret
;--------------------------------------------------------
;此函数用于编译字符串的语法检查,并生成相应的词汇表,便于进行最终文件的生成。
;@params:
; eax:文本文档指向的线性地址
;@return:
; edi:编译结果,edi不为0则表示编译正常结束了。
;使用方式:近程调用
;注意事项:暂无
syntax_check:
;关机指令
;mov dx,0x805
;mov al,0x3c
;out dx,al
push eax
push ebx
push ecx
push edx
push esi
;指向编译词汇表
mov esi,eax
mov edi,[cs:program_linear_address]
add edi,arguments_table_start ;指向编译参数表
call clear_arg_table
call clear_compile_message
mov dword edi,[ds:esi]
cmp edi,0x00
je .exit_empty_content
.check_line_start: ;从每一行的行首开始检查
call seperate_word_with_type
;jmp .debug_string
cmp edi,STRING_TYPE_END ;正常结束
je .exit
cmp edi,STRING_TYPE_NORMAL
je .command_start ;每一行只能是标号开始,或者命令开始。否则都不合法。
cmp edi,STRING_TYPE_LABEL
je .label_start
cmp edi,STRING_TYPE_LINE_END
jne .exit_illegal_line_start
call update_line_number ;更新行号
jmp .check_line_start
;开头是标号的情况。
.label_start:
dec ecx ;减去一个长度,也就是冒号。
call get_label_from_args_table
cmp edi,0x00
jne .exit_label_redefined
xor edx,edx
call add_label_to_args_table ;将label添加到参数表中。
inc ecx
jmp .check_lines_ending
;开头是命令的情况。
.command_start:
call get_command_word_location
cmp edi,0x00
je .exit_illegal_line_start
;获得参数的个数
xor eax,eax
mov byte al,[cs:edi+0x04] ;获得参数命令个数
call update_code_segment_length
cmp al,0x00
je .match_zero_params
cmp al,0x01
je .match_one_params
cmp al,0x02
je .match_two_params
jmp .exit_unknown_command ;其他情况应该提示未知命令。
;命令是两个参数的情况,必须保证两个参数都是符合规则的。
.match_two_params: ;两个参数的情况,第一个参数可以是不确定的,但是要由第二个参数决定大小
xor ebx,ebx
mov word bx,[cs:edi+0x02] ;得到命令数据长度,0表示只能接标号类型。1表示不限制数据输入,则不用创建数据大小,
;不管是什么类型的命令,后续只能跟常规类型、字符串类型、数字类型,其余的都会报错。
add esi,ecx
call seperate_word_with_type
cmp edi,STRING_TYPE_END ;正常字符串结束了
je .exit_command_missing_argument
cmp edi,STRING_TYPE_LINE_END
je .exit_command_missing_argument
cmp edi,STRING_TYPE_UNKNOWN
je .exit_illegal_argment ;其他类型则显示参数错误。
cmp edi,STRING_TYPE_SYMBOL
je .exit_illegal_argment
cmp edi,STRING_TYPE_LABEL
je .exit_illegal_argment
;判断后面的字符串不能为命令了。
cmp edi,STRING_TYPE_NORMAL
jne .string_or_number_type
call get_command_word_location
cmp edi,0x00
jne .exit_command_followed_command ;命令后跟着命令,提示保留字。
.string_or_number_type:
;命令带一个参数的情况
.match_one_params: ;一个参数的情况
xor ebx,ebx
mov word bx,[cs:edi+0x02] ;得到命令数据长度,0表示只能接标号类型。1表示不限制数据输入,则不用创建数据大小,
;不管是什么类型的命令,后续只能跟常规类型、字符串类型、数字类型,其余的都会报错。
add esi,ecx
call seperate_word_with_type
cmp edi,STRING_TYPE_END ;正常字符串结束了
je .exit_command_missing_argument
cmp edi,STRING_TYPE_LINE_END
je .exit_command_missing_argument
cmp edi,STRING_TYPE_UNKNOWN
je .exit_illegal_argment ;其他类型则显示参数错误。
cmp edi,STRING_TYPE_SYMBOL
je .exit_illegal_argment
cmp edi,STRING_TYPE_LABEL
je .exit_illegal_argment
;判断后面的字符串不能为命令了。
cmp edi,STRING_TYPE_NORMAL
jne .string_or_number_type2
call get_command_word_location
cmp edi,0x00
jne .exit_command_followed_command ;命令后跟着命令,提示保留字。
.string_or_number_type2:
.match_zero_params: ;命令开头且没有参数的情况,只要更新代码段即可。
jmp .check_lines_ending
.check_lines_ending: ;一行应该是结束的,如果不是结束,则提示非法结束。
add esi,ecx
call seperate_word_with_type
cmp edi,0x01 ;正常结束
je .exit
cmp edi,0x02 ;行末结束,开始下一行判断。
jne .exit_illegal_extra_letters_ending
call update_line_number
jmp .check_line_start
.exit:
pop esi
pop edx
pop ecx
pop ebx
pop eax
ret
.exit_wrong_label:
call show_line_message
push esi
mov esi,string_message_wrong_label_end
call show_settings_message
pop esi
jmp .exit
.exit_illegal_line_start: ;必须以命令或者标号作为行起始
call show_line_message
push esi
mov esi,string_message_illegal_line_start
call show_settings_message
pop esi
jmp .exit
.exit_label_redefined:
call show_line_message
push esi
mov esi,string_message_label_redefined
call show_settings_message
pop esi
jmp .exit
.exit_command_missing_argument: ;参数丢失
call show_line_message
push esi
mov esi,string_message_command_missing_parameter
call show_settings_message
pop esi
jmp .exit
.exit_illegal_name:
call show_line_message
push esi
mov esi,string_message_illegal_name
call show_settings_message
pop esi
jmp .exit
.exit_illegal_argment2_not_exists:
call show_line_message
push esi
mov esi,string_message_illegal_argument2_not_exists
call show_settings_message
pop esi
jmp .exit
.exit_illegal_argment:
call show_line_message
push esi
mov esi,string_message_illegal_argument
call show_settings_message
pop esi
jmp .exit
.exit_illegal_extra_letters_ending:
call show_line_message
push esi
mov esi,string_message_illegal_extra_letters_ending
call show_settings_message
pop esi
jmp .exit
.exit_unknown_command:
call show_line_message
push esi
mov esi,string_message_unknown_command
call show_settings_message
pop esi
jmp .exit
.exit_command_followed_command:
call show_line_message
push esi
mov esi,string_message_command_can_not_be_argument
call show_settings_message
pop esi
jmp .exit
.exit_empty_content:
mov edi,0x10
jmp .exit
.debug_string:
push eax
push edi
mov eax,edi
mov edi,0x03
call far [cs:common_service_entry]
mov edi,0x01
call far [cs:common_service_entry]
mov eax,0x0d
mov edi,0x02
call far [cs:common_service_entry]
pop edi
pop eax
cmp edi,0x00
je .exit ;非正常结束
cmp edi,0x01 ;正常结束
je .exit
add esi,ecx
jmp .check_line_start
;-------------------------------------------
;此函数用于打印编译错误的行号以及字符串信息。
show_line_message:
push eax
push edi
push esi
mov eax,0x0d
mov edi,0x02
call far [cs:common_service_entry]
mov edi,0x01
call far [cs:common_service_entry]
mov esi,string_message_line
call show_settings_message
mov dword eax,[cs:line_number]
mov edi,0x03
call far [cs:common_service_entry]
mov esi,string_message_line_end
call show_settings_message
pop esi
pop edi
pop eax
ret
;此函数用于清空参数表
clear_arg_table:
push ecx
push edi
mov edi,[cs:program_linear_address]
add edi,arguments_table ;指向编译参数表