> For the complete documentation index, see [llms.txt](https://koshizuow.gitbook.io/compilerbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://koshizuow.gitbook.io/compilerbook/calculator_level_language/stack_machine.md).

# 堆疊機

上一章說明了把標記列傳換成抽象語法樹的演算法。在文法中考慮到運算子的優先順序，做出了讓`*`或`/`比`+`或`-`先出現在末梢的抽象語法樹。但是要怎麼把樹轉換成組合語言呢？這章就是要說明這個轉換的方法。

首先，我們來想想為什麼不能用和加減法一樣的方法來轉成組合語言。在可以算加減法的編譯器中，是把結果放在 RAX 暫存器，在該暫存器上進行加法或減法的計算。也就是說編譯的程式中途能暫時紀錄的答案只有1個。

但是，包含乘除法的話，中途要紀錄的答案就不僅限1個了。舉例來說，考慮`2*3+4*5`這個算式，為了計算加法必須要有左右兩邊的計算結果，在算加法前要先算`2*3`和`4*5`。也就是說如果不能紀錄2個結果，就沒辦法算出全部的結果。

有種可以簡單進行這樣的計算的電腦叫作「堆疊機」（stack machine）。接下來我們暫時放下抽象語法樹，來學一下堆疊機。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://koshizuow.gitbook.io/compilerbook/calculator_level_language/stack_machine.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
