assembly - 间接访问堆栈失败
问题描述
我刚开始学习 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
解决方案
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
推荐阅读
- fortran - fortran 和用户输入值中的迭代器问题
- python - 有没有办法使用列表理解来循环两个不同长度的列表?
- powershell - 根据其窗口标题终止任务
- put - 为 Guzzle 6 put() 设置请求正文
- python-3.x - 一组微分方程中的条件分段函数
- java - 为什么不管输入,输出都保持 0 和 -1?
- python - 如果列表中的所有项目都相等,我想编写一个返回 True 的函数
- sql-server - 针对今天查询日期时间字段时遇到问题
- postgresql - 路由完成后不会直接释放 knex 连接(并且连接对象超出范围)
- kubernetes - nginx-ingress 未转发到仪表板