Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Computation Course 5JJ70 “Implementatie van rekenprocessen” Henk Corporaal p September 2009 h corporaal@tue nl [email protected] Welcome to 5JJ70 ! • 5JJ70 is the biggest course in the 1st semester. It deals with the hardware and software of a computing system: – Electronics • on the elementary components of a computer. – Computer p architecture • on the structure of computer hardware. – The hardware-software interface • on software that runs on the hardware. My contribution • Computer / Embedded System: System – “Microprocessor”, “Ordinateur”, “EDV-Anlage" ((Elektronische Datenverarbeitungsanlage), g g ) “Elaboratore elettronico” • This is the hart of any: – PC, PDA, fly-by-wire y y system, y MP3-player, p y Mobile phone, h Smart S t card, d Smart S t bomb, b b TV TV, W Washing hi machine, car engine, modem, etc. 5JJ70 Programming pg 2 Programming in C and C++ • We will learn how computer software really ‘runs’ on the hardware: – Machine language, g g , instructions,, data paths p • How to program in C and C++: – The languages g g of f real p programmers. g mm • At the end, you can attack virtually any real-life programming task! • This is intensive: you’ll learn a lot in a very short time: – Basic operations – Pointers, Pointers dynamic memory allocation – Machine language – Data structures, Object-oriented programming – Cool algorithms 5JJ70 Programming pg 3 The schedule : 1th semester • Classes (‘college’): (‘college’) 4 hours per week – Tuesday, – Friday, Friday – In EH 1.01 hours 3+4 (10.45) hours 3+4 (10 (10.45) 45) • Labs (“practicum”): 4 hours per week – Monday: hours 1+2 – Thursday: hours 7+8 – In EH6.05 and EH6.23 – You work on your laptop • Lab Assisted by – Raymond Frijns j – Sven Goossens – Emil Kleijn 5JJ70 Programming pg 4 The labs (“Practicum”) • Programming tasks: – Handed out in the lab. Take y your laptop. p p – You work individually on your programming assignments. – Discussing or asking your neighbors is OK, copying is not OK. – You will have to sign g off each assignment g with the staff. • We check whether you did it yourself. – Hand-in your assignment, using peach: peach.win.tue.nl • We’ll check for plagiarism – While working on an assignment you can take breaks as you please. – Assignments must be finished (signed off and emailed) within 7 days after handout. handout • Lab assistents will sign off and grade your assignments. 5JJ70 Programming pg 5 Computer revolution P Performa ance • Computers everywhere – PCs, portables, and workstations O(100M)/year – Embedded systems O(10G)/year – Playstations O(100M/year) • World Wide Web • Huge calculations – Human genome project – Huge simulations (e.g. weather forecast) • Information retrieval – Huge databases Processing – Online libraries – Web pages 55 %/y %/ Performance • Enormous computer gap performance growth Memory 35 %/y 7 %/y 70 80 90 00 10 Year 5JJ70 Programming pg 6 What is a computer system? Display harddisk network connection printer keyboard Q What is an Q. embedded system ? actuators sensors 5JJ70 Programming pg 7 Embedded system: ADSL modem 1. Telephone decoupling 2/3. LEDs 4. Main (TI) processor 5. JTAG test port 6 RAM, 6. RAM 8 MB chip. hi 7. Flash memory 8. Power regulator. 9 fuse. 9. fuse 10. Power connector 11. Reset button 12 Quartz crystal 12. 13. Ethernet port 14. Ethernet transformer 15. Ethernet PHY transmitter receiver 16. USB 17. Telephone (RJ11) 18. Telephone connector fuses. 5JJ70 Programming pg 8 Conceptual structure of a computer system; How does it operate? Instruction memory Data memory Processor I/O i t f interfaces Input and output devices 5JJ70 Programming pg 9 Binary representations • 0110 0101 0100 0100 1110 0111 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 1100 0110 0101 0100 0100 1110 0111 0110 •1100 Computers p contain many y megabytes g y1110 of memory. y 0110 1100 0110 0101 0100 0100 0111 0110 1100 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 1100 •0110 Without conventions completely 0101 0100 0100 1110this 0111data 0110 is 1100 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 1100 0110 0101 meaningless meaningless. 0100 0100 1110 0111 0110 1100 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 1100 0110 0101 0100 0100 0111 0110 1100 1100 0101 data, 0100 0100 1110 •1110 We must agree on0110 a way to 0110 encode data 0111 0110 1100numbers. 0110 1100 0110 0101 0100 0100 1110 0111 particularly 0110 1100 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 •0110 Th The good d news: we have h lots l of f bytes b in i today’s d ’ 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 1100 computers. 0110 0101 0100 0100 1110 0111 0110 1100 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 0110 1100 0110 0101 0100 0100 1110 0111 0110 1100 5JJ70 Programming pg 10 Computer structure CPU (e.g. pentium) Address (e.g. 32 bit) Data (e.g. (e g 32 bit) Memory (e.g. 5 512 Megabyte Sims = 128 M data words) This is a memory read D i write During it the th d data t goes ffrom th the CPU into i t the th memory • 32 bit data = 0110 0101 0100 0100 1110 0111 0110 1100 • What could this data represent? – A number (E.g. Your Postgiro account balance) – An instruction for the CPU (e.g. add two numbers) – 4 ASCII text characters of 8 bits each – 0.25 milliseconds of MP3 music or 0.045 milliseconds of CD music – Some (image) pixels, e.g. a piece of your book cover 5JJ70 Programming pg 11 Representing decimal numbers • Human calculation is mainly in the decimal number system. We use 10 symbols: 0123456789 • We call this ‘base 10’ – Why did mankind pick base 10?? (and not base 8?) • We can encode larger numbers by strings of numbers: 1969 1000 900 60 9 = + (= 1 * + (= 9 * + (= 6 * (= 9 * ( 10^3) 10^2) 10^1) 10^0) ) 5JJ70 Programming pg 12 Bases • We can use an arbitrary base g. Then the value of of digit c at position i is: valuei = c ∗ g i – (Note that we start counting digit positions at 0 ) • For example, example in the decimal system g = 10. 10 Digit 3 in 1969 is 1, 1 and therefore its value is: value3 = 1 ∗103 = 1000 • In this way we can represent numbers using an arbitrary base. Popular base values (g) are: –g = –g = –g = –g = 2: BINARY numbers 8: OCTAL numbers 10: DECIMAL number 16: HEXADECIMAL number e.g. 1001 e.g. 3661 e e.g. g 1969 e.g. 4BF7 (= decimal 9) 5JJ70 Programming pg 13 Binary numbers • Computer hardware is binary: – the base is 2, with only two symbols: 0 and 1 (why?) • Converting from binary to decimal: n −1 (an −1 an −2 ... a2 a1 a0 )two = ∑ ai ∗ 2i = i =0 an −1 ∗ 2 n −1 + an − 2 ∗ 2 n − 2 + ... + a2 ∗ 2 2 + a1 ∗ 21 + a0 ∗ 2 0 = Least significant bit: LSB Most significant bit: MSB n − 1 n − 2 ... 1 0 Bit position an −1 an − 2 ... a1 a0 digit g 2 n −1 2 n − 2 ... 21 20 weight 5JJ70 Programming pg 14 With n bits 2n possible combinations Binary (g = 1) 1 bit 0 1 2 bits 00 01 10 11 3 bits 000 001 010 011 100 101 110 111 4 bits 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Decimal (g = 10)) value 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Octal (g = 8) value 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 Hexadecimal (g = 16) value 0 1 2 3 4 5 6 7 8 9 A B C D E F 5JJ70 Programming pg 15 Bits, nibbles and bytes 1 1010 10000001 1111111111001110 11111111110011101111111111001110 • 1 bit • 4 bits = nibble (‘knabbel’) • 8 bits = byte • 16 bits (C: short int) • 32 bits ‘word’ Using n bits, we can represent positive integer numbers between 0 and 2n -1 [0,2 n 5JJ70 Programming pg 16 Representing characters: 1 byte encoding A American i St d d Code Standard C d ffor Information I f ti Interchange I t h 5JJ70 Programming pg 17 Conversion from decimal to binary • Divide by 2 4382 2191 1095 547 273 136 68 34 17 8 4 2 1 0 Remainder 0 1 1 1 1 0 0 0 1 0 0 0 1 4382ten = 1 0001 0001 1110bin What's the trick ? • Conversion between hexadecimal (base 16) and binary is simple: – 1010101100111111bin = ab3fhex 5JJ70 Programming pg 18 Pop Quiz! • We need to store the (decimal) number 2500000000 (2.5 Billion) as a binary number. – What is the minimum number of bits that are required to store this (unsigned) number? • Now we store the same number as a string of ASCII characters. – How many bits are required? 5JJ70 Programming pg 19 Operating on binary numbers • Addition is similar to decimal – starting from the LSB, add two bits – if the sum is larger g or equal q to base g (=2), ( ) produce p a carry: y 1 1111 00011111 10111010 1 10110 01 bin bin + 31 186 + 217dec • Multiplication/division is also familiar – Multiplication by a power of two is a ‘left shift’: 00011111bin * 10bin = 00111110bin 00111000bin / 10bin = 00011100bin 5JJ70 Programming pg 20 Multiplication: example • Multiplication is left shift followed by an addition: 10 X 5 1010 101 bin 1010 00000 101000 bin X 1 * 1010 00 * 1010 (=Shift left 1) + 100 * 1010 ((=Shift f left f 2)) 110010 5JJ70 Programming pg 21 More on binary number representations • binary numbers (base 2) 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001... 1001 N digits = 2n numbers representing decimal: 0...2n-1 • We have to deal with a few problems: 1) numbers are finite (overflow due to finite range) 2) fractions and real numbers 3) negative numbers 4) complex numbers, etc…. how do we represent negative numbers? i.e., which bit patterns will represent which numbers? 5JJ70 Programming pg 22 Signed binary numbers A few possible representations: sign magnitude: 000 001 010 011 100 101 110 111 = = = = = = = = +0 +1 +2 +3 -0 -1 1 -2 -3 one one'ss complement: 000 001 010 011 100 101 110 111 = = = = = = = = +0 +1 +2 +3 -3 -2 2 -1 -0 x two's two s complement: 000 = +0 001 = +1 010 = +2 011 = +3 100 = -4 101 = -3 3 110 = -2 111 = -1 xˆ = x + 1 • Issue: ease of operations, compactness 5JJ70 Programming pg 23 Getallencirkel: Sign magnitude subtraction 1000 1010 1011 0011 0101 ???? 0000 1001 0001 0 -1 1 3 - 5 = -2 0010 -2 2 -3 -(5 (5 - 3) = -2 2 3 0011 Swap 5 and 3 1100 1101 iti positive -4 4 negative -55 5 -6 6 1110 -7 1111 0110 7 0111 0100 0101 101 011 010 1 1010 Add sign (‘getallencirkel’) 5JJ70 Programming pg 24 The ‘getallencircle’, two’s complement 0000 1111 1110 0001 -1 15 -2 0 1 0010 2 14 1101 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 = = = = = = = = = = = = = = = = +0 +1 +2 +3 +4 +5 +6 +7 -8 -7 -6 -5 -4 -3 -2 -1 1100 1011 -3 3 13 positive -4 12 4 negative -5 0011 0100 11 5 0101 10 -6 1010 1001 9 -7 6 8 -8 0110 7 0111 1000 5JJ70 Programming pg 25 4-bit two’s complement (a3 a2 a1 a0) 2’s-compl = 1111 -a3 23 + a2 22 + a1 21 + a0 20 1110 0001 -1 15 -2 0 1 0010 2 14 1101 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 = = = = = = = = = = = = = = = = +0 +1 +2 +3 +4 +5 +6 +7 -8 -7 -6 -5 -4 -3 -2 -1 1100 1011 3+2=5 0000 -3 3 13 positive -4 12 -5 0011 4 negative 0011 0010 + 0101bin 0100 11 5 0101 10 -6 1010 1001 9 -7 6 8 -8 0110 7 0111 1000 Range = [-2 3 .. 2 3 -1] 5JJ70 Programming pg 26 Two’s complement subtraction 0000 1111 1110 0 1 0010 2 14 1101 0011 1011 + 1110bin -1 15 -2 3 -5 = 3 + (-5) = -2 0001 1100 1011 -3 3 13 positive -44 12 4 negative -5 0011 0100 11 5 0101 10 -6 1010 1001 9 -77 6 8 -8 0110 7 0111 1000 5JJ70 Programming pg 27 Two’s complement add: overflow possible! 0000 0011 0110 + 1001bin 1111 1110 0001 -1 15 -2 1 0010 2 14 1101 1100 1011 -3 3+6 = 9 0 3 13 positive -4 12 4 negative -5 0011 0100 11 5 0101 10 -6 1010 1001 = -7 7 overflow!!! 1001 9 -7 6 8 -8 1000 0110 7 0111 Overflow O fl if we cross here 5JJ70 Programming pg 28 Two’s complement subtract: overflow also possible! 0000 1101 1010 + 0111bin 1111 1110 -1 15 -2 0 1 0010 2 14 1101 1100 1011 -3 -3 + (-6) = 7 !! underflow 0001 3 13 positive -4 12 4 negative -5 0011 0100 11 5 0101 10 -6 1010 1001 9 -7 6 8 -8 0110 7 0111 1000 5JJ70 Programming pg 29 Two’s complement: C integer • 32 bit signed numbers: 0000 0000 0000 ... 0111 0111 1000 1000 1000 ... 1111 1111 1111 maxint 0000 0000 0000 0000 0000 0000 0000two = 0ten 0000 0000 0000 0000 0000 0000 000 0001two = + 1ten 0000 0000 0000 0000 0000 0000 0010two = + 2ten 1111 1111 0000 0000 0000 1111 1111 0000 0000 0000 1111 1111 0000 0000 0000 1111 1111 0000 0000 0000 1111 1111 0000 0000 0000 1111 1111 0000 0000 0000 1110two 1111two 0000two 0001two 0010two = = = = = + + – – – minint 2,147,483,646ten 2,147,483,647ten 2,147,483,648 , , , ten 2,147,483,647ten 2,147,483,646ten 1111 1111 1111 1111 1111 1111 1101two = – 3ten 1111 1111 1111 1111 1111 1111 1110two = – 2ten 1111 1111 1111 1111 1111 1111 1111two = – 1ten – Range [-2 31 .. 2 31 -1] • (an-1 an-2... a1 a0) 2’s-compl = -an-1 x 2n-1 + an-2 x 2n-2 + … + a0 x 20 5JJ70 Programming pg 30 Addition & subtraction: summary • just like in grade school (carry/borrow 1s) 0111 0111 0110 + 0110 - 0110 - 0101 • two's complement operations easy – subtraction b i using i addition ddi i of f negative i numbers b 0110 0110 - 0101 + 1011 • overflow (result too large for finite computer word): – e.g., adding two n-bit numbers does not yield an n-bit number 0111 + 0001 , 1000 5JJ70 Programming pg 31 Two's complement operations • Negating a two's complement number: invert all bits and add 1.. a – remember: “negate” and “invert” are quite different • Example (50 negated into -50): 50): 00110010 11001101 00000001 + invert Add 1 11001110bin 5JJ70 Programming pg 32 Two's complement: word length change Processors often load 8 or 16 bit numbers into 32 bit registers for operations. How do we keep convert the word length? • The trick: copy the most significant bit (the sign bit) into the empty bits. For example 8 bit converted into 16 bit: • 11001110 00110010 1111111111001110 0000000000110010 • Some processors have special for S h i l machine hi instructions i t ti f this. E.g. MIPS "sign extension” instructions: • • • • Lb Lbu Slti Sltiu load byte (signed) load byte (unsigned) set less than immediate (signed) set less than immediate (unsigned) 5JJ70 Programming pg 33 Detecting overflow • Never overflow when adding a positive and a negative number. • No overflow when signs are the same for subtraction • Overflow occurs when the value affects the sign: – overflow when adding two positives yields a negative – or, or adding two negatives gives a positive – or, subtract a negative from a positive and get a negative – or, subtract a positive from a negative and get a positive 5JJ70 Programming pg 34 Pop quiz! • We have two numbers in two’s complement: a and b. Consider the operations – Can overflow occur if b == 0 – Can overflow occur if a == 0 000 001 010 011 100 101 110 111 = = = = = = = = a + b and a - b ? ? ? ? +0 +1 +2 +3 -4 -3 3 -2 -1 5JJ70 Programming pg 35 Summary • Computers encode numbers binary, typically in 4, 8, 16, 32 64 or 128 bit 32, • Signed number are encoded using ‘two’s complement’ • We can only encode a finite set of numbers. In any case, we mustt be b careful f l for f overflow, fl i case th in the result lt cannot be encoded in the given amount of bits. Material: Patterson Hennessy: Section 2.4 Ward/Halstead: 2.1 - 2.5 5JJ70 Programming pg 36