CMSC 150 PROGRAM EXECUTION CS 150: Wed 1 Feb 2012
Stored Program Concept CPU ALU control unit … registers … …
Our Example Architecture Main Memory …
Our Example Architecture … Main Memory Address
Our Example Architecture Main Memory Instruction Register Program Counter Register Data Register Central Processing Unit … Address
Our Machine Language Assembly Language Instruction Machine Language Instruction CopyFrom000 CopyTo001 Add010 Subtract011 Read100 Print101 IfNegGoTo110 Stop111
Our Hypothetical Computer Main Memory Instruction Register Program Counter Register Data Register CPU …
Our Hypothetical Computer Main Memory Instruction Register Program Counter Register Data Register CPU Word size: 8 bits (1 byte) …
Our Hypothetical Computer Main Memory Instruction Register Program Counter Register Data Register CPU Word size: 8 bits (1 byte) Number of instructions: 8 …
Our Hypothetical Computer Main Memory Instruction Register Program Counter Register Data Register CPU Word size: 8 bits (1 byte) Number of instructions: 8 To represent 2 3 = 8 total instructions, need 3 bits …
Our Hypothetical Computer Main Memory Instruction Register Program Counter Register Data Register CPU Word size: 8 bits (1 byte) Number of instructions: 8 To represent 2 3 = 8 total instructions, need 3 bits Leaves 5 bits to represent addresses …
Our Hypothetical Computer Main Memory Instruction Register Program Counter Register Data Register CPU Word size: 8 bits (1 byte) Number of instructions: 8 To represent 2 3 = 8 total instructions, need 3 bits Leaves 5 bits to represent addresses 5 bits for addresses: 2 5 = 32 memory cells …
Our Hypothetical Computer Main Memory Instruction Register Program Counter Register Data Register CPU Word size: 8 bits (1 byte) Number of instructions: 8 To represent 2 3 = 8 total instructions, need 3 bits Leaves 5 bits to represent addresses 5 bits for addresses: 2 5 = 32 memory cells …
Our Hypothetical Computer Main Memory Instruction Register Program Counter Register Data Register CPU Word size: 8 bits (1 byte) Number of instructions: 8 To represent 2 3 = 8 total instructions, need 3 bits Leaves 5 bits to represent addresses 5 bits for addresses: 2 5 = 32 memory cells …
Data In Our Computer Use sign-magnitude to represent integers 8 bits for data – leftmost bit will be sign (0: non-neg; 1: neg) – 7 remaining bits: magnitude in binary = = -11
Eight Machine Language Instructions Instruction Code Meaning of the Instruction 000Copy word from memory address into DR 001Copy word to memory address from DR 010Add word in memory address to word in DR 011Subtract word in memory address from word in DR 100Read word from input into DR 101Print word in DR to output 110 Conditional execution : If word in DR is negative, copy a new memory address into PC 111Stop execution Underline: an argument (value) required by that instruction
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution one word (here, one byte)
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution Instruction code
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution Argument (memory address)
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution Decoding the Instruction:
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution Decoding the Instruction: Copy to …
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution Decoding the Instruction: Copy to …
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution Decoding the Instruction: Copy to address 10110…
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution Decoding the Instruction: Copy to address from DR This instruction copies the contents of the data register into memory cell addressed = 22 10
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution Decoding the Instruction: Read from input…
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution Decoding the Instruction: Read from input… No argument…
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution Decoding the Instruction: Read from input… No argument, hence zeros (to fill the byte)
Example of an Instruction CodeMeaning of the Instruction 000Copy from address into DR 001Copy to address from DR 010Add word in address to DR 011Subtract word in address from DR 100Read from input into DR 101Print to output from DR 110If DR is < 0, copy address into PC 111Stop execution Decoding the Instruction: Read from input into DR This instruction copies a word from the input unit into the data register
Preparing To Execute a Program Your program is first written in a high-level language (e.g., Java): source code Converted into machine language: executable To run, executable is copied into main memory PC is loaded with the memory address of the first instruction in the executable
Fetch-Execute Cycle 1. Copy the word referred to by the PC into the IR (i.e., fetch) 2. Increment the address stored in the PC 3. Decode & execute the contents of the IR 4. Unless a stop instruction, goto step 1
Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit Initial state of memory after loading program and setting PC to Executable Program
Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 1. Copy word referred to by PC into Instruction Register 2
Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 1. Copy word referred to by PC into Instruction Register 3
Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 1. Copy word referred to by PC into Instruction Register 4
Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 2. Increment the Program Counter 5
Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and Execute instruction 6
CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and Execute instruction – 000 is op code for CopyFrom 7
CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and Execute instruction – is address to copy from 8
CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and Execute instruction – contains a word 9
CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and Execute instruction – Instruction execution is complete 10
CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit Repeat: 1. Copy word referred to by PC to IR 11
CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit Repeat: 1. Copy word referred to by PC to IR 12
CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 2. Increment PC 13
CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction 14
CopyFrom IfNegGoTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – 110 is the op code for IfNegGoTo 15
CopyFrom IfNegGoTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – Check value in data register 16
CopyFrom IfNegGoTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – DR value is -1 17
CopyFrom IfNegGoTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – Copy rightmost 5 bits of IR to PC 18
CopyFrom IfNegGoTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – execution is complete 19
CopyFrom IfNegGoTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 1. Copy word referred to by PC to IR 20
CopyFrom IfNegGoTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 1. Copy word referred to by PC to IR 21
CopyFrom IfNegGoTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 2. Increment PC 22
CopyFrom IfNegGoTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction 23
CopyFrom IfNegGoTo CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – 000 is op code for CopyFrom 24
CopyFrom IfNegGoTo CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction 25
CopyFrom IfNegGoTo CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – copy value to DR. Execution complete. 26
CopyFrom IfNegGoTo CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 1. Copy word referred to by PC to IR. 27
CopyFrom IfNegGoTo CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 1. Copy word referred to by PC to IR. 28
CopyFrom IfNegGoTo CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 2. Increment PC 29
CopyFrom IfNegGoTo CopyFrom Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction 30
CopyFrom IfNegGoTo CopyFrom Subtract Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – 011 is Subtract 31
CopyFrom IfNegGoTo CopyFrom Subtract Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – data register is first operand 32
CopyFrom IfNegGoTo CopyFrom Subtract Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – word at is second operand 34
CopyFrom IfNegGoTo CopyFrom Subtract Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – word at is second operand 35
CopyFrom IfNegGoTo CopyFrom Subtract Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction: (6 10 – 1 10 ) = 5 10 =
CopyFrom IfNegGoTo CopyFrom Subtract Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction: 5 10 = Result is stored in DR. 38
CopyFrom IfNegGoTo CopyFrom Subtract Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 1. Copy word referred to by PC to IR. 39
CopyFrom IfNegGoTo CopyFrom Subtract Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 1. Copy word referred to by PC to IR. 40
CopyFrom IfNegGoTo CopyFrom Subtract Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 2. Increment PC 41
CopyFrom IfNegGoTo CopyFrom Subtract Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction 42
CopyFrom IfNegGoTo CopyFrom Subtract CopyTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – 001 is CopyTo 43
CopyFrom IfNegGoTo CopyFrom Subtract CopyTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – is target location 44
CopyFrom IfNegGoTo CopyFrom Subtract CopyTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – value in DR copied to memory 45
CopyFrom IfNegGoTo CopyFrom Subtract CopyTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 1. Copy word referred to by PC to IR 46
CopyFrom IfNegGoTo CopyFrom Subtract CopyTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 1. Copy word referred to by PC to IR 47
CopyFrom IfNegGoTo CopyFrom Subtract CopyTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 2. Increment PC 48
CopyFrom IfNegGoTo CopyFrom Subtract CopyTo Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction 49
CopyFrom IfNegGoTo CopyFrom Subtract CopyTo Stop Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit 3. Decode and execute instruction – 111 is op code for Stop 50
Main MemoryCPU Instruction Register Program Counter Data Register Input UnitOutput Unit Stored Program Concept: Instructions & data together in main memory 50 Instructions Data CPU “knows” difference based only on execution process
public static void main( String[] args ) { Random generator = new Random( ); Scanner keyboard = new Scanner( System.in ); int value = keyboard.nextInt(); String text = keyboard.next(); value += generator.nextInt( 100 ); System.out.println( text + ": " + value ); } Java executes instructions one-at-a-time in order relates to fetch-execute cycle Each Java instruction may be multiple ML instructions
Possible Overflow Using our 1-byte word limit and sign magnitude, add the numbers:
Possible Overflow Using our 1-byte word limit and sign magnitude, add the numbers: = = =
Possible Overflow Using our 1-byte word limit and sign magnitude, add the numbers: = = = sign bit (indicating negative)
Possible Overflow Using our 1-byte word limit and sign magnitude, add the numbers: So computer would report -1 as the answer! Overflow: too few bits to represent the result = = =