forked from redcode/SpecEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceRipper.asm
322 lines (267 loc) · 17.4 KB
/
SourceRipper.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
.data?
DissLabelLookup BYTE ?
DissShowBlankLines BYTE ?
DissShowHiddenLabels BYTE ?
DissShowLowercase BYTE ?
DissShowAsDEFB BYTE ?
.code
SOURCERIPPERALLOCSIZE equ 1000000
SourceRipperDlgProc proc uses ebx esi edi,
hWndDlg :DWORD,
uMsg :DWORD,
wParam :DWORD,
lParam :DWORD
local DissEdithWnd: DWORD,
DissStartAddr: DWORD,
DissEndAddr: DWORD,
lpMemory: DWORD,
lpMemEnd: DWORD
local SaveZ80PC: WORD,
LastZ80PC: WORD,
wParamLow: WORD,
wParamHigh: WORD
local Buffer1[33]: BYTE
local exitflag: BYTE,
bytecount: BYTE
local textstring: TEXTSTRING,
pTEXTSTRING: DWORD
invoke HandleCustomWindowMessages, ADDR SourceRipperDLG, hWndDlg, uMsg, wParam, lParam
.if eax == TRUE
return TRUE
.endif
RESETMSG
OnInitDialog
; set activate checkbox ID on main debugger window
mov SourceRipperDLG.Menu_ID, IDM_VIEW_SOURCECODE_RIPPER
mov DissEdithWnd, $fnc (GetDlgItem, hWndDlg, IDC_VIEWDISASSEMBLYEDT)
SETNEWWINDOWFONT DissEdithWnd, Courier_New_9, DissEditFont, DissEditOldFont
movzx eax, DissShowBlankLines
invoke CheckDlgButton, hWndDlg, IDC_DISSSHOWBLANKLINESCHK, eax
movzx eax, DissShowHiddenLabels
invoke CheckDlgButton, hWndDlg, IDC_DISSSHOWHIDDENLABELSCHK, eax
movzx eax, DissShowLowercase
invoke CheckDlgButton, hWndDlg, IDC_DISSLOWERCASECHK, eax
return TRUE
OnClose
return TRUE
OnDestroy
SETOLDWINDOWFONT DissEdithWnd, DissEditFont, DissEditOldFont
return NULL
OnCommand
mov eax, wParam
mov wParamLow, ax
shr eax, 16
mov wParamHigh, ax
.if wParamHigh == BN_CLICKED
.if wParamLow == IDC_DISSSHOWBLANKLINESCHK
invoke IsDlgButtonChecked, hWndDlg, IDC_DISSSHOWBLANKLINESCHK
mov DissShowBlankLines, al
return TRUE
.elseif wParamLow == IDC_DISSSHOWHIDDENLABELSCHK
invoke IsDlgButtonChecked, hWndDlg, IDC_DISSSHOWHIDDENLABELSCHK
mov DissShowHiddenLabels, al
return TRUE
.elseif wParamLow == IDC_DISSLOWERCASECHK
invoke IsDlgButtonChecked, hWndDlg, IDC_DISSLOWERCASECHK
mov DissShowLowercase, al
return TRUE
.elseif wParamLow == IDC_DISSSHOWASDEFBCHK
invoke IsDlgButtonChecked, hWndDlg, IDC_DISSSHOWASDEFBCHK
mov DissShowAsDEFB, al
return TRUE
.elseif wParamLow == IDC_SETSTARTASCURRADDRBTN
invoke INITTEXTSTRING, addr textstring, addr pTEXTSTRING
invoke PrtBase16, pTEXTSTRING, Z80PC, 0
invoke SendDlgItemMessage, hWndDlg, IDC_DISSSTARTEDT, WM_SETTEXT, 0, addr textstring
return TRUE
.elseif wParamLow == IDC_SETENDASCURRADDRBTN
invoke INITTEXTSTRING, addr textstring, addr pTEXTSTRING
invoke PrtBase16, pTEXTSTRING, Z80PC, 0
invoke SendDlgItemMessage, hWndDlg, IDC_DISSENDEDT, WM_SETTEXT, 0, addr textstring
return TRUE
.elseif wParamLow == IDC_DISSGENERATEBTN
invoke GetDlgItemText, hWndDlg, IDC_DISSSTARTEDT, addr Buffer1, sizeof Buffer1
invoke StringToDWord, addr Buffer1, addr lpTranslated
mov DissStartAddr, eax
.if (lpTranslated == FALSE) || (DissStartAddr > 65535)
szText DissAllowedRange, "Address Range = (0-65535)"
invoke ShowMessageBox, hWndDlg, ADDR DissAllowedRange, ADDR szWindowName, MB_OK
return TRUE
.endif
invoke GetDlgItemText, hWndDlg, IDC_DISSENDEDT, addr Buffer1, sizeof Buffer1
invoke StringToDWord, addr Buffer1, addr lpTranslated
mov DissEndAddr, eax
.if (lpTranslated == FALSE) || (DissEndAddr > 65535)
invoke ShowMessageBox, hWndDlg, ADDR DissAllowedRange, ADDR szWindowName, MB_OK
return TRUE
.endif
mov eax, DissStartAddr
.if eax > DissEndAddr
invoke ShowMessageBox, hWndDlg, SADD("Start address is greater than End address!!"), ADDR szWindowName, MB_OK
return TRUE
.endif
mov lpMemory, AllocMem (SOURCERIPPERALLOCSIZE)
.if eax == NULL
invoke ShowMessageBox, hWndDlg, SADD("Insufficient Memory Available"), ADDR szWindowName, MB_OK
return TRUE
.endif
add eax, SOURCERIPPERALLOCSIZE
mov lpMemEnd, eax
mov Highlight16bit, FALSE
mov ax, Z80PC
mov SaveZ80PC, ax
; mark all mem-mapped labels as not in use
lea edi, LabelTable
mov ecx, 65536/4
xor eax, eax
rep stosd
invoke INITTEXTSTRING, addr textstring, addr pTEXTSTRING
mov eax, lpMemory
mov byte ptr [eax], 0
invoke SETTEXTPTR, pTEXTSTRING, eax ; point the text pointer to our allocated buffer
; showing as DEFB output?
.if DissShowAsDEFB == TRUE
mov exitflag, FALSE
mov ebx, DissStartAddr ; ebx = start address
.while exitflag == FALSE
; check available memory in output buffer
invoke GETTEXTPTR, pTEXTSTRING
add eax, 80
.if eax > lpMemEnd
invoke GlobalFree, lpMemory
invoke ShowMessageBox, hWndDlg, SADD ("Insufficient Memory Available"), addr szWindowName, MB_OK
return TRUE
.endif
ADDCHAR pTEXTSTRING, 9
ADDDIRECTTEXTSTRING pTEXTSTRING, "DEFB "
mov bytecount, 4
.while bytecount > 0
call MemGetByte
inc ebx
invoke OutBase8, pTEXTSTRING, al
.if ebx > DissEndAddr
mov exitflag, TRUE
.break
.endif
.if bytecount > 1
ADDCHAR pTEXTSTRING, ",", " "
.endif
dec bytecount
.endw
ADDCHAR pTEXTSTRING, 13, 10
.endw
ADDCHAR pTEXTSTRING, 13, 10
jmp @Diss_To_Lowercase ; exit
.endif
; showing as disassembled output
; start Pass 1
mov eax, DissStartAddr
mov Z80PC, ax
lea edi, LabelTable
.while TRUE
mov ax, Z80PC
mov LastZ80PC, ax
invoke DisassembleLine, pTEXTSTRING
; check available memory in output buffer
invoke GETTEXTPTR, pTEXTSTRING
add eax, 80
.if eax > lpMemEnd
invoke GlobalFree, lpMemory
invoke ShowMessageBox, hWndDlg, SADD("Insufficient Memory Available"), ADDR szWindowName, MB_OK
return TRUE
.endif
.if FwdAddrValid == TRUE
movzx eax, FwdAddr
.if (eax >= DissStartAddr) && (eax <= DissEndAddr)
mov byte ptr [edi+eax], 1
.endif
.endif
mov ax, Z80PC
.break .if ax < LastZ80PC ; break if PC wrapped
mov eax, DissEndAddr
.break .if Z80PC > ax ; break if past range end
.endw
; start Pass 2
mov eax, DissStartAddr
mov Z80PC, ax
mov eax, lpMemory
mov byte ptr [eax], 0
invoke SETTEXTPTR, pTEXTSTRING, eax ; point the text pointer to our allocated buffer
lea edi, LabelTable
.while TRUE
mov DissLabelLookup, TRUE
movzx eax, Z80PC
mov LastZ80PC, ax
.if byte ptr [edi+eax] == 1
invoke OutBase16, addr textstring, ax, 0
ADDCHAR pTEXTSTRING, ":"
.endif
ADDCHAR pTEXTSTRING, 9
mov WantBlankLine, FALSE
invoke DisassembleLine, addr textstring
; check available memory in output buffer
invoke GETTEXTPTR, pTEXTSTRING
add eax, 80
.if eax > lpMemEnd
invoke GlobalFree, lpMemory
invoke ShowMessageBox, hWndDlg, SADD("Insufficient Memory Available"), ADDR szWindowName, MB_OK
mov DissLabelLookup, FALSE
return TRUE
.endif
mov FwdAddrRST, FALSE
ADDCHAR pTEXTSTRING, 13
ADDCHAR pTEXTSTRING, 10
.if (DissShowBlankLines == TRUE) && (WantBlankLine == TRUE)
ADDCHAR pTEXTSTRING, 13
ADDCHAR pTEXTSTRING, 10
.endif
.if DissShowHiddenLabels == TRUE
movzx eax, LastZ80PC
inc eax
movzx ebx, Z80PC
.while eax < ebx
.if byte ptr [edi+eax] == 1
mov DissLabelLookup, TRUE
invoke OutBase16, pTEXTSTRING, ax, 0
ADDCHAR pTEXTSTRING, 9
mov DissLabelLookup, FALSE
ADDDIRECTTEXTSTRING pTEXTSTRING, "EQU *-"
mov ecx, ebx
sub ecx, eax
invoke OutBase16, pTEXTSTRING, cx, 0
ADDCHAR pTEXTSTRING, 13
ADDCHAR pTEXTSTRING, 10
.endif
inc eax
.endw
.endif
mov ax, Z80PC
.break .if ax < LastZ80PC ; break if PC wrapped
mov eax, DissEndAddr
.break .if Z80PC > ax ; break if past range end
.endw
mov DissLabelLookup, FALSE
@Diss_To_Lowercase:
.if DissShowLowercase == TRUE
mov eax, lpMemory ; initial text string pointer
.while byte ptr [eax] != 0
mov bl, [eax]
.if (bl >= "A") && (bl <= "Z")
add bl, 32
mov [eax], bl
.endif
inc eax
.endw
.endif
invoke SetDlgItemText, hWndDlg, IDC_VIEWDISASSEMBLYEDT, lpMemory
mov ax, SaveZ80PC
mov Z80PC, ax
invoke GlobalFree, lpMemory
return TRUE
.endif
.endif
return TRUE
OnDefault
return FALSE
DOMSG
SourceRipperDlgProc endp