models of computation
play

Models of Computation Z. Sawa (TU Ostrava) Theoretical Computer - PowerPoint PPT Presentation

Models of Computation Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 1 / 63 Necessity of Formalization of the Notion of Algorithm The above mentioned characterization of the notion of an algorithm was a little bit


  1. Models of Computation Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 1 / 63

  2. Necessity of Formalization of the Notion of “Algorithm” The above mentioned characterization of the notion of an algorithm was a little bit vague. If we would like to prove for a problem that there is no algorithm solving the given problem, it would probably not be possible with such vague definition of the notion of an algorithm. Intuitively, we understand what properties an algorithm should have: It should consist of simple steps that can be performed “mechanically” without understanding the problem. Objects, with which the algorithm manipulates, and also the performed operations should be finite. Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 2 / 63

  3. An Algorithm Performed by a Computer For example a program in any programming language surely has these properties. Regardless of the programming language in which a program is written, its instructions are always performed on a hardware of some concrete computer on the level of instructions of a processor. So it should be clear that every program in every programming language could be written as a program consisting only of instructions of a machine code of some processor. Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 3 / 63

  4. Operation of a Computer A typical architecture of most of computers looks as follows: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 MEM IP flags R1 R5 CPU R2 R6 R3 R7 R4 R8 A computer has a memory consisting of a big number of memory cells. Every cell contains a number of some size, typically 1 byte (8 bits), i.e., a number in the range 0 . . 255. Cells are numbered. The number of a cell is called its address . Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 4 / 63

  5. Operation of a Computer A typical architecture of most of computers looks as follows: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 MEM IP flags R1 R5 CPU R2 R6 R3 R7 R4 R8 Instructions are stored in the memory (every instruction has its numerical code and the instructions are executed by a processor ). The processor has so called instruction counter IP that contains an address of an instruction that is currently executed. The processor reads an instruction from the address specified by IP, increments IP by the length of the read instruction, and executes the given instruction. Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 4 / 63

  6. Operation of a Computer A typical architecture of most of computers looks as follows: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 MEM IP flags R1 R5 CPU R2 R6 R3 R7 R4 R8 A processor usually contains several registers of some fixed length (e.g., 32 or 64 bits). Most of operations are performed on registers. A processor usually contains flags that can be used for testing of the result of the last performed operation (e.g., an overflow, if the result is zero, etc.) Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 4 / 63

  7. Operation of a Computer Typical instructions: Loading of a value of some memory cell (resp. several consecutive cells) into some register (LOAD). Storing of a value of some register into some memory cell (resp. several consecutive cells) (STORE). Remark: An address of a cell can be either direct (i.e., it is a part of the instruction), or indirect (stored in some register, or computed from values in one or several registers). Copying a value in one register into some other register (MOV). Arithmetic instructions (ADD, SUB, MUL, DIV, NEG, CMP, INC, DEC, . . . ). Logical instructions (AND, OR, XOR, NOT, . . . ). Bit shifts and rotations (SHL, SHR, . . . ) Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 5 / 63

  8. Operation of a Computer Typical instructions (continued): Unconditional jump (JMP). Remark: A destination address can be direct or indirect. Conditional jumps (JZERO, JGTZ, . . . ). Invocation of subroutines (CALL, RET). Some special instructions – manipulation with input and output, an interrupt handling, processor modes, memory access control, paging, etc. Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 6 / 63

  9. Operation of a Computer Example: An instruction written is a high-level language as x = y + 2 can be implemented by the following sequence of instructions of some (hypothetical) processor: LOAD R5,[0x001b7c44] ADD R5,2 STORE [0x001b7c38],R5 Remark: We assume that variable x is stored at address 0x001b7c38 and variable y at the address 0x001b7c44. Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 7 / 63

  10. Random Access Machine A Random Access Machine ( RAM ) is an idealized model of a computer. It consists of the following parts: Program unit – contains a program for the RAM and a pointer to the currently executed instruction Working memory consists of cells numbered 0 , 1 , 2 , . . . ; the content of the cells can be read and written to Input tape – read-only Output tape – write-only Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 8 / 63

  11. Random Access Machine input program working unit memory 7 5 2 0 1 READ 0 0 2 JZERO 10 0 1 3 STORE *3 4 ADD 2 0 2 5 STORE 2 0 3 6 LOAD 1 IC 0 7 ADD =1 4 8 STORE 1 ALU 0 5 9 JUMP 1 10 LOAD 2 0 6 11 DIV 1 0 7 12 STORE 2 13 LOAD =0 0 8 14 STORE 1 output Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 9 / 63

  12. Random Access Machine Cells 0 and 1 are special and are used as registers of the RAM: Cell 0 – a working register (accumulator) – the register that is one of operands for most of instructions and to which the result of most of operations is stored. Cell 1 – an index register – it is used for indirect addressing. Forms of operands of instructions ( i ∈ N ): form the value of the operand = i the number i the number stored in the cell at address i i * i the number stored in the cell at address i + j , where j is the current value of the index register Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 10 / 63

  13. Random Access Machine Example: LOAD <op> reads the value of the operand <op> into the working register (i.e., into cell 0). – loads 5 into the working register LOAD =5 – loads the value in cell 5 into the working register LOAD 5 – loads the value in cell 5 + j , where j is the cur- LOAD *5 rent value of the index register, into the working register Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 11 / 63

  14. Random Access Machine Input and output instructions (they have no operands): – the value from the cell of input tape on which READ is the reading head is stored into the working register and the reading head is moved right – the value in the working register is written on WRITE the output tape and the output head is moved right by one Intructions working with memory: – the value of the operand is loaded into the work- LOAD <op> ing register – the value of the operand is rewritten with the STORE <op> content of the working register (an operand of the form = i is not allowed) Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 12 / 63

  15. Random Access Machine Instruction for arithmetic operations: – the value in the working register is increased by ADD <op> the value of the operand (i.e., the value of the operand is added to it) – the value of the operand subtracted from the SUB <op> value of the working register – the value in the working register is multiplied by MUL <op> the value of the operand – the value in the working register is divided by the DIV <op> value of the operand (the result is truncated to an integer value) Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 13 / 63

  16. Random Access Machine Jump instructions: – the computation will continue with an in- JUMP <label> struction determined by the label – if the working register contains value 0, the JZERO <label> computation will continue with an instruc- tion determined by the label; otherwise it will continue with the following instruction – if the working register contains a positive JGTZ <label> value, the computation will continue with an instruction determined by the label; oth- erwise it will continue with the following in- struction A halting instruction: – the computation is halted HALT Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 14 / 63

  17. Random Access Machine Problem “Searching” Input: An integer x and a sequence of integers a 1 , a 2 , . . . , a n (where a i � = 0) terminated by 0. Output: If a i = x then the output is i (if there are several such i then the smallest one), otherwise the output is 0. start: READ LOAD 2 STORE 3 ADD =1 LOAD =1 JUMP loop loop: STORE 2 found: LOAD 2 READ output: WRITE JZERO output HALT SUB 3 JZERO found Z. Sawa (TU Ostrava) Theoretical Computer Science December 9, 2020 15 / 63

Recommend


More recommend