SELF-HELP SECTION

Here are some more examples to guide you through understanding the three architectures we have explored so far.

A Quick Example

Suppose you wanted to compute the sum of two operands stored in memory locations A and B, and then store the result in C.

The following code fragments give an example of what commands would be needed for each machine type.

Stack Machine Everything must use the stack!

Push A ; Load the contents of memory location A on the stack
Push B ; Load the contents of memory location B on the stack
ADD ; Add the top two stack locations
Pop C ; Store the result in memory location C

Good Points: Very high code density: only Push and Pop instructions need addresses so every other instruction requires only one or two bytes of code. Machine code uses Reverse Polish Notation which is very easy to compile. No save/restore code needed for procedure calls and returns.

Bad Points: Only operands at the top of the stack are accessible: to access an item buried deep in the stack all items above it must be popped. Stack manipulations (such as swaps) require extra instructions.

Accumulator Everything must use the accumulator (ACC)!

LDA A; Load the contents of memory location A into the ACC
ADD B; Add the contents of B to the ACC
STA C; Return the contents of the ACC to memory location C

Good Points: Minimal internal state. Short fixed-length instructions.

Bad Points: The CPU holds only one variable at a time: extra memory traffic is required to manipulate several variables and/or addresses.

GPR Working in registers is faster!

LW R1, A; Load the contents of memory location A into register R1.
LW R2, B; Load the contents of memory location B into register R2.
ADD R3, R1, R2; Compute sum of R1 and R2 and place in register R3.
SW C, R3; Store the answer in memory location C.

Good Points: Arithmetic doesn't overwrite source operands. Simple fixed-length encoding. Simple code generation model. Similar number of clock pulses per instruction.

Bad Points: Higher instruction count: extra load/store instructions required. Wasted space in arithmetic instructions.

THIS WAY TO THE QUIZ