-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdebug.asm
278 lines (215 loc) · 3.99 KB
/
debug.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
extern printf
section .data
fmtd: db "%d", 0xA, 0x0
fmtzd: db "%zd", 0xA, 0x0
fmtu: db "%u", 0xA, 0x0
fmtzu: db "%zu", 0xA, 0x0
fmtx: db "%X", 0xA, 0x0
fmtzx: db "%zX", 0xA, 0x0
fmtf: db "%f", 0xA, 0x0
fmtc: db "%c", 0xA, 0x0
fmts: db "%s", 0xA, 0x0
%macro save_registers 0
; Preserve caller-save registers
push rax
push rcx
push rdx
push rsi
push rdi
push r8
push r9
push r10
push r11
; Preserve xmm registers
sub rsp, 128
movsd [rsp], xmm0
movsd [rsp + 8], xmm1
movsd [rsp + 16], xmm2
movsd [rsp + 24], xmm3
movsd [rsp + 32], xmm4
movsd [rsp + 40], xmm5
movsd [rsp + 48], xmm6
movsd [rsp + 56], xmm7
movsd [rsp + 64], xmm8
movsd [rsp + 72], xmm9
movsd [rsp + 80], xmm10
movsd [rsp + 88], xmm11
movsd [rsp + 96], xmm12
movsd [rsp + 104], xmm13
movsd [rsp + 112], xmm14
movsd [rsp + 120], xmm15
%endmacro
%macro restore_registers 0
; Restore xmm registers
movsd xmm0, [rsp]
movsd xmm1, [rsp + 8]
movsd xmm2, [rsp + 16]
movsd xmm3, [rsp + 24]
movsd xmm4, [rsp + 32]
movsd xmm5, [rsp + 40]
movsd xmm6, [rsp + 48]
movsd xmm7, [rsp + 56]
movsd xmm8, [rsp + 64]
movsd xmm9, [rsp + 72]
movsd xmm10, [rsp + 80]
movsd xmm11, [rsp + 88]
movsd xmm12, [rsp + 96]
movsd xmm13, [rsp + 104]
movsd xmm14, [rsp + 112]
movsd xmm15, [rsp + 120]
add rsp, 128
; Restore caller-save registers
pop r11
pop r10
pop r9
pop r8
pop rdi
pop rsi
pop rdx
pop rcx
pop rax
%endmacro
%macro save_sp 0
push rbp
mov rbp, rsp
%endmacro
%macro restore_sp 0
mov rsp, rbp
pop rbp
%endmacro
%macro align_sp 0
and rsp, -16
%endmacro
%macro debugd8 1
save_registers
movsx rsi, %1
lea rdi, [rel fmtd]
xor eax, eax
save_sp
align_sp
call printf wrt ..plt
restore_sp
restore_registers
%endmacro
%macro debugd16 1
debugd8 %1
%endmacro
%macro debugd32 1
debugd8 %1
%endmacro
%macro debugd64 1
save_registers
mov rsi, %1
lea rdi, [rel fmtzd]
xor eax, eax
save_sp
align_sp
call printf wrt ..plt
restore_sp
restore_registers
%endmacro
%macro debugu8 1
save_registers
movzx esi, %1
lea rdi, [rel fmtu]
xor eax, eax
save_sp
align_sp
call printf wrt ..plt
restore_sp
restore_registers
%endmacro
%macro debugu16 1
debugu8 %1
%endmacro
%macro debugu32 1
save_registers
mov esi, %1
lea rdi, [rel fmtu]
xor eax, eax
save_sp
align_sp
call printf wrt ..plt
restore_sp
restore_registers
%endmacro
%macro debugu64 1
save_registers
mov rsi, %1
lea rdi, [rel fmtzu]
xor eax, eax
save_sp
align_sp
call printf wrt ..plt
restore_sp
restore_registers
%endmacro
%macro debugx8 1
save_registers
movzx esi, %1
lea rdi, [rel fmtx]
xor eax, eax
save_sp
align_sp
call printf wrt ..plt
restore_sp
restore_registers
%endmacro
%macro debugx16 1
debugx8 %1
%endmacro
%macro debugx32 1
save_registers
mov esi, %1
lea rdi, [rel fmtx]
xor eax, eax
save_sp
align_sp
call printf wrt ..plt
restore_sp
restore_registers
%endmacro
%macro debugx64 1
save_registers
mov rsi, %1
lea rdi, [rel fmtzx]
xor eax, eax
save_sp
align_sp
call printf wrt ..plt
restore_sp
restore_registers
%endmacro
%macro debugf 1
save_registers
cvtss2sd xmm0, %1
lea rdi, [rel fmtf]
mov eax, 1
save_sp
align_sp
call printf wrt ..plt
restore_sp
restore_registers
%endmacro
%macro debugc 1
save_registers
movzx esi, %1
lea rdi, [rel fmtc]
xor eax, eax
save_sp
align_sp
call printf wrt ..plt
restore_sp
restore_registers
%endmacro
%macro debugs 1
save_registers
mov rsi, %1
lea rdi, [rel fmts]
xor eax, eax
save_sp
align_sp
call printf wrt ..plt
restore_sp
restore_registers
%endmacro