组装,分段错误 [英] assembly, segmentation fault

查看:148
本文介绍了组装,分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在分割故障

这code的结果,我不知道为什么这样做,
在code只应该在堆栈通过ESP的当前位置到EBP
而只使用在EBP间接寻址方式来获得地址的值,
我不知道为什么操作系统是终止导致分段错误

  .section伪。数据
.section伪的.text
.globl _start
_开始:
MOVL $ 50%EDX
pushl,EDX%
MOVL%ESP,EBP%
MOVL(%EBP),%EBX ##这会导致问题出于某种原因,
MOVL $ 1,%eax中
诠释$ 0x80的##计划应该返回%EBX价值的退出状态


解决方案

由于杰斯特说,问题是64位的Linux工具默认为使64位程序。他写他的评论中回答的坏习惯,所以我就复制在这里:

 作为--32 test.s -o test.o; LD -melf_i386 test.o -o测试

 的gcc -m32 foo.s -ffreestanding -nostdlib -o富

您在段错误

  MOVL(EBP%),EBX%

由于%RSP 不在高32位全零,所以%ESP 是不同的从%RSP 地址。你会发现这个问题用gdb。你会注意到,你有64位寄存器。请参见 http://stackoverflow.com/tags/x86/info 获取信息上用gdb的ASM。

我要挑在这条线更多:

  pushl,EDX%

除了是一个语法错误(额外的逗号), _start 不需要保存任何寄存器。该的x86-64 ABI说你应该假设他们都充满随机垃圾除了堆栈指针。它还说,%RDX 有您应该的atexit注册的函数(),但目前Linux的地址, %RDX 归零工艺条目。 (命令行参数是在栈上。)

我presume事情是pretty多x86 32位进程启动相同的。

This code results in a segmentation fault, I have no idea why it does it, The code just supposed to pass the current location of esp at the stack to ebp and just use indirect addressing mode on ebp to get the value of the address, I don't know why the OS is terminating which results in segmentation fault

.section .data
.section .text
.globl _start
_start:
movl $50,%edx
pushl, %edx
movl %esp,%ebp
movl (%ebp),%ebx  ## this causes the problem for some reason, 
movl $1,%eax     
int $0x80         ## Program should return an exit status of %ebx value

解决方案

As Jester says, the problem is that 64bit Linux tools default to making 64bit programs. He has a bad habit of writing his answers in comments, so I'll just duplicate it here:

as --32 test.s -o test.o; ld -melf_i386 test.o -o test

or

gcc -m32 foo.s -ffreestanding -nostdlib -o foo

You segfault at

movl (%ebp),%ebx

because %rsp isn't all-zero in the upper 32 bits, so %esp is a different address from %rsp. You could find this problem with gdb. You'd have noticed that you had 64bit registers. See http://stackoverflow.com/tags/x86/info for info on using gdb for asm.

I'm going to pick on this line some more:

pushl, %edx

Besides being a syntax error (extra comma), _start doesn't need to save any registers. The x86-64 ABI says you should assume they're all full of random garbage, except the stack pointer. It also says %rdx has the address of a function you should register with atexit(), but on current Linux, %rdx is zeroed on process entry. (Command line args are on the stack.)

I presume things are pretty much the same for x86 32bit process startup.

这篇关于组装,分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆