Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC101 Reminders Labs start this week. Meet your TA! Get help with:

Similar presentations


Presentation on theme: "CISC101 Reminders Labs start this week. Meet your TA! Get help with:"— Presentation transcript:

1 CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Winter 2019 CISC101 4/12/2019 CISC101 Reminders Labs start this week. Meet your TA! Get help with: Exercise 1. Python installation issues. Lecture material. Assignment expectations…. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Numeric Representation. Commanding the CPU. Winter 2019
CISC101 - Prof. McLeod

3 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 2019 CISC101 - Prof. McLeod

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

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

6 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: 16, a hex number: (B65F)16 = 11×163+6×162+5×161+15×160 = Winter 2019 CISC101 - Prof. McLeod

7 Numeric Representation - Cont.
The algorithms show in the previous two slides show how you can convert from binary or hex to decimal. How to convert from decimal to one of the other bases? Here is another 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 2019 CISC101 - Prof. McLeod

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

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

10 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 2019 CISC101 - Prof. McLeod

11 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 2019 CISC101 - Prof. McLeod

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

13 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 2019 CISC101 - Prof. McLeod

14 Numeric Representation - Cont.
Converting between binary and hex is much easier - done by “grouping” the digits. Each hex digit results in 4 binary digits. Work away from the decimal place. For example: (2C6B.F06)16 = (?)2 ( C B F )16 ( )2 Winter 2019 CISC101 - Prof. McLeod

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

16 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 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

17 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 2019 CISC101 - Prof. McLeod

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

19 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 2019 CISC101 - Prof. McLeod

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

21 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 2019 CISC101 - Prof. McLeod

22 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 2019 CISC101 - Prof. McLeod

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

24 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 2019 CISC101 - Prof. McLeod

25 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 2019 CISC101 - Prof. McLeod

26 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("x = a * b + c") Winter 2019 CISC101 - Prof. McLeod

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

28 Disassembly Experiment, Cont.
One line of Python code required eight lines of assembly! In case you are curious, see section in the Python help docs (“Standard Library”) for the meaning of assembly instructions. These are explained on the next slide: Winter 2019 CISC101 - Prof. McLeod

29 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 2019 CISC101 - Prof. McLeod

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

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

32 Machine Code Once code is disassembled, the interpreter needs to generate machine code. It uses the opcodes, opargs and operands from each line of assembly. We can view (and sometimes write) machine code in hexadecimal. But, the interpreter will not bother with this stage – it translates from assembly to binary. Now, the code is impossible for most humans to read! Winter 2019 CISC101 - Prof. McLeod

33 Compiled vs Interpreted
Binary commands can be compiled and then written to an *.exe or “executable” file. When you run a file like this the code is sent directly to the CPU, which is very fast! Python does not make these files, but generates binary machine language on the “fly” – sending it directly to the CPU, line by line. Python “interprets” source code, generates machine language and executes it immediately. Winter 2019 CISC101 - Prof. McLeod


Download ppt "CISC101 Reminders Labs start this week. Meet your TA! Get help with:"

Similar presentations


Ads by Google