2
$\begingroup$

With regards to introductory (beginner) Von Neumann computer architecture, how does a program change the order in which instructions are executed?

I know the control unit is responsible for retrieving the instruction from memory and then decodes and executes the instruction. Their connected by a bus. The control unit and ALU are together in the CPU.

I'm guessing different lines of code are stored in different addresses in the RAM?

Is it a easy question, and I'm just thinking its difficult?

$\endgroup$

2 Answers 2

4
$\begingroup$

You are getting into to much detail. You can think of a CPU as some kind of automaton which operates roughly as follows (ignoring pipelining). There is a register called (in x86) IP (Instruction Pointer). The CPU operates an infinite loop (ignoring interrupts):

  1. Read the instruction $\iota$ at address IP; determine its length $\ell$, and increment IP by $\ell$.

  2. Execute $\iota$; this might change IP.

  3. Go back to 1.

This is implemented in some sophisticated way, but if you don't care about performance, it performs as if the CPU was actually running this infinite loop. Control instructions change IP and so control which instructions are executed.

Different programs are just collections of instructions. When you run a program, the operating system loads it into memory and then jumps to the first instruction (i.e., sets IP to the address of the first instruction). From there on, the program executes until it terminates and releases the token back to the operating system. (Modern operating systems, which are multitasking, are more complicated, but DOS ran much according to this description, ignoring interrupts.)

$\endgroup$
4
  • $\begingroup$ So the answer would simply be a program changes the order of the instructions that the CPU or RAM loads into memory? $\endgroup$
    – David G
    Commented Apr 15, 2014 at 21:36
  • $\begingroup$ Even without changing the usual order of execution, the program itself consists of a sequence of instructions, and these are loaded from memory by the CPU and executed. $\endgroup$ Commented Apr 15, 2014 at 21:39
  • $\begingroup$ Oh ok I think I'm starting to understand. So the program is the entity creating the order of execution. Different programs load different instructions from memory and are executed accordingly by the CPU. Would I be correct? $\endgroup$
    – David G
    Commented Apr 15, 2014 at 21:43
  • $\begingroup$ That's exactly right. $\endgroup$ Commented Apr 15, 2014 at 22:15
1
$\begingroup$

Memory in a computer can be thought of as one long string. You have position 1, position 2, position 3, etc.

Let's start with the simple case where every instruction occupies exactly one position in memory. Then the normal process for the computer is to execute the instruction at position 1, then to execute the instruction at position 2, then execute the instruction at position 3, etc.

Some instructions change this sequence. The simplest is a "jump" or "go to". This tells the computer that the next instruction to execute is not the one with the next higher position number, but some other instruction. So if at position 100 we have the instruction "JUMP TO 200", then instead of going to 101 next we'll go to 200.

Technically, there's a special register called the "instruction pointer" that says what the next instruction is to execute. Normally, after executing each instruction, the computer automatically adds 1 to this. A jump instruction replaces the value instead of doing the automatic add. (Some computers have relative jumps that add or subtract from the instruction pointer rather than replacing it, like JUMP +10 means add 10 to the IP.)

On many, probably most, modern computers instructions do not all have the same length. But this just adds a minor complexity. If an instruction takes 3 positions, then after executing it we add 3 to the instruction pointer instead of 1.

There can be additional complexities. Like "interrupts" can jump you out of the normal sequence. For example, on some computers, when a key is pressed this generates an interrupt, which takes the computer to a function that processes the keystroke. The current IP is saved somewhere, and when the interrupt processing is complete, we restore the IP and resume what we were doing before.

$\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.