x86 示例程序中的分段错误

Segmentation fault in x86 example program

我正在读这本书 从头开始编程,乔纳森·巴特利特 (Jonathan Bartlett)。在这个第一次显示函数调用约定的程序中,当 运行 像书中一样输入它后,我遇到了分段错误。该函数只从堆栈中取出 2 个数字,returns 第一个数字对第二个数字的 %eax 次幂。

这是有问题的程序:

.code32

.section .data

.section .text

.globl _start
_start:

  pushl 
  pushl 
  call power
  addl , %esp

  pushl %eax

  pushl 
  pushl 
  call power
  addl , %esp

  popl %ebx

  addl %eax, %ebx

  movl , %eax
  int [=10=]x80

.type power, @function
power:
  pushl %ebp
  movl %esp, %ebp
  subl , %esp

  movl 8(%ebp),  %ebx
  movl 12(%ebp), %ecx

  movl %ebx, -4(%ebp)

power_loop:
  cmpl , %ecx
  je end_power

  movl -4(%ebp), %eax
  imull %ebx, %eax
  movl %eax, -4(%ebp)
  decl %ecx

  jmp power_loop

end_power:
  movl -4(%ebp), %eax
  movl %ebp, %esp
  popl %ebp
  ret

我在 edb 中加载程序并逐步执行它,当我到达加载第一个函数参数的指令时出现分段错误。给出错误消息

无法访问地址 0x000000003EC56208。

难道我不能访问函数内部 (8 + %ebp) 和 (12 + %ebp) 指向的值吗?

我猜您想在 64 位操作系统上构建 32 位程序。您必须将这种情况告诉汇编器和链接器:

as --32 -o power.o power.s
ld -m elf_i386 -o power power.o

运行 它与 ./power.