-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strdup.s
38 lines (34 loc) · 1.3 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
; ************************************************************************** ;
; ;
; :::::::: ;
; ft_strdup.s :+: :+: ;
; +:+ ;
; By: Angi <[email protected]> +#+ ;
; +#+ ;
; Created: 2021/02/23 13:06:40 by Angi #+# #+# ;
; Updated: 2021/02/23 13:06:40 by Angi ######## odam.nl ;
; ;
; ************************************************************************** ;
global _ft_strdup
extern ___error
extern _malloc
extern _ft_strlen
extern _ft_strcpy
section .text
_ft_strdup:
push rdi
call _ft_strlen
mov rcx, rax ; len is returned in rax
inc rcx ; add one space for \0
mov rdi, rcx ; save len for malloc in rdi
call _malloc
cmp rax, 0 ; check if malloc fails
je _error
mov rdi, rax ; save the malloced string to rdi
pop rsi
call _ft_strcpy
ret
_error:
pop rdi
mov rax, 0
ret