Computer Languages, Algorithms and Program Development: How Do Computers Know What We Want Them To Do?
Computer Languages, Algorithms and Program Development: How Do Computers Know What We Want Them To Do?
Computer Languages,
Algorithms and Program
Development
Listener returns
feedback to speaker
Communicating with
a Computer
Substituting a computer for
one of the people in the
communication process.
User encodes
• Process is basically information Computer decodes
the same. information
– Response may be symbols
on the monitor.
Computer
returns results
to user
Communicating with
a Computer
A breakdown can occur any place along the cycle...
Between two people: Between a person and a computer:
• The person can’t hear you. • The power was suddenly
• The phone connection is broken in interrupted.
mid-call. • An internal wire became
• One person speaks only French, disconnected.
while the other only Japanese. • A keyboard malfunctioned.
Program sum2(input,output);
var
num1,num2,sum : integer;
begin
read(num1,num2);
sum:=num1+num2;
writeln(sum)
end.
The Programming
Language Continuum
Fourth Generation - Non-Procedural Languages
• Programming-like systems aimed at simplifying the programmers task of
imparting instructions to a computer.
• Many are associated with specific application packages.
– Query Languages:
– Report Writers:
– Application Generators:
– For example, the Microsoft Office suite supports macros and ways to
generate reports
The Programming
Language Continuum
Fourth Generation - Non-Procedural Languages (cont.)
• Object-Oriented Languages: A language that expresses a computer
problem as a series of objects a system contains, the behaviors of those
objects, and how the objects interact with each other.
– Object: Any entity contained within a system.
• Examples:
» A window on your screen.
» A list of names you wish to organize.
» An entity that is made up of individual parts.
– Some popular examples: C++, Java, Smalltalk, Eiffel.
The Programming
Language Continuum
Fifth Generation - Natural Languages
• Natural-Language: Languages that use ordinary conversation in one’s
own language.
– Research and experimentation toward this goal is being done.
• Intelligent compilers are now being developed to translate
natural language (spoken) programs into structured machine-
coded instructions that can be executed by computers.
• Effortless, error-free natural language programs are still some
distance into the future.
Assembled, Compiled, or
Interpreted Languages
All programs must be translated before their instructions can be
executed.
Computer languages can be grouped according to which
translation process is used to convert the instructions into binary
code:
• Assemblers
• Interpreters
• Compilers
Assembled, Compiled, or
Interpreted Languages
Assembled languages:
• Assembler: a program used to translate Assembly language programs.
• Produces one line of binary code per original program statement.
– The entire program is assembled before the program is sent to the
computer for execution.
– Similar to the machine code exercise we did in class
• Example of 6502 assembly language and machine code:
– JSR SWAP 20 1C 1F
– LDA X2 A5 04
– LDY =$80 A0 80
– STY X2 49 80
Assembled, Compiled, or
Interpreted Languages
Interpreted Languages:
• Interpreter: A program used to translate high-level programs.
• Translates one line of the program into binary code at a time:
• An instruction is fetched from the original source code.
• The Interpreter checks the single instruction for errors. (If an
error is found, translation and execution ceases. Otherwise…)
• The instruction is translated into binary code.
• The binary coded instruction is executed.
• The fetch and execute process repeats for the entire program.
Machine Language
Source Code Statement
X=3 Interpreter 11011101
X=X+1
…
compiler assembler
Machine Code
C++ source
Mac Compiler Mac Machine Code
• Note that under this model, compilation and execution are two different
processes. During compilation, the compiler program runs and translates source
code into machine code and finally into an executable program. The compiler
then exits. During execution, the compiled program is loaded from disk into
primary memory and then executed.
Interpreted vs. Compiled
What happens if you modify the source on a compiled programming
language (without recompiling) vs. an interpreted programming language and
execute it?
Compiled
• Runs faster
• Typically has more capabilities
– Optimize
– More instructions available
• Best choice for complex, large programs that need to be fast
Interpreted
• Slower, often easier to develop
• Allows runtime flexibility (e.g. self-modifying programs, memory management)
• Some are designed for the web
Java?
The astute members of the audience might have noticed that
Java was listed under both Interpreted and Compiled!
A Java compiler translates source code into machine
independent “byte code” that can be executed by the java
“virtual machine”.
• Java Virtual machine doesn’t actually exist – it is simply a specification
of how a machine would operate if it did exist in terms of what machine
code it understands.
• Interpreters must then be written on the different architectures that can
understand the virtual machine and convert it to the native machine code
Java
compiler Mac Interpreter
Public class Foo {
01010001
if (e.target=xyz) then PC Interpreter
01010010
this.hide();
} PalmPilot Interpreter
Java Benefits
The great benefit of Java is that if someone (e.g. Sun) can write
interpreters of java byte code for different platforms, then code
can be compiled once and then run on any other type of
machine.
• No more hassles of developing different code for different platforms
Sound too good to be true?
• Unfortunately there is still a bit of variability among Java interpreters, so
some programs will operate differently on different platforms.
• The goal is to have a single uniform byte code that can run on any
arbitrary type of machine architecture
• Java programs, due to the interpreted nature, are also much slower than
native programs (e.g., those written in C++)
Building a Program
Whatever type of problem needs to be solved, a careful thought out plan of
attack, called an algorithm, is needed before a computer solution can be
determined.
• Eliminates half of the list each time, much faster than linear search for long lists (lg N vs. N
for a list with N names)
Algorithm can have a huge impact on efficiency and ease of implementation for the
solution!
Building a Program
2) Writing the Program
• If analysis and planning have been thoroughly done, translating the plan
into a programming language should be a quick and easy task.