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

查看:95
本文介绍了推入堆栈(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.

因此,当您执行此操作时:

So when you do this:

push ebp
ret

您实际上正在跳转到碰巧存储在ebp中的任何地址.
您需要在返回之前平衡堆栈-即每个push-type指令应具有一个对应的pop-type指令:

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天全站免登陆