----------------- | ------------- | | | PC | | ---------- | |-----------| | | | | | Registers |===BUS== Memory | | |-----------| | | | | | ALU | | ---------- | ------------- | -----------------Components:
All memory accesses must be done sequentially under control of the CPU. The speed of the CPU, memory, and bus dictate the maximum rate at which the CPU can process instructions.
The main purpose of executing a program is to change the state of the
system (values in memory and registers) until some final state is reached.
This is done by assigning and
This re-assignment introduces temporal dependencies between instructions. One instruction can't be executed until a previous dependent instruction has finished---a register or memory location can be updated (overwritten) only when the value of that register or memory location is no longer needed, or is no longer valid within the context of the program.
Modern CPUs and compilers are able to extract
Functions (including recursive functions) can be computed on the von
Neumann architecture by introduction of a stack, which accumulates a
It turns out that the stack is also powerful enough to be able to replace the set of general purpose registers in a CPU. Consider the line of calculators manufactured by HP: these all use stack processing, often referred to as ``reverse Polish notation,'' or RPN.
To compute on a stack machine (or calculator), arguments to an operator are pushed on the stack, then the operator is applied to the arguments. The result of the operation is left on the top of the stack. Here is an example:
The expression we wish to calculate is: 10 + 20.
The RPN expression is: 10 20 +.
The stack instructions are:
pushc 10 ; push constant value 10
pushc 20 ; push 20
add ; add the result
(NOTE: draw a stack and trace the execution of these instructions)
Consider two other expressions: (((a+b)+c)+d) [left-associative addition]
and: (a+(b+(c+d))) [right-associative addition]
The instructions for the first expression are:
push a ; push value in memory location a
push b
add
push c
add
push d
add
while the instructions for the second expression are:
push a
push b
push c
push d
add
add
add
The effect of right associativity is to require a larger stack.
Some other useful stack instructions are:
pop x ; pop top element from stack, place in memory at location x
mul
cmp ; compare top 2 elements, set flag for <, =, >
br l ; branch unconditionally to l (location or label)
be l
bl l
bg l
Stack ``machines'' are widely used, though, but not in hardware form. A stack computer can easily be emulated in software (a machine emulator is usually called a virtual machine, or VM). The programming language Forth is based on the notion of stack computation. The PostScript document language is a functional language which ``runs'' on a stack VM, which itself is often stored in ROM and run on a RISC CPU inside a laser printer.
A VM is used most often when you would like to implement one or another
programming language. If the programming language is very machine-specific,
then there is little benefit to using a VM. On the other hand, if you would
like your language to run on
Portability is not the only benefit to using a VM. Others are:
Recall the reason why CPUs have registers in the first place: to have as much high-speed memory as close to the CPU as possible. It's not that programming with registers is easier than without; it's that it makes your program run faster to use registers whenever possible.
But does a set of registers in a VM (an
In a real CPU, the philosophy is that more registers are better, but the upper bound is limited by the amount of space available on the chip.
In a VM, there is no such limitation, so the number of potential registers
is virtually unbound. The primary consideration here, though, is
In fact, it can be argued that the VM registers contend with the other
``normal'' memory locations for space in the real CPU's caches and registers.
The more registers there are, the more contention there will be. Therefore,
it may actually be beneficial to have