-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.inc
More file actions
310 lines (273 loc) · 3.58 KB
/
lib.inc
File metadata and controls
310 lines (273 loc) · 3.58 KB
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
; exit
; print_string
; print_char
; print_newline
; print_uint
; print_int
; read_char
; read_word
; parse_uint
; parse_int
; string_equals
; string_copy
SECTION .text
; exit func
; args:
; rdi - exit code
exit:
mov rax, 60 ; sys_exit
syscall
; string_length
; args:
; rdi - string pointer
; return:
; rax - size of string
; registers used: rax
string_length:
xor rax, rax
.loop:
cmp byte [rdi+rax], 0
je .break
inc rax
jmp .loop
.break:
ret
; print_string
; args:
; rdi - string pointer
; used: rdx, rax
print_string:
push rdx
push rax
call string_length
mov rsi, rdi
mov rdx, rax ; size_t
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
syscall
pop rax
pop rdx
ret
; print_char
; args:
; rdi - a caracter code
; used: rdi, rdx, rax
print_char:
push rsi
push rax
push rdx
push rdi
mov rsi, rsp ; char to *char
mov rax, 1 ; sys_write
mov rdx, 1 ; size_t, 1 byte
mov rdi, 1 ; stdout
syscall
pop rdi
pop rdx
pop rax
pop rsi
ret
; print_newline
; print 0xa="\n"
; used: rdi
print_newline:
push rdi
mov rdi, 0x0a
call print_char
pop rdi
ret
; print_uint
; rdi - decimal number
; used: rsi, rax, rcx, rdi
print_uint:
push rsi
push rax
push rcx
push rdi
xor rsi, rsi ; counter
mov rax, rdi
mov rcx, 10 ; divisor
.loop:
xor rdx, rdx
div rcx
push rdx
inc rsi
cmp rax, 0
jnz .loop
.reverse:
pop rax
dec rsi
add rax, 48
mov rdi, rax
call print_char
cmp rsi, 0
jnz .reverse
pop rdi
pop rcx
pop rax
pop rsi
ret
; print_int
; rdi - decimal number
; used: rdi
print_int:
test rdi, rdi
js .int
call print_uint
ret
.int:
push rdi
mov rdi, '-'
call print_char
pop rdi
neg rdi
call print_uint
ret
; read_char
; used: rax, rdi, rsi, rdx
; return char in rax
read_char:
push rdi
push rsi
push rdx
push 0
mov rax, 0
mov rdi, 0
mov rsi, rsp
mov rdx, 1
syscall
pop rax
pop rdx
pop rsi
pop rdi
ret
; read_word
; rdi - buffer address
; rsi - size buffer
; used: rcx, rdx, rax
; return rax
; 0 - word > size buffer
; address - sucess
read_word:
xor rdx, rdx
mov rcx, rsi
.loop:
push rcx
call read_char
pop rcx
cmp al, 0x20
je .loop
cmp al, 0x9
je .loop
cmp al, 0x10
je .loop
cmp al, 0x0a
je .finsh
inc rdx
cmp rdx, rcx
ja .cnl
mov BYTE [rdi+rdx-1], al
jmp .loop
.finsh:
mov rax, rdi
ret
.cnl:
xor rax, rax
ret
; parse_uint
; rdi - str pointer
parse_uint:
push rcx
xor rcx, rcx ; counter
xor rbx, rbx ;
mov rax, 10
.loop:
mov sil, [rdi+rcx]
cmp sil, 48
jb .end
cmp sil, 57
ja .end
inc rcx
push rax
sub rsi, 48
mul rbx
add rax, rsi
mov rbx, rax
pop rax
jmp .loop
.end:
mov rax, rbx
mov rdx, rcx
pop rcx
ret
; parse_int
; rdi - str pointer
parse_int:
mov al, [rdi]
cmp al, '-'
je .sig
jmp parse_uint
.sig:
inc rdi
call parse_uint
neg rax
inc rdx
ret
; string_equals
; rdi - str1
; rsi - str2
; used: rdx, rax, rbx, rcx
; return rax
string_equals:
push rdx
push rbx
push rcx
xor rdx, rdx
mov rax, rdi
call .sum
mov rbx, rdx
xor rdx, rdx
mov rax, rsi
call .sum
mov rcx, rdx
cmp rbx, rcx
pop rcx
pop rbx
pop rdx
je .r1
mov rax, 0
ret
.r1:
mov rax, 1
ret
.sum:
mov cl, [rax]
add rdx, rcx
cmp cl, 0
jz .end
inc rax
jmp .sum
.end:
ret
; string_copy
; rdi - string pointer
; rsi - buffer src
; rdx - size buffer src
string_copy:
push rcx
call string_length
cmp rax, rdx
xor rax, rax
ja .end
mov rax, rsi
xor rcx, rcx ; counter
.loop:
mov bl, [rdi+rcx]
cmp bl, 0
je .end
mov BYTE [rsi+rcx], bl
inc rcx
jmp .loop
.end:
pop rcx
ret