56 lines
755 B
ArmAsm
56 lines
755 B
ArmAsm
bits 64
|
|
|
|
%include "defines.inc"
|
|
|
|
section .text
|
|
global ft_calloc
|
|
; rdi = nitems, rsi = size
|
|
ft_calloc:
|
|
; Check if one of arguments are 0 or negative
|
|
cmp rdi, 0
|
|
je malloc_0
|
|
jl end_null
|
|
cmp rsi, 0
|
|
je malloc_0
|
|
jl end_null
|
|
|
|
; save rbx
|
|
push rbx
|
|
mov rbx, rdi
|
|
|
|
; multiplication and security for overflow
|
|
xor rdi, rdi
|
|
mul rbx, rsi
|
|
cmovno rdi, rbx; Move if not overflow
|
|
cmp rdi, 0
|
|
je end_null_pop
|
|
|
|
; malloc and security if malloc failed
|
|
call malloc wrt ..plt
|
|
cmp rax, 0
|
|
je end_null_pop
|
|
|
|
; set everything to 0
|
|
bzero:
|
|
dec rbx
|
|
mov [rax + rbx], 0
|
|
|
|
cmp rbx, 0
|
|
jg bzero
|
|
|
|
pop rbx
|
|
ret
|
|
|
|
end_null_pop:
|
|
pop rbx
|
|
end_null:
|
|
xor rax, rax
|
|
ret
|
|
|
|
malloc_0:
|
|
mov rdi, 0
|
|
call malloc wrt ..plt
|
|
ret
|
|
|
|
|