The fetch-decode-execute cycle is the series of steps that a computer carries out when it runs a program.
Fetch: The control unit fetches the next instruction from memory (this data comes through von Neumann bottleneck) and updates the program counter to determine where the next instruction is located. We first have to fetch an instruction from memory and place it into the IR.
Decode: Translate the instruction into signals that the ALU can interpret and execute (it is decoded to determine what needs to be done next).
Execute: This takes two steps.
Load any required data into the CPU registers.
If a memory value is involved in the operation, it is retrieved and placed into the MBR.
Execute the instruction, placing the results in registers or memory.
All computers provide a way of interrupting the FDE cycle, this is through the use of interrupts.
Interrupts occur when:
A user break (e.g. CTRL + C) is issued.
I/O is required by the user or a program.
A critical error occurs.
Interrupt
An interrupt allows us to alter the normal flow of execution of a program when an event of higher priority occurs. They can be triggered by different sources:
I/O requests
Arithmetic errors (e.g. div 0)
Encountering invalid instructions
Each interrupt is associated with a procedure (Interrupt Service Routine) that directs the actions of the CPU to handle the events.
For general-purpose computers, it is common to disable all interrupts during the time in which an interrupt is being processed. Typically, this is achieved by setting a bit in the Status (register).
Mnemonic instructions, such as Load Y, are easy for humans to write and understand but can’t be interpreted by computers.
Assembler
Assemblers translate mnemonic instructions into **machine language** that can be executed by computers.
An object program file is created from mnemonic source code in two passes:
?
1st pass: the assembler assembles as much of the program as possible it can while it builds a symbol table that contains memory references for all symbols in the program.
2nd pass: the instructions are completed using the values from the symbol table.
The AddI instruction specifies the address of the address of the operand to be added.
It is implemented with RTL as follows:
MARMBRMARMBRAC←X←M[MAR]←MBR←M[MAR]←AC+MBR
JnS
Jump-and-store, JnS, gives us limited functionality which allows us to use simple subroutines.
MBRMARM[MAR]MBRACACPC←PC←X←MBR←X←1←AC+MBR←AC
JumpI
Jump to instruction specified by the address of the operand, JumpI, which can let us implement “return” label as JnS hence jumping back to where we were earlier.
Below are common assembly equivalents of constructs.
Conditional Evaluation
; X=4, Y=5; if (X-Y == 0); X+Y; stopIf, Load X Subt Y Skipcond 400 Jump EndIfThen, Load X Add YEndIf, HaltX, DEC 4Y, DEC 5
If, then, else
; X=4, Y=5; if (X-Y == 0) {; X+Y; } else {; X+X; }; stopIf, Load X Subt Y Skipcond 400 Jump ElseThen, Load X Add Y Jump EndIfElse, Load X Add XEndIf, HaltX, DEC 4Y, DEC 5
Definite iteration
; for (i=0; i<10; i++) {; output i; }; stop Clear Store CounterLoop, Load Counter Subt Limit Skipcond 000 Jump Next Output Load Counter Add One Store Counter Jump LoopNext, HaltCounter, DEC 0Limit, DEC 10One, DEC 1
Indefinite iteration
; while (x<0) {; output x; }; stopTest, Load X Skipcond 000 Jump Next Output Jump TestNext, Halt