This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstr.asm
116 lines (111 loc) · 1.96 KB
/
str.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
format ELF64
public string_to_number
public number_to_string
public length_string
section '.string_to_number' executable
; | input:
; rax = string
; | output:
; rax = number
string_to_number:
push rbx
push rcx
push rdx
xor rbx, rbx
xor rcx, rcx
.next_iter:
cmp [rax+rbx], byte 0
je .next_step
mov cl, [rax+rbx]
sub cl, '0'
push rcx
inc rbx
jmp .next_iter
.next_step:
mov rcx, 1
xor rax, rax
.to_number:
cmp rbx, 0
je .close
pop rdx
imul rdx, rcx
imul rcx, 10
add rax, rdx
dec rbx
jmp .to_number
.close:
pop rdx
pop rcx
pop rbx
ret
section '.number_to_string' executable
; | input:
; rax = number
; rbx = buffer
; rcx = buffer size
number_to_string:
push rax
push rbx
push rcx
push rdx
push rsi
mov rsi, rcx
dec rsi
xor rcx, rcx
.next_iter:
push rbx
mov rbx, 10
xor rdx, rdx
div rbx
pop rbx
add rdx, '0'
push rdx
inc rcx
cmp rax, 0
je .next_step
jmp .next_iter
.next_step:
mov rdx, rcx
xor rcx, rcx
.to_string:
cmp rcx, rsi
je .pop_iter
cmp rcx, rdx
je .null_char
pop rax
mov [rbx+rcx], rax
inc rcx
jmp .to_string
.pop_iter:
cmp rcx, rdx
je .close
pop rax
inc rcx
jmp .pop_iter
.null_char:
mov rsi, rdx
.close:
mov [rbx+rsi], byte 0
pop rsi
pop rdx
pop rcx
pop rbx
pop rax
ret
section '.length_string' executable
; | input
; rax = string
; | output
; rax = length
length_string:
push rbx
xor rbx, rbx
.next_iter:
cmp [rax+rbx], byte 0
je .close
inc rbx
jmp .next_iter
.close:
mov rax, rbx
pop rbx
ret