INDIAN INSTITUTE OF TECHNOLOGY ROORKEE
Understanding Program Counter
•
Sparsh Mittal
•
An example of add instruction (R-format)
funct7 rs2 rs1 funct3 rd opcode
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits
add x9,x20,x21
0 21 20 0 9 51
0000000 10101 10100 000 01001 0110011
0000 0001 0101 1010 0000 0100 1011 0011two
This slide shows how in RISC-V, each instruction takes 32 bits (4 bytes).
Program counter (PC) and how it
changes through program execution.
Example (RV32):
Memory Address: Instruction Stored:
0x00000000: addi x10 x0 6
Program counter:
0x00000004: addi x11 x0 10
0x00000000
0x00000008: add x13 x10 x11
0x0000000c: sub x14 x11 x10
Program counter (PC) and how it
changes through program execution.
Example (RV32):
Memory Address: Instruction Stored:
0x00000000: addi x10 x0 6
Program counter:
0x00000004: addi x11 x0 10
0x00000004
0x00000008: add x13 x10 x11
0x0000000c: sub x14 x11 x10
Program counter (PC) and how it
changes through program execution.
Example (RV32):
Memory Address: Instruction Stored:
0x00000000: addi x10 x0 6
Program counter:
0x00000004: addi x11 x0 10
0x00000008
0x00000008: add x13 x10 x11
0x0000000c: sub x14 x11 x10
Program counter (PC) and how it
changes through program execution.
Example (RV32):
Memory Address: Instruction Stored:
0x00000000: addi x10 x0 6
Program counter:
0x00000004: addi x11 x0 10
0x0000000c
0x00000008: add x13 x10 x11
0x0000000c: sub x14 x11 x10
Program counter (PC) and how it
changes through program execution.
Example containing a function call (RV32):
Label: Memory Address: Instruction Stored: At the beginning, PC will be
initialized to the first location of the
_start: 0x00000000: li a0 0x00000006 program that is 0x0000000 as in
the previous example.
0x00000004: li a1 0x0000000a
The increments to the PC will
0x00000008: jal add_func follow the sequence, unless we
encounter a control-flow changing
0x0000000c: j exit instruction, such as ‘Unconditional
Jump’ and ‘Conditional Jump’
add_func: 0x00000010: add a3 a0 a1 instructions.
These instructions will change the
PC contents to different locations.
0x00000014: ret
exit: 0x00000018: nop
Program counter (PC) and how it
changes through program execution.
Example containing a function call (RV32):
Label: Memory Address: Instruction Stored:
_start: 0x00000000: li a0 0x00000006
Program Counter:
0x00000004: li a1 0x0000000a
0x00000000
0x00000008: jal add_func
0x0000000c: j exit Return Address:
add_func: 0x00000010: add a3 a0 a1
0x00000000
0x00000014: ret
exit: 0x00000018: nop
Program counter (PC) and how it
changes through program execution.
Example containing a function call (RV32):
Label: Memory Address: Instruction Stored:
_start: 0x00000000: li a0 0x00000006
Program Counter:
0x00000004: li a1 0x0000000a
0x00000004
0x00000008: jal add_func
0x0000000c: j exit Return Address:
add_func: 0x00000010: add a3 a0 a1
0x00000000
0x00000014: ret
exit: 0x00000018: nop
Program counter (PC) and how it
changes through program execution.
Example containing a function call (RV32):
Label: Memory Address: Instruction Stored:
_start: 0x00000000: li a0 0x00000006
Program Counter:
0x00000004: li a1 0x0000000a
0x00000008
0x00000008: jal add_func
0x0000000c: j exit Return Address:
add_func: 0x00000010: add a3 a0 a1
0x00000000
0x00000014: ret
exit: 0x00000018: nop
Program counter (PC) and how it
changes through program execution.
Example containing a function call (RV32):
Label: Memory Address: Instruction Stored:
_start: 0x00000000: li a0 0x00000006
Program Counter:
0x00000004: li a1 0x0000000a
0x00000010
0x00000008: jal add_func
0x0000000c: j exit Return Address:
add_func: 0x00000010: add a3 a0 a1
0x0000000c
0x00000014: ret
exit: 0x00000018: nop
Program counter (PC) and how it
changes through program execution.
Example containing a function call (RV32):
Label: Memory Address: Instruction Stored:
_start: 0x00000000: li a0 0x00000006
Program Counter:
0x00000004: li a1 0x0000000a
0x00000014
0x00000008: jal add_func
0x0000000c: j exit Return Address:
add_func: 0x00000010: add a3 a0 a1
0x0000000c
0x00000014: ret
exit: 0x00000018: nop
Program counter (PC) and how it
changes through program execution.
Example containing a function call (RV32):
Label: Memory Address: Instruction Stored:
_start: 0x00000000: li a0 0x00000006
Program Counter:
0x00000004: li a1 0x0000000a
0x0000000c
0x00000008: jal add_func
0x0000000c: j exit Return Address:
add_func: 0x00000010: add a3 a0 a1
0x0000000c
0x00000014: ret
exit: 0x00000018: nop
Program counter (PC) and how it
changes through program execution.
Example containing a function call (RV32):
Label: Memory Address: Instruction Stored:
_start: 0x00000000: li a0 0x00000006
Program Counter:
0x00000004: li a1 0x0000000a
0x00000018
0x00000008: jal add_func
0x0000000c: j exit Return Address:
add_func: 0x00000010: add a3 a0 a1
0x0000000c
0x00000014: ret
exit: 0x00000018: nop