-
Notifications
You must be signed in to change notification settings - Fork 0
/
cicada-nymph.fasm
5675 lines (4615 loc) · 128 KB
/
cicada-nymph.fasm
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
;;;; before you compile the code
;;;; do not forget to choose your platform
;;;; in the following code
include "platform-configuration.inc"
;; define platform linux or windows
;; define machine 64bit or 32bit
;; in fasm, "dup" is a reserved word
dup equ duplicate
;; in fasm, "end" is a reserved word
finish equ end
end equ exit
match =64bit, machine {
jo_size = 8 ;; (byte)
xx equ dq
}
match =32bit, machine {
jo_size = 4 ;; (byte)
xx equ dd
rax equ eax
rbx equ ebx
rcx equ ecx
rdx equ edx
rsp equ esp
rbp equ ebp
rsi equ esi
rdi equ edi
syscall equ int 80h
}
match =linux =64bit, platform machine {
define linux64_sys_6_r8 r8
define linux64_sys_5_r9 r9
define linux64_sys_4_r10 r10
define linux64_sys_3_rdx rdx
define linux64_sys_2_rsi rsi
define linux64_sys_1_rdi rdi
define linux64_sys_n_rax rax
define linux64_syscall_read 0
define linux64_syscall_write 1
define linux64_syscall_open 2
define linux64_syscall_close 3
define linux64_syscall_getpid 39
define linux64_syscall_exit 60
;; about open & read & write
open_read = 0
open_write = 1
open_readAndWrite = 2
open_creat = 0100o
open_rewrite = 1000o ;; rewrite if file exist
open_append = 2000o
open_excl = 0200o ;; ensure that THIS call creates the file
open_noctty = 0400o
open_nonblock = 4000o
open_nondelay = open_nonblock
open_sync = 10000o
open_async = 20000o
open_direct = 40000o
;; to minimize cache effects of the I/O to and from this file.
open_largefile = 100000o
open_directory = 200000o
open_nofollow = 400000o ;; If pathname is a symbolic link, then the open fails.
}
match =linux =64bit, platform machine {
format ELF64 executable 3
}
match =linux =64bit, platform machine {
entry begin_to_interpret_threaded_code
segment readable executable writeable
}
match =linux =32bit, platform machine {
define linux32_sys_6_ebp ebp
define linux32_sys_5_edi edi
define linux32_sys_4_esi esi
define linux32_sys_3_edx edx
define linux32_sys_2_ecx ecx
define linux32_sys_1_ebx ebx
define linux32_sys_n_eax eax
define linux32_syscall_exit 1
define linux32_syscall_read 3
define linux32_syscall_write 4
define linux32_syscall_open 5
define linux32_syscall_close 6
define linux32_syscall_getpid 20
open_read = 0
open_write = 1
open_readAndWrite = 2
open_creat = 0100o
open_rewrite = 1000o ;; rewrite if file exist
open_append = 2000o
}
match =linux =32bit, platform machine {
format ELF executable 3
}
match =linux =32bit, platform machine {
entry begin_to_interpret_threaded_code
segment readable executable writeable
}
match =windows =64bit, platform machine {
define windows64_fun_4_r9 r9
define windows64_fun_3_r8 r8
define windows64_fun_2_rdx rdx
define windows64_fun_1_rcx rcx
define STD_INPUT_HANDLE -10
define STD_OUTPUT_HANDLE -11
}
match =windows =64bit, platform machine {
format PE64 console
}
match =windows =64bit, platform machine {
entry begin_to_interpret_threaded_code
section '.text' code writeable readable executable
}
match =windows =64bit, platform machine {
;; 這裏的 number_of_arguments 其實代表
;; 在對齊棧之後
;; 你還想要將棧的指針 向下移動多少個單位
;; 根據 windows calling convention
;; 這個數字最少是 4
macro windows64_function number_of_arguments \{
push rbp
mov rbp, rsp
mov rax, rsp
add rax, 8*number_of_arguments
mov rbx, 1111b
and rbx, rax
sub rsp, 16
add rsp, rbx
\}
macro end_windows64_function \{
mov rsp, rbp
pop rbp
\}
}
match =windows =32bit, platform machine {
define STD_INPUT_HANDLE -10
define STD_OUTPUT_HANDLE -11
}
match =windows =32bit, platform machine {
format PE console
}
match =windows =32bit, platform machine {
entry begin_to_interpret_threaded_code
section '.text' code writeable readable executable
}
current_free_address$un_initialized_memory = address$un_initialized_memory
labeling equ = current_free_address$un_initialized_memory
preserve equ current_free_address$un_initialized_memory = current_free_address$un_initialized_memory +
preserve 64 * jo_size
address$argument_stack labeling
preserve 1024 * 1024 * jo_size
match =64bit, machine {
;; if you want to extend cicada in assembly
;; the following registers must NOT be used
define pointer$argument_stack r15
}
match =64bit, machine {
macro push_argument_stack register \{
mov [pointer$argument_stack], register
add pointer$argument_stack, jo_size
\}
macro pop_argument_stack register \{
sub pointer$argument_stack, jo_size
mov register, [pointer$argument_stack]
\}
}
match =32bit, machine {
pointer$argument_stack:
xx address$argument_stack
}
match =32bit, machine {
macro push_argument_stack register \{
if register in <eax>
push ebx
mov ebx, [pointer$argument_stack]
mov [ebx], register
add ebx, jo_size
mov [pointer$argument_stack], ebx
pop ebx
else
push eax
mov eax, [pointer$argument_stack]
mov [eax], register
add eax, jo_size
mov [pointer$argument_stack], eax
pop eax
finish if
\}
macro pop_argument_stack register \{
if register in <eax>
push ebx
mov ebx, [pointer$argument_stack]
sub ebx, jo_size
mov register, [ebx]
mov [pointer$argument_stack], ebx
pop ebx
else
push eax
mov eax, [pointer$argument_stack]
sub eax, jo_size
mov register, [eax]
mov [pointer$argument_stack], eax
pop eax
finish if
\}
}
preserve 64 * jo_size
address$return_stack labeling
preserve 1024 * 1024 * jo_size
match =64bit, machine {
;; if you want to extend cicada in assembly
;; the following registers must NOT be used
define pointer$return_stack r14
}
match =64bit, machine {
macro push_return_stack register \{
mov [pointer$return_stack], register
add pointer$return_stack, jo_size
\}
macro pop_return_stack register \{
sub pointer$return_stack, jo_size
mov register, [pointer$return_stack]
\}
}
match =32bit, machine {
pointer$return_stack:
xx address$return_stack
}
match =32bit, machine {
macro push_return_stack register \{
if register in <eax>
push ebx
mov ebx, [pointer$return_stack]
mov [ebx], register
add ebx, jo_size
mov [pointer$return_stack], ebx
pop ebx
else
push eax
mov eax, [pointer$return_stack]
mov [eax], register
add eax, jo_size
mov [pointer$return_stack], eax
pop eax
finish if
\}
macro pop_return_stack register \{
if register in <eax>
mov ebx, [pointer$return_stack]
sub ebx, jo_size
mov register, [ebx]
mov [pointer$return_stack], ebx
else
mov eax, [pointer$return_stack]
sub eax, jo_size
mov register, [eax]
mov [pointer$return_stack], eax
finish if
\}
}
match =64bit, machine {
macro next \{
pop_return_stack rbx
mov rax, [rbx]
add rbx, jo_size
push_return_stack rbx
jmp qword [rax]
\}
}
match =32bit, machine {
macro next \{
pop_return_stack rbx
mov rax, [rbx]
add rbx, jo_size
push_return_stack rbx
jmp dword [rax]
\}
}
match =linux =64bit, platform machine {
__exit_with_TOS:
pop_argument_stack linux64_sys_1_rdi
mov linux64_sys_n_rax, linux64_syscall_exit
syscall
}
match =linux =64bit, platform machine {
__exit_with_zero:
xor linux64_sys_1_rdi, linux64_sys_1_rdi
mov linux64_sys_n_rax, linux64_syscall_exit
syscall
}
match =linux =64bit, platform machine {
__exit_with_six:
mov linux64_sys_1_rdi, 6
mov linux64_sys_n_rax, linux64_syscall_exit
syscall
}
match =linux =32bit, platform machine {
__exit_with_TOS:
pop_argument_stack linux32_sys_1_ebx
mov linux32_sys_n_eax, linux32_syscall_exit
syscall
}
match =linux =32bit, platform machine {
__exit_with_zero:
xor linux32_sys_1_ebx, linux32_sys_1_ebx
mov linux32_sys_n_eax, linux32_syscall_exit
syscall
}
match =linux =32bit, platform machine {
__exit_with_six:
mov linux32_sys_1_ebx, 6
mov linux32_sys_n_eax, linux32_syscall_exit
syscall
}
match =windows =64bit, platform machine {
__exit_with_TOS:
windows64_function 4
sub rsp, 8*4
pop_argument_stack windows64_fun_1_rcx
call [ExitProcess]
end_windows64_function
}
match =windows =64bit, platform machine {
__exit_with_zero:
windows64_function 4
sub rsp, 8*4
xor windows64_fun_1_rcx, windows64_fun_1_rcx
call [ExitProcess]
end_windows64_function
}
match =windows =64bit, platform machine {
__exit_with_six:
windows64_function 4
sub rsp, 8*4
mov windows64_fun_1_rcx, 6
call [ExitProcess]
end_windows64_function
}
match =windows =32bit, platform machine {
__exit_with_TOS:
pop_argument_stack rax
push rax
call [ExitProcess]
}
match =windows =32bit, platform machine {
__exit_with_zero:
push 0
call [ExitProcess]
}
match =windows =32bit, platform machine {
__exit_with_six:
push 6
call [ExitProcess]
}
;; initial link to point to 0 (as null)
link = 0
size$primitive_string_heap = 64 * 1024 ;; (byte)
address$primitive_string_heap:
times size$primitive_string_heap db 0
current_free_address$primitive_string_heap = address$primitive_string_heap
macro make_primitive_string string {
virtual at 0
.start$string:
db string
.end$string:
dw (.end$string - .start$string)
load .length word from (.end$string)
finish virtual
store word .length at (current_free_address$primitive_string_heap)
current_free_address$primitive_string_heap = current_free_address$primitive_string_heap + 2
repeat .length
virtual at 0
db string
load .char byte from (% - 1)
finish virtual
store byte .char at (current_free_address$primitive_string_heap)
current_free_address$primitive_string_heap = current_free_address$primitive_string_heap + 1
finish repeat
}
macro define_function string, jo {
define_function__#jo:
name__#jo:
xx current_free_address$primitive_string_heap
make_primitive_string string
link__#jo:
xx link
link = link__#jo
jo:
xx explain$function
;; here follows a jojo as function-body
}
explain$function:
mov rbx, [current_free_address$local_data_heap]
push_return_stack rbx
add rax, jo_size
push_return_stack rax
next
macro define_primitive_function string, jo {
define_primitive_function__#jo:
name__#jo:
xx current_free_address$primitive_string_heap
make_primitive_string string
link__#jo:
xx link
link = link__#jo
jo:
xx assembly_code__#jo
assembly_code__#jo:
;; here follows assembly code
;; as primitive function body
}
macro define_variable string, jo {
define_variable__#jo:
name__#jo:
xx current_free_address$primitive_string_heap
make_primitive_string string
link__#jo:
xx link
link = link__#jo
jo:
xx explain$variable
;; here follows a value of jo_size
;; only one value is allowed
}
explain$variable:
add rax, jo_size
mov rbx, [rax]
push_argument_stack rbx
next
macro define_macro string, jo {
define_macro__#jo:
name__#jo:
xx current_free_address$primitive_string_heap
make_primitive_string string
link__#jo:
xx link
link = link__#jo
jo:
xx explain$macro
;; here follows a jojo as function-body
}
explain$macro:
mov rbx, [current_free_address$local_data_heap]
push_return_stack rbx
add rax, jo_size
push_return_stack rax
next
macro define_exception string, jo {
define_exception__#jo:
name__#jo:
xx current_free_address$primitive_string_heap
make_primitive_string string
link__#jo:
xx link
link = link__#jo
jo:
xx explain$exception
;; here follows a jojo as function-body
}
match =64bit, machine {
explain$exception:
mov rsi, rax
.next_jojo:
pop_return_stack rbx
mov rax, qword [rbx]
cmp rax, exception_head
je .next_jo
cmp pointer$return_stack, address$return_stack
je .not_found
jmp .next_jojo
.next_jo:
;; expecting
;; rbx jojo
;; rsi jo (to cmp)
add rbx, jo_size
mov rax, qword [rbx]
cmp rax, rsi
je .found
test rax, rax
jz .next_jojo
jmp .next_jo
.found:
;; expecting
;; pointer$return_stack
;; rsi jo
pop_return_stack rax
mov pointer$argument_stack, rax
mov rbx, [current_free_address$local_data_heap]
push_return_stack rbx
add rsi, jo_size
push_return_stack rsi
next
.not_found:
call __exit_with_six
}
match =32bit, machine {
explain$exception:
mov rsi, rax
.next_jojo:
pop_return_stack rbx
mov rax, dword [rbx]
cmp rax, exception_head
je .next_jo
mov rdx, [pointer$return_stack]
cmp rdx, address$return_stack
je .not_found
jmp .next_jojo
.next_jo:
;; expecting
;; rbx jojo
;; rsi jo (to cmp)
add rbx, jo_size
mov rax, dword [rbx]
cmp rax, rsi
je .found
test rax, rax
jz .next_jojo
jmp .next_jo
.found:
;; expecting
;; pointer$return_stack
;; rsi jo
pop_return_stack rax
mov [pointer$argument_stack], rax
mov rbx, [current_free_address$local_data_heap]
push_return_stack rbx
add rsi, jo_size
push_return_stack rsi
next
.not_found:
call __exit_with_six
}
match =64bit, machine {
define_primitive_function "execute-jo", execute_jo
;; << jo -- UNKNOWN >>
pop_argument_stack rax
jmp qword [rax]
}
match =32bit, machine {
define_primitive_function "execute-jo", execute_jo
;; << jo -- UNKNOWN >>
pop_argument_stack eax
jmp dword [eax]
}
define_variable "*jo-size*", V__jo_size
xx jo_size
define_function "jo->name", jo_to_name
;; << jo -- string[address, length] >>
xx literal, jo_size, subtraction
xx literal, jo_size, subtraction
xx fetch
xx address_to_primitive_string
xx end
define_function "jo->link", jo_to_link
;; << jo -- link >>
xx literal, jo_size
xx subtraction
xx end
define_function "last-jo,dictionary?", last_jo__dictionary?
;; << jo -- bool >>
xx jo_to_link
xx fetch
xx zero?
xx end
define_function "jo->pre-jo", jo_to_pre_jo
;; << jo -- pre-jo >>
xx jo_to_link
xx fetch
xx dup, zero?, false?branch, 2
xx end
xx literal, jo_size
xx addition
xx end
define_function "jo->type", jo_to_type
;; << jo -- type >>
xx dup
xx dup, fetch
xx swap, subtraction, literal, jo_size, equal?, false?branch, 4
xx drop, zero
xx end
xx fetch
xx end
define_variable "*primitive-string-heap*", V__primitive_string_heap
xx address$primitive_string_heap
define_variable "*size,primitive-string-heap*", V__size__primitive_string_heap
xx size$primitive_string_heap
;; *current-free-address,primitive-string-heap*
;; is at epilog
define_function "address->primitive-string", address_to_primitive_string
;; << address -- string[address, length] >>
xx dup
xx literal, 2, addition ;; address
xx swap, fetch_two_bytes ;; length
xx end
define_function "primitive-function-jo?", primitive_function_jo?
;; << jo -- bool >>
xx jo_to_type
xx zero?
xx end
define_function "function-jo?", function_jo?
;; << jo -- bool >>
xx jo_to_type
xx literal, explain$function
xx equal?
xx end
define_function "macro-jo?", macro_jo?
;; << jo -- bool >>
xx jo_to_type
xx literal, explain$macro
xx equal?
xx end
define_function "exception-jo?", exception_jo?
;; << jo -- bool >>
xx jo_to_type
xx literal, explain$exception
xx equal?
xx end
define_function "variable-jo?", variable_jo?
;; << jo -- bool >>
xx jo_to_type
xx literal, explain$variable
xx equal?
xx end
define_primitive_function "end", end
pop_return_stack rbx
pop_return_stack rax
mov [current_free_address$local_data_heap], rax
next
match =64bit, machine {
define_primitive_function "<>", taca
pop_return_stack rbx
pop_return_stack rax
mov [current_free_address$local_data_heap], rax
mov rax, [rbx]
jmp qword [rax]
}
match =32bit, machine {
define_primitive_function "<>", taca
pop_return_stack ebx
pop_return_stack ecx
mov [current_free_address$local_data_heap], ecx
mov eax, [ebx]
jmp dword [eax]
;; ><><>< can not be the following
;; maybe still something wrong with pop_return_stack
;; but I care less about this now
;; define_primitive_function "<>", taca
;; pop_return_stack ebx
;; pop_return_stack eax
;; mov [current_free_address$local_data_heap], eax
;; mov eax, [ebx]
;; jmp dword [eax]
}
match =linux =64bit, platform machine {
begin_to_interpret_threaded_code:
cld ;; set DF = 0, then rsi and rdi are incremented
mov pointer$argument_stack, address$argument_stack
mov pointer$return_stack, address$return_stack
mov rax, first_jojo
push_return_stack rax
next
}
match =linux =32bit, platform machine {
begin_to_interpret_threaded_code:
cld ;; set DF = 0, then rsi and rdi are incremented
mov eax, first_jojo
push_return_stack eax
next
}
match =windows =64bit, platform machine {
_output_handle:
xx 0
_input_handle:
xx 0
begin_to_interpret_threaded_code:
cld ;; set DF = 0, then rsi and rdi are incremented
windows64_function 4
sub rsp, 8*4
mov windows64_fun_1_rcx, STD_INPUT_HANDLE
call [GetStdHandle]
mov [_input_handle], rax
end_windows64_function
windows64_function 4
sub rsp, 8*4
mov windows64_fun_1_rcx, STD_OUTPUT_HANDLE
call [GetStdHandle]
mov [_output_handle], rax
end_windows64_function
mov pointer$argument_stack, address$argument_stack
mov pointer$return_stack, address$return_stack
mov rax, first_jojo
push_return_stack rax
next
}
match =windows =32bit, platform machine {
_output_handle:
xx 0
_input_handle:
xx 0
begin_to_interpret_threaded_code:
cld ;; set DF = 0, then rsi and rdi are incremented
push STD_INPUT_HANDLE
call [GetStdHandle]
mov [_input_handle], rax
push STD_OUTPUT_HANDLE
call [GetStdHandle]
mov [_output_handle], rax
mov rax, first_jojo
push_return_stack rax
next
}
first_jojo:
xx welcome
;; xx little_test
xx initialize_dispatch_word_stack
xx initialize_local_variable
match =linux, platform {