Transcript Module6

The Binary Machine
• Modern high-level programming languages
are designed to make programming easier.
• On the other end, the low level, all modern
digital computers understand only machine
language.
• This means that a program written in a
high-level language must be translated into
machine language.
• Translation is performed by an assembler,
an interpreter, or a compiler.
• A computer has a hard-wired instruction
set unique to that computer.
The Binary Machine
• The instruction set is etched into the
computer’s microprocessor chip.
• High-level -----> Translator -----> Low-level
Source code ---------------------------> Object code
• Source code is what goes into a program
translator. Object code is what comes out.
• At the low-level, all modern digital
computers store programs and information
in binary form.
Binary Representations
• Modern digital computer hardware circuitry
is based on on-off switching. Thus, a
computer can distinguish between only the
two values “on” (1) and “off” (0).
• This means information must be
represented in binary form.
• Some terminology:
bit - binary digit.
byte - 8 bits.
word - 2 bytes or 16 bits.
Binary Representations
• Example: Computer memory is measured in
bytes.
64 MB means 64 megabytes or 64 million
bytes or 8 x 64 = 512 million bits.
• Numbers are represented in binary (base 2)
notation.
• Example: 235 in decimal notation is 11101011
in binary notation.
235 = 2 x 102 + 3 x 101 + 5 x 100
11101011 = 1 x 27 + 1 x 26 + 1 x 25 + 0 x 24
+ 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20
Binary Representations
• There’s a decimal-to-binary and binary-todecimal calculator in Module 6 of The
Analytical Engine Website.
• Characters are represented by coding them
as integers.
• A commonly used character code is ASCII
(American Standard Code for Information
Interchange).
• Example: The letter A is represented by 65
or 01000001. A right parenthesis ) is
represented by 41 or 00101001.
Encoding Instructions
• Instructions are also encoded in binary.
• accumulator - a location where intermediate
results are stored.
• Sample coded instructions:
Code
Info
Action
00000100 X Load accumulator with contents
of memory location X
00000101 X Store accumulator value in
memory location X
00001111 X Halt execution
00000000 X Add contents of memory
location X to accumulator
Encoding Instructions
• With this coding, a simple machinelanguage program might look like this:
00000100 10000000
00000000 10000001
00000101 10000000
00001111 00000000
• Here’s what it says:
Load accumulator with contents of 128.
Add contents of 129 to accumulator.
Store accumulator contents in 128.
Halt.
Encoding Instructions
• As you might guess, programming in
machine language is hard.
• Early programmers developed mnemonics
for remembering instructions.
• The above machine-language program
might be remembered as follows:
(x = cell 128, y = cell 129)
Load x
Add y
Store x
Halt
Encoding Instructions
• The next step was to write a program
which would translate these mnemonics to
machine language, thereby sparing a
programmer the grief of programming
directly in machine language.
• This led to the first program translator.
• assembler - a program that takes an
assembly language as its input source code
and produces machine language as its
output object code.
• The PIPPIN assembler.
Beyond the Assembler
• Assembly-language code is fast and
efficient, but hard to write.
• Using a higher-level language closer to our
native language would make programming
easier.
• The idea is to move away from dealing with
the computer at its level and work more at
our level.
• When programming at a high level, we don’t
want to think about the details of the
computer our program will run on.
Types of Languages
• Modern high-level programming languages
come in two flavors depending on the type
of translator used to take source code and
produce object code.
• interpreter - a translator which translates
one line of source code and instructs the
computer to perform the resulting object
code before translating another line of
source code.
• compiler - a translator which translates all
the source code and produces a complete
machine-language program.
Interpreted Languages
• Examples include BASIC, LISP, JavaScript.
• Advantages:
- Interpreters are easy to write.
- Errors are more easily localized.
• Disadvantages:
- Interpreters are slow.
- A source-code line will be translated as
often as it is encountered. For example, a
line of code in a loop that is repeated 1000
times will be translated 1000 times.
Compiled Languages
• Examples include FORTRAN, C, Java.
• Advantage:
- After compilation, execution is fast.
• Disadvantage:
- Localizing errors is more difficult.
Language Groups
1. Imperative - the fundamental unit is a
procedure called by the main program.
Examples are FORTRAN, Pascal, Ada.
2. Functional - processes are defined as
functions with no main program. An
example is LISP.
3. Declarative - the emphasis is on I/O as
opposed to processing algorithms.
Examples are COBOL, Prolog.
4. Object-oriented - hybrids organized around
programming “objects”. Examples are
C++, Java.
Language Translation
• Translation of a high-level language into
machine language involves three steps:
1. Scanning - breaking the source code into
its smallest meaningful pieces or tokens.
This is what we do when we read a
sentence and note the words it contains.
2. Parsing - arranging the tokens into a
sensible logical structure. This is what we
do when we recognize nouns, verbs,
adjectives, etc. in a sentence.
3. Code generation - generating the machine
language object code. This is what we do
when we paraphrase a sentence for a kid.
Language Generations
• Languages are characterized by how far
they are from what computers do and how
close they are to what people do.
1. First generation - machine languages.
2. Second generation - assembly languages
and assemblers.
3. Third generation - interpreted and
compiled languages.
4. Fourth generation - languages that give
“computational power to nonprogrammers.”