5.3: The Program Counter ($pc) register and calling a subprogram
- Page ID
One of the most important registers in the CPU is the
$pc register. Note that it is not one of the 32 general purpose registers that the programmer can access directly. It is a special register that keeps track of the address in memory of the next instruction to be executed. For example, consider Program5-2. Compile this program and bring up the MARS execution screen, as shown in the Figure5.1. The address column shows the memory address where each instruction is stored. Note that the first line of the program, which is highlighted in yellow because it is the next instruction to execute, is at 0x0040000, and the $pc register contains the value 0x0040000. Step to the next instruction, as in Figure 5.2, and you will see that the
$pc contains the value 00x00400004, which is the address of the next instruction to execute. Thus the
$pc specifies the next instruction to be executed.
The screen shot in Figure5.3 is at the
jal PrintNewLine instruction. Now the PC points to 0x0040001c, and the next statement would be 0x00400020. However note that tranlsation of the
jal PrintNewLine instruction in the basic column has the address 0x00400040 in it. This address is the first line in the
PrintNewLine subprogram. When the
jal instruction is executed, the
$pc register is set to 0x00400040, and the program continues by executing in the
PrintNewLine subprogram, as shown in Figure5.4. This is how the
jal operator transfer the control of the computer to run the subprogram.