Background info to understand Special Purpose Registers: Program flow

Background information:   program execution

  • A computer program consists of computer instructions and is stored in memory:

        

  • The CPU will fetch and execute the program instruction one instruction at a time

The current instruction and the next instruction

Instruction execution:

  • The CPU fetches the (computer) instructions in the computer program and execute the (fetched) instructions one at a time

  • When the CPU finishes executing one instruction, the CPU will fetch the another instruction and execute it... and so on


  • The current instruction:

      • Current instruction = the instruction (in memory) that the CPU is currently executing         

  • The next instruction:

      • Next instruction = the instruction (in memory) that the CPU will execute after it finishes executing the current instruction

Program flow

  • Program flow:

      • Program flow = the order in which the program instructions in a computer program are executed by the CPU

  • There are 2 possible locations when the CPU fetches the next instruction:

      1. The next instruction is the instruction that is located immediately after the current instruction

        I.e.:

              current instruction
              next instruction        <--------
        
      2. The next instruction is an instruction that is located some where else

        (I.e.: not right after the current instruction)

In-order or "normal" program flow

The in-order or normal program flow is when:

  • After executing the current instruction in the program, the CPU fetches and executes the instruction that physically follows the current instruction in the program

The normal program flow illustrated with a diagram:

The in-order program flow illustrated using Java
 

Consider the following Java program fragment:

  r = 5.0;              // Current statement

  area = 3.14 * r * r;  // Next statement    
  
 

Suppose the computer is currently executing the statement r = 5.0

After executing the (current) statement r=5.0; in Java program, the next statement that will be executed is the statement area=3.14*r*r which physically follows the current statement

 

This behavior is in-order or "normal" program flow

Out-of-order program flow

The out-of-order program flow is when:

  • After executing an instruction in the program, the CPU fetches and executes a instruction that is not the physically next instruction in the program

The out-of-order program flow illustrated with a diagram:

The out-of-order program flow illustrated using Java

Consider the following Java program fragment:

  if ( 4 > 3 )
  {
     answer = "Greater";   // The current statement
  }
  else
  {
     answer = "Less";      // Physically, the next statement 
  }       

  a = b + c;               // The actual next statement  
  
 

Suppose the computer is currently executing the statement answer = "Greater";

After executing the (current) statement r = 5.0; in Java program, the next statement that will be executed is the statement a = b + c; !!

Physically, the statement answer = "Less"; is located immediately after the current statement !!!

The out-of-order program flow explained in another way
 

Another way to explain the out-of-order program flow is:

  • When a program

      • jumps forward         or      
      • jumps backward

    we have a out-of-order program flow situation

Comments:

  • If-statements will always cause the program to jump forward

  • While-statements will always cause the program to jump backward
 

 

This concludes the background info for understanding the use of Special Purpose Registers...

The use of special purpose registers in the CPU

  • The purpose (use) of the special purpose registers:

      • The purpose of the special purpose registers is to implement the functionality of the CPU

  • Recall the functionality (job) of the CPU:

      1. To fetch the current instruction from the computer memory  

      2. Execute the (fetched) instruction

        (And repeat these steps indefinitely)

  • In other words:

      • The special purpose registers are used to help the CPU perform its task

I will discuss how each special purposed registers is used now...

How is the Instruction Register used ?

The Instruction Register contains the instruction that is currently being executed by the CPU:

 

(What exactly does this mean ???)

How is the Instruction Register used ?

The Instruction Register contains the instruction code (in binary) being executed by the CPU:

 

The IR register uses the binary code to send out control signals to execute the current instruction

How is the Program Counter (PC register) used ?

The PC contains the address (= location) of the next instruction that the CPU will fetch and execute:

In the above figure:   the next instruction that the CPU will fetch and execute is in memory location 44

Note:   we will study the usage of the PC in great detail a later webpage

The Processor Status Register (PSR)

The Processor Status Register (PSR):

  • The Processor Status Register contains the status information of the CPU

    Specifically:

      • When the CPU executes an instruction, the status of the execution will be recorded in the PSR

 

The PSR register contains a (large) number bits (typically 32 or 64)

Example: the PSR of the ARM processor

The relevant bits in the PSR for assembler programming

Recall the PSR of the ARM processor:

 

In assembler programming, we will only use these 4 bits (= "flags" or "conditions") in the PSR:

A flag (bit) is set (= 1) if the corresponding condition is true, otherwise it is reset (= 0).

How are the N,Z,V,C flags in the PSR used in assembler programming ?

  • The N,C,V,Z flags are used to remember (= record) the outcome of a comparison of 2 values

  • When you want to compare 2 value -- e.g.: x and y, you subtract the values: x - y

      • The N,Z,V,C flag values of the result of x - y can tell you which value is larger.

    Example:

        Compare:    x = 4 and y = 5
      
                       4
        Subtract:   -  5
                  --------
                      -1                            

    A negative outcome means: x < y !!!

How do you use the N,Z,V,C flags to check for some common comparison outcomes ?

The following table shows you the settings of the N,C,V,Z flags that represent the commonly used comparison conditions (which are: ==, !=, >, <, >= and <=):

Source: https://azeria-labs.com/arm-data-types-and-registers-part-2/

  • You do not need to remember the flag settings

  • You will learn to use easy to remember assembler instructions later

    The assembler (= compiler) will take care of testing for the appropriate flag settings