什么是操作数堆栈

什么是操作数堆栈

本文介绍了什么是操作数堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关JVM架构的内容。今天我读到了操作数堆栈的概念。根据一篇文章:

I am reading about JVM architecture. Today I read about the concept of the Operand Stack. According to an article:

我无法理解:操作数堆栈究竟是什么,它是如何工作的在jvm?

I can't understand: What exactly is an Operand Stack, and how does it work in jvm?

推荐答案

这是各个字节码操作如何获取输入,以及它们如何提供输出。

It's how the various individual bytecode operations get their input, and how they provide their output.

例如,考虑 iadd 操作,它会增加两个 int s一起。要使用它,你在堆栈上推两个值,然后使用它:

For instance, consider the iadd operation, which adds two ints together. To use it, you push two values on the stack and then use it:

iload_0     # Push the value from local variable 0 onto the stack
iload_1     # Push the value from local variable 1 onto the stack
iadd        # Pops those off the stack, adds them, and pushes the result

现在堆栈的最高值是这两个局部变量的总和。下一个操作可能会占用顶部堆栈值并将其存储在某个位置,或者我们可能会在堆栈上推送另一个值来执行其他操作。

Now the top value on the stack is the sum of those two local variables. The next operation might take that top stack value and store it somewhere, or we might push another value on the stack to do something else.

假设您要添加三个值一起。堆栈变得简单:

Suppose you want to add three values together. The stack makes that easy:

iload_0     # Push the value from local variable 0 onto the stack
iload_1     # Push the value from local variable 1 onto the stack
iadd        # Pops those off the stack, adds them, and pushes the result
iload_2     # Push the value from local variable 2 onto the stack
iadd        # Pops those off the stack, adds them, and pushes the result

现在堆栈的最高值是将这三个局部变量加在一起的结果。

Now the top value on the stack is the result of adding together those three local variables.

让我们更详细地看一下第二个例子:

Let's look at that second example in more detail:

我们假设:


  • 开始堆栈是空的(这几乎从来都不是真的,但我们并不在乎我们开始之前有什么呢?

  • 本地变量0包含 27

  • 局部变量1包含 10

  • 局部变量2包含 5

  • The stack is empty to start with (which is almost never actually true, but we don't care what's on it before we start)
  • Local variable 0 contains 27
  • Local variable 1 contains 10
  • Local variable 2 contains 5

所以最初:

+-------+
| stack |
+-------+
+-------+

然后我们做

iload_0     # Push the value from local variable 0 onto the stack

现在我们有

+-------+
| stack |
+-------+
|   27  |
+-------+

下一步

iload_1     # Push the value from local variable 1 onto the stack



+-------+
| stack |
+-------+
|   10  |
|   27  |
+-------+

现在我们进行添加:

iadd        # Pops those off the stack, adds them, and pushes the result

它弹出 10 27 离开堆栈,将它们加在一起,然后推送结果( 37 )。现在我们有:

It "pops" the 10 and 27 off the stack, adds them together, and pushes the result (37). Now we have:

+-------+
| stack |
+-------+
|   37  |
+-------+

我们的第三个 int 的时间:

Time for our third int:

iload_2     # Push the value from local variable 2 onto the stack



+-------+
| stack |
+-------+
|    5  |
|   37  |
+-------+

我们做第二个 iadd

iadd        # Pops those off the stack, adds them, and pushes the result

这给了我们:

+-------+
| stack |
+-------+
|   42  |
+-------+

(当然,这是。)

这篇关于什么是操作数堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:30