-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathp2asm.e
4224 lines (4136 loc) · 156 KB
/
p2asm.e
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
--
-- p2asm.e
-- Phix: included by plist.e, also used in demo/pGUI/filedump.exw
--
with trace
--DEV BUG: ; 189 mov qword[rsi+16],21
-- mov [rsi+16], dword 21 ;#00402BCC: 48:307106 10 15000000 vu 00 40 1 18
-- (Yes, the rhs /is/ a dword, but the target is a qword...)
--
-- Technical note/DEV
-- The appearance or not of qword/dword/word/byte is really appallingly coded. Note that
-- eg mov [reg],reg does /not/ want a size qualifier to appear anywhere. It really needs
-- shifting into a central routine (see qualifier() for the idea) with as many control
-- flags/parameters etc as needed, and callable umpteen times with tiny penalty.
-- As things stand, it tests and_bits(rex,#08)/dmode=64/showdword/islea/dormspacer, with
-- no real consistency or anything approaching any kind of clarity of design...
-- There is at least one particularly nasty ugly "yuk" (search for "6/9/14" below).
-- 20/08/2013 Added basic/ad-hoc 64-bit support. NOTE: this largely consisted of scanning
-- appendix B of AMD[3] and planting lots of ?9/0 in this code. Users familiar
-- with 64-bit code may be disappointed to hear there is absolutely no support
-- (as yet) for VEX and XOP (AMD only) prefixes, not that there was ever any
-- support for SIMD/SSE etc instructions in 32-bit mode.
-- 25/01/2014 Added a few random sse2 instructions, just enough for the fasm mandel demo.
-- (there is no equivalent code in pilasm.e as of yet)
--integer wasopcodewarning = 1 -- DEV (temp)
global integer terminal -- fatal error; cease decode
terminal = 0
integer machine -- should be set to 32 or 64 by decodeinit()
integer machine_arch -- should be set to arch_PE or arch_ELF ""
global constant arch_PE = 1,
arch_ELF = 2
--global atom lastpush -- for opCallOnce extra detail
-- 4/11/14:
--global atom lastmov -- for opFrame, opCallOnce extra detail
global atom lastmov = 0 -- for opFrame, opCallOnce extra detail
-- (used in plist.e/disassemble())
atom lastmoveax = 0
global integer wasOpCode
wasOpCode = 0
global integer csidx
integer dump_jump_table -- 0=off, -1=start, 1=rest
dump_jump_table = 0
integer jump_table_index
atom min_jt_addr -- for use with ""
global integer r_isdata = -1
global integer r_iscode = -1
--with trace
function isPlausibleJumpTable(atom addr)
--
-- We have just seen a jmp[reg*4+imm32]. Phix emits such, followed by a jump table (ie
-- an array of machine addresses in the middle of the code), and of course we need to
-- decide whether to print the following binary as a jump table or dissassemble it as
-- normal. While the only Phix hll statement to emit said instruction is a switch, it
-- could theoretically be used in #ilASM, or we could be dissassembling a non-Phix
-- program. Since eg "switch -1000,-1001,-1002" is perfectly valid, we cannot examine
-- the valid of imm32 and decide anything based on that. Instead we must examine the
-- following bytes (the jump table) in excruiating detail: they should be absolute
-- addresses between addr and end of the section and the thing ends when we (exactly)
-- hit the lowest address we met while scanning the table. There are no zeroes, since
-- a switch 1,3,5.. patches 2,4,6 to "end switch", nor are there any backward jumps
-- (as no branch straightening occurs on jump tables).
-- I am going to be a little lax here and treat values +/-ImageBase as in range. [MAYBE]
--
integer tidx = csidx
integer c
atom v
integer wordsize
min_jt_addr = -1
while 1 do
v = 0
wordsize = 4
-- if X64 then
if machine=64 then
wordsize = 8
end if
-- for i=1 to 4 do
for i=1 to wordsize do
if tidx>=length(code_section) then
return 0
end if
tidx+=1
c = and_bits(code_section[tidx],#FF)
v += c*power(256,i-1)
end for
-- addr += 4
addr += wordsize
-- erm...
-- if machine=64 and hasrm and mode=0 and rm=5 then
-- -- RIP addressing:
---- v += addr+ilen
-- v += addr
-- end if
-- if v<ImageBase+CSvaddr or v>=ImageBase+CSvaddr+CSvsize then
if newEmit then
if r_iscode=-1 then ?9/0 end if
if v=0 or call_func(r_iscode,{v})=0 then
return 0
end if
else
if v<addr or v>=ImageBase+CSvaddr+CSvsize then
return 0
end if
end if
if min_jt_addr=-1 then
min_jt_addr = v
elsif v<min_jt_addr then
min_jt_addr = v
end if
if min_jt_addr=addr
or min_jt_addr-ImageBase=addr then
exit
end if
end while
return 1
end function
--DEV should be moved out of this source:
--with trace
function getbyte()
if csidx>=length(code_section) then
terminal = 1
return 0
end if
csidx+=1
return and_bits(code_section[csidx],#FF)
end function
function nextopisnop()
integer csidx2 = csidx, byte
while csidx2<length(code_section) do
csidx2 += 1
byte = and_bits(code_section[csidx2],#FF)
if byte!=0o146 then
if byte=0o220 then return 1 end if
exit
end if
end while
return 0
end function
constant NP=1, U=2, V=3, UV=4, VU=5, unknown=6
constant pair = {"np","u ","v ","uv","vu","??"}
constant eaxbit = #01,
ecxbit = #02,
edxbit = #04,
ebxbit = #08,
-- espbit = #10,
-- ebpbit = #20,
esibit = #40,
edibit = #80,
regbit = {#01,#02,#04,#08,#10,#20,#40,#80,#100,#200,#400,#800,#1000,#2000,#4000,#8000},
-- eax,ecx,edx,ebx,esp,ebp,esi,edi
regbit8 ={#01,#02,#04,#08,#01,#02,#04,#08,#100,#200,#400,#800,#1000,#2000,#4000,#8000},
-- al, cl, dl, bl, ah, ch, dh, bh
regbita ={#48,#88,#60,#A0,#40,#80,#20,#08}
-- BX+SI,BX+DI,BP+SI,BP+DI,SI,DI,BP,BX
integer pairing, modified, referenced, clocks, agiset
integer hasdisp
constant hexfmt="%02x"
--constant insfmt="%02x"
constant insfmt="%03o"
sequence sop -- segment override prefix
object asm
sequence hex
integer c, ilen
constant ddaa={"daa","das","aaa","aas"},
sops={#26, #2E, #36, #3E, #64, #65},
sopt={"ES:","CS:","SS:","DS:","FS:","GS:"},
idpp={"inc","dec","push","pop"},
ppba={"pushad","popad","bound","arpl"}
integer dmode -- data (word size) mode
dmode = 32 -- default is 32-bit
-- (overidden for DOS stub and by #66 prefix)
integer amode -- addressing mode
amode = 32 -- default is 32-bit
-- (overidden for DOS stub and by #67 prefix)
integer rex = 0 -- rex prefix (64-bit mode) (WRXB=8421)
-- rW = and_bits(rex,#08) -- 64-bit operand size
-- rR = and_bits(rex,#04) -- msb extension to modRM reg field
-- rX = and_bits(rex,#02) -- msb extension to sib index field
-- rB = and_bits(rex,#01) -- msb extension to modRM r/m field
integer D,W
constant g0={"add","or","adc","sbb","and","sub","xor","cmp"}
constant o100 = #40, -- floor(c/o100) obtains first octal digit
o010 = #08, -- and_bits(floor(c/o010),o007) obtains second octal digit
o007 = #07 -- and_bits(c,o007) obtains third octal digit
integer hasrm
integer mode,reg,rm
procedure getrm()
c = getbyte()
mode = floor(c/o100) -- first octal digit (0..3)
reg = and_bits(floor(c/o010),o007) -- second octal digit (0..7)
rm = and_bits(c,o007) -- third octal digit (0..7)
if machine=64 then
if and_bits(rex,#04) then reg += 8 end if
if mode=3 or rm!=4 then
if and_bits(rex,#01) then rm += 8 end if
end if
end if
hex &= sprintf(insfmt,c)
ilen += 1
hasrm = 1
end procedure
constant segreg={"ES","CS","SS","DS"}
constant r8={"al","cl","dl","bl","ah","ch","dh","bh","r8l","r9l","r10l","r11l","r12l","r13l","r14l","r15l"},
--DEV r8b={"al","cl","dl","bl","spl","bpl","sil","dil","r8b","r9b","r10b","r11b","r12b","r13b","r14b","r15b"}, -- (if there is a rex prefix? (#40 will do))
-- (r8b..r15b are exactly the same as r8l..r15l)
r8l={"spl","bpl","sil","dil"},
r16={"ax","cx","dx","bx","sp","bp","si","di","r8w","r9w","r10w","r11w","r12w","r13w","r14w","r15w"},
r32={"eax","ecx","edx","ebx","esp","ebp","esi","edi","r8d","r9d","r10d","r11d","r12d","r13d","r14d","r15d"},
r64={"rax","rcx","rdx","rbx","rsp","rbp","rsi","rdi","r8","r9","r10","r11","r12","r13","r14","r15"},
a32={"BX+SI","BX+DI","BP+SI","BP+DI","SI","DI","BP","BX"}
function getr8(integer r)
sequence reg
if and_bits(rex,#40) and r>=4 and r<=7 then
reg = r8l[r-3]
referenced = or_bits(referenced,regbit[r+1])
else
reg = r8[r+1]
referenced = or_bits(referenced,regbit8[r+1])
end if
return reg
end function
--/*
procedure getrm()
c = getbyte()
mode = floor(c/o100) -- first octal digit (0..3)
reg = and_bits(floor(c/o010),o007) -- second octal digit (0..7)
rm = and_bits(c,o007) -- third octal digit (0..7)
if machine=64 then
if and_bits(rex,#04) then reg += 8 end if
if mode=3 or rm!=4 then
if and_bits(rex,#01) then rm += 8 end if
end if
end if
hex &= sprintf(insfmt,c)
ilen += 1
hasrm = 1
end procedure
--*/
procedure getreg()--integer use_r64=0)
--DEV set a flag that we need no size qualifier? (possibly too late anyway to do it here)
if W then
--10/10/14: (put back 3/11/14, over mov eax,[rsi] line 451 pMem.e)
if machine=64 and and_bits(rex,#08) then
-- if machine=64 and (use_r64 or and_bits(rex,#08)) then
-- if machine=64 then
-- elsif machine=64 and (dmode=64 or and_bits(rex,#08)) then -- (DEV try me!)
--6/11/16: (untried [reg|rm+=8 may|has already have been done in getrm()?])
--if and_bits(rex,#04) then
-- asm &= r64[reg+9]
--else
--end if
asm &= r64[reg+1]
elsif dmode=32 then
asm &= r32[reg+1]
else
asm &= r16[reg+1]
end if
referenced = or_bits(referenced,regbit[reg+1])
else
-- asm &= r8[reg+1]
-- referenced = or_bits(referenced,regbit8[reg+1])
asm &= getr8(reg)
end if
end procedure
global atom addr
addr=0
object v
integer useHexBase
useHexBase=0
-- moved to pglobals 12/8/14
--global sequence knownAddr = {}
--global sequence knownNames = {}
procedure getimm(integer size, integer spacer)
-- a spacer of '[' has the ']' added automatically (and appears in hex!)
integer c,c2
object sc2, si
integer tlsbase
--object dbg
v = 0
hex &= ' '
ilen += size
for i=1 to size do
c = getbyte()
hex &= sprintf(hexfmt,c)
v += c*power(256,i-1)
end for
if size=1 then
if v>#7F then
v -= #100
end if
elsif size=2 then
if v>#7FFF then
v -= #10000
end if
elsif size=4 then
if v>#7FFFFFFF then
v -= #100000000
end if
elsif size!=8 then
?9/0
end if
--DEV might not be good for jump table handling... (and dump_jump_table=0?)
--4/9/14: (erm...) [erm,... NO!]
if machine=64 and hasrm and mode=0 and rm=5 and find(spacer,"[j") then
-- if machine=64
-- and ((hasrm and mode=0 and rm=5) or (hasrm=0 and mode=3 and rm=4))
-- and find(spacer,"[j") then
-- RIP addressing:
v += addr+ilen
-- Technical note (15/1/14): Whenever a getimm() (or equally dorm()) is followed by another getimm(),
-- you need to manually adjust ilen. There were quite a few places (marked 15/1/14) where I did this.
-- (I have assumed that on non-64 a few ilen +/-k makes absolutely no difference.)
end if
if spacer='[' then
if length(sop) then
asm &= sop
sop = ""
end if
if size=4 then
--DEV 3/5/08:
-- if v and v>=irange[1] and v<=irange[2] then
-- asm &= getImport(v)
-- else
---- if cfa then trace(1) addref(v) end if
if v>#40000000 then
c = find(and_bits(v,#3FFFFFFF)*4-8,craddr) -- (convert ref to addr refcount)
else
c = find(v,craddr)
end if
if c then
c2 = cridx[c]
else
if newEmit then
--DEV
c = 0
c2 = 0
else
c = floor((v-(ImageBase+DSvaddr))/4)+1 -- calculate var idx
if c>0 and c<=length(vmap) then
c2 = vmap[c]
else
c2 = 0
end if
end if
end if
si = -1
-- if c>0 and c<=length(vmap) then
-- c2 = vmap[c]
if not newEmit and c2>0 and c2<=length(symtab) then
sc2 = symtab[c2]
if sequence(sc2) then
if sc2[S_NTyp]=S_Const
and sc2[S_vtype]=T_integer
and and_bits(sc2[S_State],K_lit) then
si = sprintf("Const %d",sc2[S_value])
else
si = sc2[S_Name]
if equal(si,-1) then -- Unnamed
if and_bits(sc2[S_State],K_Fres) -- Function result
and c2<length(symtab) then -- avoid any possible ioob
si = symtab[c2+1]
if sequence(si)
and find(si[S_NTyp],{S_Type,S_Func})
and not equal(si[S_Name],-1) then
si = "res:"&si[S_Name]
else
si = -1
end if
end if
if equal(si,-1) then
si = sprintf("symtab[%d]",c2)
end if
end if
end if
end if
-- 14/10/13: (for filedump)
else
c = find(v,knownAddr)
if c then
si = knownNames[c]
end if
end if
-- end if
--DEV:::
-- if not equal(si,-1) then
if not atom(si) then
--?{si,asm,v}
if dmode=16 then
asm &= "word"
end if
if length(asm)+length(si)<23 then
asm &= sprintf("[#%08x] (%s)",{v,si})
else
asm &= '['&si&']'
end if
else
if machine=64 then
if and_bits(rex,#08) or dmode=64 then
--yuk: (search for "6/9/14" below)
if length(asm)<5 or asm[-5..-1]!="qword" then
asm &= "qword"
end if
elsif W then
if dmode=128 then
asm &= "xword"
elsif dmode=32 then
asm &= "dword"
else
asm &= "word"
end if
else
asm &= "byte"
end if
end if
asm &= sprintf("[#%08x]",v)
end if
elsif size=2 then
asm &= sprintf("[#%04x]",v)
else
?9/0 -- should never happen
end if
elsif spacer='j' then
--20/8/2013:
-- if size = 2 then ?9/0 end if
if size = 2 then
asm &= sprintf(" #%04x",v)
else
if newEmit then
-- if wasopcodewarning then
-- printf(1,"\nwarning: wasOpCode setting not attempted (p2asm line 366, v=%08x)\n",{v})
-- wasopcodewarning= 0
-- end if
--trace(1)
--dbg = sprintf("%08x",v)
--DEV or should we just use knownAddr for everything?
v += addr+ilen
-- v += ilen
-- wasOpCode = 0
-- for i=1 to length(glblname) do
----dbg = {i,sprintf("%08x",glboffset[i]),sprintf("%08x",glboffset[i]),sprintf("%08x",symtab[glblabel[i]][S_il])}
---- if v=glboffset[i]+symtab[glblabel[i]][S_il] then
-- if v=glboffset[i]+symtab[glblabel[i]][S_il]+IB+BOC then
-- wasOpCode = i
-- exit
-- end if
-- end for
-- v += addr+ilen
-- v += addr
-- if wasOpCode then
-- if 0 then
-- asm &= sprintf(" #%08x (:%s)",{v,glblname[wasOpCode]})
-- wasOpCode = 0
--DEV: (we might want SOD and SOC to go with BOD and BOC)
-- elsif v>=ImageBase+CSvaddr and v<ImageBase+CSvaddr+CSvsize then
-- elsif 0 then
si = find(v,codeTable)
if si then
si = symtab[codeIdx[si]][S_Name]
if atom(si) then
si = sprintf("??%d??",si)
end if
if length(asm)+length(si)<20 then
asm &= sprintf(" #%08x (code:%s)",{v,si})
else
asm &= " code:"&si
end if
else
-- asm &= sprintf(" #%08x",v)
-- end if
-- else
-- 14/10/13: (for filedump)
si = find(v,knownAddr)
if si then
si = knownNames[si]
if length(asm)+length(si)<20 then
asm &= sprintf(" #%08x (%s)",{v,si})
--DEV
if si=":%opFrame" then
wasOpCode = opFrame
elsif si=":%opCallOnce" then
wasOpCode = opCallOnce
else
wasOpCode = 0
end if
else
asm &= " "&si
end if
else
asm &= sprintf(" #%08x",v)
end if
end if
else -- not newEmit
v += addr+ilen
wasOpCode = find(v,VMep)
if wasOpCode then
-- asm &= ' '&opNames[wasOpCode]
asm &= sprintf(" #%08x (%s)",{v,opNames[wasOpCode]})
--20/8/2013
-- elsif v>ImageBase+CSvaddr and v<ImageBase+DSvaddr then
-- elsif v>=ImageBase+CSvaddr and v<ImageBase+DSvaddr then
elsif v>=ImageBase+CSvaddr and v<ImageBase+CSvaddr+CSvsize then
si = find(v,codeTable)
if si then
si = symtab[codeIdx[si]][S_Name]
if length(asm)+length(si)<20 then
asm &= sprintf(" #%08x (code:%s)",{v,si})
else
asm &= " code:"&si
end if
else
asm &= sprintf(" #%08x",v)
end if
else
-- 14/10/13: (for filedump)
si = find(c,knownAddr)
if si then
si = knownNames[si]
if length(asm)+length(si)<20 then
asm &= sprintf(" #%08x (%s)",{v,si})
else
asm &= " "&si
end if
else
asm &= sprintf(" #%08x",v)
end if
end if
end if
-- if cfa then addref(v) end if
end if
elsif spacer='#' then
if size=1 then
asm &= sprintf("#%02x",v)
elsif size=2 then
asm &= sprintf("#%04x",v)
elsif size=8 then
asm &= sprintf("#%016x",v)
else
asm &= sprintf("#%08x",v)
end if
else
-- if cfa then addref(v) end if
if spacer='+' and v<0 then
asm &= sprintf("%d",v)
-- else
elsif spacer!='+' or v then -- 21/12 DEV put this back once char tests (routine manual()) squished....
if v>#40000000 then
c = find(and_bits(v,#3FFFFFFF)*4-8,craddr) -- (convert ref to addr refcount)
else
c = find(v,craddr)
end if
if size=8 then
c2 = 0
elsif c then
c2 = cridx[c]
else
if newEmit then
--DEV
c = 0
else
c = floor((v-(ImageBase+DSvaddr))/4)+1 -- calculate var idx
end if
if c>0 and c<=length(vmap) then
c2 = vmap[c]
else
c2 = 0
end if
end if
si = -2
-- if c>0 and c<=length(vmap) then
-- c2 = vmap[c]
if c2>0 and c2<=length(symtab) then
si = symtab[c2]
if si[S_NTyp]=S_Const
and si[S_vtype]=T_integer
and and_bits(si[S_State],K_lit) then
si = sprintf("Const %d",si[S_value])
else
-- si = symtab[c2][S_Name]
si = si[S_Name]
if equal(si,-1) then
if c2<length(symtab) then
si = symtab[c2+1]
if sequence(si) and find(si[S_NTyp],{S_Type,S_Func}) then
si = "res:"&si[S_Name]
else
si = -1
end if
end if
if equal(si,-1) then
si = sprintf("symtab[%d]",c2)
end if
end if
end if
-- end if
elsif newEmit then
si = -1 --DEV will knock out h4 etc... [solved, I think]
elsif v>=ImageBase+CSvaddr and v<ImageBase+CSvaddr+CSvsize then
si = find(v,codeTable)
if si then
si = "code:"&symtab[codeIdx[si]][S_Name]
else
si = -1
end if
end if
if not atom(si) then
if length(asm)+length(si)<22 then
asm &= sprintf("%s#%08x (%s)",{spacer,v,si})
else
asm &= spacer&si
end if
elsif newEmit=0 and (si=-1
or (v>=ImageBase+CSvaddr and v<ImageBase+CSvaddr+CSvsize)
or (r_isdata!=-1 and call_func(r_isdata,{v})) -- (for filedump)
or (useHexBase and size=4)) then -- for jump tables (added 3/4/2010) [and return addresses 22/2/2012]
asm &= sprintf("%s#%08x",{spacer,v})
elsif newEmit
and ((r_isdata!=-1 and call_func(r_isdata,{v})) or
(useHexBase and (size=4))) then -- for jump tables (added 3/4/2010) [and return addresses 22/2/2012]
asm &= sprintf("%s#%08x",{spacer,v})
elsif v=#40000000
-- or (X64=1 and v=#4000000000000000) then
or (machine=64 and v=#4000000000000000) then
asm &= spacer&"h4"
elsif v=-128 then
asm &= spacer&"#80"
elsif v=-126 then
asm &= spacer&"#82"
elsif v=18 then
asm &= spacer&"#12"
else
if newEmit then
-- if wasopcodewarning then
-- printf(1,"warning: wasOpCode setting not attempted (p2asm line 506, v=%08x)\n",{v})
---- wasopcodewarning = 0
-- end if
if v!=0 and r_iscode!=-1 and call_func(r_iscode,{v}) then
wasOpCode = 0
--trace(1)
--dbg = sprintf("%08x",v)
for i=1 to length(glblname) do
--dbg = {i,sprintf("%08x",glboffset[i]),sprintf("%08x",glboffset[i]),sprintf("%08x",symtab[glblabel[i]][S_il])}
-- tlsbase = symtab[glblabel[i]][S_il]
c2 = glblabel[i]
if c2!=0 then
tlsbase = symtab[c2][S_il]
if tlsbase!=0 and v=glboffset[i]+tlsbase then
wasOpCode = i
exit
end if
end if
end for
if wasOpCode then
asm &= sprintf("%s#%08x (:%s)",{spacer,v,glblname[wasOpCode]})
else
asm &= sprintf("%s#%08x",{spacer,v})
end if
wasOpCode = 0
else
asm &= sprintf("%s%d",{spacer,v})
end if
else
wasOpCode = find(v,VMep)
if wasOpCode and v!=0 then
-- asm &= spacer&opNames[wasOpCode]
asm &= sprintf("%s%08x (%s)",{spacer,v,opNames[wasOpCode]})
else
asm &= sprintf("%s%d",{spacer,v})
end if
end if
end if
end if
end if
--DEV added 26/02/2012 (untested)
-- useHexBase = 0
end procedure
integer scale,index,base
procedure getsib()
c = getbyte()
scale = floor(c/o100) -- first octal digit (0..3)
index = and_bits(floor(c/o010),o007) -- second octal digit (0..7)
base = and_bits(c,o007) -- third octal digit (0..7)
if machine=64 then
if and_bits(rex,#02) then index += 8 end if
if and_bits(rex,#01) then base += 8 end if --DEV and clear the bit? (see getrm)
end if
hex &= sprintf(insfmt,c)
ilen += 1
end procedure
integer wasRetAddr
integer islea = 0
integer dormspacer = 1
integer notphix = 0
global procedure setnotphix()
-- (for filedump.exw)
notphix = 1
end procedure
procedure dorm(integer showdword=0, integer use_r64=1)
-- cfa is 1 if parameter can be a control flow address (eg a jmp instruction!)
integer p
,wasp
object name
if dormspacer then
if not find(asm[length(asm)]," ,:") then asm &= ' ' end if
end if
if mode=3 then -- mmm is register
--DEV use getreg(), but using rm not reg...
if W then
-- 8/10/14:
-- if machine=64 and and_bits(rex,#08) then
-- 8/11/14 (over or ax,ax)
-- if machine=64 then
if dmode=16 then
asm &= r16[rm+1]
-- 23/11/14 (over and esi,-2) [and backed out, breask other stuff...]
-- elsif machine=64 then
-- elsif machine=64 and (dmode=64 or and_bits(rex,#08)) then
elsif machine=64 and (use_r64 or dmode=64 or rex!=0) then
asm &= r64[rm+1]
elsif dmode=32 then
--DEV (should never trigger)
if rm>7 then
printf(1,"p2asm line 761: check #%08x\n",addr)
asm &= r64[rm+1]
else
asm &= r32[rm+1]
end if
else
------DEV??
---- asm &= sop&r16[rm+1]
---- sop=""
----DEV
----if rm>7 then
---- printf(1,"p2asm line 772: check #%08x\n",addr)
---- asm &= r64[rm+1]
----else
-- asm &= r16[rm+1]
?9/0
----end if
end if
referenced = or_bits(referenced,regbit[rm+1])
else
--DEV
--if rm>7 then
-- printf(1,"p2asm line 783 **check #%08x\n",addr)
-- asm &= r64[rm+1]
--else
-- asm &= r8[rm+1]
-- referenced = or_bits(referenced,regbit8[rm+1])
asm &= getr8(rm)
--end if
end if
else
clocks += 1
-- agiset = 0 --done earlier
if length(sop) then
-- if length(asm)=0 or asm[length(asm)]=' ' then
-- if find(asm[length(asm)]," ,") then
asm &= sop
-- else
-- asm &= ' '&sop
-- end if
sop = ""
end if
if mode=0
and rm=5
and amode!=16 then
-- asm &= ','
getimm(4,'[')
-- asm &= ']'
hasdisp = 1
else
if dmode=128 and showdword then
asm &= "xword"
-- 15/12/14 (over result = result[1..tmp] in prntf/round())
-- elsif dmode=64 then
--14/2/17:
-- elsif dmode=64 and showdword then
elsif machine=64 and showdword then
asm &= "qword"
elsif dmode=32 and showdword then
-- 3/11/14: (?)
--5/1/15 (jump tables)
--if machine=64 and amode=16 then
if machine=64 and (amode=16 or useHexBase) then
asm &= "qword"
else
asm &= "dword"
end if
elsif dmode=16 then
asm &= "word"
end if
if rm=4 and amode!=16 then
getsib()
-- if index=4 and scale!=0 then
if index=4 then
if scale!=0 then
-- error() --DEV
-- asm &= "?? ERROR: illegal op (s>0 on i=4) ??"
asm = "undefined! (s>0 on i=4)"
terminal = 1
return
-- eg ; 00 B4 E0 00500000 <=!=> add [eax+esp*8+5000],dh
-- Fasm gives error: invalid address.
-- Ollydebug (1.08b) dissassembles as add[eax+5000],dh and like fasm
-- rejects attempts to assemble the above with "invalid indexing mode".
-- Update: Some forms could just be ignored, eg
-- CPU Disasm
-- Hex dump Octal Command
-- DB5C24 04 333 134 044 004 FISTP DWORD PTR [ESP+4]
-- DB5CE4 04 333 134 344 004 FISTP DWORD PTR [ESP+4]
-- 8D4424 04 215 104 044 004 LEA EAX,[ESP+4]
-- 8D4464 04 215 104 144 004 LEA EAX,[ESP+4]
-- 8D44E4 04 215 104 344 004 LEA EAX,[ESP+4]
-- However, pilx86 should only emit the "classical" form (xr4 04b).
end if
-- if machine=64 and and_bits(rex,#08) then
if machine=64 then
-- if dmode=32 and showdword then
if dmode=32 and not islea and dormspacer then
asm &= "qword"
end if
asm &= '['&r64[base+1]
else
asm &= '['&r32[base+1]
end if
agiset = or_bits(agiset,regbit[base+1])
-- end if
-- xr4 sib As follows:
--
--"sib" Encoding: (Scale*Index+Base)
-- 0r4 sib DS:[base + scale*index]
-- 1r4 sib disp8 DS:[base + scale*index + disp8]
-- 2r4 sib disp32 DS:[base + scale*index + disp32]
-- Exceptions:
---- 0r4 si5 disp32 DS:[scale*index + disp32]
-- r=4,5 SS: default for ESP, EBP
---- xr4 04b No Index (scale must be 0)
---- xr4 s4b (s>0) Undefined!
--
-- if mode=0 and base=5 then
elsif mode=0 and base=5 then
--DEV
if index>7 then
printf(1,"p2asm line 884: check #%08x\n",addr)
asm &= '['&r64[index+1]
--9/11/16: (over jump tables)
elsif machine=64 then
asm &= '['&r64[index+1]
else
asm &= '['&r32[index+1]
end if
agiset = or_bits(agiset,regbit[index+1])
else
--if index=4 then trace(1) end if
--DEV
--if base>7 or index>7 then
-- printf(1,"p2asm line 897: check #%08x\n",addr)
if machine=64 then
-- if not and_bits(rex,#08) then
-- printf(1,"p2asm line 900: check #%08x\n",addr)
-- end if
asm &= '['&r64[base+1]&'+'&r64[index+1]
else
asm &= '['&r32[base+1]&'+'&r32[index+1]
end if
agiset = or_bits(agiset,regbit[base+1])
agiset = or_bits(agiset,regbit[index+1])
end if
if scale=0 then
elsif scale=1 then
asm &= "*2"
elsif scale=2 then
asm &= "*4"
elsif scale=3 then
asm &= "*8"
end if
if mode=1 then -- 8-bit displacement
getimm(1,'+')
hasdisp=1
elsif mode=2 -- 32-bit displacement
or (mode=0 and base=5) then
getimm(4,'+')
hasdisp=1
end if
asm &= ']'
--DEV:
--asm&="ebp?"
else
-- if machine=64 and and_bits(rex,#08) then
-- 23/11/14 (over cmp dword[esi],imm32, pHeap.e line 2284) [and backed out, breaks other stuff...]
-- if machine=64 then
-- if machine=64 and (use_r64 or dmode=64 or and_bits(rex,#08)) then
if machine=64 and (use_r64 or dmode=64 or rex!=0) then
--if not and_bits(rex,#08) then
-- printf(1,"p2asm line 935: check #%08x\n",addr)
--end if
asm &= '['&r64[rm+1]
agiset = or_bits(agiset,regbit[rm+1])
elsif amode=32 then
--DEV
if rm>7 then
printf(1,"p2asm line 942: check #%08x\n",addr)
asm &= '['&r64[rm+1]
else
asm &= '['&r32[rm+1]
end if
agiset = or_bits(agiset,regbit[rm+1])
else
if mode=0 and rm=6 then
getimm(2,'[')
-- asm &= ']'
hasdisp=1 --DEV??
else
asm &= sop&'['&a32[rm+1]
sop=""
agiset = or_bits(agiset,regbita[rm+1])
end if
end if
if mode=1 then -- 8-bit displacement
getimm(1,'+')
hasdisp=1
elsif mode=2 then -- 32-bit displacement
--23/1/15 (over mov [ebp-160],ebx,diag()/base)
-- if amode=32 then
if amode=32
or amode=64 then
getimm(4,'+')
else
getimm(2,'+')
end if
hasdisp=1
end if
asm &= ']'
--DEV:
--asm &= "ebp2?"
if rm=5 then -- ebp
-- if v=16 then
--EXCEPT
-- if (X64=0 and v=16)
-- or (X64=1 and v=32) then
-- if (NEWRETSLOT=0 and ((X64=0 and v=16) or (X64=1 and v=32)))
-- or (NEWRETSLOT=1 and ((X64=0 and v=28) or (X64=1 and v=56))) then
-- if (X64=0 and v=28)
if (machine=32 and v=28)
-- or (X64=1 and v=56) then
or (machine=64 and v=56) then
--trace(1)
if notphix=0 then
asm &= " (retaddr)"
end if
wasRetAddr = 1
inFrame = 0
-- elsif v=20 then
-- elsif (X64=0 and v=20)
elsif (machine=32 and v=20)
-- or (X64=1 and v=40) then
or (machine=64 and v=40) then
asm &= " (prevebp)"