macos - How to use malloc in asm
问题描述
I'm trying to use malloc to allocate memory dynamically in asm Intel x86_64 with nasm, but I don't understand how to use it.
For exemple, if y want to allocate a 8 bytes memory area, should I push 8 on the stack and then call malloc, like this ?
extern _malloc
section .text
global _my_function
_my_function:
push 20
call _malloc
ret
Or should I move 20 to the rdi register, which is the normally the function's first argument like that ?
extern _malloc
section .text
global _my_function
_my_function:
mov rdi, 20
call _malloc
ret
I tried both but none of them worked and I didn't find any malloc documentation for nasm.
I'm trying to use malloc in order to recode the strdup function from the string library, here's my actual code :
extern _ft_strlen
extern _ft_strcpy
extern _malloc
section .text
global _ft_strdup
_ft_strdup:
push rsi
push rdi ; rdi = str
call _ft_strlen ; rax = ft_strlen(str)
mov r9, rdi ; save rdi (str) into r9
mov rdi, rax ; rdi = len
inc rdi ; rdi = len + 1
call _malloc ; rax = new_str (allocated)
cmp eax, 0 ; if malloc failed
je _failure ; └──► return NULL
mov rsi, r9 ; rsi = str
mov rdi, rax ; rdi = new_str
call _ft_strcpy ; ft_strcpy(new_str, str)
pop rdi
pop rsi
ret
_failure:
xor rax, rax
pop rdi
pop rsi
ret
section .text
global _ft_strcpy
_ft_strcpy:
push rdi
push rsi
jmp _loop
_loop:
mov r8b, BYTE [rdi] ; Save *dst into r8b
mov r9b, BYTE [rsi] ; Save *src into r8b
cmp r9b, 0 ; if *src == '\0'
je finish ; └──► exit from _loop
mov [rdi], r9 ; *dst = r9 (r9 = *src)
inc rdi ; dst++
inc rsi ; src++
jmp _loop
finish:
mov [rdi], r9 ; *dst = r9 (r9 = *src = '\0')
pop rsi
pop rdi
mov rax, rdi ; rax = initial value of dst
ret ; Return rax (dst pointer)
section .text
global _ft_strlen
_ft_strlen:
push rdi
xor rax, rax ; rax = 0
jmp _loop
_loop:
cmp [rdi], byte 0 ; if *str == '\0'
je finish ; └──► exit from _loop
inc rax ; rax++ (len)
inc rdi ; str++
jmp _loop
finish:
pop rdi
ret ; Return rax (len)
When I call ft_strdup, I get a SEGV on unknown address 0x000000000000
error from sanitize.
I call the function this way :
int main(int ac, char **av)
{
char new_str = ft_strdup(av[1]);
return (0);
}
解决方案
推荐阅读
- java - 如何在 JpaRepository 上保存悲观主义锁
- postgresql - Postgresql 请求显示连接是 Kerberos 还是 NTLM 的表
- ngrx - Ngrx Effects 如何获取上一个动作的payload
- c# - 在 ObservableCollection<> 中对每个组的每个元素进行排序
- java - 通用读取分隔的 protobuf 消息
- javascript - node.js 应用程序中的 console.log 未在 sumoLogic 中拆分为多个日志
- sql - 将 SQL Server 函数转换为 Oracle
- javascript - JavaScript 静态变量未设置
- c - 我怎样才能使C可以将单词与字母分开
- powerbi - 从前一行中减去一个值并重复直到 0