简单C/NASM x86_64/C程序打印浮点中的Segfault 在我的C代码中,我调用汇编(NASM x86_64)函数(main.C) 在汇编中,我调用C函数(printf.asm) 该函数使用printf打印float。(c_printf.c) 我有毛病了
我应该用另一种方式编译/链接吗?也许我从汇编中调用C函数的方法不正确 我编译时使用:简单C/NASM x86_64/C程序打印浮点中的Segfault 在我的C代码中,我调用汇编(NASM x86_64)函数(main.C) 在汇编中,我调用C函数(printf.asm) 该函数使用printf打印float。(c_printf.c) 我有毛病了,c,linux,assembly,nasm,x86-64,C,Linux,Assembly,Nasm,X86 64,我应该用另一种方式编译/链接吗?也许我从汇编中调用C函数的方法不正确 我编译时使用: gcc -c c_printf.c nasm -f elf64 -F dwarf -g printf.asm gcc -o main printf.o c_printf.o main.c 我的代码: main.c printf.asm global start_asm extern printf_float start_asm: call printf_float ret c_printf
gcc -c c_printf.c
nasm -f elf64 -F dwarf -g printf.asm
gcc -o main printf.o c_printf.o main.c
我的代码:
main.c
printf.asm
global start_asm
extern printf_float
start_asm:
call printf_float
ret
c_printf.c
#include <stdio.h>
void printf_float(void) {
printf("%f\n", 42.0f);
}
你搞砸了堆栈对齐。而不是调用printf_float;retdo
jmp printf_float
。我想从C函数返回汇编。示例:我想调用printf_float两次,然后手动保持堆栈对齐。例如子rsp,8;调用printf_float;加上rsp,8;ret
注意,call
将rsp更改为8,ABI需要16字节对齐,因此需要额外的8字节。谢谢!我确信堆栈对齐是8字节,而不是16字节。这解释了很多。16字节,因此更容易使用对齐的SSE指令。现在32位模式也一样。
#include <stdio.h>
void printf_float(void) {
printf("%f\n", 42.0f);
}
==16257== Memcheck, a memory error detector
==16257== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==16257== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==16257== Command: ./main
==16257==
--16257-- WARNING: Serious error when reading debug info
--16257-- When reading debug info from /home/starsep/stack/main:
--16257-- Overrun whilst parsing .debug_abbrev section(2)
==16257==
==16257== Process terminating with default action of signal 11 (SIGSEGV)
==16257== General Protection Fault
==16257== at 0x4E8F824: printf (printf.c:28)
==16257== by 0x40055F: printf_float (in /home/starsep/stack/main)
==16257== by 0x400534: ??? (printf.asm:6)
==16257== by 0x4E5A82F: (below main) (libc-start.c:291)
==16257==
==16257== HEAP SUMMARY:
==16257== in use at exit: 0 bytes in 0 blocks
==16257== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==16257==
==16257== All heap blocks were freed -- no leaks are possible
==16257==
==16257== For counts of detected and suppressed errors, rerun with: -v
==16257== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
[1] 16257 segmentation fault valgrind ./main