我对ASM的强烈反应

Flo*_*eau 1 macos assembly glibc x86-64 nasm

你好我学校的每个人都必须在ASM [intel] [NASM]做我自己的strdup功能.

我有一个奇怪的问题......

在我的代码中,如果我 call _malloc

我的代码段错误与此错误:

Program received signal SIGSEGV, Segmentation fault.
0x00007fff849612da in stack_not_16_byte_aligned_error () from /usr/lib/system/libdyld.dylib
Run Code Online (Sandbox Code Playgroud)

我不明白为什么,因为在.text中我说过 extern _malloc

有人知道我为什么会犯这个错误?:)

这是我的代码:

section .text
     global _ft_strdup
     extern _strlen
     extern _malloc
     ;  extern _ft_memcpy

_ft_strdup:
     call _strlen           ;rax = len of str
     mov r8, rdi            ;r8 = str = src
     inc rax                ;rax++
     ;  mov r9, rax         ;len of dest with '\0'
     mov rdi, rax           ;to send the len for malloc
     call _malloc           ;rax = ptr of dest
     ;  cmp rax, 0          ;malloc failled
     ;  jle _error_malloc
     ;  mov rdi, rax        ;malloc param 1 of ft_memcpy
     ;  mov rsi, r8         ;str in param 2 of ft_memcpy
     ;  mov rdx, r9         ;len of str with '\0' param 3 of ft_memcpy
     ;  call _ft_memcpy     ;call ft_memcpy
     ret
_error_malloc:
     xor rax, rax           ;return NULL
     ret
Run Code Online (Sandbox Code Playgroud)

所有以函数开头的函数ft_都与libc Thx all相同

fuz*_*fuz 5

此错误消息表示您malloc使用不充分对齐的堆栈进行了调用.amd64的SysV-ABI要求在函数调用时将堆栈对齐到16个字节.

在您自己的代码中,您可以通过确保始终将偶数个四字字推入堆栈来确保这一点,并记住在进入时,由于返回地址已经在堆栈上,堆栈未对齐8个字节.

如果没有看到您的源代码,很难提供更具体的帮助.