+
CSC110: Tutorial 2
Mr Tony Chung a.chung@lancaster.ac.uk http://www.tonychung.net/
+ 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
Mr Tony Chung a.chung@lancaster.ac.uk http://www.tonychung.net/
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
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
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
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
How it works. Available instructions. Architecture (registers and buses) Hardware features
The rest is down to you!
5
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.
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
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
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
10
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
12
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
14
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
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
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
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
Answer on the board…
19
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
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
fraction Exp Variations use coefficient or mantissa.
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
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
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
Given an 8-bit representation with 5-bits for fraction and 3
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
Some of the material today is based on content at:
http://chortle.ccsu.edu/AssemblyTutorial/ Chapter 29 in particular.
Please email topic suggestions.
26