推入堆栈时出现分段错误(NASM) [英] Segmentation fault when pushing on stack (NASM)

查看:35
本文介绍了推入堆栈时出现分段错误(NASM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个 nasm 程序.以下代码:

I'm trying to get a nasm program running. The following code:

segment .data

contAir:    dt 1.11330e-10
constOil:   dt 2.33656e-10

segment .text

global calc

calc:

mov edx, 0
push ebp
;mov ebp, esp

;mov eax, [ebp + 8]

ret

将 ebp 推入堆栈时出现分段错误(核心转储).这是为什么?我在 Ubuntu 虚拟机上运行此代码.有趣的是,有时我会收到非法指令"错误.

I get a segmentation fault (core dump) when pushing ebp on the stack. Why is that? I'm running this code on an Ubuntu virtual machine. Funny thing is, sometimes I get an "illegal instruction" error.

推荐答案

将 ebp 推入堆栈时出现分段错误(核心转储).这是为什么?我在 Ubuntu 虚拟机上运行此代码.有趣的是,有时我会收到非法指令"错误.

I get a segmentation fault (core dump) when pushing ebp on the stack. Why is that? I'm running this code on an Ubuntu virtual machine. Funny thing is, sometimes I get an "illegal instruction" error.

我敢打赌,您不会在 push 处遇到分段错误,而是在 ret 处遇到分段错误.ret 指令的作用是从堆栈中弹出返回地址(通常由 call 指令将返回地址推到那里)并跳转到它.

I'd bet that you're not getting the segmentation fault at the push, but rather at the ret. What the ret instruction does is pop the return address from the stack (which typically will have been pushed there by a call instruction) and jumps to it.

所以当你这样做时:

push ebp
ret

您实际上是在跳转到恰好存储在 ebp 中的任何地址.
您需要在返回之前平衡堆栈 - 即每个推送类型指令都应该有一个对应的弹出类型说明:

You're effectively jumping to whatever address happened to be stored in ebp.
You need to balance the stack before returning - i.e. each push-type instruction should have a corresponding pop-type instruction:

push ebp
; ... other code goes here ...
pop ebp
ret

这篇关于推入堆栈时出现分段错误(NASM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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