-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.inc
250 lines (186 loc) · 2.66 KB
/
strings.inc
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
BITS 64
section .data
LINE_END db 0xa, 0x0
section .text
; rsi -- pointer to string
_print_string:
push rax
push rcx
push rdx
push rdi
call _strlen
mov rdx, rax ; String length
mov rax, 1 ; Syscall number
mov rdi, 1 ; fb number (output)
; rsi -- string address
syscall
pop rdi
pop rdx
pop rcx
pop rax
ret
; rsi -- pointer to string
_print_string_ln:
push rsi
call _print_string
mov rsi, LINE_END
call _print_string
pop rsi
ret
; rsi -- pointer to string
; out rax -- length
_strlen:
push rdi
push rcx
mov rdi, rsi
xor rax, rax
xor rcx, rcx
not rcx
repne scasb
not rcx
dec rcx
mov rax, rcx
pop rcx
pop rdi
ret
; rsi -- first string
; rdi -- second string
; out rax -- result
_strcmp:
push rsi
push rdi
push rcx
call _strlen
xchg rcx, rax
repe cmpsb
xor rax, rax
setnz al
pop rcx
pop rdi
pop rsi
ret
; rax -- number
; rsi -- output string buffer
_hex:
push rsi
push rdi
push rdx
.loop:
mov dl, al
and dl, 0xF
cmp dl, 0xa
jb _hex.digit
sub dl, 0xa
add dl, 'a'
jmp _hex.print
.digit:
add dl, '0'
.print:
mov [rsi], dl
inc rsi
shr rax, 4
test rax, rax
jnz _hex.loop
mov byte [rsi], 'x'
mov byte [rsi + 1], '0'
mov byte [rsi + 2], 0x0
pop rdx
pop rdi
pop rsi
call _reverse
ret
; rax -- number
; rsi -- output string buffer
_itoa:
push rax
push rcx
push rdx
push rsi
push rdi
mov rcx, 10
mov rdi, rax
cqo
xor rax, rdx
sub rax, rdx
.loop:
cqo
idiv rcx
add dl, '0'
mov byte [rsi], dl
inc rsi
cmp rax, 0
jnz _itoa.loop
cmp rdi, 0
jge _itoa.skip_sign
mov byte [rsi], '-'
inc rsi
.skip_sign:
mov byte [rsi], 0x0
pop rdi
pop rsi
pop rdx
pop rcx
pop rax
call _reverse
ret
; rsi -- pointer to string
; rax -- resulted number
; rcx -- error
_atoi:
push rdx
push rsi
push rdi
mov rdi, 1
cmp byte [rsi], '-'
jnz _atoi.skip_sign
neg rdi
inc rsi
.skip_sign:
xor rax, rax
xor rcx, rcx
.loop:
mov cl, byte [rsi]
cmp cl, '0'
jb _atoi.error
cmp cl, '9'
ja _atoi.error
sub cl, '0'
mov rdx, 10
mul rdx
add rax, rcx
inc rsi
cmp byte [rsi], 0x0
jnz _atoi.loop
cqo
imul rdi
xor rcx, rcx
.exit:
pop rdi
pop rsi
pop rdx
ret
.error:
mov rcx, -1
jmp _atoi.exit
; rsi -- pointer to string
_reverse:
push rax
push rdx
push rdi
push rsi
call _strlen
lea rdi, [rsi + rax - 1]
.loop:
mov al, [rsi]
mov dl, [rdi]
mov [rsi], dl
mov [rdi], al
inc rsi
dec rdi
cmp rsi, rdi
jbe _reverse.loop
pop rsi
pop rdi
pop rdx
pop rax
ret