Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 150 PROGRAM EXECUTION CS 150: Wed 1 Feb 2012.

Similar presentations


Presentation on theme: "CMSC 150 PROGRAM EXECUTION CS 150: Wed 1 Feb 2012."— Presentation transcript:

1 CMSC 150 PROGRAM EXECUTION CS 150: Wed 1 Feb 2012

2 Stored Program Concept CPU ALU control unit … registers 127 128 129 130 131 132 01010010 133 134...... … … 01010110 01110010 11010010 10110010 10111110 11100000 00000001 01010010 11100000

3 Our Example Architecture Main Memory …

4 Our Example Architecture … Main Memory 00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 Address

5 Our Example Architecture Main Memory 00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 Instruction Register Program Counter Register Data Register Central Processing Unit … Address

6 Our Machine Language Assembly Language Instruction Machine Language Instruction CopyFrom000 CopyTo001 Add010 Subtract011 Read100 Print101 IfNegGoTo110 Stop111

7 Our Hypothetical Computer Main Memory Instruction Register Program Counter Register Data Register CPU …

8 Our Hypothetical Computer Main Memory Instruction Register Program Counter Register Data Register CPU Word size: 8 bits (1 byte) …

9 Our Hypothetical Computer Main Memory Instruction Register Program Counter Register Data Register CPU Word size: 8 bits (1 byte) Number of instructions: 8 …

10 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 …

11 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 …

12 Our Hypothetical Computer Main Memory 00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 11110 11111 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 …

13 Our Hypothetical Computer Main Memory 00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 11110 11111 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 …

14 Our Hypothetical Computer Main Memory 00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 11110 11111 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 …

15 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 00001011 = +11 10001011 = -11

16 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

17 Example of an Instruction 00110110 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)

18 Example of an Instruction 00110110 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

19 Example of an Instruction 00110110 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)

20 Example of an Instruction 00110110 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:

21 Example of an Instruction 00110110 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 …

22 Example of an Instruction 00110110 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 …

23 Example of an Instruction 00110110 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…

24 Example of an Instruction 00110110 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 from DR This instruction copies the contents of the data register into memory cell addressed 10110 2 = 22 10

25 Example of an Instruction 10000000 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…

26 Example of an Instruction 10000000 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…

27 Example of an Instruction 10000000 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)

28 Example of an Instruction 10000000 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

29 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

30 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

31 0000000001001 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register Program Counter 00000 Data Register Input UnitOutput Unit Initial state of memory after loading program and setting PC to 00000 1 Executable Program

32 0000000001001 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register Program Counter 00000 Data Register Input UnitOutput Unit 1. Copy word referred to by PC into Instruction Register 2

33 0000000001001 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register Program Counter 00000 Data Register Input UnitOutput Unit 1. Copy word referred to by PC into Instruction Register 3

34 0000000001001 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001001 Program Counter 00000 Data Register Input UnitOutput Unit 1. Copy word referred to by PC into Instruction Register 4

35 0000000001001 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001001 Program Counter 00001 Data Register Input UnitOutput Unit 2. Increment the Program Counter 5

36 0000000001001 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001001 Program Counter 00001 Data Register Input UnitOutput Unit 3. Decode and Execute instruction 6

37 0000000001001 CopyFrom 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001001 Program Counter 00001 Data Register Input UnitOutput Unit 3. Decode and Execute instruction – 000 is op code for CopyFrom 7

38 0000000001001 CopyFrom 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001001 Program Counter 00001 Data Register Input UnitOutput Unit 3. Decode and Execute instruction – 01001 is address to copy from 8

39 0000000001001 CopyFrom 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001001 Program Counter 00001 Data Register Input UnitOutput Unit 3. Decode and Execute instruction – 01001 contains a word 9

40 0000000001001 CopyFrom 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001001 Program Counter 00001 Data Register 10000001 Input UnitOutput Unit 3. Decode and Execute instruction – Instruction execution is complete 10

41 0000000001001 CopyFrom 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001001 Program Counter 00001 Data Register 10000001 Input UnitOutput Unit Repeat: 1. Copy word referred to by PC to IR 11

42 0000000001001 CopyFrom 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11000101 Program Counter 00001 Data Register 10000001 Input UnitOutput Unit Repeat: 1. Copy word referred to by PC to IR 12

43 0000000001001 CopyFrom 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11000101 Program Counter 00010 Data Register 10000001 Input UnitOutput Unit 2. Increment PC 13

44 0000000001001 CopyFrom 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11000101 Program Counter 00010 Data Register 10000001 Input UnitOutput Unit 3. Decode and execute instruction 14

45 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11000101 Program Counter 00010 Data Register 10000001 Input UnitOutput Unit 3. Decode and execute instruction – 110 is the op code for IfNegGoTo 15

46 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11000101 Program Counter 00010 Data Register 10000001 Input UnitOutput Unit 3. Decode and execute instruction – Check value in data register 16

47 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11000101 Program Counter 00010 Data Register 10000001 Input UnitOutput Unit 3. Decode and execute instruction – DR value is -1 17

48 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11000101 Program Counter 00010 Data Register 10000001 Input UnitOutput Unit 3. Decode and execute instruction – Copy rightmost 5 bits of IR to PC 18

49 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11000101 Program Counter 00101 Data Register 10000001 Input UnitOutput Unit 3. Decode and execute instruction – execution is complete 19

50 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11000101 Program Counter 00101 Data Register 10000001 Input UnitOutput Unit 1. Copy word referred to by PC to IR 20

51 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001010 Program Counter 00101 Data Register 10000001 Input UnitOutput Unit 1. Copy word referred to by PC to IR 21

52 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001010 Program Counter 00110 Data Register 10000001 Input UnitOutput Unit 2. Increment PC 22

53 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001010 Program Counter 00110 Data Register 10000001 Input UnitOutput Unit 3. Decode and execute instruction 23

54 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001010 Program Counter 00110 Data Register 10000001 Input UnitOutput Unit 3. Decode and execute instruction – 000 is op code for CopyFrom 24

55 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001010 Program Counter 00110 Data Register 10000001 Input UnitOutput Unit 3. Decode and execute instruction 25

56 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001010 Program Counter 00110 Data Register 00000110 Input UnitOutput Unit 3. Decode and execute instruction – copy value to DR. Execution complete. 26

57 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00001010 Program Counter 00110 Data Register 00000110 Input UnitOutput Unit 1. Copy word referred to by PC to IR. 27

58 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 01101100 Program Counter 00110 Data Register 00000110 Input UnitOutput Unit 1. Copy word referred to by PC to IR. 28

59 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 01101100 Program Counter 00111 Data Register 00000110 Input UnitOutput Unit 2. Increment PC 29

60 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 01101100 Program Counter 00111 Data Register 00000110 Input UnitOutput Unit 3. Decode and execute instruction 30

61 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 01101100 Program Counter 00111 Data Register 00000110 Input UnitOutput Unit 3. Decode and execute instruction – 011 is Subtract 31

62 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 01101100 Program Counter 00111 Data Register 00000110 Input UnitOutput Unit 3. Decode and execute instruction – data register is first operand 32

63 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 01101100 Program Counter 00111 Data Register 00000110 Input UnitOutput Unit 3. Decode and execute instruction – word at 01100 is second operand 34

64 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 01101100 Program Counter 00111 Data Register 00000110 Input UnitOutput Unit 3. Decode and execute instruction – word at 01100 is second operand 35

65 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 01101100 Program Counter 00111 Data Register 00000110 Input UnitOutput Unit 3. Decode and execute instruction: (6 10 – 1 10 ) = 5 10 = 00000101 2. 36

66 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 01101100 Program Counter 00111 Data Register 00000101 Input UnitOutput Unit 3. Decode and execute instruction: 5 10 = 00000101 2. Result is stored in DR. 38

67 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 01101100 Program Counter 00111 Data Register 00000101 Input UnitOutput Unit 1. Copy word referred to by PC to IR. 39

68 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00101010 Program Counter 00111 Data Register 00000101 Input UnitOutput Unit 1. Copy word referred to by PC to IR. 40

69 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00101010 Program Counter 01000 Data Register 00000101 Input UnitOutput Unit 2. Increment PC 41

70 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00101010 Program Counter 01000 Data Register 00000101 Input UnitOutput Unit 3. Decode and execute instruction 42

71 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 CopyTo 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00101010 Program Counter 01000 Data Register 00000101 Input UnitOutput Unit 3. Decode and execute instruction – 001 is CopyTo 43

72 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 CopyTo 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000110 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00101010 Program Counter 01000 Data Register 00000101 Input UnitOutput Unit 3. Decode and execute instruction – 01010 is target location 44

73 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 CopyTo 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000101 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00101010 Program Counter 01000 Data Register 00000101 Input UnitOutput Unit 3. Decode and execute instruction – value in DR copied to memory 45

74 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 CopyTo 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000101 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 00101010 Program Counter 01000 Data Register 00000101 Input UnitOutput Unit 1. Copy word referred to by PC to IR 46

75 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 CopyTo 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000101 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11100000 Program Counter 01000 Data Register 00000101 Input UnitOutput Unit 1. Copy word referred to by PC to IR 47

76 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 CopyTo 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000101 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11100000 Program Counter 01001 Data Register 00000101 Input UnitOutput Unit 2. Increment PC 48

77 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 CopyTo 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000101 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11100000 Program Counter 01001 Data Register 00000101 Input UnitOutput Unit 3. Decode and execute instruction 49

78 0000000001001 CopyFrom 1000000000000 0000111000101 IfNegGoTo 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 CopyFrom 1010100000000 0011001101100 Subtract 1011000000000 0011100101010 CopyTo 1011100000000 0100011100000 Stop 1100000000000 0100110000001 1100100000000 0101000000101 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11100000 Program Counter 01001 Data Register 00000101 Input UnitOutput Unit 3. Decode and execute instruction – 111 is op code for Stop 50

79 0000000001001 1000000000000 0000111000101 1000100000000 0001000001010 1001000000000 0001101001011 1001100000000 0010011100000 1010000000000 0010100001010 1010100000000 0011001101100 1011000000000 0011100101010 1011100000000 0100011100000 1100000000000 0100110000001 1100100000000 0101000000101 1101000000000 0101100000100 1101100000000 0110000000001 1110000000000 0110100000000 1110100000000 0111000000000 1111000000000 0111100000000 1111100000000 Main MemoryCPU Instruction Register 11100000 Program Counter 01001 Data Register 00000101 Input UnitOutput Unit Stored Program Concept: Instructions & data together in main memory 50 Instructions Data  CPU “knows” difference based only on execution process

80 public static void main( String[] args ) { Random generator = new Random( 8675309 ); 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

81 Possible Overflow  Using our 1-byte word limit and sign magnitude, add the numbers: 01100110 2 + 00011011 2

82 Possible Overflow  Using our 1-byte word limit and sign magnitude, add the numbers: 01100110 2 + 00011011 2 = 102 10 + 27 10 = 129 10 = 10000001 2

83 Possible Overflow  Using our 1-byte word limit and sign magnitude, add the numbers: 01100110 2 + 00011011 2 = 102 10 + 27 10 = 129 10 = 10000001 2 sign bit (indicating negative)

84 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 01100110 2 + 00011011 2 = 102 10 + 27 10 = 129 10 = 10000001 2

85 00001001 11000101 00001010 01001011 11100000 00001010 01101100 00101010 11100000 10000001 00000110 00000100 00000001


Download ppt "CMSC 150 PROGRAM EXECUTION CS 150: Wed 1 Feb 2012."

Similar presentations


Ads by Google