csc110 tutorial 2 mr tony chung a chung lancaster ac uk
play

+ 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


  1. + CSC110: Tutorial 2 Mr Tony Chung a.chung@lancaster.ac.uk http://www.tonychung.net/

  2. + Today’s Objectives 2  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

  3. + Getting the Simulator to Work 3  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.

  4. + Microcode 4  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.

  5. + Programmer’s Objectives 5  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 or other architectures…  How it works.  Available instructions.  Architecture (registers and buses)  Hardware features  The rest is down to you!

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

  7. + Addition Unit 7  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!

  8. + Pointer Based Memory 8  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.

  9. + Using The Program Counter 9  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.

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

  11. + Answer 1: Increment Register D 11  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: -

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

  13. + Answer 2: B = C * 2 + 1 13  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.)

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

  15. + Answer 3: Store A * C in #1 15  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. 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

  16. + Walkthrough 16  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)

  17. + Walkthrough Continues 17  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.)

  18. + Walkthrough Continues More 18  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*)

  19. + Question 4: Sum #1 to #10 > #20 19 (Class discussion)  Answer on the board…

  20. + Integer Numbers (if time) 20  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…

  21. + Floating Point Numbers 21  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… f * b e fraction Exp Variations use coefficient or mantissa.

  22. + Handling Fractions 22  Positional notation (base 10). Add the columns. 1 5 3 . 3 0 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 0.009  Positional notation (base 2 to base 10). Add the columns.  Notice each decimal bit is half the previous (think doubling). Ans=6.625. 1 1 0 . 1 0 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 0 . 1/10 0/100 1/1000 4 2 0 . 1 * 0.5 0 * 0.25 1 * 0.125 4 2 0 . 0.5 0 0.125

  23. + Converting Base 10 to Base 2 23  Conversion is a matter of chosing whether or not to include a bit… So for 37… 64 32 16 8 4 2 1 0 1 0 0 1 0 1  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++ }

  24. + Converting Base 10 Decimals to 24 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 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

  25. + Floating Point Questions 25  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…

  26. + Questions 26  Some of the material today is based on content at:  http://chortle.ccsu.edu/AssemblyTutorial/  Chapter 29 in particular.  Please email topic suggestions.

Recommend


More recommend