【发布时间】:2017-01-14 08:06:51
【问题描述】:
我正在制作一个基本的汇编减法函数并将结果打印到控制台。这是我认为应该工作的代码:
(编译as output.s,ld a.out -e _start -o output)
.bss
output:
.int
.text
.global _start
_start:
movl $9, %eax
movl %eax, %ebx
movl $8, %eax
subl %eax, %ebx
movl %ebx, (output)
# ASCII for digits is 0x30 greater than digit value
addl $0x30, output
movl $2, %edx # write 2 bytes (need 1 for null?)
movl $output, %ecx # output
movl $1, %ebx # write to stdin
movl $4, %eax # syscall number for write
int $0x80 # invoke syscall
# CR
movl $2, %edx
movl $13, (output)
movl $output, %ecx
movl $1, %ebx
movl $4, %eax
int $0x80
# LF
movl $2, %edx
movl $10, (output)
movl $output, %ecx
movl $1, %ebx
movl $4, %eax
int $0x80
# exit
movl $0, %ebx
movl $1, %eax
int $0x80
但是,这个程序有段错误。 我发现如果我在最后添加一个简单的 .data 部分:
.data
pingle:
.int 666
它工作正常。为什么我需要 .data 段?当我每次写入 2 个字节时,我是否会溢出其中一个段?还是多次覆盖output?
非常感谢任何想法!
【问题讨论】:
-
段错误在哪里?在 gdb 中运行您的程序以找出答案。可能有一个
.data部分恰好将可写内存放在您的程序错误地尝试读取或写入的位置,但这只是一个猜测,直到您显示哪条指令出现段错误以及寻址模式中使用的寄存器和符号的内容. -
终于找到了副本。 stackoverflow.com/questions/36821123/… 有完全不同的症状,但原因是一样的:认为
.double带有一个空列表会为一个 double 保留空间。
标签: assembly x86 segmentation-fault