from high level to machine code - · pdf filefrom high level to machine code hardware ......
TRANSCRIPT
1
27 October 2007 © Ariel Shamir 1
From High Level to Machine Code
Hardware
Execution Cycle
Java, C++, VBJava, C++, VB
CompilationCompilation
Machine CodeMachine Code
Algorithm/Model
27 October 2007 © Ariel Shamir 2
Compilation Overview
• Algorithm vs. Programs
• From Algorithm to Machine Code
• Compilers vs. Interpreters
• Java Virtual Machine
• Operating Systems
•• Algorithm vs. ProgramsAlgorithm vs. Programs
•• From Algorithm to Machine CodeFrom Algorithm to Machine Code
•• Compilers vs. InterpretersCompilers vs. Interpreters
•• Java Virtual MachineJava Virtual Machine
•• Operating SystemsOperating Systems
2
27 October 2007 © Ariel Shamir 3
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.
••An algorithm is described to the An algorithm is described to the computer as a computer as a computer programcomputer program..
••A computer program is simply a list A computer program is simply a list of unambiguous instructions meant of unambiguous instructions meant to be followed by a computer.to be followed by a computer.
••The operations sequence has to be The operations sequence has to be correct or the program will fail.correct or the program will fail.
27 October 2007 © Ariel Shamir 4
Programming Languages
There are several families of the
programming languages:
– Procedural (Pascal, C++, Java)
– Logical (Prolog)
– Visual (Visual Basic)
– Document (HTML, LATEX)
There are several families of the There are several families of the
programming languages:programming languages:
–– Procedural (Pascal, C++, Java) Procedural (Pascal, C++, Java)
–– Logical (Prolog)Logical (Prolog)
–– Visual (Visual Basic)Visual (Visual Basic)
–– Document (HTML, LATEX)Document (HTML, LATEX)
3
27 October 2007 © Ariel Shamir 5
Languages Hierarchy
Procedural languages can be divided
into:
– Machine languages
– Low-level (Assembler )
– Intermediate-level (C, Fortran)
– High-Level (C++, Java)
Procedural languages can be divided Procedural languages can be divided
into:into:
–– Machine languagesMachine languages
–– LowLow--level (Assembler )level (Assembler )
–– IntermediateIntermediate--level (C, Fortran)level (C, Fortran)
–– HighHigh--Level (C++, Java)Level (C++, Java)
27 October 2007 © Ariel Shamir 6
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.
The computer can only execute programs The computer can only execute programs expressed in the computer expressed in the computer machine machine languagelanguage
Writing programs in machine language is Writing programs in machine language is possible but very difficult and time possible but very difficult and time consuming.consuming.
Using a more human readable language Using a more human readable language (high(high--level language) is better.level language) is better.
4
27 October 2007 © Ariel Shamir 7
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)
••A program written in any high level A program written in any high level
computer language (java, C++, basic) computer language (java, C++, basic)
must be translated into the machine must be translated into the machine
language in order to be executed. language in order to be executed.
••The translation process is called The translation process is called
Compilation (or Assembly)Compilation (or Assembly)
27 October 2007 © Ariel Shamir 8
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
Find a minimum in a group of Find a minimum in a group of elements A:elements A:
set the set the minmin as the value of first elementas the value of first element
for each element in A for each element in A
if it is smaller than if it is smaller than minmin
then assign then assign minmin the value of the elementthe value of the element
the minimum of A is the minimum of A is minmin
5
27 October 2007 © Ariel Shamir 9
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.
Computer algorithms are usually Computer algorithms are usually
written in pseudowritten in pseudo--code.code.
PseudoPseudo--code is code that is easy to code is code that is easy to
understand and yet is easily understand and yet is easily
transformed into programming transformed into programming
language.language.
27 October 2007 © Ariel Shamir 10
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
PseudoPseudo--code realizing the above code realizing the above algorithm:algorithm:
temporary_min temporary_min ←← A[1]A[1]
for i for i ←← 1 to i 1 to i ←← length(A)length(A)
if A[i] < temporary_minif A[i] < temporary_min
temporary_min temporary_min ←← A[i]A[i]
return temporary_minreturn temporary_min
6
27 October 2007 © Ariel Shamir 11
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;
}
The pseudoThe pseudo--code converted to Java code: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 12
From Algorithm to Machine Execution
CompilationCompilation
Machine CodeMachine Code
Design and Programming
High Level CodeHigh Level Code
Algorithm
Pseudo-Code
OutputInput
Execution
7
27 October 2007 © Ariel Shamir 13
Implementing
•Designing & writing the algorithms
•Writing code (programming)
•Compiling
•Testing/Debugging
•Maintaining
••Designing & writing the algorithmsDesigning & writing the algorithms
••Writing code (programming)Writing code (programming)
••Compiling Compiling
••Testing/DebuggingTesting/Debugging
••MaintainingMaintaining
27 October 2007 © Ariel Shamir 14
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.
The process of programming requires:The process of programming requires:
–– Programming language.Programming language.
–– A translator of the programming language into A translator of the programming language into machine language.machine language.
The programming language has to be well The programming language has to be well defined syntactically and semantically.defined syntactically and semantically.
The meaning of what you write has to be The meaning of what you write has to be precisely defined and understood precisely defined and understood (mathematically) by the computer.(mathematically) by the computer.
8
27 October 2007 © Ariel Shamir 15
TranslatorsCompiler
• 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
CompilerCompiler
•• A compiler is a program A compiler is a program that translates a source that translates a source program (usually high program (usually high level language) into level language) into target program (usually target program (usually machine language machine language program)program)
•• The resulting program The resulting program can be executed many can be executed many timestimes
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
InterpreterInterpreter
•• An interpreter is a An interpreter is a
program that reads, program that reads,
translates and executes translates and executes
the source program the source program
statement by statementstatement by statement
•• Translation must be Translation must be
done each time the done each time the
program runsprogram runs
27 October 2007 © Ariel Shamir 16
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).
In this course we will use Java In this course we will use Java programming language.programming language.
Java is an object oriented, highJava is an object oriented, high--level, third level, third generation programming language, like C, generation programming language, like C, Fortran, Smalltalk.Fortran, Smalltalk.
Java Shares much of C's syntax.Java Shares much of C's syntax.
Originally designed by a group at Sun Originally designed by a group at Sun MicroSystemsMicroSystems (then called Oak).(then called Oak).
9
27 October 2007 © Ariel Shamir 17
Java As a High Level Language
Java defers from most high level
languages in its compilation. Java uses
‘half-precompiled’ elements called byte-
code.
An additional layer above the physical
machine interprets the byte-code. This
layer is called the “Virtual Machine”.
Java defers from most high level Java defers from most high level
languages in its compilation. Java uses languages in its compilation. Java uses
‘‘halfhalf--precompiledprecompiled’’ elements called byteelements called byte--
code. code.
An additional layer above the physical An additional layer above the physical
machine interprets the bytemachine interprets the byte--code. This code. This
layer is called the layer is called the ““Virtual MachineVirtual Machine””..
27 October 2007 © Ariel Shamir 18
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.
The java compiler translates java The java compiler translates java program into the byte code. program into the byte code.
Byte code is in fact a machine code Byte code is in fact a machine code for the Java virtual machine (VM).for the Java virtual machine (VM).
VM interprets the byte code and VM interprets the byte code and translates it to commands for each translates it to commands for each certain processor and OS.certain processor and OS.
10
27 October 2007 © Ariel Shamir 19
Java VM
The use of the
byte code makes
java platform
independent:
The use of the The use of the
byte code makes byte code makes
java platform java platform
independent:independent:
Java Program
Byte Code
Intel Machine Code
Mac Machine Code
Java Virtual Machine
PC Macintosh
Sun Machine Code
Sparc
27 October 2007 © Ariel Shamir 20
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.”
Platform independent: Platform independent: ““write once, run write once, run anywhereanywhere””..
Improve robustness, remove unsafe Improve robustness, remove unsafe language loopholes.language loopholes.
According to Sun:According to Sun:
““ Java is simple, objectJava is simple, object--oriented, distributed, oriented, distributed, interpreted, robust, secure, architectureinterpreted, robust, secure, architecture--neutral, neutral, portable, highportable, high--performance, multithread, and performance, multithread, and dynamic language.dynamic language.””
11
27 October 2007 © Ariel Shamir 21
Java Disadvantages
The main Java disadvantage are efficiency concerns since it is more interpreted.
Looking to the future:
VM performance will improve.
The main Java disadvantage are The main Java disadvantage are efficiency concerns since it is more efficiency concerns since it is more interpreted.interpreted.
Looking to the future:Looking to the future:
VM performance will improve.VM performance will improve.
27 October 2007 © Ariel Shamir 22
Java Interpreter Environments
The two main environments:
• Operating system (Applications)
• Web browser (Applets)
The two main environments:The two main environments:
•• Operating system (Applications)Operating system (Applications)
•• Web browser (Applets)Web browser (Applets)
12
27 October 2007 © Ariel Shamir 23
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.
In the operating system environment the In the operating system environment the
Java program is called Java program is called ““applicationapplication””..
Applications have a more rigid structure.Applications have a more rigid structure.
Applications can be textual or graphical.Applications can be textual or graphical.
Applications are less secure but also less Applications are less secure but also less
restrictive.restrictive.
27 October 2007 © Ariel Shamir 24
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).
The browser acts as an intermediate The browser acts as an intermediate
between the program and the OS.between the program and the OS.
The JVM can resides inside the browser.The JVM can resides inside the browser.
The program has to work in graphical The program has to work in graphical
mode.mode.
The program is called The program is called ““AppletApplet”” (a small (a small
application).application).
13
27 October 2007 © Ariel Shamir 25
Java Code
Java Source Code
Java Byte Code
Machine Code
Java Compiler
BytecodeCompiler
Web browser
JavaInterpreter
JavaInterpreter
Across the Internet using HTML
Operating System
27 October 2007 © Ariel Shamir 26
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)?
••How can we interact with a How can we interact with a
computer continuously?computer continuously?
•• What does the computer do when What does the computer do when
we donwe don’’t execute programs?t execute programs?
••Who is in charge of the User Who is in charge of the User
Interface (today GUI)?Interface (today GUI)?
14
27 October 2007 © Ariel Shamir 27
Bridging the Gap
?
Hardware:CPU, Memory, Execution cycle
User and Software:Computer Applicationprograms
27 October 2007 © Ariel Shamir 28
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.
The operating system is a program that The operating system is a program that runs all the time and manages the runs all the time and manages the resources of the computer. Among other resources of the computer. Among other things, it allows us to load and execute things, it allows us to load and execute other programs.other programs.
The operating system itself is loaded to the The operating system itself is loaded to the memory as part of the boot process which memory as part of the boot process which takes place when the computer is started.takes place when the computer is started.
15
27 October 2007 © Ariel Shamir 29
Operating System Issues
Handling resources (disks, printers…)
Task management (single, multi,
preemptive…)
Graphical user interface (icons,
menus, dialogs…)
Handling resources (disks, printersHandling resources (disks, printers……) )
Task management (single, multi, Task management (single, multi,
preemptivepreemptive……) )
Graphical user interface (icons, Graphical user interface (icons,
menus, dialogsmenus, dialogs……))
27 October 2007 © Ariel Shamir 30
Operating Systems Examples
DOS: single-tasking, non-graphical
Mac OS (<10): non-preemptive multi-tasking, graphical
Unix, Linux: preemptive multi-tasking, non-graphical
Windows: preemptive multi-tasking, graphical
DOS:DOS: singlesingle--tasking, nontasking, non--graphicalgraphical
Mac OS (<10):Mac OS (<10): nonnon--preemptive multipreemptive multi--tasking, graphicaltasking, graphical
Unix, Linux:Unix, Linux: preemptive multipreemptive multi--tasking, nontasking, non--graphicalgraphical
Windows:Windows: preemptive multipreemptive multi--tasking, tasking, graphicalgraphical
16
27 October 2007 © Ariel Shamir 31
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)
The process of loading the operating The process of loading the operating system from disk into memory, when system from disk into memory, when the computer is first turned on.the computer is first turned on.
A ROMA ROM--based part of the memory based part of the memory stores those first execution stores those first execution instructions (for example, the BIOS instructions (for example, the BIOS for basic input/output system)for basic input/output system)
27 October 2007 © Ariel Shamir 32
Summary
Hardware
Execution Cycle
Operating System
Compilers etc.
Algorithm/Model
Program Code