In computing, a virtual machine (VM) is an emulation of a computer system.
Virtual machines are based on computer architectures and provide functionality
of a physical computer.
Virtual Machines - Wikipedia
system virtual machines - provide substitute for a real machine
process virtual machines - allow executing of computer programs in a platform
independent environment
We will be looking only at the second type of virtual machines and VM will
mean that for us.
Virtual Machines
Implement a computational model
Just like real machines AKA computers
run on top of the real machine
you can have a hardware implementation of VM
Computational model
Regular Languages
Context Free Languages
Everything that is computable
Computational model
Regular Expressions
Pushdown automata
Turing machines
Turing machine
has infinite tape with cells
each cells can hold one of finite number of values
head that can read, write and move to the left and right
finite set of states, one designated as starting state
finite program that given (state, value) return the new (state, action, value)
Turing machine operation
starts with the initial state
Read the value at the head position
look up (state, value) in the table - if missing HALT
Set state to new state, write new value at the head position and move the
head left or right
Go to 2
From theory to practice
Infinite tapes are expensive
Not great user experience to interact via checking the cells on the tape
Still computers and VMs are an implementations of Turing machines.
The tape is not infinite :/
less powerful
we have to reuse the tape
What is inside a machine
Processor - implements the machine running algorithm
reads, writes to the tape, moves the head and changes the current state
Memory - the tape, consists of cells of variable sizes filled with 0 and 1
almost always random accessible
Program - the state transition function/table
resides in the memory - von Neumann architecture
Program Counter (PC) - the current state
How the machine works?
Reads the instruction at PC
Decodes the instruction at PC
Modifies the memory according to the instruction
Moves PC to the next instruction
Go to 1
Instructions
CISC - Complex Instruction Set Computer
one instruction can do a lot of operations
RISC - Reduced Instruction Set Computer
Register Memory - most operations can use both - registers and memory
addresses
Register Register - most operations take place only between registers and
there are dedicated instructions that allow loading and storing data in memory
Instructions
x86 - CISC, Register Memory
the CPU actually runs microcode that is RISC …
ARM - RISC, Register Register
Instructions
transfer data between registers and memory
add, substract, multiply, divide values
boolean operations on values
change the PC
call a subroutine
Subroutines
not required by Turing machines
but very useful, that are part of every machine
Stack for storing return addresses (at least)
Stack Pointer (SP)
“Real” vs Virtual Machine
the “real” machine is implemented in the hardware
the operations of the machine are mapped 1:1 to the hardware actions
these statements used to be true, not any more
Virtual machines
Stack based
Register based
Stack based VM
all operations work with arguments on the stack
operands are popped from the stack
the result is pushed on the stack
read // from input to top of stack
dup
loop:
push 1
sub
dup
push 0
jmpeq end
swap
dup -1
add
swap
jmp loop
end:
pop
print // pop result and print
the instructions are smaller and the code is compact