QBoard » Statistical modeling » Stats - Tech » Assembly language - Stack machine

Assembly language - Stack machine

  • I am learning assembly language in my spare time to become a better developer.

    I understand the difference between stack-based machines and register-based machines at a conceptual level, but I am wondering how stack-based machines are actually implemented. If a virtual machine, e.g. JVM or .NET, runs on a register-based architecture, e.g. x86 or x64, then it must use registers at the assembly level (as far as I am concerned). I am obviously missing something here. Therefore I am unsure of the distinction at assembly language.

    I have read articles on here e.g. Stack-based machine depends on a register-based machine? and also on Wikipedia, but I don't believe they answer my question directly.

      September 30, 2021 1:59 PM IST
    0
  • then it must use registers at the assembly level

    That's not a requirement, processors have a cpu stack that behaves a lot like the virtual stack in the intermediary language. You can translate the instructions almost one-to-one to cpu instructions. Certainly one of the reasons that stack based VMs are popular, the jitter is easy to implement.

    The only hangup with doing it that way is that the machine code isn't very efficient. It is the job of the jitter optimizer to find ways to use the cpu registers efficiently and make the code faster that way.

    The opposite problem is present in register-based VMs. It is a harder problem to solve since the real CPU doesn't have as many registers as the VM. So the jitter must find ways to use the stack to spill registers that the hardware doesn't provide.

      January 7, 2022 12:50 PM IST
    0
  • Stack based machines are rarely implemented in hardware - I've only every heard of one such implementation, and have never had the chance to work on one.
    In reality Stack machines are implemented on real register based processors by native interpreters. In essence, the theoretical Stack machine is emulated by the real Register based machine.
    So to answer your question: although the machine code of the stack machine doesn't have registers, the native interpreter that executes these instructions does have registers and will be using them.
    Q: So why the indirection? A: Portability - the instruction set of the stack machine can be emulated on any number of different register based machines. This means that the same JVM application can be run on any machine that has an interpreter, hence the old Java slogan "Write once, Run anywhere"
      October 6, 2021 9:21 AM IST
    0