RUNNING A PROGRAM
SOFTWARE PROGRAMS ARE SETS OF INSTRUCTIONS. FOR A CPU TO EXECUTE THESE
INSTRUCTIONS, EACH ONE MUST FIRST BE TRANSLATED INTO MACHINE CODE –
SIMPLE BINARY CODES THAT ACTIVATE PARTS OF THE CPU.
THE CPU ONLY PERFORMS A FEW BASIC FUNCTIONS:
• PERFORMING MATHEMATICAL OPERATIONS LIKE ADDITION AND SUBTRACTION
• MOVING DATA FROM ONE MEMORY LOCATION TO ANOTHER
• MAKING DECISIONS AND JUMPS TO A NEW SET OF INSTRUCTIONS BASED ON THOSE
DECISIONS
A PIECE OF SOFTWARE, SUCH AS A GAME OR WEB BROWSER, COMBINES THESE FUNCTIONS
TO PERFORM MORE COMPLEX TASKS.
ARITHMETIC LOGIC UNIT (ALU)
THE ALU CARRIES OUT CALCULATIONS AND MAKES DECISIONS ON THE DATA SENT TO THE PROCESSOR.
CONTROL UNIT (CU)
THE CU, WHICH IS ALSO CALLED THE CONTROLLER, CONTROLS DATA MOVING THROUGH THE
PROCESSOR, AND CONTROLS THE TIMING OF OPERATIONS AND THE INSTRUCTIONS SENT TO THE
PROCESSOR AND THE PERIPHERAL DEVICES. THE CU DIRECTS THE SYSTEM TO CARRY OUT PROGRAM
INSTRUCTIONS. IT DOES THE FETCHING, DECODING, AND MANAGING OF INSTRUCTIONS.
REGISTERS
REGISTERS ARE ALSO CALLED INTERNAL MEMORY OR IMMEDIATE ACCESS MEMORY STORES. A REGISTER
IS A SMALL AMOUNT OF FAST TEMPORARY MEMORY WITHIN THE PROCESSOR WHERE THE ALU OR THE
CU CAN STORE AND CHANGE VALUES NEEDED TO EXECUTE INSTRUCTIONS.
DIFFERENT PROCESSORS HAVE DIFFERENT SETS OF REGISTERS. ONE IMPORTANT REGISTER IS
THE PROGRAM COUNTER. THIS KEEPS TRACK OF THE RUNNING ORDER OF THE INSTRUCTIONS AND
SHOWS WHICH INSTRUCTION IN THE PROGRAM IS DUE TO BE EXECUTED NEXT.
• Most programmers do not write programs as sequences of binary digits. They
use programming languages like Java, C++, Ruby, BASIC or Python. These
are also known as high-level programming languages.
• High-level languages need to be translated into a binary code before
the CPU can execute them. The benefit of high-level languages is that they
are more like everyday written language than sequences of binary numbers,
so people find them easier to write and understand.
• Languages can be defined as human-readable or machine-readable:
• Human-readable language instructions are encoded in a language that
humans can understand more easily. This includes high- and low-level
languages. Low-level languages, such as assembly language, more closely
reflect the mechanical workings of the CPU.
• Machine-readable language instructions are written using only the binary
codes of the processor's instruction set.
• Translators
A piece of translator software, which is usually included within programming
software, converts high-level languages into machine code. These translators are
known as compilers and interpreters. Programs are translated using either a compiler
or interpreter.
• Compiler
A compiler translates a human-readable program directly into an executable,
machine-readable form before the program can run.
• Interpreter
An interpreter translates a human-readable program into an executable,
machine-readable form, instruction by instruction. It then executes each translated
instruction before moving on to the next one. A program is translated every time it is
run. Python is an example of an interpreted language.
Instruction sets
Instructions are decoded as a set of sequenced operations. These operations
instruct the ALU and control unit inside the CPU.
Opcode
The opcode tells the processor the job that needs to be done. A simple
operation might be 'add' or 'subtract'. If we use the analogy of a recipe, the
opcode might be 'chop' or 'mix'.
Each opcode instruction is very limited in what it can tell the processor to do. A
CPU's instruction set contains the opcodes that it will accept.
There are two types of opcode:
• an opcode that tells the circuitry which operation to carry out
• an opcode along with some data to be processed
• Operand
• The operand specifies the data that needs to be acted on. The operand can
also refer to a place in memory, such as a register.
• If we use the analogy of a recipe, an ingredient (eg an onion) is the thing
being acted upon by the opcode. So the opcode 'chop' could act on the
operand 'onion'. If the operand refers to a place in memory, this could be
seen as the 'chopping board'. The operand (data) 'onion' could be in the
memory location 'chopping board'. The instruction could be to chop the onion
on the chopping board
Assembly language
Assembly language is a low-level language which closely reflects
how opcodes and operands work. It is much quicker for computers to convert
assembly instructions to machine code than to convert high-level source code to
machine code.
Typical assembly language opcodes include:
• add
• subtract
• load
• compare
• branch
• store
• Mnemonics
• Assembly language instructions use abbreviations called mnemonics. An example of a
mnemonic assembly language instruction is LDA 50 which stores the value 50 into a register of
the CPU. Mnemonics are easier for humans to remember and understand than binary machine
code instructions.
The following is a basic example of how an opcode and operand might look for one line in the
machine code of a program:
•The first instruction in assembly language is written as LOAD 0004.
•The opcode is the first part, LOAD.
•The second part, 0004, is the memory address.
•The processor will load the data value that is held at the address 0004 into one of the
processor's memory registers.
Assembly Mnemonic Binary Hex
LOAD 0004 LDA 0004 1010 0100 A4
• Fetch-decode-execute cycle
• The main job of the CPU is to execute programs using the fetch-decode-execute cycle (also
known as the instruction cycle). This cycle begins as soon as you turn on a computer.
• To execute a program, the program code is copied from secondary storage into the main
memory. The CPU's program counter is set to the memory location where the first instruction in
the program has been stored, and execution begins. The program is now running.
• In a program, each machine code instruction takes up a slot in the main memory. These slots
(or memory locations) each have a unique memory address. The program counter stores the
address of each instruction and tells the CPU in what order they should be carried out.
• When a program is being executed, the CPU performs the fetch-decode-execute cycle, which
repeats over and over again until reaching the STOP instruction.
• Summary of the fetch-decode-execute cycle
• The processor checks the program counter to see
which instruction to run next.
• The program counter gives an address value in the
memory of where the next instruction is.
• The processor fetches the instruction value from this
memory location.
• Once the instruction has been fetched, it needs to be
decoded and executed. For example, this could
involve taking one value, putting it into the ALU, then
taking a different value from a register and adding
the two together.
• Once this is complete, the processor goes back to the
program counter to find the next instruction.
• This cycle is repeated until the program ends.

Running a Program.pdf

  • 1.
    RUNNING A PROGRAM SOFTWAREPROGRAMS ARE SETS OF INSTRUCTIONS. FOR A CPU TO EXECUTE THESE INSTRUCTIONS, EACH ONE MUST FIRST BE TRANSLATED INTO MACHINE CODE – SIMPLE BINARY CODES THAT ACTIVATE PARTS OF THE CPU. THE CPU ONLY PERFORMS A FEW BASIC FUNCTIONS: • PERFORMING MATHEMATICAL OPERATIONS LIKE ADDITION AND SUBTRACTION • MOVING DATA FROM ONE MEMORY LOCATION TO ANOTHER • MAKING DECISIONS AND JUMPS TO A NEW SET OF INSTRUCTIONS BASED ON THOSE DECISIONS A PIECE OF SOFTWARE, SUCH AS A GAME OR WEB BROWSER, COMBINES THESE FUNCTIONS TO PERFORM MORE COMPLEX TASKS.
  • 3.
    ARITHMETIC LOGIC UNIT(ALU) THE ALU CARRIES OUT CALCULATIONS AND MAKES DECISIONS ON THE DATA SENT TO THE PROCESSOR. CONTROL UNIT (CU) THE CU, WHICH IS ALSO CALLED THE CONTROLLER, CONTROLS DATA MOVING THROUGH THE PROCESSOR, AND CONTROLS THE TIMING OF OPERATIONS AND THE INSTRUCTIONS SENT TO THE PROCESSOR AND THE PERIPHERAL DEVICES. THE CU DIRECTS THE SYSTEM TO CARRY OUT PROGRAM INSTRUCTIONS. IT DOES THE FETCHING, DECODING, AND MANAGING OF INSTRUCTIONS. REGISTERS REGISTERS ARE ALSO CALLED INTERNAL MEMORY OR IMMEDIATE ACCESS MEMORY STORES. A REGISTER IS A SMALL AMOUNT OF FAST TEMPORARY MEMORY WITHIN THE PROCESSOR WHERE THE ALU OR THE CU CAN STORE AND CHANGE VALUES NEEDED TO EXECUTE INSTRUCTIONS. DIFFERENT PROCESSORS HAVE DIFFERENT SETS OF REGISTERS. ONE IMPORTANT REGISTER IS THE PROGRAM COUNTER. THIS KEEPS TRACK OF THE RUNNING ORDER OF THE INSTRUCTIONS AND SHOWS WHICH INSTRUCTION IN THE PROGRAM IS DUE TO BE EXECUTED NEXT.
  • 4.
    • Most programmersdo not write programs as sequences of binary digits. They use programming languages like Java, C++, Ruby, BASIC or Python. These are also known as high-level programming languages. • High-level languages need to be translated into a binary code before the CPU can execute them. The benefit of high-level languages is that they are more like everyday written language than sequences of binary numbers, so people find them easier to write and understand.
  • 6.
    • Languages canbe defined as human-readable or machine-readable: • Human-readable language instructions are encoded in a language that humans can understand more easily. This includes high- and low-level languages. Low-level languages, such as assembly language, more closely reflect the mechanical workings of the CPU. • Machine-readable language instructions are written using only the binary codes of the processor's instruction set.
  • 7.
    • Translators A pieceof translator software, which is usually included within programming software, converts high-level languages into machine code. These translators are known as compilers and interpreters. Programs are translated using either a compiler or interpreter. • Compiler A compiler translates a human-readable program directly into an executable, machine-readable form before the program can run. • Interpreter An interpreter translates a human-readable program into an executable, machine-readable form, instruction by instruction. It then executes each translated instruction before moving on to the next one. A program is translated every time it is run. Python is an example of an interpreted language.
  • 8.
    Instruction sets Instructions aredecoded as a set of sequenced operations. These operations instruct the ALU and control unit inside the CPU. Opcode The opcode tells the processor the job that needs to be done. A simple operation might be 'add' or 'subtract'. If we use the analogy of a recipe, the opcode might be 'chop' or 'mix'. Each opcode instruction is very limited in what it can tell the processor to do. A CPU's instruction set contains the opcodes that it will accept. There are two types of opcode: • an opcode that tells the circuitry which operation to carry out • an opcode along with some data to be processed
  • 9.
    • Operand • Theoperand specifies the data that needs to be acted on. The operand can also refer to a place in memory, such as a register. • If we use the analogy of a recipe, an ingredient (eg an onion) is the thing being acted upon by the opcode. So the opcode 'chop' could act on the operand 'onion'. If the operand refers to a place in memory, this could be seen as the 'chopping board'. The operand (data) 'onion' could be in the memory location 'chopping board'. The instruction could be to chop the onion on the chopping board
  • 10.
    Assembly language Assembly languageis a low-level language which closely reflects how opcodes and operands work. It is much quicker for computers to convert assembly instructions to machine code than to convert high-level source code to machine code. Typical assembly language opcodes include: • add • subtract • load • compare • branch • store
  • 11.
    • Mnemonics • Assemblylanguage instructions use abbreviations called mnemonics. An example of a mnemonic assembly language instruction is LDA 50 which stores the value 50 into a register of the CPU. Mnemonics are easier for humans to remember and understand than binary machine code instructions. The following is a basic example of how an opcode and operand might look for one line in the machine code of a program: •The first instruction in assembly language is written as LOAD 0004. •The opcode is the first part, LOAD. •The second part, 0004, is the memory address. •The processor will load the data value that is held at the address 0004 into one of the processor's memory registers. Assembly Mnemonic Binary Hex LOAD 0004 LDA 0004 1010 0100 A4
  • 12.
    • Fetch-decode-execute cycle •The main job of the CPU is to execute programs using the fetch-decode-execute cycle (also known as the instruction cycle). This cycle begins as soon as you turn on a computer. • To execute a program, the program code is copied from secondary storage into the main memory. The CPU's program counter is set to the memory location where the first instruction in the program has been stored, and execution begins. The program is now running. • In a program, each machine code instruction takes up a slot in the main memory. These slots (or memory locations) each have a unique memory address. The program counter stores the address of each instruction and tells the CPU in what order they should be carried out. • When a program is being executed, the CPU performs the fetch-decode-execute cycle, which repeats over and over again until reaching the STOP instruction.
  • 13.
    • Summary ofthe fetch-decode-execute cycle • The processor checks the program counter to see which instruction to run next. • The program counter gives an address value in the memory of where the next instruction is. • The processor fetches the instruction value from this memory location. • Once the instruction has been fetched, it needs to be decoded and executed. For example, this could involve taking one value, putting it into the ALU, then taking a different value from a register and adding the two together. • Once this is complete, the processor goes back to the program counter to find the next instruction. • This cycle is repeated until the program ends.