-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_write.s
45 lines (39 loc) · 1.81 KB
/
ft_write.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
; **************************************************************************** ;
; ;
; ::: :::::::: ;
; ft_write.s :+: :+: :+: ;
; +:+ +:+ +:+ ;
; By: apuchill <[email protected]> +#+ +:+ +#+ ;
; +#+#+#+#+#+ +#+ ;
; #+# #+# ;
; ### ########.fr ;
; ;
; **************************************************************************** ;
; ------------------------------------------------------------------------------
; FUNCTION PROTOTYPE (man 2 write)
; ssize_t ft_write(int fd, const void *buf, size_t count)
;
; INPUT
; 1st argument: rdi -> int fd
; 2nd argument: rsi -> const void *buf
; 3rd argument: rdx -> size_t count
;
; OUTPUT
; return: rax -> ssize_t count
; ------------------------------------------------------------------------------
global ft_write
extern __errno_location
section .text
ft_write:
mov rax, 1 ; put 1 (syscall ID of write) into rax
syscall ; ret = write(fd, buf, count)
cmp rax, 0
jl .ret_error ; if ret < 0 then ret_error()
ret ; return (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)