Introduction to Computer
Programming
              Mr. Orfel L. Bejarin,MIT   1
   A microprocessor is designed to “understand” a set
    of commands called an “instruction set”
   All instructions must be provided to the CPU in its
    native language, called machine language.
   All data transmission, manipulation, storage, and
    retrieval is done by the machine using electrical
    pulses representing sequences of binary digits.
   If eight-digit binary codes are used, there are 256
    numbered instructions from 00000000 to
    11111111.
                                 Mr. Orfel L. Bejarin,MIT   11/4/2013   2
   Instructions for adding two numbers would
    consist of a sequence of these eight-digit
    codes from 00000000 to 11111111.
   Instructions written in this form are referred
    to as machine language.
   It is the native language that the CPU
    “speaks” and “understands”.
   It is possible to write an entire program in
    machine language. However, this is very
    time consuming and difficult to read and
    understand.
                              Mr. Orfel L. Bejarin,MIT   3
   Fortunately, special languages have been
    developed that are more easily
    understood (than machine language).
   These special languages are called
    programming languages.
   These languages provide a way to write
    computer programs that are understood
    by both computers and people.
   Programming languages have their own
    vocabulary and rules of usage.
   Some languages are very technical, while
    others are similar to English.
                           Mr. Orfel L. Bejarin,MIT   4
   The programming language that is most
    like machine language is assembly
    language.
   Assembly language uses letters and
    numbers to represent machine language
    instructions.
   An assembler is a program that reads the
    codes the programmer writes in assembly
    language and “assembles” a machine
    language program based on those codes.
   However, assembly language is still difficult
    to read.                  Mr. Orfel L. Bejarin,MIT   5
   For example, the machine code for adding
    two integers might be:
01000011001110100011110101000001001010110
 1000010
   While the assembly language code might be:
    LOAD A
    ADD B
    STORE C
    ◦ This causes the number in A to be added to the number in
      B, and the result is stored for later use in C.
                                      Mr. Orfel L. Bejarin,MIT   6
   Machine Language and Assembly Language
    are both called low-level languages.
   In a low-level language, it is necessary for
    the programmer to know the instruction set
    of the CPU in order to program the
    computer.
   Each instruction in a low-level language
    corresponds to one or only a few
    microprocessor instructions.
                             Mr. Orfel L. Bejarin,MIT   7
   A high-level language is any programming
    language that uses words and symbols to
    make it relatively easy to read and write a
    computer program.
   In a high-level language, instructions do
    not necessarily correspond one-to-one with
    the instruction set of the CPU.
   One command in a high-level language may
    correspond to many microprocessor
    instructions.
                            Mr. Orfel L. Bejarin,MIT   8
   Many high-level languages have been
    developed. These include:
    FORTRAN, COBOL, BASIC, Logo, Pascal, C,
    C++, Java, and others.
   These languages simplify even further the
    terminology and symbolism necessary for
    directing the machine to perform various
    manipulations of data.
                             Mr. Orfel L. Bejarin,MIT   9
   High Level Languages:
    ◦ Reduce the number of instructions that must be
      written.
    ◦ Allow programs to be written in a shorter amount
      of time than a low-level language would take.
    ◦ Reduce the number of errors that are made,
      because…
      The instructions are easier to read.
    ◦ Are more portable (the programs are easier to
      move among computers with different
      microprocessors).
                                       Mr. Orfel L. Bejarin,MIT   10
   Low Level Languages:
    ◦ Instructions can be written to enable the computer
      to do anything that the hardware will follow.
    ◦ Require less memory
    ◦ Run more quickly
                                  Mr. Orfel L. Bejarin,MIT   11
   Consider the following programs that add two
    numbers together:
    BASIC          Pascal                C++                        LOGO
    10 I = 3       program AddIt;        int main( )                to add :I :J :K
    20 J = 2       var                   {                          MAKE “I :3
    30 K = I + J    i, j, k : integer;       int i, j, k;           MAKE “J :2
                    begin                    i = 3;                 MAKE “K :I + :J
                    i := 3;                  j = 2;                 end
                    j := 2;                  k = i + j;
                    k := i + j;              return 0;
                    end.                 }
                                                      Mr. Orfel L. Bejarin,MIT        12
   Programmers writing in a high-level
    language enter the program’s instructions
    into a text editor.
   The files saved in this format are called
    text files.
   A program written in a high-level language
    is called source code.
   The programs are translated into machine
    language by interpreters or compilers.
   The resulting machine language code is
    known as object code.
                           Mr. Orfel L. Bejarin,MIT   13
   An interpreter is a program that translates
    the source code of a high-level language
    into machine language.
   Each instruction is interpreted from the
    programming language as needed (line by
    line of code).
   Every time the program is run, the
    interpreter must translate each instruction
    again.
   In order to “run” the program, the
    interpreter must be loaded into the
    computer’s memory.        Mr. Orfel L. Bejarin,MIT   14
   A compiler is another program that
    translates a high-level language into
    machine language.
   A compiler makes the translation once so
    that the source code don’t have to be
    translated each time the program is run.
    ◦ The source code is translated into a file called
      an object file.
    ◦ A program called a linker is used to create an
      executable program.
    ◦ Most modern compilers let you compile and
      link in a single operation, and have an “IDE”
      (Integrated Development Environment) to
      enter text, debug, compile, link, and run
      programs.
                                 Mr. Orfel L. Bejarin,MIT   15
 Bug: An error in coding or logic that
  causes a program to malfunction or to
  produce incorrect results.
 Debug: To detect, locate, and correct
  logical or syntactical errors in a program.
 Folklore attributes the first use of the
  term “bug” to a problem in one of the
  first electronic computers that was traced
  to a moth caught between the contacts of
  a relay in the machine.
http://www.microsoft.com/canada/home/
  terms/2.7.1.1_B.asp
                          Mr. Orfel L. Bejarin,MIT   16