What Is a Program Counter?
The program counter is the place where the address of the next instruction is stored.
- Chinese name
- Program counter
- Foreign name
- Program Counter
- Foreign language abbreviation
- PC
- The program counter is the place where the address of the next instruction is stored.
- When an instruction is executed, it needs to first access the instruction from the instruction address into the instruction register according to the instruction address stored in the PC. At the same time, the address in the PC is either incremented automatically or the address of the next instruction given by the branch pointer. After that, the instruction is analyzed and executed. The execution of the first instruction is completed, and then the address of the second instruction is fetched according to the PC, and so on, each instruction is executed.
Program Counter Introduction
- A program counter is a register in a computer processor that contains the address (location) of the instruction currently being executed. As each instruction is fetched, the program counter's memory address is incremented by one. After each instruction is fetched, the program counter points to the next instruction in the sequence. When the computer restarts or resets, the program counter usually returns to [1] zero.
- One of the main contents of von Neumann's computer architecture is "program pre-storage, computer automatic execution"! The program (instruction sequence) to be executed by the processor is pre-stored in the memory of the computer in the form of a binary code sequence. The processor fetches these codes one by one into the processor and then decodes and executes them to complete the execution of the entire program. In order to ensure the continuous execution of the program, the CPU must have some means to determine the address of the next fetch instruction. The program counter (PC) does exactly this, so it is often called the 'instruction counter'.
- Before the program starts to execute, the starting address of the program instruction sequence, that is, the address of the memory unit where the first instruction of the program is located, is sent to the PC, and the CPU reads the first instruction (instruction fetch) from the memory according to the instruction of the PC. When the instruction is executed, the CPU automatically modifies the content of the PC, that is, each time the instruction is executed, the PC adds an amount equal to the number of bytes (instruction bytes) contained in the instruction, so that the PC always points to the next instruction to be fetched. Instruction address. Since most instructions are executed sequentially, the process of modifying a PC is usually simply a "instruction byte count" of the PC.
When the program is transferred, the final result of the execution of the transfer instruction is to change the value of the PC, which is the destination address of the transfer. The processor always fetches, decodes, and executes in accordance with the PC's direction to achieve program transfer.
The ARM processor uses R15 as the PC, which always points to the fetch unit, and there is only one PC register in the ARM processor, which is shared by all modes. R15 has a 32-bit width (the following mark is R15 [31: 0], which means the '31st bit' to '0th bit' of R15), the ARM processor can directly address a 4GB address space (2 ^ 32 = 4G ). [2]
Program counter features
- To ensure that the program (understood as a process in the operating system) can continue to execute, the processor must have some means to determine the address of the next instruction. The program counter does exactly this, so it is often called the instruction counter. Before the program starts to execute, it must send its start address, which is the address of the memory unit where the first instruction of the program is located, to the program counter, so the content of the program counter is the address of an instruction fetched from memory. When the instruction is executed, the processor will automatically modify the contents of the PC, that is, each time the instruction is executed, the PC adds an amount equal to the number of bytes contained in the instruction, so that it always holds the address of the next instruction to be executed . Since most instructions are executed sequentially, the modification process is usually simply a 1 increment to the PC. [3]
- However, when a branch instruction such as a JMP (Jump, Foreign Language: JUMP) instruction is encountered, the address of the subsequent instruction (ie, the content of the PC) must be obtained from the address field in the instruction register. In this case, the next instruction fetched from memory will be specified by the branch instruction, rather than fetched sequentially as usual. Therefore, the structure of the program counter should be a structure with two functions of registering information and counting. [3]