Skip to main content
Engineering LibreTexts

5.3: The Program Counter ($pc) register and calling a subprogram

  • Page ID
    27122
  • 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.

    Figure 5-1: $pc when program execution starts

    Screen Shot 2020-06-29 at 9.07.18 PM.png

    Figure 5-2: $pc after the execution of the first instruction

    Screen Shot 2020-06-29 at 9.07.58 PM.png

    Figure 5-3: Just before calling PrintNewLine subprogram

    Screen Shot 2020-06-29 at 9.08.59 PM.png

    Figure 5-4: Program has transferred control into the PrintNewLine subprogram

    Screen Shot 2020-06-29 at 9.10.15 PM.png

    • Was this article helpful?