首页 > 解决方案 > 间接访问堆栈失败

问题描述

我刚开始学习 intel att 汇编并遇到了下一个问题:当我尝试使用访问堆栈时出现段错误。我确实间接访问了内存位置,但是当我尝试使用堆栈这样做时,我得到了段错误。我的系统是:

Linux cat 4.19.36 #1-NixOS SMP Sat Apr 20 07:16:05 UTC 2019 x86_64 GNU/Linux

我使用气体编译器:

  as -gfstab  -o a.o a.s
  ld -o a.out a.o

这是我的小代码示例:

.code64
.globl _start
.text

_start:  
  movl (%esp) , %eax

  xor %eax , %eax
  inc %eax
  int $0x80

该程序已编译并链接,但是当我尝试启动它时,我得到了:

as -gfstab  -o a.o a.s
ld -o a.out a.o
./a.out 
make: *** [makefile:4: new] Segmentation fault

我用gdb来解决这个问题。间接访问堆栈给了我下一个结果:

6   movl (%esp) , %eax
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
_start () at a.s:6
6   movl (%esp) , %eax

我从互联网上举了这个例子,他们说这是做事的方式。我做错了什么?TNX

标签: assemblyx86

解决方案


Resolved!

the matter is that when you are in x64 mode you MUST use %r.. registers for working with stack. thats all

so the actual code looks like that:

.code64
.globl _start
.text

_start :  

  push $5 
  push $3
  push $2
  call myproc 
  add $24 , %rsp

  xor %rax , %rax
  inc %rax
  int $0x80

myproc :

  mov  8 (%rsp) , %rbx
  mov 16 (%rsp) , %rax
  add %rax , %rbx
  mov 24 (%rsp) , %rax
  add %rax , %rbx
  ret

tnx Jester for your help


推荐阅读