0
$\begingroup$

I know that the CPU has a program counter which takes instructions that are required to execute a program, from the memory, one by one. I also know that once the first instruction is executed, the program counter automatically increments by 1 and accesses the data in the cell with the corresponding address.

Now my question is, what if the next instruction in the memory cell, i.e. the instruction in the memory cell after incrementing the counter by 1 is not the required instruction to execute the given program? And what if the next instruction that is required is, in a memory cell who's address can be got to only after 'n' increments?

If it helps I used this video as a reference

EDIT: If it isn't clear, what I'm trying to ask is this: Suppose to execute a given program I need 5 instructions- A,B,C,D,E.

Now suppose instruction A is loaded in a memory cell with address 0000H. So when the program counter reads 0000, it takes the instruction from 0000H, and when the counter reads 0001, it takes the memory from 0001H and so on. Now what if instruction C is in 0007? After 0001H the program counter would increment to 0002. But i don't need the instruction from 0002H. I need it from 0007. So what does the counter do in such a situation?

$\endgroup$
  • $\begingroup$ That video covers jumps also, at least the "write target address to PC" portion of it $\endgroup$ – harold Apr 20 at 12:18
  • $\begingroup$ @harold the binary number in the program counter accesses a memory who's address is same as that of the binary number in the counter. So if the program counter is always incrementing by 1, how it can jump to a new instruction? $\endgroup$ – noorav Apr 20 at 12:56
2
$\begingroup$

Code isn't arranged randomly in memory. The next instruction to be executed will, by default, be the one at the next memory location, unless something specific (a "jump" instruction) is done to execute code from somewhere else.

So, if your program is literally "Do A, then B, then C, then D, then E", the compiler will place those instructions in consecutive memory cells. If, for some reason, they were in non-consecutive memory cells, the program would have to become something like "Do A, then jump to B's location, then do B, then jump to C's location, etc." The computer has no way of knowing that it's not supposed to execute the next instruction in memory, except being told to do that. And being told to do that requires the execution of some kind of instruction.

$\endgroup$
1
$\begingroup$

Your assumption that the PC always increases by 1 (or to the following instruction) is inaccurate.

There are instructions that cause the PC to increase / decrease / change to a specified location. These are usually called JUMP instructions, but also include (conditional) branches, loops, (procedure) calls/return, and interrupt call/return. This is how you can control the flow of your program.

$\endgroup$
0
$\begingroup$

There are two types of processors:

  • most ones just increase program counter by 1 each time. Should it be changed other way, the special instruction is placed which tells processor address of the next instruction
  • other ones just contain in each instruction address of the next instruction. This makes code larger, but you have advantage of having to execute less commands and therefore be a little faster. F.e. such approach usually used by microcode
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.