-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strdup.s
52 lines (46 loc) · 1.93 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
; **************************************************************************** ;
; ;
; ::: :::::::: ;
; ft_strdup.s :+: :+: :+: ;
; +:+ +:+ +:+ ;
; By: apuchill <[email protected]> +#+ +:+ +#+ ;
; +#+#+#+#+#+ +#+ ;
; #+# #+# ;
; ### ########.fr ;
; ;
; **************************************************************************** ;
; ------------------------------------------------------------------------------
; FUNCTION PROTOTYPE (man 3 strdup)
; char *ft_strdup(const char *s1)
;
; INPUT
; 1st argument: rdi -> const char *s1
;
; OUTPUT
; return: rax -> char *dst
; ------------------------------------------------------------------------------
global ft_strdup
extern __errno_location
extern malloc
extern ft_strlen
extern ft_strcpy
section .text
ft_strdup:
call ft_strlen ; len = ft_strlen(s1)
inc rax ; len++ (increment for '\0')
mov rbx, rdi ; tmp = s1
mov rdi, rax ; size = len
call malloc ; ret_malloc = malloc(size)
cmp rax, 0
je .ret_error ; if (!ret_malloc) then ret_error()
mov rdi, rax ; dst = ret_malloc
mov rsi, rbx ; src = tmp (= s1)
call ft_strcpy
ret
.ret_error:
mov rdi, rax ; tmp = ret
neg rdi ; tmp = -tmp (invert value for positive errno)
call __errno_location ; ret = &errno (get pointer to errno)
mov [rax], rdi ; *ret = tmp (put return value into errno)
mov rax, -1 ; ret = -1
ret ; return (ret)