Assembly Language - Machine

I learn assembly language in my free time to become a better developer.

I understand the difference between stack-based machines and register-based machines at a conceptual level, but I wonder how the stack-based machines actually execute. If a virtual machine, for example. JVM or .NET, is based on a register architecture, for example. x86 or x64, then it should use registers at the assembly level (as far as I know). Obviously, I don’t see something here. Therefore, I am not sure about the difference in assembler.

I read articles here, for example. Does a stack-based machine depend on a registry-based machine? , as well as from Wikipedia, but I do not believe that they directly answer my question.

+7
source share
3 answers

then it should use assembly level registers

This is not a requirement, processors have a processor stack, which behaves the same as a virtual stack in a mediator language. You can translate almost one-to-one instructions into processor instructions. Of course, one of the reasons that stack-based virtual machines are popular is that jitter is easy to implement.

The only freeze in this is that machine code is not very efficient. The task of the jitter optimizer is to find ways to efficiently use processor registers and make code faster this way.

The opposite problem is present in register-based virtual machines. This is more difficult to solve, since there are not as many registers on a real processor as a virtual machine. Thus, jitter should find ways to use the stack to spill registers that the hardware does not provide.

+4
source

Stack-based machines are rarely implemented on hardware β€” I only heard of one such implementation and never had the opportunity to work on it.

In fact, stacked machines are implemented on real registry processors with their own interpreters. In fact, the theoretical Stack machine is emulated by a real registry-based machine.

So, to answer your question: although the machine code of the stacked computer has no registers, its own interpreter that executes these instructions has registers and will use them.

Q: So why indirectly? A: Portability - the set of instructions on a stack machine can be emulated on any number of different machines based on registers. This means that the same JVM application can be run on any machine with an interpreter, so the old Java slogan "Write once, Run where"

+7
source

For reference, the Burroughs B5000 and Inmos Transputer were stacked machines. DEC PDP11 had such flexible addressing modes that it could be used as a stack machine. I think Niklaus Wirth Lilith may have been a stack machine (more than 20 years ago my mind slides :-)

They really did not have a register name / number in the instructions for finding operands, because they were on the stack.

Instructions can load instant (constant) values ​​onto the stack or load / store in memory.

Thus, they were not add.w r0, r1, r5 or add.w eax, [#fe34] . It was add.w

So, an example (not entirely accurate, it was more complex) of the assembler sequence could be

 loadstack 0xfe34 -- got fe34 onto stack loadstackindirect -- use address on the stack, to load the value at that address add.w -- assumes we already have the other operand on the stack -- result back onto the stack 

To compute and load a value in an array, the stack can be used because there cannot be an indexed addressing mode.

Thus, the instructions were small, and a lot of work was done implicitly with the stack and stack pointer. IIRC transporters actually had a stack of three values, and the compiler (or developers) had to ensure that this was supported.

XMOS now sells the modern "equivalent" and is used by some of the same people.

More than 20 years have passed since I wrote Transputer code, so sorry for being a bit vague.

The UCSD Pascal system used a virtual machine defined by software, which was a stacked machine. The idea was to do something that was portable for new computers, but also easy to write, easy to compile, and wise to work. This virtual machine was defined in Pascal's own dialect. When it was ported to real computers, registers will be used to store the stack pointer and probably some ingenuity in how the top of the stack was processed (using registers) to get reasonable performance.

+6
source

All Articles