43
Number Formats Represent negative and positive numbers as bits Option: Use base 2 with a sign bit 1 means negative 0 means positive Problem: 2 0's 0 and -0 Option: Use base -2 3 111 = 4+-2+1 5 101 = 4+1 Problem: hard to tell if a number is positive or negative Problem: different number of negative and positive numbers 4 bit numbers From 1010 -10 To 0101 5 -6 = 1110 -8 4 -2 1 Option 1's complement Complement - reverse 0's and 1's Problem: 0 and -0 Option 2's complement 1's complement +1 Only 1 0! 2/10 Wednesday, January 29, 2014 1:45 PM csc260 Page 1

2/10 - Nassau Community College · 3 "Andrew Tanner" Zip uses a generic ... If each gate takes 1 ns the full adder takes 2ns ... Cache is faster and more local memory for temporary

  • Upload
    lydieu

  • View
    218

  • Download
    4

Embed Size (px)

Citation preview

Number FormatsRepresent negative and positive numbers as bits

Option:Use base 2 with a sign bit1 means negative 0 means positiveProblem: 2 0's 0 and -0

Option:Use base -23111 = 4+-2+15101 = 4+1Problem: hard to tell if a number is positive or negativeProblem: different number of negative and positive numbers4 bit numbers From1010 -10To 0101 5

-6 = 1110 -8 4 -2 1

Option 1's complementComplement - reverse 0's and 1'sProblem: 0 and -0

Option 2's complement1's complement +1Only 1 0!

2/10Wednesday, January 29, 20141:45 PM

csc260 Page 1

Only 1 0!Problem 10000… is undefined or arbitrarily defined Solution - don't use that one or use it for errorsMany computers just say it represents largest negative number

6 bit represents from -31 to 3115001111 = 8+4+2+1-15110000+1 = 110001

-15-

001110+1 = 001111

6 bit -8 8=001000110111+1 = 111000000111+1 = 001000

Each bit doubles10 bits ~ 100020 bits ~ 100000032 bits ~ 400000000064 bits ~ 18000000000000000000

If addressing memory 32 bits run out of address' after 4 gbytes64 can address up to around 18 exabytes

1 byte can hold up to 256 charactersUnicode tries to represent every character system on planet with 2 bytesProbably fails

More bits can represent more exponentially.Each bit slows down communication and computation.More bits, more errors.Error correcting codes can prevent or fix errors (but then more

csc260 Page 2

Error correcting codes can prevent or fix errors (but then more computation)

More bits need more storage.

Using the least bits possible speeds things up and fit into smaller storage.Using too few bits can be disastrous.

If we used 2 bytes to store file size then none of our files can be larger than 16 kbytes

Report 1 due 2/16Project 1 due 3/1

http://kipirvine.com/asm/workbook/floating_tut.htm

Floating point3 = 11*2^0= 1.1*2^1Sign bit = 0Exponent = 1+127 = 128 Mantissa .10 10000000 10000000000000000000000

csc260 Page 3

the American Mathematical Association of Two-Year Colleges (AMATYC) Student Mathematics League contest will be held on Thursday, February 27, 2014, in room B-213 during Club Hour.

The level of the test is precalculus mathematics. The test contains 20 questions drawn from a standard syllabus in College Algebra and Trigonometry and may involve precalculus algebra, trigonometry, synthetic and analytic geometry, and probability; questions that are completely self-contained may be included as well. All questions are short-answer or multiple choice.

Calculators are allowed, including graphing calculators, provided they do not have a typewriter-like keyboard (for example, the TI-92) or a disk drive.

Past contest questions and answers are available at:http://www.amatyc.org/?page=SMLPastQuestions

For interested students, a sign-up sheet is posted outside my office, room B-3060.

AMATYC contestWednesday, February 12, 20147:27 AM

csc260 Page 4

-8888=0101100088-64 = 2424-16=8 Complement=101001112's Complement = 10101000

15 in 32 bit float1111.0 * 2^0Normalize1.111 * 2^30 10000010 11100000000000000000000

1/5248-5 = 3 6-5 = 1 1.1001 * 2^-30 01111100 10011001100110011001100

22/7 = 3.1429

Binary scientific11.001 *2^0

248-7=1 1.100*2^10 10000000 10010010010010010010010

HWTranslate to float

0.0011*2^0

2/24Monday, February 24, 20145:10 PM

csc260 Page 5

Translate to float-88-88/13.01

Translate from float10111111011010000000000000000000

Compression (using less bits)

Bits that are always 1 or always 0 don't need to be represented.

If there are a small number of bit patterns (numbers) to represent you can make a table and refer to them by indices.

1/3 "Yellow" 3.14159255 "Andrew Tanner"

0 1/3

1 "Yellow"

2 3.14159255

3 "Andrew Tanner"

Zip uses a generic compression algorithm (works on any size data).Generic compression algorithms take advantage of nonrandomness. For example smaller numbers are more likely than bigger ones, numbers and other bit patterns repeat frequently.

HardwareBus - transmits info between parts of the computerTwice as many wires (bits) twice as much infoTwice as much space, power and heat.

Print technology builds circuitry up till recently meant all

csc260 Page 6

Print technology builds circuitry up till recently meant all hardware was flat.

Heat. Need a way for heat 2 escape.

Gates do the computation in computersGates are built from transistorsTransistors are amplifiers.

Screen clipping taken: 2/24/2014 7:30 PM

Transistors are made of material that wants to be a conductor but doesn't have enough electrons to pull it off.Without electrons it is an insulatorSupplied with electrons it becomes a conductor.

Simplest gate is a Not gate (inverter)

⏚Pasted from <http://en.wikipedia.org/wiki/Ground_(electricity)>

resistance 0 stops the signal 1 lets it through

csc260 Page 7

Pasted from <http://en.wikipedia.org/wiki/Ground_(electricity)>

HW read 3.1.1

csc260 Page 8

quiz show the bits for -6.3 in 32 bit floating point

Pasted from <https://twitter.com/search?q=%40math4sher&src=typd>

1 sign bit

110. ? = 6.?.6 01.2 1.4 0.8 01.6 11.2 1.4 0.8 0

110.01001 *2^01.1001001*2^2

1 10000001 10010011001100110011001

Screen clipping taken: 3/3/2014 5:37 PM

Screen clipping taken: 3/3/2014 6:46 PM

3/3Monday, March 03, 20145:03 PM

csc260 Page 9

Screen clipping taken: 3/3/2014 5:39 PM

Xor gate

A B X

1 1 0

1 0 1

0 1 1

0 0 0

True if different

3 bit twos complement3 0112 0101 0010 000-1 111-2 110-3 101

Is X positive is X even Is X = Y

Is X (5 bit) prime

S 8 4 2 1

csc260 Page 10

Is X (5 bit) prime

S 8 4 2 1

0 0 0 1 0

0 0 0 1 1

0 0 1 0 1

0 0 1 1 1

0 1 0 1 1

0 1 1 0 1

HW Take a 5 bit two's complement number and write the gates to determine if it is a multiple of 3. See if there is a smaller set of gates that can do the same thing.

csc260 Page 11

If each gate takes 1 ns the full adder takes 2nsTo add two 32 bit numbers the 32 adders must run sequentially1 at a time.So a 32 bit adder takes 64 ns.There are ways to speed that up.

ALU can do a variety of operations. A control bit string tells it which operation to do.

System clock 1 clock cycle is long enough so that every operation will give consistent results.Some operations may be documented to take more than 1 cycle.

Screen clipping taken: 3/3/2014 7:10 PM

Asynchronous circuits tell you when they are ready.

csc260 Page 12

Every nontrivial computer is synchronous.Communication between computers is usually asynchronous.HW read 3.4.2 to 3.4.4

csc260 Page 13

100 1.

127 64 32 16 8 4 2 1

0 1 1 0 0 1 0 0

10011011 complement +110011100 2's complementCheck01100011+1=01100100

0.0011102.4/9 08/9 016/9 -> 7/9 114/9 -> 5/9 110/9 -> 1/9 12/9 01.110001*2^-3Sign 124 = 127+-31 01111100 11000111000111000111000

8 4 2 13.

3/10Monday, March 10, 20144:54 PM

csc260 Page 14

csc260 Page 15

Screen clipping taken: 3/10/2014 5:46 PM

Screen clipping taken: 3/10/2014 5:47 PM

Pg 181

csc260 Page 16

4.

Read is still 3 cycles at 8 ns because all of these still fit.

If 2ns3 cycles before memory address is ready.Need one more cycle at end while holding data for cpuInstead of 3 cycles need 7 cycles.

Add instructionSp-1 to MAR and SP; readTos to HMDR+H to TOS and MDR; writePC+1 to PC got execute instruction

Screen clipping taken: 3/10/2014 6:18 PM

csc260 Page 17

Screen clipping taken: 3/10/2014 6:55 PM

Screen clipping taken: 3/10/2014 6:58 PM

Micro instructionBunch of these implement an assembly language

Screen clipping taken: 3/10/2014 6:18 PM

csc260 Page 18

Screen clipping taken: 3/10/2014 7:25 PM

csc260 Page 19

Screen clipping taken: 3/10/2014 7:37 PM

HW write a micro instruction that implements the max of the two top items on the stack.

csc260 Page 20

X & 1X & 2Char getBitN(n) { return (x&(1<<n))==1?'1':'0'; }

000000001 <<3000001000

3/17Monday, March 17, 20145:01 PM

csc260 Page 21

csc260 Page 22

Swap top 2 members of stackSwap1 mar =sp-1; rd read 2nd location on stackSwap2 h = tos safeguard old topSwap3 tos = mdr 2nd location data now goes to tosSwap4 mdr = h ; wr write the old top of stack to second locationSwap5 wait for memorySwap6 mdr = tos write the top of stackSwap7 mar = sp; wr; goto Main1

csc260 Page 23

30.3 = 11110.0100111110 = 30.6 01.2 1.4 0.8 01.6 11.2 10011

1.111001001*2^40 10000011 11100100110011001100110

3/24Monday, March 24, 20145:11 PM

csc260 Page 24

Screen clipping taken: 3/24/2014 5:11 PM

Isum1 Mar = SP=SP-1; rd read in next to top word on stackIsum2 H = TOSIsum3 H = MDR + H H has sum of top 2 items on stackIsum4 Mar = SP = SP-1; rd read 3rd item on stackIsum5 Isum6 MDR = TOS = MDR+H; wr; goto Main1 sum of top 3 written to stack

How would you write imax (stack top becomes maximum of top 2 items)Imax1 Mar = SP =SP-1; rdImax2 H = TOSImax3 MDR - H; if(N) goto keepTop1 else goto newTop1

keepTop1 MDR = H; wr; goto Main1

newTop1 TOS = MDR; goto Main1

csc260 Page 25

ComponentsPeripherals: everything but cpu and memory

Kinds peripherals: storage, user I/O, network I/O

Most peripherals communicates by memory mapping (some part or page of memory is devoted to the peripheral).

Most High level languages try to hide this in I/O packages (drivers).

Every computer is I/O bound. Its speed is determined by the speed of peripherals.

User I/O slowestStorage fastestNetwork in between

Cache is faster and more local memory for temporary copies that mirror Less local slower peripherals.

Caches use locality to work.

User I/O : displays, audio, key input, touch input, audio input, sensorsNetwork I/O: lan, wifi, mobile networks, internet, local nets, gps, satellite, bluetoothStorage: hard disk, media, electronic disk, network disks

Report 2 due 4/5

csc260 Page 26

Imax1 Mar = SP=SP-1; rd read in next to top word on stackImax2 H = TOSImax3 MDR-H if(N) imax4 else pop3Imax4 MDR=H; wr; goto Main1

TestJasmin.java/** * some test code in java to test out jasmin*/

public class TestJasmin

public static void main(String[] argv)

int count = 0;while(count < 10)

System.out.println(count);count++;

{

} // end while

{

} // entering Canada

{

} // end TestJasmin

TestJasmin.j; test out writing jasmin code; translate TestJasmin.java into jvm assembly

.source TestJasmin.java

.class public test/TestJasmin

.super java.lang.Object

3/31Monday, March 31, 20145:14 PM

csc260 Page 27

; default constructor .method <init>()V.limit stack 1.limit locals 1

aload_0invokespecial java/lang/Object/<init>()Vreturn

.line 5

.end method

.method public static main([Ljava/lang/String;)V

.limit stack 3

.limit locals 3

getstatic java/lang/System/out Ljava/io/PrintStream;

aload 1ldc "Hello"invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

astore 1

; count will be location 2iconst_0istore_2 ; initialize count =0

.line 9

iload_2sipush 10if_icmpge EndLoop

Loop:.line 10

aload 1 ; System.outiload_2 ; countinvokestatic java/lang/String/valueOf(I)Ljava/lang/String; ; translate count into a stringinvokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

.line 12

csc260 Page 28

java/io/PrintStream/println(Ljava/lang/String;)V

iload_2 ; counticonst_1 iaddistore 2 ; count++

.line 13

goto LoopEndLoop:return

.line 14

.end method

csc260 Page 29

Ones1 H=11.Ones2 MDR =0Ones3 TOS AND H if(Z) goto Ones5 else Ones4Ones4 MDR = MDR+1Ones5 TOS = TOS >> 1 if(Z) goto Ones6 else goto Ones3Ones6 TOS = MDROnes7 MAR = SP; wr; goto Main1

0.011101100010 * 2^02.12/13 011/13 19/13 15/13 1

10/13 0 7/13 1 1/13 1 2/13 0 4/13 0 8/13 0 3/13 1 6/13 01.110110001001 * 2^-20 01111101 11011000100111011000100

3.

S 2 1 S 2 1 S 4 2 1

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 1 0 0 0 1

0 0 0 0 1 0 0 0 1 0

0 0 0 0 1 1 0 0 1 1

0 0 0 1 0 1 1 1 0 1

0 0 0 1 1 0 1 1 1 0

4/7Monday, April 07, 20145:11 PM

csc260 Page 30

0 0 0 0 1 1 0 0 1 1

0 0 0 1 0 1 1 1 0 1

0 0 0 1 1 0 1 1 1 0

0 0 0 1 1 1 1 1 1 1

0 0 1 0 0 0 0 0 0 1

0 0 1 0 0 1 0 0 1 0

0 0 1 0 1 0 0 0 1 1

0 0 1 0 1 1 0 1 0 0

0 0 1 1 0 1 1 1 1 0

0 0 1 1 1 0 1 1 1 1

0 0 1 1 1 1 0 0 0 0

0 1 0 0 0 0 0 0 1 0

0 1 0 0 0 1 0 0 1 1

0 1 0 0 1 0 0 1 0 0

0 1 0 0 1 1 0 1 0 1

0 1 0 1 0 1 1 1 1 1

0 1 0 1 1 0 0 0 0 0

0 1 0 1 1 1 0 0 0 1

0 1 1 0 0 0 0 0 1 1

0 1 1 0 0 1 0 1 0 0

0 1 1 0 1 0 0 1 0 1

0 1 1 0 1 1 0 1 1 0

0 1 1 1 0 1 0 0 0 0

0 1 1 1 1 0 0 0 0 1

0 1 1 1 1 1 0 0 1 0

1 0 1 0 0 0 1 1 0 1

1 0 1 0 0 1 1 1 1 0

1 0 1 0 1 0 1 1 1 1

1 0 1 0 1 1 0 0 0 0

1 0 1 1 0 1 1 0 1 0

1 0 1 1 1 0 1 0 1 1

1 0 1 1 1 1 1 1 0 0

1 1 0 0 0 0 1 0 1 0

1 1 0 0 0 1 1 1 1 1

1 1 0 0 1 0 0 0 0 0

1 1 0 0 1 1 0 0 0 1

1 1 0 1 0 1 1 0 1 1

1 1 0 1 1 0 1 1 0 0

1 1 0 1 1 1 1 1 0 1

1 1 1 0 0 0 1 1 1 1

1 1 1 0 0 1 0 0 0 0

1 1 1 0 1 0 0 0 0 1

1 1 1 0 1 1 0 0 1 0

1 1 1 1 0 1 1 1 0 0

1 1 1 1 1 0 1 1 0 1

1 1 1 1 1 1 1 1 1 0

1 more set of gates when 2nd sign bit is 1

csc260 Page 31

Project 3 due 5/3

Test.java:import dataStructure.Stack;import java.util.*;import javax.swing.JOptionPane;

/** driver to test stack class */public class TestStack

public static void main(String[] args)

Stack tester = new Stack();try

for(;;) // user interaction loop

String stackState;String[] options = {"Push","Pop"};if(tester.isEmpty()) stackState = "Stack is Empty";else stackState = "Stack Top is "+tester.getTop();switch( JOptionPane.showOptionDialog(null,stackState,"Push or Pop",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null,options,options[0]))

tester.push(JOptionPane.showInputDialog(null,"Push: "));

case 0: // user wants to push

break;

tester.pop();default: // user wants to pop

{

}

{

} // end for

{

}catch (java.util.EmptyStackException e)

JOptionPane.showMessageDialog(null,"Can't do that to Empty Stack");

{

}

{

}

{

}Stack.java:package dataStructure;

import java.util.*;

/** implements a recursive stack data structure */public class Stack implements Cloneable

/** true when the stack is empty (default) */protected boolean empty=true;/** holds the item on the top of the stack */protected Object top;

{

protected Stack rest = null;

/** true when stack is empty */public boolean isEmpty() { return empty; }/** retrieves top of stack */public Object getTop() throws java.util.EmptyStackException { if(!empty) return top; else throw new java.util.EmptyStackException(); }

/** holds the rest of the stack */

csc260 Page 32

java.util.EmptyStackException(); }/** pushes new item on top of stack */public void push(Object toPush)

rest = (Stack) this.clone(); // the current stack becomes the rest}

try {

catch(Exception e) { }empty = false; // won't be empty if we pushtop = toPush; // put in new top

{

} // end push/** pop's top of stack */public void pop() throws java.util.EmptyStackException

if(isEmpty()) throw new java.util.EmptyStackException();else

top = rest.top;empty = rest.empty;rest = rest.rest;

{

}

{

} // end pop

} // end Stack

csc260 Page 33

Report 3 due 5/10

Exam 5/12

.class public Quiz

.super java.lang.Object

; default constructor .method <init>()V.limit stack 1.limit locals 1

aload_0invokespecial java/lang/Object/<init>()Vreturn

.line 5

.end method

; answer to quiz.method public static quizFunction(II)I.limit locals 2

iload_0iload_1if_icmple return1iload_0ireturn

.limit stack 4

iload_1ireturn

return1:

.end method ; end quizFunction

.method public static quizFunction2(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;.limit locals 2

4/28Monday, April 28, 20145:25 PM

csc260 Page 34

.limit locals 2

aload_0aload_1invokevirtual java/lang/String/compareTo(Ljava/lang/String;)Iifne return1aload_0areturn

.limit stack 4

aload_1areturn

return1:

.end method ; end quizFunction

Stack.j; translation of stack.java to jasmin

.source Stack.java

.class public dataStructure/Stack

.super java/lang/Object; .implements java/util/Cloneable

.field protected empty Z ; true when stack is empty

.field protected top Ljava/lang/Object; ; top of stack

.field protected rest LdataStructure/Stack; ; rest of the stack

; default constructor .method public <init>()V.limit stack 1.limit locals 1

; initialize stack as an objectaload_0invokespecial java/lang/Object/<init>()V

.line 5

.line 9

csc260 Page 35

aload_0iconst_1

.line 9

putfield dataStructure/Stack/empty Z ; empty = true

aload_0aconst_null ; nullputfield dataStructure/Stack/rest Ljava/lang/Object; ; rest = nullreturn

.line 13

.end method ; default constructor

aload_0getfield dataStructure/Stack/empty Zireturn

.method public isEmpty()Z

.end method ; isEmpty

.method public getTop()Ljava/lang/Object;

aload_0getfield dataStructure/Stack/empty Zifne topThrowEmptyaload_0getfield dataStructure/Stack/top Ljava/lang/Object; ; topareturn ; return top

.throws java/util/EmptyStackExpression

topThrowEmpty: new java/util/EmptyStackExpression dup ldc "Empty Stack has no top" invokenonvirtual java/lang/Exception/<init>(Ljava/lang/String;)V athrow.end method ; getTop

.method public push(Ljava/lang/Object;)V

aload_0new dataStructure/Stack ; copy self

csc260 Page 36

new dataStructure/Stack ; copy self; copy emptydupaload_0getfield dataStructure/Stack/empty Zputfield dataStructure/Stack/empty Z; copy topdupaload_0getfield dataStructure/Stack/top Ljava/lang/Object;putfield dataStructure/Stack/top Ljava/lang/Object;; copy restdupaload_0getfield dataStructure/Stack/rest LdataStructure/Stack;putfield dataStructure/Stack/rest LdataStructure/Stack;putfield dataStructure/Stack/rest LdataStructure/Stack;

; put parameter in topaload_0aload_1putfield dataStructure/Stack/top Ljava/lang/Object;

; set empty falseaload_0iconst_0

return putfield dataStructure/Stack/empty Z ; empty = false

.end method ; push

.method public pop()V

aload_0getfield dataStructure/Stack/empty Zifne popThrowEmpty

.throws java/util/EmptyStackExpression

csc260 Page 37

ifne popThrowEmptyaload_0dupgetfield dataStructure/Stack/rest LdataStructure/Stack;getfield dataStructure/Stack/empty Zputfield dataStructure/Stack/empty Z

aload_0dupgetfield dataStructure/Stack/rest LdataStructure/Stack;getfield dataStructure/Stack/top Ljava/lang/Object;putfield dataStructure/Stack/top Ljava/lang/Object;

aload_0dupgetfield dataStructure/Stack/rest LdataStructure/Stack;getfield dataStructure/Stack/rest LdataStructure/Stack;putfield dataStructure/Stack/rest LdataStructure/Stack;

returnpopThrowEmpty: new java/util/EmptyStackExpression dup ldc "Empty Stack can't pop" invokenonvirtual java/lang/Exception/<init>(Ljava/lang/String;)V athrow.end method ; pop

csc260 Page 38

; answer to review quiz3..method public static sequentialArray(II)[I.limit locals 5

5/5Monday, May 05, 20145:01 PM

csc260 Page 39

.limit locals 5

iload_1iconst_1iaddiload_0isub ;1+last-firstnewarray int ; int[] toReturn = new int[1+last-first]iload_0istore 3 ; 3 holds the number to put in the array ( int number)iconst_0 istore 4 ; 4 holds the array index (int index)

.limit stack 8

iload 3iload_1 ;if_icmpgt donedup ; copy array referenceiload 4iload 3iastore ; toReturn[index] = numberiload 4iconst_1iaddistore 4 ; index++iload 3iconst_1iaddistore 3 ; number++goto arrayLoop

arrayLoop:

areturndone:

.end method ; end sequentialArray

-7 1

-6 2

-5 1

-4 1

-3 1

S 4 2 1

csc260 Page 40

-4 1

-3 1

-2 1

-1 0

0 0

1 0

2 1

3 1

4 1

5 1

6 2

7 1

4.

Not enough memory, too much disk so we use the disk as part of our memory, hence VIRTUAL MEMORY

We do not use most of the memory most of the time.Disks like to be accessed in bulkRead and write pages (~1k)Keep the K's of memory most useful.Most likely instructruction to be executed is the next one.Even if we jump its usually to a nearby piece of memory (in the same method or at least in the same class.The stack location are all near each other and array elements are usually accessed sequentially.General rule we stick to small regions in memory most of the time.

Example lets say memory holds 5 pages of 100Accessing

1 1

2 12

3 123

4 1234

5 12345

6 23456

1 1 - working set

2 12

1 12

3 123

1 123

csc260 Page 41

4 1234

5 12345

6 23456

3 123

1 123

2 123

1 123

4 1234

1 1234

2 1234

3 1234

1 1234

5 12345

1 12345

3 12345

6 Which page goes? FIFO says 1 LRU says 4FIFO LRU23456 12356

Two dumb protocols for dumping pages:FIFO the first page loaded is the first dumpedLRU the least recently used page is the first dumped.

Page FIFO Working Set FIFO Pages Read LRU Working Set LRU Pages Read

1 1 1 1 1

2 12 2 12 2

3 123 3 123 3

1 123 3 231 3

4 234 4 314 4

1 341 5 341 4

3 341 5 413 4

2 412 6 132 5

1 412 6 321 5

5 125 7 215 6

3 253 8 153 7

1 531 9 531 7

2.

csc260 Page 42

1 531 9 531 7

csc260 Page 43