Download presentation
Presentation is loading. Please wait.
Published byLetitia Cummings Modified over 8 years ago
1
Winter 2016CISC101 - Prof. McLeod1 Today Go over the Python disassembly “experiment” again. Code interpretation vs. code compilation. History and features of Python.
2
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. McLeod2
3
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. McLeod3
4
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. McLeod4
5
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. McLeod5
6
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. McLeod6 10 30 10 2 30 10 60 10 70 0 a a b a b c a z b * ca + b * c
7
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 2016CISC101 - Prof. McLeod7
8
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 hex. But, the interpreter will not bother with this stage – it translates from assembly to binary. Winter 2016CISC101 - Prof. McLeod8
9
Compiled vs Interpreted More on this topic later. 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. Python “interprets” source code and generates machine language and executes it immediately. Winter 2016CISC101 - Prof. McLeod9
10
10 Computer Languages, History After Assembly language, the next generation of computer languages went up one more level, getting closer to something readable - for example: Fortran, Cobol and Lisp. These languages led to an explosion of over 200 languages being developed in the 60’s and 70’s, such as Basic, Pascal, C, Ada and Smalltalk. Python is a relative newcomer, arriving on the scene in the early 90’s. See: https://en.wikipedia.org/wiki/Timeline_of_programming_langu ages Winter 2016
11
Aside – 99 Bottles of Beer… See: http://www.99-bottles-of-beer.net/ Programs in 1500 different programming languages to generate the lyrics to the “song”. CISC101 - Prof. McLeod11Winter 2016
12
Aside - Malbolge A programming language designed to be extremely hard to program in and impossible to read. Named after the eighth circle of Hell in Dante’s “Inferno”. Featured in an episode of Elementary (S01E10). –They supposedly had a program in Malbolge that de- crypted a safe’s combination. –What they really had was “Hello World” in Malbolge: CISC101 - Prof. McLeod12Winter 2016
13
“Hello World” in Malbolge ('&%:9]!~}|z2Vxwv-,POqponl$Hjig%eB@@>}= CISC101 - Prof. McLeod13Winter 2016
14
CISC101 - Prof. McLeod14 History of Python The language was created by Guido van Rossum at Stichting Mathematisch Centrum in the Netherlands in the early 90’s. He is still very involved with the language and retains the title “BDFL”, which stands for “Benevolent Dictator for Life”. Python is named after “Monty Python”, not the snake!! Winter 2016
15
CISC101 - Prof. McLeod15 History of Python, Cont. He wanted to make the language: –an easy and intuitive language, but just as powerful as major competitors. –open source, so anyone can contribute to its development. –use code that is as understandable as plain English. –to be suitable for everyday tasks, allowing for short development times. First released in 1994, the language was inspired by Modula-3, Lisp, SETL, Haskell, Icon and Java. A compilation of all the “Best-Of’s” from many other languages! Winter 2016
16
CISC101 - Prof. McLeod16 Features of Python High Level –Most notable are the built-in data structures. Object Oriented –OOP helps you to build code in a modular way. But, Python allows you to write code without knowing anything about OOP! Scalable –Packaging of code allows even very large programming projects to be manageable. Extensible –You can easily use external code modules written in Python, C, C++, C#, Java or Visual Basic. Winter 2016
17
CISC101 - Prof. McLeod17 Features of Python, Cont. Portable –Runs on any platform/OS combination that can run C. Easy to Learn (!) –Relatively few keywords, simple language structure and clear syntax. OOP can be avoided while learning. Easy to Read –Much less punctuation than other languages. Forces you to use good indentation. Easy to Maintain –Results from the two above features! Winter 2016
18
CISC101 - Prof. McLeod18 Features of Python, Cont. Robust –Exception handlers and safe, sane and informative crashes. Good for Rapid Prototyping –Often used with other languages to create prototypes and provide a testing platform. Built-In Memory Management –Like Java. Avoids a major problem in C/C++. Interpreted –Not compiled – each command is executed as it is read from the program. Speed is increased using byte- compiled files (like Java). Winter 2016
19
Hello World Write a program that prompts the user for his first name and then displays “Hello name” to the console. Put the code in a main function. Just for fun, disassemble the program when it is done. Winter 2016CISC101 - Prof. McLeod19
20
Questions Do you have to put code in a main function? Why is it called “main()”? What’s with the “()”? They are empty! Why is there a call to main() at the end of the program? How can you tell what code is inside main() and what is outside? Winter 2016CISC101 - Prof. McLeod20
21
Questions, Cont. What does the input() BIF return? How can you get a number, like an int, from input()? What does the print() BIF return? What does “+” do with strings? I can add numbers with “+”, as well. Can I add a number to a string? Winter 2016CISC101 - Prof. McLeod21
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.