如何在汇编语言中堆栈的工作? [英] How does the stack work in assembly language?

查看:265
本文介绍了如何在汇编语言中堆栈的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在试图了解栈的作品,所以我决定教自己一些汇编语言,我用这本书:

I'm currently trying to understand how the stack works, so I've decided teach myself some assembly language, I'm using this book:

http://savannah.nongnu.org/projects/pgubook/

我使用天然气,做我的开发上的Linux薄荷

I'm using Gas and doing my development on Linux Mint.

我有点被什么东西迷惑:

I'm a bit confused by something:

据我知道一个堆栈是简单的数据结构。所以,我认为,如果我是在装配编码我不得不执行堆栈自己。然而,这似乎并没有因为有类似的命令是这样

As far as I was aware a stack is simply a data structure. So I assumed if I was coding in assembly I'd have to implement the stack myself. However this doesn't seem to be the case as there are commands like

pushl
popl

因此​​,在组装编码该 86 的结构以及使用该气体的语法时:在堆叠只是一种数据结构,是已经实现了?或者,它实际上是在硬件层面实现的?或者是别的什么?也最汇编语言对其他芯片组已经堆已经实现?

So when coding in assembly for the x86 architecture and using the Gas syntax: is the stack just a data structure that's already implemented? Or is it actually implemented at the hardware level? Or is it something else? Also would most assembly languages for other chip sets have the stack already implemented?

我知道这是一个有点愚蠢的问题,但实际上我这个颇为困惑。

I know this is a bit of a foolish question but I'm actually quite confused by this.

推荐答案

我觉得主要是你要一个之间的混淆程序的堆栈的老栈

I think primarily you're getting confused between a program's stack and any old stack.

一个堆栈

是由在最后的信息先出系统的抽象数据结构。你把任意对象压入堆栈,然后你再带他们,就像一个输入/输出托盘,上面的项目总是被带下一个,你总是把顶端。

Is an abstract data structure which consists of information in a Last In First Out system. You put arbitrary objects onto the stack and then you take them off again, much like an in/out tray, the top item is always the one that is taken off and you always put on to the top.

一个程序堆栈

是一个堆栈,它是在执行期间所使用的存储器的部分中,它通常具有每个节目的静态大小和频繁用于存储功能的参数。你推的参数入栈中,当你调用一个函数,该函数可以直接解决栈或从栈中弹出了变量。

Is a stack, it's a section of memory that is used during execution, it generally has a static size per program and frequently used to store function parameters. You push the parameters onto the stack when you call a function and the function either address the stack directly or pops off the variables from the stack.

一个程序堆栈不是一般的硬件(虽然它保存在内存中,因此可以说这样),但堆栈指针,它指向堆栈的当前区域通常是CPU寄存器。这使得它有点不是一个后进先出栈更加灵活,因为你可以改变在该堆栈正在解决的地步。

A programs stack isn't generally hardware (though it's kept in memory so it can be argued as such), but the Stack Pointer which points to a current area of the Stack is generally a CPU register. This makes it a bit more flexible than a LIFO stack as you can change the point at which the stack is addressing.

您应该阅读并确保理解维基百科一文,因为它给出了一个硬件堆栈,其中很好的说明是你正在处理。

You should read and make sure you understand the wikipedia article as it gives a good description of the Hardware Stack which is what you are dealing with.

还有这教程这也解释了堆在旧的16位寄存器的条件,但可能是有益的,另一个特别是关于堆栈。

There is also this tutorial which explains the stack in terms of the old 16bit registers but could be helpful and another one specifically about the stack.

从尼尔斯Pipenbrinck:

From Nils Pipenbrinck:

这是值得注意的,一些处理器不执行所有指令用于访问和操纵堆栈(PUSH,POP,堆栈指针等),但在x86确实因为它的使用频率。在这种情况下,如果你想要一个堆栈你就必须实现它自己(有些MIPS和一些ARM处理器没有堆栈中创建)。

It's worthy of note that some processors do not implement all of the instructions for accessing and manipulating the stack (push, pop, stack pointer, etc) but the x86 does because of it's frequency of use. In these situations if you wanted a stack you would have to implement it yourself (some MIPS and some ARM processors are created without stacks).

例如,以MIPS推指令会等来实现:

For example, in MIPs a push instruction would be implemented like:

addi $sp, $sp, -4  # Decrement stack pointer by 4  
sw   $t0, ($sp)   # Save $t0 to stack  

和一个POP指令将如下所示:

and a Pop instruction would look like:

lw   $t0, ($sp)   # Copy from stack to $t0  
addi $sp, $sp, 4   # Increment stack pointer by 4  

这篇关于如何在汇编语言中堆栈的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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