-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strdup.s
57 lines (47 loc) · 1.63 KB
/
ft_strdup.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
; **************************************************************************** ;
; ;
; ::: :::::::: ;
; ft_strdup.s :+: :+: :+: ;
; +:+ +:+ +:+ ;
; By: vtarasiu <[email protected]> +#+ +:+ +#+ ;
; +#+#+#+#+#+ +#+ ;
; Created: 2019/07/19 20:45:27 by vtarasiu #+# #+# ;
; Updated: 2019/07/20 17:17:09 by vtarasiu ### ########.fr ;
; ;
; **************************************************************************** ;
global ft_strdup
global _ft_strdup
extern _ft_strlen
extern _malloc
extern malloc
; assume it's macOS
%deftok MALLOC '_malloc'
%ifdef __linux__
%deftok MALLOC 'malloc'
%endif
ft_strdup:
_ft_strdup:
push rbx
push rbp
mov rbp, rsp
push rdi
push rdi
call _ft_strlen ; count size
push rax
mov rdi, rax ; move desired size for malloc to its first register
add rdi, 1 ; add space for \0 at the end
call MALLOC ; get new pointer
cmp rax, 0
pop rcx
pop rsi
push rax
je .strdup_fail
mov rdi, rax
rep movsb
mov byte [rdi + 0], 0
.strdup_fail:
pop rax
pop rdi
pop rbp
pop rbx
ret