Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2016CISC101 - Prof. McLeod1 Today Numeric representation (or “How does binary and hexadecimal work?”). How can a CPU understand instructions written.

Similar presentations


Presentation on theme: "Winter 2016CISC101 - Prof. McLeod1 Today Numeric representation (or “How does binary and hexadecimal work?”). How can a CPU understand instructions written."— Presentation transcript:

1 Winter 2016CISC101 - Prof. McLeod1 Today Numeric representation (or “How does binary and hexadecimal work?”). How can a CPU understand instructions written in a high level language like Python?

2 CISC101 - Prof. McLeod2 Numeric Representation Binary numbers or “base 2” is a natural representation of numbers to a computer. As a transition, hexadecimal (or “hex”, base 16) numbers are also used. Octal (base 8) numbers are hardly ever used anymore – so I won’t talk about this base. Decimal (base 10) numbers are not naturally represented in computers. Winter 2016

3 CISC101 - Prof. McLeod3 Reminder – Python Integer Literals From Exercise 1: –Binary literals – prefix the number with 0b –Octal literals – prefix the number with 0o –Hex literals – prefix the number with 0x –Decimal literals – don’t prefix the number with anything! Winter 2016

4 CISC101 - Prof. McLeod4 Numeric Representation - Cont. In base 2 (digits either 0 or 1): r=2, a binary number: (110101.11) 2 = 1×2 5 +1×2 4 +0×2 3 +1×2 2 +0×2 1 +1×2 0 +1×2 -1 +1×2 -2 =53.75 (in base 10) “r” is the “radix” or the base of the number Winter 2016

5 CISC101 - Prof. McLeod5 Numeric Representation - Cont. Hexadecimal Numbers: a base-16 system with 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F: For example: r = 16, a hex number: (B65F) 16 = 11×16 3 +6×16 2 +5×16 1 +15×16 0 = 46687 10 Winter 2016

6 CISC101 - Prof. McLeod6 Numeric Representation - Cont. The above series show how you can convert from binary or hex to decimal. How to convert from decimal to one of the other bases? Here is an algorithm that you can use: integral part: divide by r and keep the remainder. decimal part: multiply by r and keep the carry “r” is the base - either 2 or 16 Winter 2016

7 CISC101 - Prof. McLeod7 Numeric Representation - Cont. For example, convert 625.15 10 to binary: So, 625 10 is 1001110001 2 Divisor(r)DividendRemainder 2 625 2 312 (quotient) 1 2 156 0 2 78 0 2 39 0 2 19 1 2 9 1 2 4 1 2 2 0 2 1 0 0 1 most significant digit least significant digit Winter 2016

8 CISC101 - Prof. McLeod8 Numeric Representation - Cont. For the “0.15 10 ” part: So, 0.15 10 is 0.001001 2 625.15 is: (1001110001.001001) 2 Multiplier(r)MultiplicandCarry 2 0.15 2 0.30(product) 0 2 0.60 0 2 1.20 1 2 0.40 0 2 0.80 0 2 1.60 1 2 1.20 1 2 0.40 0 2 0.80 0... Winter 2016

9 CISC101 - Prof. McLeod9 Aside - Roundoff Error Consider 0.1, for example: (0.1) 10 = (0.0 0011 0011 0011 0011 0011…) 2 What happens to the part of a real number that cannot be stored? It is lost - the number is either truncated or rounded. The “lost part” is called the Roundoff Error. Winter 2016

10 An Experiment Without worrying about the syntax details for now, enter the following lines at the Python prompt: >>> sum = 0 >>> for i in range(1000) : sum = sum + 0.1 >>> sum Why is the value in sum not what it should be? Winter 2016CISC101 - Prof. McLeod10

11 Not All Numbers! A computer only has a finite amount of memory to store a number: –Only a portion of most real numbers can be stored. –The majority of real numbers cannot be stored exactly on a computer! –Only numbers that are a sum of powers of 2 can be stored exactly. –Most calculations made with real numbers on a computer can only yield approximate results! CISC101 - Prof. McLeod11Winter 2016

12 CISC101 - Prof. McLeod12 Numeric Representation - Cont. Converting between binary and hex is much easier - done by “grouping” the digits: For example: (2C6B.F06) 16 = (?) 2 ( 2 C 6 B. F 0 6 ) 16 ( 0010 1100 0110 1011. 1111 0000 0110) 2 Winter 2016

13 Numeric Representation - Cont. And going from binary to hex: (10110011101.11011) 2 = (?) 16 ( 0101 1001 1101. 1101 1000 ) 2 ( 5 9 D. D 8 ) 16 CISC101 - Prof. McLeod13Winter 2016

14 CISC101 - Prof. McLeod14 Commanding the Processor Suppose we want the processor to carry out the operation: X = A * B + C Assume we have used some other operations to put numbers in three memory locations at locations 1024, 1025 and 1026 in RAM, corresponding to A, B and C. We want the result to go into memory location 1027, corresponding to X. Winter 2016

15 CISC101 - Prof. McLeod15 X = A * B + C Remember the “von Neumann Cycle”? The operations in the ALU (or “Arithmetic Logic Unit”) part of the CPU would be: 1.Fetch the contents of location 1024 and put it into a register. 2.Fetch the contents of location 1025 and multiply it with the number in the register, storing the result in the register. 3.Fetch the contents of location 1026 and add this value to the contents of the register. 4.Move the contents of the register to memory location 1027. Winter 2016

16 CISC101 - Prof. McLeod16 X = A * B + C, Cont. Naturally, these instructions have to be communicated to the CPU in binary: 1. 00010000000000000000010000000000 2. 00100100000000000000010000000001 3. 00100011000000000000010000000010 4. 00010001000000000000010000000011 byte 1 bytes 2, 3 and 4 operand opcode Winter 2016

17 Aside – Bytes and Bits A byte is 8 bits. A bit is either 0 or 1. So a byte can range from: 00000000, to 11111111 In base 10 this is 0 to 255 In hex this is 0 to FF So one byte can be represented by two hex digits. CISC101 - Prof. McLeod17Winter 2016

18 CISC101 - Prof. McLeod18 X = A * B + C, Cont What are these instructions in base 10? 1.161024 2.361025 3.351026 4.171027 The operands are the memory locations The opcodes are 16 for “load”, 36 for “multiply”, 35 for “add” and 17 for “store”. The default register is used, so it does not need to be specified. Winter 2016

19 CISC101 - Prof. McLeod19 Machine Language These 4 byte binary commands are examples of machine language. Normally these commands would be viewed in base 16, or hex: 1.0x10000400 2.0x24000401 3.0x23000402 4.0x11000403 Winter 2016

20 CISC101 - Prof. McLeod20 X = A * B + C, Cont (Pseudo) Assembly language instructions: 1.LOAD A, ACC 2.MULT B, ACC 3.ADD C, ACC 4.STOR ACC, X Each assembly language keyword is immediately translated into the corresponding machine language code by an “interpreter” program called an “Assembler”. Winter 2016

21 CISC101 - Prof. McLeod21 X = A * B + C, Cont A higher level language goes one step above assembly language. For example, in C, C++, C# or Java, you would write: X = A * B + C; (Python is the same, except no semi-colon.) Each high-level line of code gets translated into many lines of assembly code and each line of assembly code must be translated into binary machine language. Much easier to write high level code, right? Winter 2016

22 Disassembly Experiment Python has a module called “dis” that can show you the assembly language version of a Python line of code, function, object or an entire module. At the >>> prompt type: >>> import dis >>> a = 10 >>> b = 30 >>> c = 2 >>> dis.dis("z = a + b * c") Winter 2016CISC101 - Prof. McLeod22

23 Disassembly Experiment, Cont. You see: 1 0 LOAD_NAME 0 (a) 3 LOAD_NAME 1 (b) 6 LOAD_NAME 2 (c) 9 BINARY_MULTIPLY 10 BINARY_ADD 11 STORE_NAME 3 (z) 14 LOAD_CONST 0 (None) 17 RETURN_VALUE Winter 2016CISC101 - Prof. McLeod23

24 Disassembly Experiment, Cont. One line of Python code required eight lines of assembly! Note that the multiply is carried out before the addition. Why did the interpreter choose that order? In case you are curious, see section 31.12.1 in the Python help docs for the meaning of assembly instructions. These ones are explained on the next slide: Winter 2016CISC101 - Prof. McLeod24

25 Disassembly Experiment, Cont. LOAD_NAME pushes a value onto the stack from a variable name. BINARY_MULTIPLY multiplies the two numbers on top of the stack and puts the result on top of the stack. BINARY_ADD adds the two numbers on top of the stack and puts the result back on the stack. STORE_NAME takes the value on the top of the stack and stores it into a variable name. LOAD_CONST puts a zero on top of the stack, to clear it. RETURN_VALUE returns the value on top of the stack (zero in this case) as a result of the evaluation of the expression. Winter 2016CISC101 - Prof. McLeod25

26 Disassembly Experiment, Cont. The “stack” is used to temporarily hold values and the results of calculations. How the stack changes after each instruction: top: Winter 2016CISC101 - Prof. McLeod26 10 30 10 2 30 10 60 10 70 0 a a b a b c a z b * ca + b * c

27 Aside – What’s a Stack? A “LIFO” – “Last In, First Out” type of data structure. (Like a plate dispenser in a cafeteria!) You “push” values on to the top of the stack and “pop” them off. Winter 2016CISC101 - Prof. McLeod27


Download ppt "Winter 2016CISC101 - Prof. McLeod1 Today Numeric representation (or “How does binary and hexadecimal work?”). How can a CPU understand instructions written."

Similar presentations


Ads by Google