Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2018 CISC101 11/29/2018 CISC101 Reminders

Similar presentations


Presentation on theme: "Winter 2018 CISC101 11/29/2018 CISC101 Reminders"— Presentation transcript:

1 Winter 2018 CISC101 11/29/2018 CISC101 Reminders “Potency” survey closes at Sunday, Jan. 21 at midnight. Your group name in onQ is your grader’s name. Let me know if you are not in a group. Labs start/continue this week. Meet your TA! Get help with: Exercise 1. Python installation issues. Lecture material. Assignment expectations…. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Numeric representation (or How does binary and hexadecimal work?). Commanding the CPU. Winter 2018 CISC101 - Prof. McLeod

3 Numbers: Base 10 Base 2 Base 16 1 2 10 3 11 4 100 5 101 6 110 7 111 8
1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 1010 A 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F 16 10000 Etc! Winter 2018

4 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 2018 CISC101 - Prof. McLeod

5 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 2018 CISC101 - Prof. McLeod

6 Numeric Representation - Cont.
In base 2 (digits either 0 or 1): r=2, a binary number: ( )2= 1×25+1×24+0×23+1×22+0×21+1×20 +1×2-1 +1×2-2 =53.75 (in base 10) “r” is the “radix” or the base of the number Winter 2018 CISC101 - Prof. McLeod

7 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×163+6×162+5×161+15×160 = Winter 2018 CISC101 - Prof. McLeod

8 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 2018 CISC101 - Prof. McLeod

9 Numeric Representation - Cont.
For example, convert to binary: So, is Divisor(r) Dividend Remainder (quotient) least significant digit most significant digit Winter 2018 CISC101 - Prof. McLeod

10 Numeric Representation - Cont.
For the “0.1510” part: So, is is: ( )2 Multiplier(r) Multiplicand Carry (product) ... Winter 2018 CISC101 - Prof. McLeod

11 Aside - Roundoff Error Consider 0.1, for example:
(0.1)10 = ( …)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 2018 CISC101 - Prof. McLeod

12 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 2018 CISC101 - Prof. McLeod

13 An Experiment, Cont. How many other fractional, real numbers will result in roundoff error when they are stored in a computer? Which ones won’t result in roundoff error? (Hint try adding instead of 0.1…) Winter 2018 CISC101 - Prof. McLeod

14 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! Winter 2018 CISC101 - Prof. McLeod

15 Numeric Representation - Cont.
Converting between binary and hex is much easier - done by “grouping” the digits: For example: (2C6B.F06)16 = (?)2 ( C B F )16 ( )2 Winter 2018 CISC101 - Prof. McLeod

16 Numeric Representation - Cont.
And going from binary to hex: ( )2 = (?)16 ( )2 ( D D )16 Winter 2018 CISC101 - Prof. McLeod

17 Commanding the Processor
CISC101 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 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

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

19 X = A * B + C, Cont. Naturally, these instructions have to be communicated to the CPU in binary: byte 1 bytes 2, 3 and 4 operand opcode Winter 2018 CISC101 - Prof. McLeod

20 Aside – Bytes and Bits A byte is 8 bits. A bit is either 0 or 1.
So a byte can range from: , to 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. Winter 2018 CISC101 - Prof. McLeod

21 Aside - CPU Pins How many pins on the CPU would be required to input these instructions? Winter 2018 CISC101 - Prof. McLeod

22 X = A * B + C, Cont What are these instructions in base 10?
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 2018 CISC101 - Prof. McLeod

23 Machine Language These 4 byte binary commands are examples of machine language. Normally these commands would be viewed in base 16, or hex: 0x 0x 0x 0x Winter 2018 CISC101 - Prof. McLeod

24 X = A * B + C, Cont Since “normal” people have problems remembering binary codes and even decimal codes for operations, a “shorthand” language called Assembly Language was introduced at a level above “machine language”. Winter 2018 CISC101 - Prof. McLeod

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

26 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 2018 CISC101 - Prof. McLeod

27 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 2018 CISC101 - Prof. McLeod

28 Disassembly Experiment, Cont.
You will see: LOAD_NAME (a) 2 LOAD_NAME (b) 4 LOAD_NAME (c) 6 BINARY_MULTIPLY 8 BINARY_ADD 10 STORE_NAME (z) 12 LOAD_CONST (None) 14 RETURN_VALUE Winter 2018 CISC101 - Prof. McLeod

29 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 in the Python help docs (“Standard Library”) for the meaning of assembly instructions. These ones are explained on the next slide: Winter 2018 CISC101 - Prof. McLeod

30 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 2018 CISC101 - Prof. McLeod

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

32 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. (Aside - What is a “stack overflow”? Often results from infinite recursion, for example.) Winter 2018 CISC101 - Prof. McLeod


Download ppt "Winter 2018 CISC101 11/29/2018 CISC101 Reminders"

Similar presentations


Ads by Google