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$
2
  • $\begingroup$ That video covers jumps also, at least the "write target address to PC" portion of it $\endgroup$
    – user555045
    Commented Apr 20, 2019 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$
    – penguin99
    Commented Apr 20, 2019 at 12:56

4 Answers 4

3
$\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$
2
$\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$
0
$\begingroup$

After the instruction at address 0001 is read, the program counter increments to 0002. So, the next instruction will be at address 0002 in RAM. Now, if instruction C needs be executed after B, the instruction at address 0002 can contain the opcode for the JUMP command with the address of instruction C (0007 in this case) as the operand. After this, the program counter will be set to the address of the C instruction and it will be fetched and executed.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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