Where are data and instructions held so that they are available to the cpu for processing
Instruction SetsSchematic diagram of a simple computerExecution CycleFetchAn instruction, stored in the memory, is fetched into the control unit by supplying the memory with the address of the instruction. Decode Show
Memory Execute Write Example InstructionADD [0],[1],[2] ; Add the contents of memory location 1 to location 2 and put the result in location 0.Execution Sequence1. Address of the instruction is sent to memory with a READ control signal2. Instruction is fetched into control unit 3. Instruction is decoded. 4. Address 1 sent to memory with READ signal 5. Data fetched from memory location 1 into the datapath 6. Address 2 sent to memory with READ signal 7. Data fetched from memory location 2 into the datapath 8. ADD operation is sent to ALU in datapath 9. Address 0 sent to memory with WRITE signal 10. Result sent from datapath to memory and written to location 0. What is an instruction setMachine Language is very specific to a certain type of CPU. Each CPU will have a certain repertoire of instructions that it can decode and execute. This is called the instruction set of the CPU or the Instruction Set Architecture (ISA).Information which must be present in an instructionOperatione.g. ADD, SUBTRACT etc. Where to get the data Where to put the result Where to find the next instruction Parts of an InstructionOpcodeThe operation itself is usually represented by a code called the opcode (for OPeration CODE) Operands Addresses Example
Format used to represent an instruction in Assembly LanguageThere is no standard format for the description of instructions, sometimes even for the same CPU. The opcode will almost certainly come first; however some Assembly Languages have the destination operand first and some have it last.Types of instructionMachines with a single type of instructionIn order to put all the information necessary into a single instruction it must have the following format
Example Means Disadvantages of using a single type of instruction Some very early computers had instructions such as this, but not modern machines. Three address machinesNot all instructions need to change the order of instruction execution, so Next/False can be a default:Assume that Next/False is the next sequential instruction Assume most instructions will be sequential Type 1 will be a three address instruction (ALU operations), type 2 will be a one address instruction (Control instructions). Type 1 Instructions: ALU operations
Add Flags to the Datapath Example SUB [0],[1],[2] Advantages and Disadvantages of 3 address machines Two Address MachinesThree address instructions are still very long, but they may be made shorter:Assume that the destination is the same as one of the sources Define an instruction called MOV (Transfer instruction) Type 1 Instructions: Memory-Memory ALU operations (including MOV)
Now in order to perform the same operation we need three instructions: Advantages and Disadvantages of 2 address machines One Address MachinesTwo address instructions may be made even shorter:Allow an operand to be a code which represents a temporary location within the datapath. These temporary locations are called registers. If there is only one it is usually called the accumulator. Registers are usually referred to by a symbolic name e.g. A,B,AX,R1,R2 etc. Type 1 Instructions: Register-Memory ALU operations
Now, in order to perform the same operation we need four instructions: Advantages and Disadvantages of 1 address machines The machine will be slower in this case but not in all cases. Programs are now even shorter. The registers may be used for temporary results which are not needed immediately or for holding frequently used operands e.g. the end count in a "for" loop. Zero Address MachinesNo machine can have only instructions with no addresses. However it is possible have all operations except those which get data from memory and those which put data into the memory as zero address instructions. There are two common ways of achieving this.1. Allow all operands to be registers. Type 1 Instructions: Register-Register ALU operations
Now in order to perform the same operation we need five instructions: Often this type of machine is referred to as a load/store machine because the only instructions with addresses are the load and store instructions. Some machines allow three operand zero address instructions, this reduces the number of MOV instructions. 2. Assume all operations implicitly use a stack. Define two instructions PUSH and POP which can be used to move the data on the top of the stack to and from the memory. ALU operations will always use the top two words on the stack for sources and put the result on the top of the stack. Type 1 Instructions: Stack ALU operations Type 2 Instructions: Control instructions
Example Now in order to perform the same operation we need five instructions: This is sometimes called a stack machine, and such machines do exist e.g. INMOS TRANSPUTER, but they are rare. Advantages and Disadvantages of zero address machines Programs may be shorter, but for the stack machine there
may be extra overhead incurred by having to manipulate the stack. Comparison of three, two, one and zero address machines.A good way of looking at the effectiveness of these types of instruction, is to look at the number of memory accesses necessary for the evaluation of a simple expression. The memory accesses will be split into instruction fetches and data accesses (since data accesses are often slower).Example Expression X=Y*(Y+Z) Where X,Y and Z are stored in memory locations 0,1 and 2
In practice machines may allow a variety of different types of instruction. For example the Pentium is a one address machine, but it also has zero address register-register instructions, and some stack operations. SummaryInstructions may be classified by the number of operands and the number of addresses which they use.Instructions are executed sequentially using a Program Counter to hold the address of the next instruction. Control instructions are used to change the program counter based on flags set by a previous instruction. Transfer instructions may be used to move data without performing any operation on it. Registers may be used to hold temporary results or frequently used operands. Sometimes stack operations are useful for storing temporary data which may not fit in registers. A stack is also useful for implementing recursion. Addressing modesDirect AddressingSo far we have assumed that all references to memory are to explicitly stated memory locations. This way of referencing memory is called Direct addressing. It is possible to write programs which only use direct addressing, but such programs have a serious drawback - a program must be allowed to change its own code. Example program using only
Direct Addressing Machine Instruction Set
Add one to each number in a table starting at a given address, with a given length. Pseudocode procedure add_one(start:byte,num:byte); Assembly Language Program:
Comments on this program The temporary variable 'address' is used to refer to the location in memory which is to be changed. This variable must be within the program because the only way to reference memory is through a direct address and so it is stored at get+1. Unfortunately our program uses 'address' twice, once at get+1 and once at put+1. Thus if 'address' is changed then both get+1 and put+1 must also be changed. This creates a confusing program. Often in the program a constant is needed, e.g. for ctr:=ctr-1 the constant 1 is necessary. This constant must be stored in a memory location, such a constant is called a literal. New addressing modesChanges in the addressing of memory which would improve the program.If a register were allowed to hold an address, then the program would not need to change itself. This type of addressing is called Register Indirect addressing. If a constant could be part of an instruction then literals would not be necessary. This is called Immediate Addressing. New Instruction Set With Register Indirect and Immediate Addressing.
New Assembly Language Program
Description of Common Addressing ModesRegister Addressinge.g. ADD A,B Adds the value in register B to the value in register A and puts the result in register A. Both operands in this instruction use Register addressing, the value referenced is held in a register within the datapath. This addressing mode is used for access to temporary or commonly used variables. Register Addressing Mode Immediate Addressing e.g. ADD A,23 Adds 23 on to the value in A register. The value 23 is part of the instruction and is called an immediate. This addressing mode is used for constants. Immediate Addressing Mode * 50% to 60% fit within 8 bits * 75% to 80% fit within 16 bits Displacement Addressing This mode has two common uses. (note that the value may be a signed integer) 1. Accessing local variables
Average of 5 programs from SPECint92 and Average of 5 programs from SPECfp92 X-axis is in powers of 2 1% of addresses > 16-bits Register Indirect Addressing e.g. ADD A,[B] Add the value at the address held the B register to the value in the A register. This mode is used to allow pointers to data held in memory. Register Indirect Addressing Mode Indexed Addressinge.g. ADD A,[B+C] Use B+C as the address of a value to add to the A register. This mode is often used to access the elements of a table or array where B is the address of the start of the table and C is an index into the table. Indexed Addressing Mode Direct Addressinge.g. ADD A,[200] Add the value at address 200 to the A register. Used for access to global variables which will have a fixed location in memory. Direct Addressing ModeMemory Indirect Addressing This is a combination of indexed and displacement, it may be used for accessing array elements. Auto-increment and Auto-decrement Addressing These modes may be used for stepping through the elements of a table or array, and for performing stack operations where none are defined. Scaled
Relative (or PC relative) Summary of Addressing Modes
Data Size So far we have assumed that there is only one type of data, in reality machines have to deal with bytes, halfwords, words and double words. Some Instruction sets allow every instruction to work with every data size, others only allow word accesses and perhaps byte accesses. Instruction Coding Instructions may be encoded so that they are variable in size (e.g. pentium) or fixed (PowerPC). Variable sized instructions allow a program to take up less memory but the CPU must be more complex to deal with the variable length. Hybrid coding forces an instruction to be one of a few fixed lengths. What holds data and processing instructions temporarily until the CPU needs them?Primary storage, also known as main storage or memory, is the area in a computer in which data is stored for quick access by the computer's processor. The terms random access memory (RAM) and memory are often as synonyms for primary or main storage.
Which part of the CPU that holds data being processed?Random access memory (RAM)
Its function is to store programs and data so that they are ready for use when the CPU needs them.
Where does a CPU receive instructions from?The CPU is designed to understand a set of instructions - the instruction set. It fetches the instructions from the main memory and executes them. This is done repeatedly from when the computer is booted up to when it is shut down. The CPU fetches the instructions one at a time from the main memory into the registers.
|