Lecture 6: Pipelining and instructions that change the flow of a program

• The idea of PIPELINING is to speed up the operation of the Microcontroller • The next instruction in sequence is fetched whilst the current instruction is being executed • Problems arise when the instruction being executed changes the flow of the program

• Test and branch instructions. These instuctions test a bit in a register and if the result of true then the progam counter is incremented again to skip the next instruction in memory • Call subroutine which saves the current program counter and then load the program counter with the address in the instruction • A Goto which replaces the current program counter value with the address in the instruction

Example Instruction

• When a test is true, the next instruction in sequence that has been fetched during the current instruction is the wrong one • Another fetch is required to bring the program counter + 1 contents and “nops” are executed whilst this happens making the instruction last twice as long • A similar process occurs for CALL and GOTO

• The CALL instruction changes the flow of the program by changing the program counter to the start address of the subroutine contained in the CALL instruction. • But the CALL instruction returns to where it was called from in the program by saving the return address in a special area of memory called a STACK • A STACK is LIFO buffer, i.e. a Last In First Out buffer

Why a LIFO buffer
• A LIFO buffer allows a subroutine to call another subroutine if required • The limit to this process is the number of locations in the STACK area of memory often called the Depth of the STACK • This process of a call within a call is known as nested calls

Nested CALL Example

• INTERRUPTS also change the flow of the program but normally in response to a hardware event • This could be a signal received from outside the microcontroller or from one of the peripherals inside the device itself • The microcontroller saves the address of the next instruction on the stack before changing the program counter to the address of the interrupt routine in memory • This can be fixed or partly supplied by the interrupting device • A special type of return is used to go back to the address saved on the stack

• Different microcontrollers have different interrupt structures but most have a means of enabling individual interrupts and can prevent any interrupt being acted upon • Some processors had what was called a non maskable interrupt which could not be “turned off” in the software • All have flags or bits in a register to say what caused the interrupts (or interrupts) • Some have a means of setting a priority of operation if more than one interrupt occurs at once