From High Level to Algorithm/Model Java, C++, VB Compilation Execution Cycle Hardware 27 October 2007 Ariel Shamir 1 Compilation Overview Algorithm vs. Programs From Algorithm to Compilers vs. Interpreters Java Virtual Machine Operating Systems 27 October 2007 Ariel Shamir 2 Computer Programs An algorithm is described to the computer as a computer program. A computer program is simply a list of unambiguous instructions meant to be followed by a computer. The operations sequence has to be correct or the program will fail. 27 October 2007 Ariel Shamir 3 1
Programming Languages There are several families of the programming languages: Procedural (Pascal, C++, Java) Logical (Prolog) Visual (Visual Basic) Document (HTML, LATEX) 27 October 2007 Ariel Shamir 4 Languages Hierarchy Procedural languages can be divided into: Machine languages Low-level (Assembler ) Intermediate-level (C, Fortran) High-Level (C++, Java) 27 October 2007 Ariel Shamir 5 High Level Vs. Low Level The computer can only execute programs expressed in the computer machine language Writing programs in machine language is possible but very difficult and time consuming. Using a more human readable language (high-level language) is better. 27 October 2007 Ariel Shamir 6 2
Program Compilation A program written in any high level computer language (java, C++, basic) must be translated into the machine language in order to be executed. The translation process is called Compilation (or Assembly) 27 October 2007 Ariel Shamir 7 Algorithm Vs. Program Find a minimum in a group of elements A: set the min as the value of first element for each element in A if it is smaller than min then assign min the value of the element the minimum of A is min 27 October 2007 Ariel Shamir 8 Pseudo-code Computer algorithms are usually written in pseudo-code. Pseudo-code is code that is easy to understand and yet is easily transformed into programming language. 27 October 2007 Ariel Shamir 9 3
Pseudo-code Vs. Code Pseudo-code realizing the above algorithm: temporary_min A[1] for i 1 to i length(a) if A[i] < temporary_min temporary_min A[i] return temporary_min 27 October 2007 Ariel Shamir 10 Java Code The pseudo-code converted to Java code: int min(int A[]) { int temporary_min = A[0]; for(int i=1; i<a.length; i++) if (A[i] < temporary_min) temporary_min =A[i]; return temporary_min; } 27 October 2007 Ariel Shamir 11 From Algorithm to Machine Execution Algorithm Pseudo-Code Design and Programming Input High Level Code Compilation Execution Output 27 October 2007 Ariel Shamir 12 4
Implementing Designing & writing the algorithms Writing code (programming) Compiling Testing/Debugging Maintaining 27 October 2007 Ariel Shamir 13 Language Syntax & Semantics The process of programming requires: Programming language. A translator of the programming language into machine language. The programming language has to be well defined syntactically and semantically. The meaning of what you write has to be precisely defined and understood (mathematically) by the computer. 27 October 2007 Ariel Shamir 14 Translators Compiler A compiler is a program that translates a source program (usually high level language) into target program (usually machine language program) The resulting program can be executed many times Interpreter An interpreter is a program that reads, translates and executes the source program statement by statement Translation must be done each time the program runs 27 October 2007 Ariel Shamir 15 5
Java In this course we will use Java programming language. Java is an object oriented, high-level, third generation programming language, like C, Fortran, Smalltalk. Java Shares much of C's syntax. Originally designed by a group at Sun MicroSystems (then called Oak). 27 October 2007 Ariel Shamir 16 Java As a High Level Language Java defers from most high level languages in its compilation. Java uses half-precompiled elements called bytecode. An additional layer above the physical machine interprets the byte-code. This layer is called the Virtual Machine. 27 October 2007 Ariel Shamir 17 Byte Code The java compiler translates java program into the byte code. Byte code is in fact a machine code for the Java virtual machine (VM). VM interprets the byte code and translates it to commands for each certain processor and OS. 27 October 2007 Ariel Shamir 18 6
Java VM The use of the byte code makes java platform independent: Java Program Byte Code Java Virtual Machine Intel PC Mac Macintosh Sun Sparc 27 October 2007 Ariel Shamir 19 Java Advantages Platform independent: write once, run anywhere. Improve robustness, remove unsafe language loopholes. According to Sun: Java is simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high-performance, multithread, and dynamic language. 27 October 2007 Ariel Shamir 20 Java Disadvantages The main Java disadvantage are efficiency concerns since it is more interpreted. Looking to the future: VM performance will improve. 27 October 2007 Ariel Shamir 21 7
Java Interpreter Environments The two main environments: Operating system (Applications) Web browser (Applets) 27 October 2007 Ariel Shamir 22 Operating System In the operating system environment the Java program is called application. Applications have a more rigid structure. Applications can be textual or graphical. Applications are less secure but also less restrictive. 27 October 2007 Ariel Shamir 23 Web Browser The browser acts as an intermediate between the program and the OS. The JVM can resides inside the browser. The program has to work in graphical mode. The program is called Applet (a small application). 27 October 2007 Ariel Shamir 24 8
Java Code Java Source Code Java Compiler Java Byte Code Across the Internet using HTML Operating System Java Interpreter Bytecode Compiler Machine Code Web browser Java Interpreter 27 October 2007 Ariel Shamir 25 How Do Computers Keep Performing Tasks? How can we interact with a computer continuously? What does the computer do when we don t execute programs? Who is in charge of the User Interface (today GUI)? 27 October 2007 Ariel Shamir 26 Bridging the Gap? Hardware: CPU, Memory, Execution cycle User and Software: Computer Application programs 27 October 2007 Ariel Shamir 27 9
The Operating System The operating system is a program that runs all the time and manages the resources of the computer. Among other things, it allows us to load and execute other programs. The operating system itself is loaded to the memory as part of the boot process which takes place when the computer is started. 27 October 2007 Ariel Shamir 28 Operating System Issues Handling resources (disks, printers ) Task management (single, multi, preemptive ) Graphical user interface (icons, menus, dialogs ) 27 October 2007 Ariel Shamir 29 Operating Systems Examples DOS: single-tasking, non-graphical Mac OS (<10): non-preemptive multitasking, graphical Unix, Linux: preemptive multitasking, non-graphical Windows: preemptive multi-tasking, graphical 27 October 2007 Ariel Shamir 30 10
Boot-strapping The process of loading the operating system from disk into memory, when the computer is first turned on. A ROM-based part of the memory stores those first execution instructions (for example, the BIOS for basic input/output system) 27 October 2007 Ariel Shamir 31 Summary Algorithm/Model Program Code Compilers etc. Operating System Execution Cycle Hardware 27 October 2007 Ariel Shamir 32 11