我是 x86-64 汇编的新手,我正在运行一个简单的 x64 汇编代码:
.global main
.text
main:
mov $message, %rdi
sub $8, %rsp
call puts
add $8, %rsp
ret
message:
.asciz "Hello, World"
在cygwin下用gcc编译代码后,总是报错:
segmentation error
但如果我删除
call puts
程序运行没有错误。那么这个call out语句有什么问题呢?
最佳答案
您必须在只读数据部分定义message
...
像这样...
.rodata # read-only data section
message:
.string "Hello, World!"
在 32 位机器上,使用 AT&T 语法,代码如下...
.section .rodata
msg:
.string "Hello, World!" # msg is in the read only data section.
.section .text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl $msg, (%esp) # We move the argument of puts to (%esp)
call puts # puts is called
movl $0, (%esp) # moved the argument of(0) exit to (%esp)
call exit # exit is called
关于c - 调用c函数时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064530/