Download
today n.
Skip this Video
Loading SlideShow in 5 Seconds..
Today PowerPoint Presentation

Today

121 Views Download Presentation
Download Presentation

Today

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Today • Course Web Site: http://research.cs.queensu.ca/home/cisc101f • How computers work, Cont.: • Malbolge! • Python disassembly experiment. • History of the Python language. • “Hello World” ritual? (if we have time) CISC101 - Prof. McLeod

  2. But, First: • My office hours: • Mondays 10:45am to 2pm • Thursdays 11:45am to 3pm • I will be in the lab (JEF155) today for an hour starting at 2:30 pm for Q&A. CISC101 - Prof. McLeod

  3. 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. McLeod

  4. 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. McLeod

  5. “Hello World” in Malbolge ('&%:9]!~}|z2Vxwv-,POqponl$Hjig%eB@@>}=<M:9wv6WsU2T|nm-,jcL(I&%$#" `CB]V?Tx<uVtT`Rpo3NlF.Jh++FdbCBA@?]!~|4XzyTT43Qsqq(Lnmkj"Fhg${z@> CISC101 - Prof. McLeod

  6. Yikes! • Python is much, much friendlier!! • “Hello World” in Python: print("Hello World") CISC101 - Prof. McLeod

  7. 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") CISC101 - Prof. McLeod

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

  9. 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 these instructions. • These ones are explained on the next slide: CISC101 - Prof. McLeod

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

  11. 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 b z c a b a a a CISC101 - Prof. McLeod

  12. Aside – What’s a Stack? • A “LIFO” – “Last In, First Out” type of data structure. • 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.) CISC101 - Prof. McLeod

  13. 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. • Now, the code is impossible for a human to read! CISC101 - Prof. McLeod

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

  15. History of Python • The language was created by Guido van Rossum at StichtingMathematisch 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!! CISC101 - Prof. McLeod

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

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

  18. 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! CISC101 - Prof. McLeod

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

  20. Hello World • Write a program that prompts the user for his first name and then displays “Hello name” to the console. • Just for fun, disassemble the program when it is done. • Explain what the code is doing. CISC101 - Prof. McLeod

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

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