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
|
# some common helpers shared by phases of the SubX converter
== code
# instruction effective address register displacement immediate
# . op subop mod rm32 base index scale r32
# . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes
# 'table' is a stream of (key, value) rows
# keys are always strings (addresses; size 4 bytes)
# values may be any type, but rows (key+value) always occupy 'row-size' bytes
# scan 'table' for a row with a key 'key' and return the address of the corresponding value
# if no row is found, save 'key' to the next available row
# if there are no rows free, abort
# return the address of the value
# Beware: assume keys are immutable; they're inserted by reference
# TODO: pass in an allocation descriptor
get-or-insert: # table : (address stream {string, _}), key : (address string), row-size : int -> EAX : (address _)
# pseudocode:
# curr = table->data
# max = &table->data[table->write]
# while curr < max
# if string-equal?(key, *curr)
# return curr+4
# curr += row-size
# if table->write >= table->length
# abort
# *max = key
# table->write += row-size
# return max+4
#
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# . save registers
51/push-ECX
52/push-EDX
56/push-ESI
# ESI = table
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI
# curr/ECX = table->data
8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX
# max/EDX = table->data + table->write
8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX
8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX
$get-or-insert:search-loop:
# if (curr >= max) break
39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX
7d/jump-if-greater-or-equal $get-or-insert:not-found/disp8
# if (string-equal?(key, *curr)) return curr+4
# . EAX = string-equal?(key, *curr)
# . . push args
ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12)
# . . call
e8/call string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . if (EAX != 0) return EAX = curr+4
3d/compare-EAX-and 0/imm32
74/jump-if-equal $get-or-insert:mismatch/disp8
8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX
eb/jump $get-or-insert:end/disp8
$get-or-insert:mismatch:
# curr += row-size
03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX
# loop
eb/jump $get-or-insert:search-loop/disp8
$get-or-insert:not-found:
# result/EAX = 0
31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX
# if (table->write >= table->length) abort
8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX
3b/compare 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 8/disp8 . # compare ECX with *(ESI+8)
73/jump-if-greater-or-equal-unsigned $get-or-insert:abort/disp8
# *max = key
# . EAX = key
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0xc/disp8 . # copy *(EBP+12) to EAX
# . *max = EAX
89/copy 0/mod/indirect 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to *EDX
# table->write += row-size
# . EAX = row-size
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0x10/disp8 . # copy *(EBP+16) to EAX
# . table->write += EAX
01/add 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # add EAX to *ESI
# return max+4
# . EAX = max
89/copy 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # copy EDX to EAX
# . EAX += 4
05/add-to-EAX 4/imm32
$get-or-insert:end:
# . restore registers
5e/pop-to-ESI
5a/pop-to-EDX
59/pop-to-ECX
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
$get-or-insert:abort:
# . _write(2/stderr, error)
# . . push args
68/push "get-or-insert: too many segments"/imm32
68/push 2/imm32/stderr
# . . call
e8/call _write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . syscall(exit, 1)
bb/copy-to-EBX 1/imm32
b8/copy-to-EAX 1/imm32/exit
cd/syscall 0x80/imm8
# never gets here
test-get-or-insert:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes)
81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP
68/push 0x10/imm32/length
68/push 0/imm32/read
68/push 0/imm32/write
89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX
$test-get-or-insert:first-call:
# - start with an empty table, insert one key, verify that it was inserted
# EAX = get-or-insert(table, "code", 8 bytes per row)
# . . push args
68/push 8/imm32/row-size
68/push "code"/imm32
51/push-ECX
# . . call
e8/call get-or-insert/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-ints-equal(EAX - table->data, 4, msg) # first row's value slot returned
# . check-ints-equal(EAX - table, 16, msg)
# . . push args
68/push "F - test-get-or-insert/0"/imm32
68/push 0x10/imm32
29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX
50/push-EAX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-get-or-insert:check2:
# check-ints-equal(table->write, row-size = 8, msg)
# . . push args
68/push "F - test-get-or-insert/1"/imm32
68/push 8/imm32/row-size
ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-string-equal(*table->data, "code", msg)
# . . push args
68/push "F - test-get-or-insert/2"/imm32
68/push "code"/imm32
ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12)
# . . call
e8/call check-string-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-get-or-insert:second-call:
# - insert the same key again, verify that it was reused
# EAX = get-or-insert(table, "code", 8 bytes per row)
# . . push args
68/push 8/imm32/row-size
68/push "code"/imm32
51/push-ECX
# . . call
e8/call get-or-insert/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-ints-equal(EAX - table->data, 4, msg)
# . check-ints-equal(EAX - table, 16, msg)
# . . push args
68/push "F - test-get-or-insert/3"/imm32
68/push 0x10/imm32
29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX
50/push-EAX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# no new row inserted
# . check-ints-equal(table->write, row-size = 8, msg)
# . . push args
68/push "F - test-get-or-insert/4"/imm32
68/push 8/imm32/row-size
ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-string-equal(*table->data, "code", msg)
# . . push args
68/push "F - test-get-or-insert/5"/imm32
68/push "code"/imm32
ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12)
# . . call
e8/call check-string-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-get-or-insert:third-call:
# - insert a new key, verify that it was inserted
# EAX = get-or-insert(table, "data", 8 bytes per row)
# . . push args
68/push 8/imm32/row-size
68/push "data"/imm32
51/push-ECX
# . . call
e8/call get-or-insert/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# table gets a new row
# check-ints-equal(EAX - table->data, 12, msg) # second row's value slot returned
# . check-ints-equal(EAX - table, 24, msg)
# . . push args
68/push "F - test-get-or-insert/6"/imm32
68/push 0x18/imm32
29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX
50/push-EAX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-ints-equal(table->write, 2 rows = 16, msg)
# . . push args
68/push "F - test-get-or-insert/7"/imm32
68/push 0x10/imm32/two-rows
ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-string-equal(*table->data+8, "data", msg)
# check-string-equal(*(table+20), "data", msg)
# . . push args
68/push "F - test-get-or-insert/8"/imm32
68/push "data"/imm32
ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0x14/disp8 . # push *(ECX+20)
# . . call
e8/call check-string-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-get-or-insert:end:
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
# 'table' is a stream of (key, value) rows
# keys are always strings (addresses; size 4 bytes)
# values may be any type, but rows (key+value) always occupy 'row-size' bytes
# scan 'table' for a row with a key 'key' and return the address of the corresponding value
# if no row is found, save 'key' in the next available row
# if there are no rows free, abort
# TODO: pass in an allocation descriptor
get-or-insert-slice: # table : (address stream {string, _}), key : (address slice), row-size : int -> EAX : (address _)
# pseudocode:
# curr = table->data
# max = &table->data[table->write]
# while curr < max
# if slice-equal?(key, *curr)
# return curr+4
# curr += row-size
# if table->write >= table->length
# abort
# *max = slice-to-string(Heap, key)
# table->write += row-size
# return max+4
#
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# . save registers
51/push-ECX
52/push-EDX
56/push-ESI
# ESI = table
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI
# curr/ECX = table->data
8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 0xc/disp8 . # copy ESI+12 to ECX
# max/EDX = table->data + table->write
8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX
8d/copy-address 0/mod/indirect 4/rm32/sib 1/base/ECX 2/index/EDX . 2/r32/EDX . . # copy ECX+EDX to EDX
$get-or-insert-slice:search-loop:
# if (curr >= max) break
39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX
73/jump-if-greater-or-equal-unsigned $get-or-insert-slice:not-found/disp8
# if (slice-equal?(key, *curr)) return curr+4
# . EAX = slice-equal?(key, *curr)
# . . push args
ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12)
# . . call
e8/call slice-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . if (EAX != 0) return EAX = curr+4
3d/compare-EAX-and 0/imm32
74/jump-if-equal $get-or-insert-slice:mismatch/disp8
8d/copy-address 1/mod/*+disp8 1/rm32/ECX . . . 0/r32/EAX 4/disp8 . # copy ECX+4 to EAX
eb/jump $get-or-insert-slice:end/disp8
$get-or-insert-slice:mismatch:
# curr += row-size
03/add 1/mod/*+disp8 5/rm32/EBP . . . 1/r32/ECX 0x10/disp8 . # add *(EBP+16) to ECX
# loop
eb/jump $get-or-insert-slice:search-loop/disp8
$get-or-insert-slice:not-found:
# result/EAX = 0
31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX
# if (table->write >= table->length) abort
8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX
3b/compare 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 8/disp8 . # compare ECX with *(ESI+8)
7d/jump-if-greater-or-equal $get-or-insert-slice:abort/disp8
# *max = slice-to-string(Heap, key)
# . EAX = slice-to-string(Heap, key)
# . . push args
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12)
68/push Heap/imm32
# . . call
e8/call slice-to-string/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . *max = EAX
89/copy 0/mod/indirect 2/rm32/EDX . . . 0/r32/EAX . . # copy EAX to *EDX
# table->write += row-size
# . EAX = row-size
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 0x10/disp8 . # copy *(EBP+16) to EAX
# . table->write += EAX
01/add 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # add EAX to *ESI
# return max+4
# . EAX = max
89/copy 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # copy EDX to EAX
# . EAX += 4
05/add-to-EAX 4/imm32
$get-or-insert-slice:end:
# . restore registers
5e/pop-to-ESI
5a/pop-to-EDX
59/pop-to-ECX
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
$get-or-insert-slice:abort:
# . _write(2/stderr, error)
# . . push args
68/push "get-or-insert-slice: too many segments"/imm32
68/push 2/imm32/stderr
# . . call
e8/call _write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . syscall(exit, 1)
bb/copy-to-EBX 1/imm32
b8/copy-to-EAX 1/imm32/exit
cd/syscall 0x80/imm8
# never gets here
test-get-or-insert-slice:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# var table/ECX : (address stream {string, number}) = stream(2 rows * 8 bytes)
81 5/subop/subtract 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # subtract from ESP
68/push 0x10/imm32/length
68/push 0/imm32/read
68/push 0/imm32/write
89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX
# EDX : (address slice) = "code"
68/push _test-code-segment-end/imm32/end
68/push _test-code-segment/imm32/start
89/copy 3/mod/direct 2/rm32/EDX . . . 4/r32/ESP . . # copy ESP to EDX
$test-get-or-insert-slice:first-call:
# - start with an empty table, insert one key, verify that it was inserted
# EAX = get-or-insert-slice(table, "code" slice, 8 bytes per row)
# . . push args
68/push 8/imm32/row-size
52/push-EDX
51/push-ECX
# . . call
e8/call get-or-insert-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-ints-equal(EAX - table->data, 4, msg) # first row's value slot returned
# . check-ints-equal(EAX - table, 16, msg)
# . . push args
68/push "F - test-get-or-insert-slice/0"/imm32
68/push 0x10/imm32
29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX
50/push-EAX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-get-or-insert-slice:check2:
# check-ints-equal(table->write, row-size = 8, msg)
# . . push args
68/push "F - test-get-or-insert-slice/1"/imm32
68/push 8/imm32/row-size
ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-string-equal(*table->data, "code", msg)
# . . push args
68/push "F - test-get-or-insert-slice/2"/imm32
68/push "code"/imm32
ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12)
# . . call
e8/call check-string-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-get-or-insert-slice:second-call:
# - insert the same key again, verify that it was reused
# EAX = get-or-insert-slice(table, "code" slice, 8 bytes per row)
# . . push args
68/push 8/imm32/row-size
52/push-EDX
51/push-ECX
# . . call
e8/call get-or-insert-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-ints-equal(EAX - table->data, 4, msg)
# . check-ints-equal(EAX - table, 16, msg)
# . . push args
68/push "F - test-get-or-insert-slice/3"/imm32
68/push 0x10/imm32
29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX
50/push-EAX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# no new row inserted
# . check-ints-equal(table->write, row-size = 8, msg)
# . . push args
68/push "F - test-get-or-insert-slice/4"/imm32
68/push 8/imm32/row-size
ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-string-equal(*table->data, "code", msg)
# . . push args
68/push "F - test-get-or-insert-slice/5"/imm32
68/push "code"/imm32
ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0xc/disp8 . # push *(ECX+12)
# . . call
e8/call check-string-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-get-or-insert-slice:third-call:
# - insert a new key, verify that it was inserted
# EDX : (address slice) = "data"
c7 0/subop/copy 0/mod/indirect 2/rm32/EDX . . . . . _test-data-segment/imm32 # copy to *EDX
c7 0/subop/copy 1/mod/*+disp8 2/rm32/EDX . . . . 4/disp8 _test-data-segment-end/imm32 # copy to *(EDX+4)
# EAX = get-or-insert-slice(table, "data" slice, 8 bytes per row)
# . . push args
68/push 8/imm32/row-size
52/push-EDX
51/push-ECX
# . . call
e8/call get-or-insert-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# table gets a new row
# check-ints-equal(EAX - table->data, 12, msg) # second row's value slot returned
# . check-ints-equal(EAX - table, 24, msg)
# . . push args
68/push "F - test-get-or-insert-slice/6"/imm32
68/push 0x18/imm32
29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX
50/push-EAX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-ints-equal(table->write, 2 rows = 16, msg)
# . . push args
68/push "F - test-get-or-insert-slice/7"/imm32
68/push 0x10/imm32/two-rows
ff 6/subop/push 0/mod/indirect 1/rm32/ECX . . . . . . # push *ECX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# check-string-equal(*table->data+8, "data", msg)
# check-string-equal(*(table+20), "data", msg)
# . . push args
68/push "F - test-get-or-insert-slice/8"/imm32
68/push "data"/imm32
ff 6/subop/push 1/mod/*+disp8 1/rm32/ECX . . . . 0x14/disp8 . # push *(ECX+20)
# . . call
e8/call check-string-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-get-or-insert-slice:end:
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
# write an entire stream's contents to a buffered-file
# ways to do this:
# - construct a 'maximal slice' and pass it to write-slice-buffered
# - flush the buffered-file and pass the stream directly to its fd (disabling buffering)
# we'll go with the first way for now
write-stream-data: # f : (address buffered-file), s : (address stream) -> <void>
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# . save registers
50/push-EAX
51/push-ECX
56/push-ESI
# ESI = s
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 0xc/disp8 . # copy *(EBP+12) to ESI
# var slice/ECX = {s->data, s->data + s->write}
# . push s->data + s->write
8b/copy 0/mod/indirect 6/rm32/ESI . . . 0/r32/EAX . . # copy *ESI to EAX
8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/ESI 0/index/EAX . 0/r32/EAX 0xc/disp8 . # copy ESI+EAX+12 to EAX
50/push-EAX
# . push s->data
8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 0xc/disp8 . # copy ESI+12 to EAX
50/push-EAX
# . ECX = ESP
89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX
# write-slice-buffered(f, slice)
# . . push args
51/push-ECX
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8)
# . . call
e8/call write-slice-buffered/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
$write-stream-data:end:
# . restore locals
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . restore registers
5e/pop-to-ESI
59/pop-to-ECX
58/pop-to-EAX
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
test-write-stream-data:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# setup
# . clear-stream(_test-output-stream)
# . . push args
68/push _test-output-stream/imm32
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# . clear-stream(_test-output-buffered-file+4)
# . . push args
b8/copy-to-EAX _test-output-buffered-file/imm32
05/add-to-EAX 4/imm32
50/push-EAX
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# . clear-stream(_test-input-stream)
# . . push args
68/push _test-input-stream/imm32
# . . call
e8/call clear-stream/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# initialize input
# . write(_test-input-stream, "abcd")
# . . push args
68/push "abcd"/imm32
68/push _test-input-stream/imm32
# . . call
e8/call write/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# write-stream-data(_test-output-buffered-file, _test-input-stream)
# . . push args
68/push _test-input-stream/imm32
68/push _test-output-buffered-file/imm32
# . . call
e8/call write-stream-data/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# check that the write happened as expected
# . flush(_test-output-buffered-file)
# . . push args
68/push _test-output-buffered-file/imm32
# . . call
e8/call flush/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# . check-stream-equal(_test-output-stream, "abcd", msg)
# . . push args
68/push "F - test-write-stream-data"/imm32
68/push "abcd"/imm32
68/push _test-output-stream/imm32
# . . call
e8/call check-stream-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
has-metadata?: # word : (address slice), s : (address string) -> EAX : boolean
# pseudocode:
# var twig : &slice = next-token-from-slice(word->start, word->end, '/') # skip name
# curr = twig->end
# while true
# twig = next-token-from-slice(curr, word->end, '/')
# if (twig.empty()) break
# if (slice-equal?(twig, s)) return true
# curr = twig->end
# return false
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# . save registers
51/push-ECX
52/push-EDX
56/push-ESI
57/push-EDI
# ESI = word
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 . # copy *(EBP+8) to ESI
# EDX = word->end
8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 2/r32/EDX 4/disp8 . # copy *(ESI+4) to EDX
# var twig/EDI : (address slice) = {0, 0}
68/push 0/imm32/end
68/push 0/imm32/start
89/copy 3/mod/direct 7/rm32/EDI . . . 4/r32/ESP . . # copy ESP to EDI
# next-token-from-slice(word->start, word->end, '/', twig)
# . . push args
57/push-EDI
68/push 0x2f/imm32/slash
52/push-EDX
ff 6/subop/push 0/mod/indirect 6/rm32/ESI . . . . . . # push *ESI
# . . call
e8/call next-token-from-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP
# curr/ECX = twig->end
8b/copy 1/mod/*+disp8 7/rm32/EDI . . . 1/r32/ECX 4/disp8 . # copy *(EDI+4) to ECX
$has-metadata?:loop:
# next-token-from-slice(curr, word->end, '/', twig)
# . . push args
57/push-EDI
68/push 0x2f/imm32/slash
52/push-EDX
51/push-ECX
# . . call
e8/call next-token-from-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0x10/imm32 # add to ESP
# if (slice-empty?(twig)) return false
# . EAX = slice-empty?(twig)
# . . push args
57/push-EDI
# . . call
e8/call slice-empty?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# . if (EAX != 0) return false
3d/compare-EAX-and 0/imm32
75/jump-if-not-equal $has-metadata?:false/disp8
# if (slice-equal?(twig, s)) return true
# . EAX = slice-equal?(twig, s)
# . . push args
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12)
57/push-EDI
# . . call
e8/call slice-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . if (EAX != 0) return true
3d/compare-EAX-and 0/imm32
75/jump-if-not-equal $has-metadata?:true/disp8
# curr = twig->end
8b/copy 1/mod/*+disp8 7/rm32/EDI . . . 1/r32/ECX 4/disp8 . # copy *(EDI+4) to ECX
eb/jump $has-metadata?:loop/disp8
$has-metadata?:true:
b8/copy-to-EAX 1/imm32/true
eb/jump $has-metadata?:end/disp8
$has-metadata?:false:
b8/copy-to-EAX 0/imm32/false
$has-metadata?:end:
# . reclaim locals
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . restore registers
5f/pop-to-EDI
5e/pop-to-ESI
5a/pop-to-EDX
59/pop-to-ECX
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
test-has-metadata-true:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# (EAX..ECX) = "ab/imm32"
b8/copy-to-EAX "ab/imm32"/imm32
8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX
8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX
05/add-to-EAX 4/imm32
# var in/ESI : (address slice) = {EAX, ECX}
51/push-ECX
50/push-EAX
89/copy 3/mod/direct 6/rm32/ESI . . . 4/r32/ESP . . # copy ESP to ESI
# EAX = has-metadata?(ESI, "imm32")
# . . push args
68/push "imm32"/imm32
56/push-ESI
# . . call
e8/call has-metadata?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# check-ints-equal(EAX, 1, msg)
# . . push args
68/push "F - test-has-metadata-true"/imm32
68/push 1/imm32/true
50/push-EAX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
test-has-metadata-false:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# (EAX..ECX) = "ab/c"
b8/copy-to-EAX "ab/c"/imm32
8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX
8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX
05/add-to-EAX 4/imm32
# var in/ESI : (address slice) = {EAX, ECX}
51/push-ECX
50/push-EAX
89/copy 3/mod/direct 6/rm32/ESI . . . 4/r32/ESP . . # copy ESP to ESI
# EAX = has-metadata?(ESI, "d")
# . . push args
68/push "d"/imm32
56/push-ESI
# . . call
e8/call has-metadata?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# check-ints-equal(EAX, 0, msg)
# . . push args
68/push "F - test-has-metadata-false"/imm32
68/push 0/imm32/false
50/push-EAX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
test-has-metadata-ignore-name:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# (EAX..ECX) = "a/b"
b8/copy-to-EAX "a/b"/imm32
8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX
8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX
05/add-to-EAX 4/imm32
# var in/ESI : (address slice) = {EAX, ECX}
51/push-ECX
50/push-EAX
89/copy 3/mod/direct 6/rm32/ESI . . . 4/r32/ESP . . # copy ESP to ESI
# EAX = has-metadata?(ESI, "a")
# . . push args
68/push "a"/imm32
56/push-ESI
# . . call
e8/call has-metadata?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# check-ints-equal(EAX, 0, msg)
# . . push args
68/push "F - test-has-metadata-ignore-name"/imm32
68/push 0/imm32/false
50/push-EAX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
test-has-metadata-multiple-true:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# (EAX..ECX) = "a/b/c"
b8/copy-to-EAX "a/b/c"/imm32
8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX
8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX
05/add-to-EAX 4/imm32
# var in/ESI : (address slice) = {EAX, ECX}
51/push-ECX
50/push-EAX
89/copy 3/mod/direct 6/rm32/ESI . . . 4/r32/ESP . . # copy ESP to ESI
# EAX = has-metadata?(ESI, "c")
# . . push args
68/push "c"/imm32
56/push-ESI
# . . call
e8/call has-metadata?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# check-ints-equal(EAX, 1, msg)
# . . push args
68/push "F - test-has-metadata-multiple-true"/imm32
68/push 1/imm32/true
50/push-EAX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
test-has-metadata-multiple-false:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# (EAX..ECX) = "a/b/c"
b8/copy-to-EAX "a/b/c"/imm32
8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX
8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX
05/add-to-EAX 4/imm32
# var in/ESI : (address slice) = {EAX, ECX}
51/push-ECX
50/push-EAX
89/copy 3/mod/direct 6/rm32/ESI . . . 4/r32/ESP . . # copy ESP to ESI
# EAX = has-metadata?(ESI, "d")
# . . push args
68/push "d"/imm32
56/push-ESI
# . . call
e8/call has-metadata?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# check-ints-equal(EAX, 0, msg)
# . . push args
68/push "F - test-has-metadata-multiple-false"/imm32
68/push 0/imm32/false
50/push-EAX
# . . call
e8/call check-ints-equal/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
compute-width: # word : (address array byte)
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
# . save registers
51/push-ECX
# EAX = word
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 0/r32/EAX 8/disp8 . # copy *(EBP+8) to ECX
# ECX = word + word->length
8b/copy 0/mod/indirect 0/rm32/EAX . . . 1/r32/ECX . . # copy *EAX to ECX
8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/EAX 1/index/ECX . 1/r32/ECX 4/disp8 . # copy EAX+ECX+4 to ECX
# EAX = word->data
05/add-to-EAX 4/imm32
# var in/ECX : (address slice) = {EAX, ECX}
51/push-ECX
50/push-EAX
89/copy 3/mod/direct 1/rm32/ECX . . . 4/r32/ESP . . # copy ESP to ECX
# if has-metadata?(word, "imm32") or has-metadata?(word, "disp32"): return 4
# . has-metadata?(word, "imm32")
68/push "imm32"/imm32
51/push-ECX
e8/call has-metadata?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . if EAX: return 4
3d/compare-EAX-and 1/imm32
b8/copy-to-EAX 4/imm32 # ZF is set, so we can overwrite EAX now
74/jump-if-equal $compute-width:end/disp8
# . has-metadata?(word, "disp32")
68/push "disp32"/imm32
51/push-ECX
e8/call has-metadata?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . if EAX: return 4
3d/compare-EAX-and 1/imm32
b8/copy-to-EAX 4/imm32 # ZF is set, so we can overwrite EAX now
74/jump-if-equal $compute-width:end/disp8
# if has-metadata?(word, "imm16") or has-metadata?(word, "disp16"): return 2
# . has-metadata?(word, "imm16")
68/push "imm16"/imm32
51/push-ECX
e8/call has-metadata?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . if EAX: return 2
3d/compare-EAX-and 1/imm32
b8/copy-to-EAX 2/imm32 # ZF is set, so we can overwrite EAX now
74/jump-if-equal $compute-width:end/disp8
# . has-metadata?(word, "disp16")
68/push "disp16"/imm32
51/push-ECX
e8/call has-metadata?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . if EAX: return 2
3d/compare-EAX-and 1/imm32
b8/copy-to-EAX 2/imm32 # ZF is set, so we can overwrite EAX now
74/jump-if-equal $compute-width:end/disp8
# else: return 1
b8/copy-to-EAX 1/imm32
$compute-width:end:
# . restore registers
59/pop-to-ECX
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
test-compute-width:
# . prolog
55/push-EBP
89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP
$test-compute-width:imm8:
# EAX = compute-width("0x2/imm8")
68/push "0x2/imm8"/imm32
e8/call compute-width/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# check-ints-equal(EAX, 1, msg)
68/push "F - test-compute-width: 0x2/imm8"/imm32
50/push-EAX
68/push 1/imm32
e8/call check-ints-equal/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-compute-width:imm16:
# EAX = compute-width("4/imm16")
68/push "4/imm16"/imm32
e8/call compute-width/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# check-ints-equal(EAX, 2, msg)
68/push "F - test-compute-width: 4/imm16"/imm32
50/push-EAX
68/push 2/imm32
e8/call check-ints-equal/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-compute-width:imm32:
# EAX = compute-width("4/imm32")
68/push "4/imm32"/imm32
e8/call compute-width/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# check-ints-equal(EAX, 4, msg)
68/push "F - test-compute-width: 4/imm32"/imm32
50/push-EAX
68/push 4/imm32
e8/call check-ints-equal/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-compute-width:disp8:
# EAX = compute-width("foo/disp8")
68/push "foo/disp8"/imm32
e8/call compute-width/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# check-ints-equal(EAX, 1, msg)
68/push "F - test-compute-width: foo/disp8"/imm32
50/push-EAX
68/push 1/imm32
e8/call check-ints-equal/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-compute-width:disp16:
# EAX = compute-width("foo/disp16")
68/push "foo/disp16"/imm32
e8/call compute-width/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# check-ints-equal(EAX, 2, msg)
68/push "F - test-compute-width: foo/disp16"/imm32
50/push-EAX
68/push 2/imm32
e8/call check-ints-equal/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-compute-width:disp32:
# EAX = compute-width("foo/disp32")
68/push "foo/disp32"/imm32
e8/call compute-width/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# check-ints-equal(EAX, 4, msg)
68/push "F - test-compute-width: foo/disp32"/imm32
50/push-EAX
68/push 4/imm32
e8/call check-ints-equal/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
$test-compute-width:no-metadata:
# EAX = compute-width("45")
68/push "45"/imm32
e8/call compute-width/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32 # add to ESP
# check-ints-equal(EAX, 1, msg)
68/push "F - test-compute-width: 45 (no metadata)"/imm32
50/push-EAX
68/push 1/imm32
e8/call check-ints-equal/disp32
# . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
5d/pop-to-EBP
c3/return
== data
_test-input-stream:
# current write index
0/imm32
# current read index
0/imm32
# length
0x100/imm32 # 256 bytes
# data (16 lines x 16 bytes/line)
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# a test buffered file for _test-input-stream
_test-input-buffered-file:
# file descriptor or (address stream)
_test-input-stream/imm32
# current write index
0/imm32
# current read index
0/imm32
# length
6/imm32
# data
00 00 00 00 00 00 # 6 bytes
_test-output-stream:
# current write index
0/imm32
# current read index
0/imm32
# length
0x100/imm32 # 256 bytes
# data (16 lines x 16 bytes/line)
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# a test buffered file for _test-output-stream
_test-output-buffered-file:
# file descriptor or (address stream)
_test-output-stream/imm32
# current write index
0/imm32
# current read index
0/imm32
# length
6/imm32
# data
00 00 00 00 00 00 # 6 bytes
_test-code-segment:
63/c 6f/o 64/d 65/e
_test-code-segment-end:
_test-data-segment:
64/d 61/a 74/t 61/a
_test-data-segment-end:
# . . vim:nowrap:textwidth=0
|