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

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

问题描述

我目前正在尝试了解堆栈的工作原理,因此我决定自学一些汇编语言,我正在使用这本书:

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

我正在使用 Gas 并在 Linux Mint.

我有点困惑:

据我所知,堆栈只是一种数据结构.所以我假设如果我在汇编中编码,我必须自己实现堆栈.然而,情况似乎并非如此,因为有像

这样的命令

pushl人口

因此,在为 x86 架构进行汇编编码并使用 Gas 语法时:是堆栈只是一个已经实现的数据结构?还是实际上是在硬件层面实现的?或者是别的什么?其他芯片组的大多数汇编语言是否已经实现了堆栈?

我知道这是一个有点愚蠢的问题,但实际上我对此很困惑.

解决方案

我认为主要是您在程序的堆栈任何旧的堆栈之间感到困惑.>

一个堆栈

是一种抽象数据结构,由后进先出系统中的信息组成.您将任意对象放入堆栈中,然后再次取下它们,就像进/出托盘一样,最上面的项目总是被取下的,而您总是放在最上面.

程序栈

是一个栈,它是在执行过程中使用的一段内存,它通常每个程序都有一个静态大小,经常用于存储函数参数.调用函数时将参数压入堆栈,函数要么直接寻址堆栈,要么从堆栈中弹出变量.

程序堆栈通常不是硬件(尽管它保存在内存中,因此可以这样争论),但是指向堆栈当前区域的堆栈指针通常是一个 CPU 寄存器.这使它比 LIFO 堆栈更灵活,因为您可以更改堆栈寻址的点.

您应该阅读并确保理解维基百科文章,因为它提供了很好地描述了您正在处理的硬件堆栈.

还有这个教程根据旧的 16 位寄存器解释了堆栈,但可能会有所帮助,另一个专门关于堆栈.

来自尼尔斯·派宾布林克:

值得注意的是,一些处理器没有实现访问和操作堆栈的所有指令(入栈、出栈、栈指针等),但 x86 实现了,因为它的使用频率很高.在这些情况下,如果您想要一个堆栈,则必须自己实现它(一些 MIPS 和一些 ARM 处理器是在没有堆栈的情况下创建的).

例如,在 MIP 中,推送指令的实现方式如下:

addi $sp, $sp, -4 # 栈指针减4sw $t0, ($sp) # 将 $t0 存入堆栈

一个 Pop 指令看起来像这样:

lw $t0, ($sp) # 从栈复制到 $t0addi $sp, $sp, 4 # 将堆栈指针增加 4

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/

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

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.

A 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.

A Programs Stack

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.

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.

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.

From Nils Pipenbrinck:

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).

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  

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