-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings_lib_elf64.nasm
More file actions
173 lines (150 loc) · 2.25 KB
/
strings_lib_elf64.nasm
File metadata and controls
173 lines (150 loc) · 2.25 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
BITS 64
global _start
section .text
; -------------------------------------------------
_strcpy:
mov r8, rdi ; str1
mov r9, rsi ; buffer
mov r10, rdx ; number start
xor r11, r11
call _strcpy_loop
ret
_strcpy_loop:
mov byte r12b, [r8+r11]
cmp r12b, 0
je _strcpy_end
mov byte [r9+r10], r12b
inc r11
inc r10
jmp _strcpy_loop
_strcpy_end:
ret
; -------------------------------------------------
_strlen:
mov r8, rdi ; str
xor r11, r11
call _strlen_loop
ret
_strlen_loop:
mov byte r12b, [r8+r11]
cmp r12b, 0
je _strlen_end
inc r11
jmp _strlen_loop
_strlen_end:
ret
; -------------------------------------------------
_strcat:
push rdi ; str1
push rsi ; str2
push rdx ; buffer
call _strlen
pop rsi
pop rdi
mov rdx, r11
push rsi
call _strcpy
pop rsi
pop rdi
xor rdx, rdx
call _strcpy
ret
; -------------------------------------------------
_charcat:
; rdi str1
; rsi char1
; rdx buffer
push rdx
push rsi
push rdi
call _strlen
mov byte [rdx+r11], sil
mov rsi, rdx
xor rdx, rdx
call _strcpy
pop rdi
pop rsi
pop rdx
ret
; -------------------------------------------------
_strreverse:
push rdi ; str
push rsi ; buffer
mov r8, rdi
lea r9, buffer2 ; buffer tool
call _strlen
xor r12, r12
call _strreverse_loop
mov rdi, r9
xor rdx, rdx
call _strcpy
pop rsi
pop rdi
ret
_strreverse_loop:
mov byte r10b, [r8+r12]
cmp r11, 0
jle _strreverse_end
dec r11
mov byte [r9+r11], r10b
inc r12
jmp _strreverse_loop
_strreverse_end:
ret
; -------------------------------------------------
_int2str:
mov r8, rdi ; int
mov r9, rsi ; buffer
push rdi
push rsi
xor r10, r10
mov r11, 10
call _int2str_loop
mov rdi, rsi
call _strreverse
pop rsi
pop rdi
ret
_int2str_loop:
cmp r8, 0
jle _int2str_end
xor rdx, rdx
mov rax, r8
div r11
mov r12, rdx
add r12b, 0x30
mov byte [r9+r10], r12b
mov r8, rax
inc r10
jmp _int2str_loop
_int2str_end:
mov byte [r9+r10], 0
ret
; -------------------------------------------------
_start:
mov rdi, 4005
lea rsi, buffer
call _int2str
lea rdi, buffer
mov rsi, 0xA
lea rdx, buffer
call _charcat
call _printbuff
jmp _exit
_printbuff:
mov rax, 1
mov rdi, 1
mov rsi, buffer
mov rdx, r11
syscall
ret
_exit:
mov rax, 60
mov rdi, 0
syscall
section .data
str1 db "spider-",0
str2 db "man",0xA,0
section .bss
buffer resb 500
buffer2 resb 200 ; for _strreverse !