SEN2201 · Week 5 · The von Neumann Model (instruction processing)

The von Neumann model and the instruction cycle

The five parts of a computer, the jobs of PC/IR/MAR/MDR and the six phases of an instruction (FETCH, DECODE, EVALUATE ADDRESS, FETCH OPERANDS, EXECUTE, STORE RESULT). With LC-3 examples.

von Neumann modelstored programmemoryprocessing unitALUcontrol unitprogram counter (PC)instruction register (IR)MARMDRinstruction cyclefetchdecodeevaluate addressfetch operandsexecutestore resultword length

In the first four weeks you saw the parts: gates, adder, mux, register, memory. This week we put them together and make a computer. The model described by John von Neumann in 1946 is still the basis of every processor today. On this page you will learn the five parts of the model and how an instruction is processed (the instruction cycle). In the examples we use LC-3 instructions; the details of the instructions come in weeks 7-8.

On the midterm, this topic usually brings short-answer questions: list the five parts, what PC/IR/MAR/MDR hold, order the six phases, write the FETCH steps, what happens after FETCH if PC = x3000.

The von Neumann model

           +-------------------------------+
           |            MEMORY             |
           |        MAR         MDR        |
           +-------------------------------+
                  ^               ^
                  |               |
   INPUT  ---> +------------------------+ ---> OUTPUT
 (keyboard)    |    PROCESSING UNIT     |      (screen)
               |   ALU      TEMP (R0-R7)|
               +------------------------+
                           ^
                           |
               +------------------------+
               |      CONTROL UNIT      |
               |       PC        IR     |
               +------------------------+
PartJobIn the LC-3
MemoryStores the program and data2162^{16} locations × 16 bits, MAR and MDR
Processing unitDoes the calculationALU (ADD, AND, NOT), R0-R7
InputTakes data from the outside worldKeyboard
OutputGives data to the outside worldScreen (monitor)
Control unitManages which step is donePC, IR, the control FSM

Memory: MAR and MDR

These two registers are the only way to access memory.

  • Read: write the address into the MAR, and memory puts the contents of that location into the MDR.
  • Write: write the address into the MAR and the value into the MDR, and give the write signal (WE).

Processing unit

It consists of the ALU (arithmetic and logic unit), which does the operations, and small, fast registers that hold intermediate results. Going to memory is slow; that is why frequently used values are kept in registers. The LC-3 has 8 of them: R0-R7. The number of bits the ALU processes at a time is called the word length (LC-3: 16 bits).

Control unit: PC and IR

  • PC (program counter): the address of the next instruction. Its name says “counter”, but it is really a pointer.
  • IR (instruction register): the instruction currently being processed, itself.

The control unit is an FSM (week 4). It decides which signals to turn on in each clock cycle.

The stored-program concept

The program’s instructions also sit in memory as bits, like any data. The computer is not rewired for a new job; a new program is loaded into memory. This is called the stored-program concept and is the essence of the von Neumann model.

PITFALL · confusing the PC with the IR. The PC holds an address (like x3001), the IR holds an instruction (like x1263). Writing “the PC holds the current instruction” is a classic mistake: after FETCH the PC is already pointing to the next one.

The instruction

An instruction consists of two parts: the opcode (what to do) and the operands (what to do it with). In the LC-3 every instruction is 16 bits, and the opcode is the top 4 bits (IR[15:12]).

x1263 = 0001 001 001 1 00011
        ADD  R1  R1  imm #3      →   ADD R1, R1, #3   (R1 ← R1 + 3)

The instruction cycle

For every instruction, the computer goes through the same six phases in order, then moves on to the next instruction. Without stopping, until HALT.

#PhaseWhat happens?
1FETCHThe instruction is brought from memory to the IR, the PC is incremented
2DECODEThe opcode is decoded: which instruction, where are the operands?
3EVALUATE ADDRESSIf a memory access is needed, the address is computed
4FETCH OPERANDSThe operands are taken from registers or memory
5EXECUTEThe operation is done (the ALU adds, ANDs…)
6STORE RESULTThe result is written to its destination (register or memory)

FETCH in detail

1. MAR ← PC          (send the address to memory)
   PC  ← PC + 1      (the PC is incremented in the same cycle)
2. MDR ← M[MAR]      (memory is read, may take several cycles)
3. IR  ← MDR         (the instruction is in the IR)

If you start with PC = x3000, when FETCH is over PC = x3001, IR = M[x3000]. The PC is incremented before the instruction runs. This detail will be very important in week 7 in PC-relative address calculation: the offset is added to the incremented PC.

DECODE

IR[15:12] enters a 4-to-16 decoder. Exactly one output of the decoder becomes 1: the instruction is determined. The remaining 12 bits are interpreted as different fields depending on the instruction.

Not every instruction uses every phase

InstructionEVALUATE ADDRESSFETCH OPERANDSEXECUTESTORE RESULT
ADD R1, R2, R3noneR2, R3 are readthe ALU addsR1 ← result
LDR R4, R2, #-3R2 + (-3)MDR ← M[address]noneR4 ← MDR
LD R1, LABELPC + offsetMDR ← M[address]noneR1 ← MDR
ST R1, LABELPC + offsetR1 is readnoneM[address] ← R1
BRz LABELPC + offsetnoneif Z = 1 then PC ← addressnone

Worked example

At address x3000 in memory there is LDR R4, R2, #-3, R2 = x4005 and M[x4002] = x00AB.

FETCH            MAR ← x3000, PC ← x3001, MDR ← M[x3000], IR ← MDR
DECODE           opcode 0110 = LDR, DR = R4, BaseR = R2, offset6 = -3
EVALUATE ADDRESS x4005 + (-3) = x4002
FETCH OPERANDS   MAR ← x4002, MDR ← M[x4002] = x00AB
EXECUTE          (none)
STORE RESULT     R4 ← x00AB, condition code: P (positive)
result: R4 = x00AB, PC = x3001

This instruction went to memory twice: once to fetch the instruction, once to read the data. ADD goes once (only the FETCH), LDI three times (instruction, address, data).

Changing the sequential flow

Normally every FETCH increments the PC by one; the program advances line by line. Control instructions change this flow. For example, the BRz instruction writes a new address into the PC in the EXECUTE phase if the last value written was zero. The next FETCH starts from there. Loops and ifs are built this way (week 8).

Clock and HALT

The control unit works with clock cycles. An instruction takes several cycles (memory access is usually the slowest part). If the clock stops, the computer stops. In the LC-3, the HALT (TRAP x25) instruction shuts off the clock signal by calling a routine of the operating system.

Common mistakes

Wrong ideaThe truth
The PC holds the current instructionThe PC is the address of the next instruction; the instruction is in the IR
The PC is incremented after the instruction finishesIn FETCH, before the instruction runs
The MAR holds the dataMAR is the address, MDR is the data
Every instruction uses all six phasesADD has no EVALUATE ADDRESS, LD has no EXECUTE
Phases: FETCH, EXECUTE, DECODE …FETCH, DECODE, EVALUATE ADDRESS, FETCH OPERANDS, EXECUTE, STORE RESULT
The program and the data are in separate memoriesStored program: the same memory
x30FF + 1 = x3000x3100
A branch happens in FETCHThe PC is changed in EXECUTE

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

Name the five components of the von Neumann model.

Show answer
Memory, processing unit, input, output, control unit

Memory (with MAR and MDR), processing unit (ALU and temporary registers), input (keyboard), output (screen) and control unit (PC and IR). The program and the data sit in the same memory.

Question 2easy

What does the program counter (PC) contain? What does the instruction register (IR) contain?

Show answer
PC: address of the next instruction; IR: the instruction being processed

The PC holds an address: the place in memory of the next instruction. The IR holds the instruction itself (the 16-bit machine code). The name PC is misleading; it is not a counter but a pointer (instruction pointer).

Question 3easy

Which register holds the address for a memory access, and which holds the data being read or written?

Show answer
MAR holds the address, MDR holds the data

To read memory, the address is written into the MAR (memory address register), and memory puts the value read into the MDR (memory data register). To write, the address is put in the MAR, the data in the MDR, and the write signal is given.

Question 4easy

List the six phases of the instruction cycle in order.

Show answer
FETCH, DECODE, EVALUATE ADDRESS, FETCH OPERANDS, EXECUTE, STORE RESULT

Every instruction follows this order. But not every instruction uses every phase: ADD does not compute a memory address, and the EXECUTE phase of LD is empty.

Question 5medium

Write the register transfers of the FETCH phase.

Show answer
MAR ← PC, PC ← PC + 1, MDR ← M[MAR], IR ← MDR

First the address in the PC goes to the MAR, and at the same time the PC is incremented by one. Memory is read, and the instruction arrives in the MDR. In the last step the instruction in the MDR is copied to the IR. The PC is incremented BEFORE the instruction runs.

Question 6easy

Before the FETCH phase, PC = x30FF. What is the PC after FETCH?

Show answer
x3100

During FETCH, PC ← PC + 1. x30FF + 1 = x3100 (in hex F + 1 = 10, the carry goes left). The PC is incremented first whatever the instruction is; a branch instruction can change it later in EXECUTE.

Question 7medium

Which phase does ADD R1, R2, R3 not need?

Show answer
EVALUATE ADDRESS

The operands of ADD are in registers. No memory address is computed. In FETCH OPERANDS R2 and R3 are read, in EXECUTE the ALU adds, and in STORE RESULT the result is written to R1.

Question 8medium

LDR R4, R2, #-3 is executed with R2 = x4005. What address is computed in the EVALUATE ADDRESS phase, and in which phase is memory read?

Show answer
x4002; FETCH OPERANDS

EVALUATE ADDRESS: R2 + (-3) = x4005 - 3 = x4002. FETCH OPERANDS: MAR ← x4002, MDR ← M[x4002]. STORE RESULT: R4 ← MDR. The EXECUTE phase is empty in this instruction.

Question 9easy

What is meant by the stored-program concept?

Show answer
Instructions and data are stored in the same memory

In the von Neumann model the program is data too: instructions sit in memory as bits and are read like data. The computer is not rewired for a new job; a new program is loaded into memory.

Question 10medium

How does a branch instruction change the sequential flow of a program, and in which phase?

Show answer
It loads a new address into the PC during EXECUTE

Normally FETCH increments the PC by one and the program moves on to the next instruction. A branch (BR, JMP) writes a new address into the PC in the EXECUTE phase. The next FETCH starts from this address.

Question 11hard

How many memory accesses (including the instruction fetch) does each LC-3 instruction need: ADD, LDR, LDI?

Show answer
1, 2, 3

ADD: only the FETCH (reading the instruction). LDR: FETCH + reading the data = 2. LDI: FETCH + reading the address from memory + reading the data at that address = 3. That is why LDI is slower.

Question 12hard

In the LC-3, how wide are the MAR and the MDR? Explain why.

Show answer
Both 16 bits

The MAR holds an address: the LC-3's address space is 2162^{16}, so the address is 16 bits. The MDR holds the contents of a memory location: the addressability is 16 bits. They are 16 bits for different reasons; on another machine they could differ (for example a 32-bit address, 8-bit addressability).

Verification: The PC increment, the address calculations and the number of memory accesses of each instruction were measured by running instruction by instruction on an LC-3 simulator we wrote ourselves (verify/SEN2201/lc3.mjs) (verify/SEN2201/05.mjs).

Review card

von Neumann 5 parts: Memory (MAR, MDR) | Processing unit (ALU, registers) | Input | Output | Control unit (PC, IR) Stored program: instructions and data in the SAME memory PC = the ADDRESS of the next instruction | IR = the CURRENT instruction ITSELF MAR = address to access | MDR = data read/to be written Word length: bits the ALU processes at once (LC-3: 16) 6 phases: FETCH > DECODE > EVALUATE ADDRESS > FETCH OPERANDS > EXECUTE > STORE RESULT FETCH: MAR<-PC, PC<-PC+1, MDR<-M[MAR], IR<-MDR (the PC is incremented first!) DECODE: IR[15:12] opcode -> 4-to-16 decoder ADD (register): no EVALUATE ADDRESS LDR: EA=base+offset | FO: MDR<-M[EA] | SR: DR<-MDR Branch: new address into the PC in EXECUTE Memory accesses: ADD 1, LD/LDR 2, LDI 3 The control unit is an FSM; each phase takes one or more clock cycles