-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strdup.s
34 lines (30 loc) · 1.16 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
global ft_strdup
extern ft_strlen
extern ft_strcpy
extern malloc
extern __errno_location
section .text
ft_strdup: ; rdi = char *s1
push rbp
mov rbp, rsp
push rdi ; push on the Stack value of 1st arg of ft_strdup to reuse it later
call ft_strlen
mov rdi, rax ; move return ft_strlen function in 1rst arg (size of alloc) of malloc
add rdi, 1
call malloc
cmp rax, 0x0 ; check if return pointer by malloc is NULL
jz ft_strdup_null
mov rdi, rax ; move return of malloc in 1st argument of ft_strcpy
pop rsi ; pop value from the Stack of 1st arg of strdup in 2nd arg of ft_strcpy
call ft_strcpy
pop rbp
ret
ft_strdup_null:
push rax ; push pointer NULL on the Stack in rax to reuse rax
mov rcx, 12 ; move value of errno : ENOMEM == 12 in rcx register
call __errno_location
mov [rax], rcx ; move ENONEM errno value in errno variable
pop rax ; retrieve return value of ft_strdup in rax register
pop rdi
pop rbp
ret