Mod odule 10: A A Wor orking Com omputer 1 CP CPSC SC 121: - - PowerPoint PPT Presentation

mod odule 10 a a wor orking com omputer
SMART_READER_LITE
LIVE PREVIEW

Mod odule 10: A A Wor orking Com omputer 1 CP CPSC SC 121: - - PowerPoint PPT Presentation

Mod odule 10: A A Wor orking Com omputer 1 CP CPSC SC 121: 121: the BI BIG ques questions ns How can we build a computer that is able to execute a user-defined program? We are finally able to answer this question. Our


slide-1
SLIDE 1

Mod

  • dule 10:

A A Wor

  • rking Com
  • mputer

1

slide-2
SLIDE 2

CP CPSC SC 121: 121: the BI BIG ques questions ns

  • How can we build a computer that is able to

execute a user-defined program?

  • We are finally able to answer this question.
  • Our answer builds up on many of the topics you

learned about in the labs since the beginning of the term.

2

slide-3
SLIDE 3

Le Learn rning goals: : in-cl class

  • Specify the overall architecture of a (Von Neumann)

stored program computer – an architecture where both program and data are bits (i.e., state) loaded and stored in a common memory.

  • Trace execution of an instruction through a working

computer in a logic simulator (currently logisim): the basic fetch-decode-execute instruction cycle and the data flow to/from the arithmetic logic unit (ALU), the main memory and the Program Counter (PC).

  • Feel confident that, given sufficient time, you could

understand how the circuit executes machine- language instructions.

3

slide-4
SLIDE 4

Mo Module 10 10 Outline

  • A little bit of history
  • Implementing a working computer
  • Appendix

4

slide-5
SLIDE 5

Th The programmable loom

5

slide-6
SLIDE 6

Th The Difference Engine

6

slide-7
SLIDE 7

Z1 Z1, Z2 Z2, Z3 Z3, …

7

slide-8
SLIDE 8

Th The ENIAC

8

slide-9
SLIDE 9

9

slide-10
SLIDE 10

Manchester Small-Scale Experimental Machine

10

slide-11
SLIDE 11

A A qui quick k roadm dmap p thr hrough ugh our ur cour urse ses

  • CPSC 121: learn about gates, and how we can use

them to design a circuit that executes very simple instructions.

  • CPSC 213: learn how the constructs available in

languages such as Racket, C, C++ or Java are implemented using these simple instructions.

  • CPSC 313: learn how we can design computers

that execute programs efficiently and meet the needs of modern operating systems.

11

slide-12
SLIDE 12

Mo Module 10 10 Outline

  • A little bit of history
  • Implementing a working computer
  • Appendix

12

slide-13
SLIDE 13

Vo Von-Ne Neumann a arch chitect cture

Memory (contains both programs and data). Control Unit Arithmetic & Logic Unit CPU (Central Processing Unit) Input/Output

13

slide-14
SLIDE 14

Me Memo mory

  • Contains both instructions and data.
  • Divided into a number of memory locations
  • Think of positions in a list: (list-ref mylist pos)
  • Or in an array: myarray[pos] or arrayList:

arrayl.get(pos).

...

1 2 3 4 5 6 7 8 9 10 11

...

01010111

14

slide-15
SLIDE 15

Me Memo mory

  • Each memory location contains a fixed number of

bits.

  • Most commonly this number is 8.
  • Values that use more than 8 bits are stored in

multiple consecutive memory locations.

  • Characters use 8 bits (ASCII) or 16/32 (Unicode).
  • Integers use 32 or 64 bits.
  • Floating point numbers use 32, 64 or 80 bits.

15

slide-16
SLIDE 16

Th The arithmetic and logic unit

  • Arithmetic and Logic Unit
  • Performs arithmetic and logical operations (+, -, *,

/, and, or, etc).

16

slide-17
SLIDE 17

Th The control unit

  • Control Unit
  • Decides which instructions to execute.
  • Executes these instructions sequentially.
  • Not quite true, but this is how it appears to the

user.

17

slide-18
SLIDE 18

Ou Our worki king g co computer

  • Implements the design presented in the textbook by

Bryant and O'Hallaron (used for CPSC 213/313).

  • A small subset of the IA32 (Intel 32-bit) architecture.
  • It has 12 types of instructions.
  • One program counter register (PC)
  • contains the address of the next instruction.
  • 8 general-purpose 32-bit registers
  • each of them contains one 32 bit value.
  • used for values that we are currently working with.

18

slide-19
SLIDE 19

Ex Exampl ple ins nstruc uctions ns

irmovl 0x1A, %ecx irmovl V, rB R[rB] ← V

  • What does this instruction do based on its

documentation above?

  • a. It adds the constant 0x1A to the value in %ecx.
  • b. It stores the constant 0x1A in %ecx.
  • c. It takes the value at the memory address 0x1A

and stores it in %ecx.

19

slide-20
SLIDE 20

Ex Exampl ple ins nstruc uctions ns

irmovl 0x1A, %ecx irmovl V, rB R[rB] ← V

  • This instruction stores a constant in a register.
  • In this case, the value 1A (hexadecimal) is stored

in %ecx.

20

slide-21
SLIDE 21

Ex Exampl ple ins nstruc uctions ns

subl %eax, %ebx subl rA, rB R[rB] ← R[rB] − R[rA]

  • What does this instruction do based on its

documentation above?

  • a. It calculates the value of %eax minus the value of

%ebx and stores the result in %eax.

  • b. It calculates the value of %eax minus the value of

%ebx and stores the result in %ebx.

  • c. It calculates the value of %ebx minus the value of

%eax and stores the result in %eax.

  • d. It calculates the value of %ebx minus the value of

%eax and stores the result in %ebx.

21

slide-22
SLIDE 22

Ex Exampl ple ins nstruc uctions ns

subl %eax, %ebx subl rA, rB R[rB] ← R[rB] − R[rA]

  • The subl instruction subtracts its arguments.
  • The names %eax and %ebx refer to two registers.
  • This instruction takes the value contained in %eax,

subtracts it from the value contained in %ebx, and stores the result back in %ebx.

22

slide-23
SLIDE 23

Ex Exampl ple ins nstruc uctions ns

rmmovl %ecx, $8(%ebx) rmmovl rA, D(rB) M[D + R[rB]] ← R[rA]

  • What does this instruction do based on its

documentation above?

  • a. It reads the value in %ebx, adds $8 to it, and stores

the result into %ecx.

  • b. It reads the value in %ecx, stores it in the register

given by the value of %ebx plus $8.

  • c. It reads the value in %ebx, adds $8 to it and stores it

in the memory address given by the value in %ecx.

  • d. It reads the value in %ecx, and stores it in the

memory location given by the value in %ebx plus $8.

23

slide-24
SLIDE 24

Ex Exampl ple ins nstruc uctions ns

rmmovl %ecx, $8(%ebx) rmmovl rA, D(rB) M[D + R[rB]] ← R[rA]

  • The rmmovl instruction stores a value into

memory (Register to Memory Move).

  • In this case it takes the value in register %ecx.
  • And stores it in the memory location whose

address is:

  • The constant 8
  • PLUS the current value of register %ebx.

24

slide-25
SLIDE 25

Ex Exampl ple ins nstruc uctions ns

jge $1000 jge Dest PC ← Dest if last result ≥ 0

25

slide-26
SLIDE 26

Ex Exampl ple ins nstruc uctions ns

jge $1000

  • This is a conditional jump instruction.
  • It checks to see if the result of the last arithmetic or

logic operation was zero or positive (Greater than or Equal to 0).

  • If so, the next instruction is the instruction stored in

memory address 1000 (hexadecimal).

  • If not, the next instruction is the instruction that

follows the jge instruction.

  • Documentation:
  • jge Dest

PC ← Dest if last result ≥ 0

26

slide-27
SLIDE 27

In Inter erpr preting ting an an ins instr truc uctio tion

  • How does the computer know which instruction

does what?

  • Each instruction is a sequence of 8 to 48 bits.
  • Some of the bits tell it which instruction it is.
  • Other bits tell it what operands to use.
  • These bits are used as select inputs for several

multiplexers.

27

slide-28
SLIDE 28

In Inter erpr preting ting an an ins instr truc uctio tion

Example 1: subl %eax, %ebx Represented by 6103 (hexadecimal) %ebx %eax subtraction arithmetic or logic operation (the use of “6” to represent them instead of 0 or F or any

  • ther value is completely arbitrary).

28

slide-29
SLIDE 29

In Inter erpr preting ting an an ins instr truc uctio tion

Example 2: irmovl 0x35, %ebx Represented by 30F300000035 (hexadecimal) 0x35 %ebx no register here ignored move constant into a register

29

slide-30
SLIDE 30

In Inter erpr preting ting an an ins instr truc uctio tion

Example 2: rmmovl %ecx, $8(%ebx) Represented by 401300000008 (hexadecimal) $8 %ebx %ecx ignored register to memory move

30

slide-31
SLIDE 31

Si Six stages of executing an instru ruction

  • 1. Fetch: read instruction from memory and decide
  • n new PC value
  • 2. Decode: read values from registers
  • 3. Execute: use the ALU to perform computations
  • 1. Some of them are obvious from the instruction (e.g.

subl)

  • 2. Other instructions use the ALU as well (e.g. rmmovl)
  • 4. Memory: read data from or write data to memory
  • 5. Write-back: store value(s) into register(s).
  • 6. PC update: store the new PC value.

Not all stages do something for every instruction.

31

slide-32
SLIDE 32

Ex Execut uting ng an n ins nstruc uction

Example 1: irmovl 0x35, %ebx

  • 1. Fetch:
  • a. current instruction ← 30F100000035
  • b. next PC value ← current PC value + 6
  • 2. Decode: nothing needs to be done
  • 3. Execute: valE ← valC
  • 4. Memory: nothing needs to be done
  • 5. Write-back: %ebx ← valE
  • 6. PC update: PC ← next PC value

32

slide-33
SLIDE 33

Ex Execut uting ng an n ins nstruc uction

Example 2: subl %eax, %ebx

  • 1. Fetch:
  • a. current instruction ← 6103
  • b. next PC value ← current PC value + 2
  • 2. Decode:
  • a. valA ← value of %eax
  • b. valB ← value of %ebx
  • 3. Execute: valE ← valB – valA
  • 4. Memory: nothing needs to be done.
  • 5. Write-back: %ebx ← valE
  • 6. PC update: PC ← next PC value

33

slide-34
SLIDE 34

Ex Execut uting ng an n ins nstruc uction

Example 3: rmmovl %ecx, $8(%ebx)

  • 1. Fetch:
  • a. current instruction ← 401300000008
  • b. next PC value ← current PC value + 6
  • 2. Decode:
  • a. valA ← value of %ecx
  • b. valB ← value of %ebx
  • 3. Execute: valE ← valB + 00000008
  • 4. Memory: M[valE] ← valA
  • 5. Write-back: nothing needs to be done
  • 6. PC update: PC ← next PC value

34

slide-35
SLIDE 35

Sa Samp mple program

irmovl $3,%eax irmovl $23, %ebx irmovl $facade, %ecx subl %eax, %ebx rmmovl %ecx, $8(%ebx) halt

35

slide-36
SLIDE 36

Th The Fetch stage

  • Fetch: read instruction and decide on new PC

value

  • During the Fetch stage, which component of the

computer do we work with?

  • a. Memory
  • b. Arithmetic and logic unit
  • c. The 8 general-purpose registers
  • d. The program counter register
  • e. None of the above

36

slide-37
SLIDE 37

Th The Decode stage

  • Decode: read values from registers
  • During the Decode stage, which component of the

computer do we work with?

  • a. Memory
  • b. Arithmetic and logic unit
  • c. The 8 general-purpose registers
  • d. The program counter register
  • e. None of the above

37

slide-38
SLIDE 38

Th The Execute stage

  • Execute: use the ALU to perform computation.

Some of them are obvious from the instruction (e.g. subl). Other instructions use the ALU as well (e.g. rmmovl)

  • During the Execute stage, which component of

the computer do we work with?

  • a. Memory
  • b. Arithmetic and logic unit
  • c. The 8 general-purpose registers
  • d. The program counter register
  • e. None of the above

38

slide-39
SLIDE 39

Th The Memory stage

  • Memory: read data from or write data to memory
  • During the Memory stage, which component of

the computer do we work with?

  • a. Memory
  • b. Arithmetic and logic unit
  • c. The 8 general-purpose registers
  • d. The program counter register
  • e. None of the above

39

slide-40
SLIDE 40

Th The Write-ba back stage

  • Write-back: store value(s) into register(s).
  • During the Write-back stage, which component of

the computer do we work with?

  • a. Memory
  • b. Arithmetic and logic unit
  • c. The 8 general-purpose registers
  • d. The program counter register
  • e. None of the above

40

slide-41
SLIDE 41

Th The PC Update stage

  • PC update: store the new PC value.
  • During the PC Update stage, which component of

the computer do we work with?

  • a. Memory
  • b. Arithmetic and logic unit
  • c. The 8 general-purpose registers
  • d. The program counter register
  • e. None of the above

41

slide-42
SLIDE 42

Si Six stages of executing an instru ruction

  • 1. Fetch: read instruction from memory and decide
  • n new PC value
  • 2. Decode: read values from registers
  • 3. Execute: use the ALU to perform computations
  • 1. Some of them are obvious from the instruction (e.g.

subl)

  • 2. Other instructions use the ALU as well (e.g. rmmovl)
  • 4. Memory: read data from or write data to memory
  • 5. Write-back: store value(s) into register(s).
  • 6. PC update: store the new PC value.

Not all stages do something for every instruction.

42

slide-43
SLIDE 43

Mo Module 10 10 Outline

  • A little bit of history
  • Implementing a working computer in Logisim
  • Appendix

43

slide-44
SLIDE 44

Re Registers and the memory

  • Registers (32 bits each):
  • Instructions that only need one register use F for

the second register.

  • %esp is used as stack pointer.
  • Memory contains 232 bytes; all memory accesses

load/store 32 bit words.

%eax %esp %ecx %ebp %edx %esi %ebx %edi 1 2 3 4 5 6 7

44

slide-45
SLIDE 45

Re Register/memory transfers:

  • rmmovl rA, D(rB)
  • M[D + R[rB]] ← R[rA]
  • Example: rmmovl %edx, 20(%esi)
  • mrmovl D(rB), rA
  • R[rA] ← M[D + R[rB]]

45

slide-46
SLIDE 46

Data transfer and arithmetic c instruct ctions

  • Other data transfer instructions
  • rrmovl rA, rB

R[rB] ← R[rA]

  • irmovl V, rB

R[rB] ← V

  • Arithmetic instructions
  • addl rA, rB

R[rB] ← R[rB] + R[rA]

  • subl rA, rB

R[rB] ← R[rB] − R[rA]

  • andl rA, rB

R[rB] ← R[rB] ∧ R[rA]

  • xorl rA, rB

R[rB] ← R[rB] ฀ R[rA]

46

slide-47
SLIDE 47

Jum Jumps

  • Unconditional jumps
  • jmp Dest

PC ← Dest

  • Conditional jumps
  • jle Dest

PC ← Dest if last result ≤ 0

  • jl Dest

PC ← Dest if last result < 0

  • je Dest

PC ← Dest if last result = 0

  • jne Dest

PC ← Dest if last result ≠ 0

  • jge Dest

PC ← Dest if last result ≥ 0

  • jg Dest

PC ← Dest if last result > 0

47

slide-48
SLIDE 48

Co Conditional mo moves

  • cmovle rA, rB

R[rB] ← R[rA] if last result ≤ 0

  • cmovl rA, rB

R[rB] ← R[rA] if last result < 0

  • cmove rA, rB

R[rB] ← R[rA] if last result = 0

  • cmovne rA, rB

R[rB] ← R[rA] if last result ≠ 0

  • cmovge rA, rB

R[rB] ← R[rA] if last result ≥ 0

  • cmovg rA, rB

R[rB] ← R[rA] if last result > 0

48

slide-49
SLIDE 49

Pr Procedure calls and return support

  • call Dest
  • R[%esp]←R[%esp]-4; M[R[%esp]]←PC;

PC←Dest;ret PC←M[R[%esp]]; [%esp]←R[%esp]+4

  • pushl rA
  • R[%esp]←R[%esp]-4; M[R[%esp]]←R[rA]
  • popl rA
  • R[rA]←M[R[%esp]]; R[%esp]←R[%esp]+4
  • Others: halt, nop

49

slide-50
SLIDE 50

Ins Instr truc uctio tion n formats ts

halt nop cmovXX rA, rB irmovl V, rB rmmovl rA, D(rB) mrmovl D(rB), rA OPI rA, rB jXX Dest call Dest ret pushl rA popl rA 1 0 9 0 2 fn r rB B 0 rA F 6 fn rA rB A 0 rA F 3 0 F rB V 4 0 rA rB D 5 0 rA rB D 8 0 Dest 7 fn Dest 1 2 3 4 5

50

slide-51
SLIDE 51

Ins Instr truc uctio tion n formats ts

  • Arithmetic instructions:
  • addl → fn = 0

subl → fn = 1

  • andl → fn = 2

xorl → fn = 3

  • Conditional jumps and moves:
  • jump → fn = 0

jle → fn = 1

  • jl → fn = 2

je → fn = 3

  • jne → fn = 4

jge → fn = 5

  • je → fn = 6

51