CPU におけるプログラムカウンタの説明はどれか。
平成24年度 春期 ITパスポート試験 問61
This question's explanation and your answers so far.
問 61
- ア
正解 The correct answer is ア. The program counter (also called the instruction address register or the sequential control counter) is a register inside the control unit of the CPU, and it holds the main memory address where the next instruction to be executed is stored. The CPU repeats a cycle of fetching an instruction, decoding it, and executing it, and in the very first step it goes to main memory to read the instruction from the address that the program counter points to. The fetched instruction is moved into the instruction register, and at the same time the value of the program counter is automatically updated so that it points to the next instruction (it is advanced by the length of the instruction). Thanks to this automatic update, a program is executed one instruction after another from top to bottom, in the order it was written. In the case of a branch instruction (a jump) or a subroutine call, this automatic update does not happen; instead the address of the branch destination is set into the program counter, and execution continues from there. In other words, the program counter is an arrow that points to where the CPU will execute next, and its content is neither the instruction itself nor a count, but always an address (a location) in main memory. Therefore ア is the correct description.
Incorrectイ
It is not the program counter that holds the size of the main memory area required to run a program. How much memory area to reserve in order to run a program is information that the memory management function of the OS manages when the program is loaded; it is not something that a register inside the CPU holds at all times. In addition, what the program counter holds is an address (a location), not a size, so the kind of thing being pointed to is different.
Incorrectウ
There is no register in a CPU that holds the number of instructions making up a program. Because the name program counter contains the word counter, it is easy to misunderstand it as counting how many instructions there are, but what it actually counts up is not the number of instructions that have been executed; it is the position of the next address to be read. Its value is updated as an address, and if a branch occurs it can even go back to an earlier address, so it never becomes a number that expresses a count of instructions.
Incorrectエ
Holding the main memory address where the data (the operand) required for executing an instruction is stored is the role of registers involved in address calculation, such as the address register, the index register, and the base register. Data is read and written using the effective address obtained from the address part of the instruction word. What the program counter points to is not data but the location where the instruction itself is placed, and on the point of whether the target is data or an instruction this choice is wrong. This is a classic trap that is easy to confuse, so it is worth keeping the distinction clear.
- The program counter (instruction address register) is the register that holds the main memory address where the next instruction to be executed is stored. Each time an instruction is fetched it is automatically updated to point to the next instruction, and for a branch instruction the address of the branch destination is set into it. The key point is that what it holds is neither a count of instructions nor the size of an area, but an address.
- The division of roles among the main registers inside a CPU: the program counter (the address of the next instruction), the instruction register (the fetched instruction itself), the address register and the index register (the main memory address of data, and effective address calculation), and the accumulator and general-purpose registers (the data to be operated on and the results of operations). Not mixing up the address of an instruction with the address of data is what decides whether you get the point.