<p>What are differences between stack- and register-based virtual machine's bytecode commands?</p>
<p>For example <a href="https://en.wikipedia.org/wiki/Python_(programming_language)" rel="nofollow noreferrer">Python</a> has stack-based virtual machine.</p>
<p>But <a href="https://en.wikipedia.org/wiki/Lua_(programming_language)" rel="nofollow noreferrer">Lua</a> has register-based vm.</p>
<p>So, what are the differences between bytecode commands?</p>

<stack><registers><virtual-machine>
What are differences between stack- and register-based virtual machine's bytecode commands?
CC BY-SA 4.0
2023-06-08T07:03:35.697Z
I rewrite my question. Maybe now it is more focusing. (Sorry for my english)
чистов_n
CC BY-SA 4.0
2023-06-08T15:11:10.660Z
Maybe this question should be rephrased to be a pro/con on stack vs registers?
kouta-kun
CC BY-SA 4.0
2023-06-09T01:06:33.233Z
Stack and registers are machines and ISAs features. Why are you asking about VMs in particular?
Longinus
CC BY-SA 4.0
2023-06-09T03:09:25.010Z
The last edit was too much in conflict with the original question and invalidated the existing answer; if the author wants to ask that question it's probably ok, but we shouldn't change it underneath them or add commentary to that extent.
Michael Homer
<p>Register-based VMs (most often) need a way to specify operand registers for each instruction (i.e. <code>add rax, rbx</code>), while stack-based VMs get away with operating on the top of the stack (i.e. <code>add</code> just takes the pops the two stack items and pushes the sum back). I know that Python's bytecode also includes some convenience instructions to, for example, duplicate the top of stack, or to rotate the top three stack items around (<code>DUP_TOP</code>, <code>ROT_THREE</code>, and there's more like this). <em>(EDIT: I've just checked, and they appear to have been replaced with a more general <code>COPY(i)</code> and <code>SWAP(i)</code> in Python 3.11)</em>. A register-based VM would have no real need for these, since they can be easily expressed through register manipulations without any observable performance hit</p>

CC BY-SA 4.0
2023-06-08T07:11:01.943Z
2023-06-08T07:18:58.690Z
2023-06-08T07:18:58.690Z
abel1502
# What are differences between stack- and register-based virtual machine's bytecode commands?
Posted by: username_0 on 2023-06-08
What are differences between stack- and register-based virtual machine's bytecode commands?

For example [Python](https://en.wikipedia.org/wiki/Python_(programming_language)) has stack-based virtual machine.

But [Lua](https://en.wikipedia.org/wiki/Lua_(programming_language)) has register-based vm.

So, what are the differences between bytecode commands?
**Tags:** stack registers virtual-machine
> **Comments:**
> **username_0**: I rewrite my question. Maybe now it is more focusing. (Sorry for my english)  
> **username_1**: Maybe this question should be rephrased to be a pro/con on stack vs registers?  
> **username_2**: Stack and registers are machines and ISAs features. Why are you asking about VMs in particular?  
> **username_3**: The last edit was too much in conflict with the original question and invalidated the existing answer; if the author wants to ask that question it's probably ok, but we shouldn't change it underneath them or add commentary to that extent.  
---
## 1 answers
### Answer by username_4 on 2023-06-08. Score: 5 | Accepted answer
Register-based VMs (most often) need a way to specify operand registers for each instruction (i.e. `add rax, rbx`), while stack-based VMs get away with operating on the top of the stack (i.e. `add` just takes the pops the two stack items and pushes the sum back). I know that Python's bytecode also includes some convenience instructions to, for example, duplicate the top of stack, or to rotate the top three stack items around (`DUP_TOP`, `ROT_THREE`, and there's more like this). *(EDIT: I've just checked, and they appear to have been replaced with a more general `COPY(i)` and `SWAP(i)` in Python 3.11)*. A register-based VM would have no real need for these, since they can be easily expressed through register manipulations without any observable performance hit
---
