+ CSC110: Tutorial 2 Mr Tony Chung a.chung@lancaster.ac.uk - - PowerPoint PPT Presentation

csc110 tutorial 2 mr tony chung a chung lancaster ac uk
SMART_READER_LITE
LIVE PREVIEW

+ CSC110: Tutorial 2 Mr Tony Chung a.chung@lancaster.ac.uk - - PowerPoint PPT Presentation

+ CSC110: Tutorial 2 Mr Tony Chung a.chung@lancaster.ac.uk http://www.tonychung.net/ + Todays Objectives 2 Microcode (25 mins+) Overview The Programmers Model Examples / Questions Binary Numbers (25 mins, else


slide-1
SLIDE 1

+

CSC110: Tutorial 2

Mr Tony Chung a.chung@lancaster.ac.uk http://www.tonychung.net/

slide-2
SLIDE 2

+ Today’s Objectives

 Microcode (25 mins+)

 Overview  The Programmer’s Model  Examples / Questions

 Binary Numbers (25 mins, else optional)

 Integers and Floating Point  Binary Decimals and Fractions  Conversion  Floating Point Exercises

 Questions / General Help

2

slide-3
SLIDE 3

+ Getting the Simulator to Work

 http://info.comp.lancs.ac.uk/year1/notes/csc131/  Need browser + Java.  Works on Windows and Linux.  Not so good on Mac:

 Edit Microcode.html.  Replace BOTH “width=800” with “width=900”.  Reload.

3

slide-4
SLIDE 4

+ Microcode

 Computers are state machines.

 Some form of signal (clock) moves the machine from one state to

another.

 During the transisition, data flows across buses between hardware

units and logic/arithmetic takes place.

 Units include hardware registers, data banks, external devices

(via ports), etc.

 You will learn more about this in coming csc131 lectures.  There are opportunities to dive in deeper next year in csc363.

 Microcode is the lowest language you are likely to see:

 It deals directly with logic gates and hardware functions.  Assembly code is one level up, which is more likely to be seen.

4

slide-5
SLIDE 5

+ Programmer’s Objectives

 It is rare for a programmer to go this low, but it does happen.  The questions are similar to what you might ask in assembly

  • r other architectures…

 How it works.  Available instructions.  Architecture (registers and buses)  Hardware features

 The rest is down to you!

5

slide-6
SLIDE 6

+ The Microcode API

6

Registers Registers A, B, C and D MPC = Program Counter (current line #) MIR = Line of code being run MAR = Memory address (pointer) MDR = Memory data (data) Hardware Functions Program counter Pointer-based Memory TESTZERO TESTNEG Adder (with - and <<) Control Signals 1 1 Read Register A 2 1 Read Register B 3 1 Read Register C 4 1 Read Register D 5 1 Read Literal 1 6 1 Read MDR 7 1 Perform Subtract 8 1 Perform Shift Left 9 2 Write to Register A 10 2 Write to Register B 11 2 Write to Register C 12 2 Write to Register D 13 2 Write to MDR 14 2 Write to MAR 15 3 Main memory to MDR 16 3 MDR to Main memory 17 4 Read Literal 1 18 4 Read 10 MSB of MIR (current instruction) 19 4 TESTZERO (of register A) 20 4 TESTNEG (of register A) 21 4 Read 4 MSB of MDR (from memory location MPC) 22 4 Read MPC Operation 5 Phases. Control signals used in relevant phase. TESTZERO/TESTNEG work on Register A. TESTZERO/TESTNEG are 1 if true, 2 if false Adder 0 by default. Must update MPC to move on (in Phase 4/5). Numbers are signed.

slide-7
SLIDE 7

+ Addition Unit

 Bus 1 + Bus 2, then:

 Optional subtraction  Optional bitshift

 Result to Bus 3  Can’t directly do C=A+B.

 A and B are on the same bus.  Need to move one of them onto a register on bus 2.  Need TWO instructions to complete this!

7

slide-8
SLIDE 8

+ Pointer Based Memory

 Why?

 Can’t have a register for everything…  Not enough space in instruction to store memory address.

 Two registers: MAR and MDR  MAR: Set this to memory address.  MDR: Use this as intermediate register.  MDR must be explicitly loaded or saved in Phase 3:

 Control Signal 15: Main Memory to MDR  Control Signal 16: MDR to Main Memory  Not automatic.

8

slide-9
SLIDE 9

+ Using The Program Counter

 Program Counter: MPC (Which instruction to running)

 Loaded in Phase 5 with value held in Adder (from Phase 4).  Must explicitly increment it (MPC=MPC(22)+’1’(17).  Else will jump to 0.

 Manipulate for logic or to jump (in Phase 4).

 Jump next if A is zero  Add MPC(22) and TESTZERO(19).  If zero MPC=MPC+2.  Else MPC=MPC+1.  Jump next if A is negative  Add MPC(22) and TESTNEG(20).  If negative MPC=MPC+2.  Else MPC=MPC+1.

9

slide-10
SLIDE 10

+ Question 1: Increment Register D (As a class)

10

slide-11
SLIDE 11

+ Answer 1: Increment Register D

 Objectives:

 Read Register D and ‘1’.  Add together.  Store in Register D.

 Answer:

 Phase 1: 4 (Read D), 5 (Read ‘1’)  Phase 2: 12 (Write D)  Phase 3: -  Phase 4: -  Phase 5: -

11

slide-12
SLIDE 12

+ Question 2: B = C * 2 + 1 (Discussion)

12

slide-13
SLIDE 13

+ Answer 2: B = C * 2 + 1

 Objectives:

 Load Register C.  Bitshift.  Store in Register B.  Increment Register B.

 Answer:

 Phase 1: 3 (Read C), 8 (Shift Left)  Phase 2: 2 (Write B)  Phase 3: -  Phase 4: 22 (Read MPC), 17 (Read ‘1’)  Phase 5: MPC updated for you  Then add 1 to C… (TWO commands in total.)

13

slide-14
SLIDE 14

+ Question 3: Store A * C in #1 (Groups)

14

slide-15
SLIDE 15

+ Answer 3: Store A * C in #1

 Objective (Remember that A and C have been set for you)

 Initialise MDR to zero.  Loop A times, adding C to MDR.  Set MAR (address).  Write MDR to memory.

15

Phase 1 Phase 2 Phase 3 Phase 4 MDR = 0 Nothing (‘0’) 13 Not yet 22, 17 A == 0? 22, 19 t:Goto 5 8,10 18 f:MDR+C>MDR 6,3, 13 22, 17 A = A-1, goto 1 1, 5, 7, 9 17 MAR=‘1’, write 5, 14 16 22, 17

slide-16
SLIDE 16

+ Walkthrough

 Instruction 0:

 Initialise MDR to zero.  Do nothing in phase 1 to get zero.  Phase 2 write to MDR  Need to increment MPC to get to instruction 1.

 Instruction 1:

 Test A to see if it is zero.  A has already been set.  Don’t do anything in phases 1, 2 or 3.  Increment MPC by TESTZERO:  End up at 2 if true. (Then break loop)  End up at 3 if false. (Continue loop)

16

slide-17
SLIDE 17

+ Walkthrough Continues

 Instruction 2:

 Break the loop.  Jump to instruction 5.  Carefully construct upper 10 MSB of instruction.  0000000101  Put 10 MSB into MPC.

 Instruction 3:

 Add C to MDR.  Phase 1 read C and MDR.  Phase 2 write to MDR.  Nothing in Phase 3.  Increment MPC in Phase 4. (Next instruction.)

17

slide-18
SLIDE 18

+ Walkthrough Continues More

 Instruction 4.

 Decrease A and return to top of loop.  Phase 1 read A and ‘1’. Set subtract.  Phase 2 write to A.  Phase 3 do nothing.  Phase 4 read ‘1’. (for instruction 1)

 Instruction 5.

 Set address to 1 and write.  Phase 1 read ‘1’.  Phase 2 write to MAR.  Phase 3 write MAR to memory.  Phase 4 increment MPC (optional*)

18

slide-19
SLIDE 19

+ Question 4: Sum #1 to #10 > #20 (Class discussion)

 Answer on the board…

19

slide-20
SLIDE 20

+ Integer Numbers (if time)

 Integers store whole numbers.

 All integer calculations are truncated, so:  1 / 2 = 0 (not 0.5)  5 / 2 = 2 (not 2.5)  (Remember to be careful with calculations that might produce a

zero before another division: results in a ‘divide by zero’ error.)

 You can store decimals by moving the point

 Ie. Working in pence, rather than pounds.  But still have the calculation problem.

 Floats allow for higher accuracy or much larger numbers…

20

slide-21
SLIDE 21

+ Floating Point Numbers

 Floating point numbers are stored as fraction, base and

exponent.

 Decimal computers are base 2.  The number of bits assigned to ‘f’ and ‘e’ can be changed.  That allows us to store highly accurate small numbers or less

accurate huge numbers.

 Be aware of this accuracy problem! Especially when dealing with

money, power stations, etc!

 Need to review decimals and stuff first…

21

f *be

fraction Exp Variations use coefficient or mantissa.

slide-22
SLIDE 22

+ Handling Fractions

 Positional notation (base 10). Add the columns.  Positional notation (base 2 to base 10). Add the columns.

 Notice each decimal bit is half the previous (think doubling). Ans=6.625.

22

1 5 3 . 3 9 1*10^2 5*10^1 3*10^0 . 3*10^-1 0*10^-2 9*10^-3 1 * 100 5 * 10 3 * 1 . 3 * 1/10 0 * 1/100 9 * 1/1000 100 50 3 . 3/10 0/100 9/1000 100 50 3 . 0.3 0.009 1 1 . 1 1 1*2^2 1*2^1 0*2^0 . 1*2^-1 0*2^-2 1*2^-3 1 * 100 1 * 10 0 * 1 . 1 * 1/10 0 * 1/100 1 * 1/1000 100 10 . 1/10 0/100 1/1000 4 2 . 1 * 0.5 0 * 0.25 1 * 0.125 4 2 . 0.5 0.125

slide-23
SLIDE 23

+ Converting Base 10 to Base 2

 Conversion is a matter of chosing whether or not to include a

bit… So for 37…

 Now do 64, 8 and 19…..  Conversion algorithm from chortle.ccsu.edu (see later)

place = 0 while( number > 0 ){ digit[place] = number % 2; number = number / 2; place++ }

23

64 32 16 8 4 2 1 1 1 1

slide-24
SLIDE 24

+ Converting Base 10 Decimals to Base 2

 Method:

 Double and test…  Stop at 0 or capacity (some go on for ever…)

 Example: 0.625  Then do 0.675 and 0.889

24

Decimal Binary 0.625 0. 0.625 * 2 1.250 0.1 0.25 * 2 0.5 0.10 0.5 * 2 1.0 0.101

slide-25
SLIDE 25

+ Floating Point Questions

 Given an 8-bit representation with 5-bits for fraction and 3

  • bits for exponent, what is the largest number that can be

stored?

 What is the next closest number that can be stored?  Answer these for a 4/4 representation – what is different?  Answers for next week…

25

slide-26
SLIDE 26

+ Questions

 Some of the material today is based on content at:

 http://chortle.ccsu.edu/AssemblyTutorial/  Chapter 29 in particular.

 Please email topic suggestions.

26