我在linux上使用NASM编写基本汇编程序,该程序从C库(printf)调用函数。不幸的是,我在这样做时发生了分段错误。注释掉对printf的调用可以使程序无误地运行。从组件调用C函数时发生Seg故障
; Build using these commands:
; nasm -f elf64 -g -F stabs <filename>.asm
; gcc <filename>.o -o <filename>
;
SECTION .bss ; Section containing uninitialized data
SECTION .data ; Section containing initialized data
text db "hello world",10 ;
SECTION .text ; Section containing code
global main
extern printf
;-------------
;MAIN PROGRAM BEGINS HERE
;-------------
main:
push rbp
mov rbp,rsp
push rbx
push rsi
push rdi ;preserve registers
****************
;code i wish to execute
push text ;pushing address of text on to the stack
;x86-64 uses registers for first 6 args, thus should have been:
;mov rdi,text (place address of text in rdi)
;mov rax,0 (place a terminating byte at end of rdi)
call printf ;calling printf from c-libraries
add rsp,8 ;reseting the stack to pre "push text"
**************
pop rdi ;preserve registers
pop rsi
pop rbx
mov rsp,rbp
pop rbp
ret
库函数的调用约定又是什么?我的猜测是你使用了错误的。 – 2013-03-22 16:46:31
在我正在阅读的书中(涵盖32位程序集,而不是64位),它只是说推动字符串地址,调用函数,清理堆栈指针。我认为文本将是唯一需要的参数,因为printf搜索空字节终止。 – user2177208 2013-03-22 16:56:10
文本字符串必须以0字节结尾!如果没有,'printf()'知道它会在哪里结束? – 2013-03-22 17:00:30