-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.s
324 lines (278 loc) · 8.17 KB
/
print.s
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
;------------------------------------------------
; printf() function implementation.
; Copywhat (C) 2022 Denis Dedkov
global cdecl_print
global print
;------------------------------------------------
; print stdcall to cdecl adapter
;------------------------------------------------
print:
pop rax ; Get return address
push r9 ; Push first stdcall arguments
push r8 ;
push rcx ;
push rdx ;
push rsi ;
push rdi ;
mov r10, rax ; Save return address
call cdecl_print
push r10
ret 6 * 8
;------------------------------------------------
;------------------------------------------------
;------------------------------------------------
; cdecl_print(fmt, ...)
;------------------------------------------------
; Loads the data from the given locations, converts
; them to character string equivalents and writes
; the results to the output stream 1.
;
; Calling convention: cdecl
;
; Parameters:
;
; fmt - pointer to a null-terminated multibyte string
; specifying how to interpret the data
;
; ... - arguments specifying data to print.
;
;
; Following conversion specifiers supported:
;
; +===+============================+
; | % | Explanation |
; +===+============================+
; | c | single character |
; +---+----------------------------+
; | s | character string |
; +---+----------------------------+
; | d | decimal representation |
; +---+----------------------------+
; | x | hexadecimal representation |
; +---+----------------------------+
; | o | octal representation |
; +---+----------------------------+
; | b | binary representation |
; +---+----------------------------+
;
; IMPORTANT! Internal buffer overflow
; is undefined behaviour!
;
%define write 0x1
%define stdout 0x1
%define arg(id) [rbp + 16 + 8 * id]
section .text
cdecl_print:
cld
push rbp
mov rbp, rsp
xor r8, r8 ; Initialize args counter.
mov rsi, arg(0) ; Get fmt string.
mov rdi, prbuf ; Set dest to printf buffer.
.copy:
lodsb ; Read character from fmt string
cmp al, '%' ; and check if it is a format keyword.
je .format ; Copy character otherwise.
stosb ;
test al, al ; Leave if the character copied was
jnz .copy ; a null terminator.
lea rdx, [rdi - 1] ; Calculate prbuf length being used.
sub rdx, prbuf ;
mov rsi, prbuf ; Call system 64bit write syscall.
mov rdi, stdout ;
mov rax, write ;
syscall ;
mov rax, r8 ; Return number of processed arguments
pop rbp
ret
.format:
xor rax, rax ; Get format specifier.
lodsb ; Make sure that it is not '%'.
cmp al, '%' ;
je .pct ;
lea rdx, [rax - 'b'] ; Normalize format to use jump table
cmp rdx, 'x' - 'b' ; later. Jump to default label in
ja .def ; case of overflow.
inc r8 ; Get next argument and call
mov rax, arg(r8) ; suitable function by jump table.
jmp [.jmptab + 8 * rdx] ;
;------------------------------------------------
; Jump table handlers:
; Note! rax - function parameter.
.pct:
.chr:
stosb
jmp .copy
.dec:
call itoa10
jmp .copy
.hex:
mov cl, 0x4 ; Shift: 16 = 2^4.
mov rbx, 0xf ; Mask: 00001111.
call itoa2x ;
jmp .copy
.oct:
mov cl, 0x3 ; Shift: 8 = 2^3.
mov rbx, 0x7 ; Mask: 00000111.
call itoa2x ;
jmp .copy
.bin:
mov cl, 0x1 ; Shift: 2 = 2^1.
mov rbx, 0x1 ; Mask: 00000001.
call itoa2x ;
jmp .copy
.str:
push rsi
mov rsi, rax
call strcpy
pop rsi
jmp .copy
.def:
mov rcx, 0xdead ; TODO: error handling.
jmp .copy ;
%define dup(from, to) times (to - from - 1)
section .data
.jmptab:
dq .bin
dq .chr
dq .dec
dup('d', 'o') dq .def
dq .oct
dup('o', 's') dq .def
dq .str
dup('s', 'x') dq .def
dq .hex
%undef dup
section .bss
prbuf resb 1024
section .text
%undef arg
%undef write
%undef stdout
;------------------------------------------------
;------------------------------------------------
;------------------------------------------------
; strcpy()
;------------------------------------------------
; Copies null-terminated string from one buffer
; to another.
;
; Expect: rsi - null terminated string copy from
; rdi - buffer copy to
;
; Return: rdi - address of the character
; following the symbol
;
; Destrs: rax rsi rdi
;
; Important! Null-terminator is not copied.
;------------------------------------------------
section .text
strcpy:
cld
xor rax, rax
.copy:
lodsb
test al, al
jz .stop
stosb
jmp .copy
.stop:
ret
;------------------------------------------------
;------------------------------------------------
;------------------------------------------------
; itoa10()
;------------------------------------------------
; Converts an integer value to a string using
; the base of 10.
;
; Expect: rax - integer to convert
; rdi - buffer to store string
;
; Return: rdi - address of the character
; following the last digit
;
; Destrs: rax rbx rdx rdi
;------------------------------------------------
section .text
itoa10:
cld
push rsi
test rax, rax ; Take into account the first
jns .init ; sign bit. Place '-' character
neg rax ; if value is negative and make
mov byte [rdi], '-' ; it positive.
inc rdi ;
.init:
mov rsi, rdi ; Save write initial position.
mov rbx, 10 ; Set base
.savedgt:
xor rdx, rdx ; Save remainder to the print
div rbx ; buffer.
add rdx, '0' ;
mov byte [rdi], dl ;
inc rdi ;
test rax, rax
jnz .savedgt
mov rbx, rdi ; Can't use rdi, due it is being
dec rbx ; returned.
.reverse:
mov al, byte [rsi] ; Reverse digits in memory.
xchg byte [rbx], al ;
mov byte [rsi], al ;
dec rbx ;
inc rsi ;
cmp rbx, rsi
ja .reverse
pop rsi
ret
;------------------------------------------------
;------------------------------------------------
;------------------------------------------------
; itoa2x()
;------------------------------------------------
; Converts an integer value to a string using
; the base of the power of 2.
;
; Expect: rax - integer to convert
; cl - base (power of 2)
; rbx - mask
; rdi - buffer to store string
;
; Return: rdi - address of the character
; following the last digit.
;
; Destrs: rax rbx rdx rdi
;------------------------------------------------
section .text
itoa2x:
cld
push rsi
mov rdx, rax ; Move to make similar to itoa10.
mov rsi, rdi ; Save write initial position.
.savedgt:
mov rax, rdx ; Save remainder to the print
and rax, rbx ; buffer.
mov al, byte [xlattab + rax] ;
shr rdx, cl ;
stosb ;
test rdx, rdx ; Leave when all digits are
jnz .savedgt ; saved.
mov rbx, rdi ; Can't use rdi, due it is being
dec rbx ; returned.
.reverse:
mov al, byte [rsi] ; Reverse digits in memory.
xchg byte [rbx], al ;
mov byte [rsi], al ;
dec rbx ;
inc rsi ;
cmp rbx, rsi
ja .reverse
pop rsi
ret
section .data
xlattab db '0123456789abcdef'
section .text
;------------------------------------------------
;------------------------------------------------