HiveBrain v1.2.0
Get Started
← Back to all entries
patternModerate

What does the program counter do?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
thewhatprogramdoescounter

Problem

I was browsing the architecture of a simple CPU but there is one thing a cannot understand. Why is there data from the ALU going to the program counter and what is that data for?

Solution

Since this is a simple CPU, it's almost certainly to compute branch targets. Branch instructions typically represent the location to branch to as a small signed number to add to the program counter. The reason for this is that conditional branches almost always occur within a procedure or function, so it makes sense to encode them as an offset, which uses fewer bits.

In "real" CPUs, another common use case is to support position-independent code.

Context

StackExchange Computer Science Q#149687, answer score: 11

Revisions (0)

No revisions yet.