note1 by zhangyun

VIEWS: 13 PAGES: 42

									                Computer Basics

• Computer system: hardware + software
• Hardware: the physical components
   – Central Processing Unit (CPU)
   – Memory
   – Input/Output devices
• Software: the instructions that tell the hardware
  what to do
   – Operating Systems
   – Application Software
   – User programs

                                                      1
         Common Hardware Components

•   Processor (CPU)
•   Memory
•   Input device(s)
•   Output device(s)
•   CPU and memory are physically housed together


                                 Memory



                   Input                            Output
                  Devices       Processor           Devices

                                                              2
               CPU and Memory

• The CPU is the ―brain‖ of the computer
   – Understands and executes computer programs

• Memory is the storage unit
  – Holds computer programs to be executed and their
    data
  – Main and auxiliary memory




                                                   3
          Inside the CPU



           Control
            Unit
                                   To main memory,
                                   and input / output
                                   devices
Arithmetic /
   Logic             Registers
    Unit




               This is the CPU’s
               internal memory
                                                        4
                 Kinds of Memory

• Main
  – Often referred to as Random-Access Memory
    (RAM)
  – Temporarily stores program and data (while
    program is executing)
  – Requires power to retain its contents

• Auxiliary
   – Additional storage capacity
   – Permanent (more or less)
   – Saves program and results
   – Includes floppy & hard disk drives, CDs, flash
     memory sticks, etc.                              5
          Main Memory Organization

• Comparable to a            Address   Contents
  collection of numbered     3021      11110000
  mailboxes.
                             3022      11001100

                             3023      10101010
• Each memory cell has
  two important              3024      11001110
  attributes                 3025      11000000
   – Address or location
                             3026      00000000
   – Contents or values,
                             3027      11111111
     represented digitally
     as a binary number      3028      10110101
                             3029      11001110
                                                  6
            Main Memory Organization

• Bit = Binary digIT            Address Contents
   – A binary digit can have
                                3021    11110000 Item 1:
      only one of two values:
       0 or 1                   3022    11001100 2 octets
• Byte = 8 bits                                    Item 2:
   – Also called ―octet‖        3023    10101010
                                                   1 octet
• Main memory is a list of      3024    11001110
  numbered locations that
                                                   Item 3:
  contain one byte of data in   3025    11000000
                                                   3 octets
  each location
                                3026    00000000
• Number of bytes per data
  item may vary                 3027    11111111
                                                   Item 4:
                                                   1 octet
                                3028    10110101 Item 5:
                                3029    11001110 2 octets     7
                           Binary Numbers

• Internally, computers store values as binary (base 2)
  numbers, rather than decimal (base 10) numbers



                                 ×100
                   ×102
                          ×101
• Decimal:         1 6 5
                           ×25




                                               ×22


                                                     ×20
                                   ×24
             ×27
                    ×26




                                         ×23


                                               ×21

• Binary:     1 0 1 0 0 1 0 1
              27 +25   +22 +20
             128 +32 +4     +1 = 165
                                                           8
               Decimal to Binary Conversion

• Convert 106 to binary by repeatedly dividing by 2. Use the
  remainders as the binary digits, with the first remainder being
  the right-most digit

  106     /   2   = 53,   remainder   0
   53     /   2   = 26,   remainder   1
   26     /   2   = 13,   remainder   0
   13     /   2   = 6,    remainder   1
    6     /   2   = 3,    remainder   0
    3     /   2   = 1,    remainder   1
    1     /   2   = 0,    remainder   1

Result:       106 =   11010102        indicates a binary number
                                                                    9
          Storage of character data

• Once again, we have only 0 and 1 digits to work with.
• Each character is assigned its own numeric code:
   – ASCII character set (ASCII = American Standard
     Code for Information Interchange)
      • 128 characters
      • most common character set (if you speak
        American English )
      • used in older languages and computers
   – UNICODE character set
      • over 64,000 characters (international)
      • includes ASCII as a subset
      • used in Java
                                                      10
                          Unicode

• In the computer, every character is stored using a numeric code.
   – The most commonly used characters in Java have codes in
     the range 0 through 127.
   – For these characters the UNICODE code is the same as the
     ASCII code.
• Examples:

   character         a    b    A    B    (space)     0        ?

   UNICODE value    97   98    65   66      32       48      63

• Character codes are chosen so that ordering the numeric codes
  equates to alphabetical order.
   – Note that while order is preserved among upper case and
     among lower case characters, the two cases have different
     codes.
                                                                  11
       Storage of Floating-point Data

• How would 3.14159265358979323846 be stored?

• One approach:
  – Adjust all numbers so that they are of the form
    Z x 10E, where -1 < Z < 1
  – Use 1 bit to store the sign (+ or -) of the digits
  – Use 23 bits to store the digits
  – Use 1 bit to store the sign of the exponent
  – Use 7 bits to store the value of E
   SDDDDDDDDDDDDDDDDDDDDDDDSEEEEEEE
   – Approximate decimal equivalent: 0.ddddddd x 10ee
                                                         12
               Range and Precision

• Range represents the scope of values that can be
  stored:
   – Using the format from the previous slide:
      • Most positive number: ~ +3 x 1038
      • Most negative number: ~ -3 x 1038
      • Positive number closest to zero: ~ +1.5 x 10-45

• Precision represents how many significant digits are
  stored
   – In the format from the previous slide, six digits
     are generally the maximum that can be reliably
     stored.
                                                          13
                Effects of Range

• Exceeding the range of legal values results in
  OVERFLOW
   – number greater than most positive, or less than
     most negative
• A number that is too close to zero to be properly
  stored results in UNDERFLOW

• Suppose that we can store numbers between
  -99 and +99, with one decimal place allowed.

   -98 - 98  (overflow)
   15 / 0.1  (overflow)
   0.5 × 0.1  (underflow)                             14
              Effects of precision

• Suppose we were using decimal digits, and we had only
  three digits for storage
   1 / 3 = 0.33333333…
• ―Nearest‖ value that can be stored:
   0.333
• What about (1 / 3) x 3 ?
  0.999
• What about 222 + 4.44 ?
  0.226 E3
• This is called ―round-off‖ error

                                                     15
  Auxiliary Memory Organization

              Directory            “root” or “home”
                                   directory


  Directory        File     File



File     Directory




File   File      Directory         Directory




                 File     File   File   File
                                                      16
                      Running a Program

  Program—a set of instructions for a computer to follow

                             Program




     Data / Events
                             Computer         Output
(input for the program)




                                                       17
            Input/Output Devices

• Communication between computers and the outside
  world.

• Transforming information form different non-digital
  forms into digital form, and vice versa.




                                                        18
             Input/Output Devices

• Common input devices
   – Keyboards (converts each key or key combination
     to digital code)
   – Pointing devices (mouse, track balls, touch pads,
     etc.)
   – Scanner (converts an image to a grid of pixels)
• Common output devices
   – Monitors
   – Printers
   – Speakers for audio output
• Some devices do both:
                                                         19
              Types of Programs

• Operating System (OS)
  – Windows (98, Me, NT, 2000, XP), MacOS, UNIX /
    LINUX, etc.

• Application Software
   – word-processor/editor
   – web browser
   – compiler or assembler

• User Software

                                                20
    Functions of the Operating System

• Software responsible for directing all computer
  operations, and managing all computer resources.
   – Manages allocation of memory, processing time,
     etc.
   – Collects input from the input devices and provides
     this information to the currently running program
   – Conveys program output to the screen, printer,
     etc.
   – Reads/writes data to/from auxiliary storage
   – Security management


                                                      21
             Application Software

• Software used for a specific task such as word
  processing, database management, environments for
  developing user programs, spreadsheets, etc.

• Runs on top of the OS using its services; therefore,
  must be compatible with it




                                                         22
                  User Programs

• Developed by the user with the help of specialized
  application software.

• Runs on top of the OS.

• We will be developing many user programs during the
  next four months.




                                                       23
             Types of User Interfaces

• Command-line
   – All functions performed by typing commands on keyboard.
   – Text-only display.
   – Examples:
      • DOS, UNIX
• GUI (Graphical User Interface)
   – Icons, Menus, Buttons on graphical display.
   – Typically uses additional input devices as well as keyboard:
     mouse, pointer, …
   – Also called ―event-driven‖
   – Examples:
      • MacOS, Windows

                                                                    24
Programming Language Hierarchy


      High-Level Language (HLL)


         Assembly Language


         Machine Language


              Hardware
                                  25
                 Machine Language
                    (low level)

• A collection of binary instructions understood by a
  specific type of CPU chip.
• Non-standard; low-level of detail related to the given
  hardware
• Very hard to understand by human programmers
• Hardware dependent
• in order to be executed, any program must have been
  previously translated into machine language




                                                       26
                Assembly Language
                  (middle level)

• A (supposedly ) human readable version of machine
  language
• Words, abbreviations, letters and numbers replace 0s
  and 1s
• Easily translated from human readable to machine
  executable code (that is, from assembly language to
  machine language)
• Like machine code, assembly language is hardware
  dependent.



                                                     27
             High-Level Languages

• ―Closer‖ to the human way of expression, using
  higher-level of abstraction concepts from the
  application area for which it is intended.
• Mostly standardized, thus independent of a given
  hardware.
• Computers do NOT understand high-level languages
• Need a compiler (a translation program) to convert
  programs written in high-level languages into machine
  language.



                                                      28
           High-level languages

Language      When invented   Typical uses
FORTRAN       1956            Scientific computation
LISP          1958            Expert systems
COBOL         1960            Business
APL           1962            Mathematics, statistics
Basic         1971            Teaching, business
C             1972            Operating system creation
C++           1984            General purpose,
                              communications
Java          1993            General purpose, internet
                              applications

                                                          29
                       Other languages

Language                 When invented      Known for…
Pascal                   1971               Teaching
Smalltalk                1972               Object-oriented
Prolog                   1972               Logic, expert systems
Ada                      1979               US Defence Dept. software
                                            standard language
*SQL = Structured        1979               Databases
Query Language
*HTML = HyperText        1992               Web page formatting
Markup Language
*XML = eXtensible        1996               Internet data exchange
Markup Language
C#                       2002               Microsoft’s answer to Java


* Special-purpose languages: not for general programming.                30
               Getting from Source
                 to Machine Code

• Source code:
   – Input to a translation program.
   – Examples:
       • The program you write
       • Intermediate-level code that needs further
         translation.
   – Must (eventually) be translated to machine code.
• Object code:
   – Output from a translation program
   – Object code is usually machine code or a lower
     level language.
                                                        31
                   Getting from Source
                     to Machine Code
• Compiler
   – A program that translates from source code to object code.
      • Source code in this case is normally a high-level language
      • Object code could be machine code, an intermediate
        language, or assembly language.

• Assembler
   – Specific name for a compiler of the assembly language for
     particular hardware.
   – Output is always machine code.

• Interpreter:
   – A program that does both program translation and execution
   – Interpreter translates one statement at a time, and then
     executes the translated statement.
   – Source code can be either a high-level language, or an
     intermediate language.                                   32
         Example of language levels

• Suppose that we want to add two numbers.




                                             33
         Low-level problem steps (1)

• Suppose that we want to add two numbers.
   – Where is the first number?
   – Where is the second number?
   – What should we do with the two numbers?
   – Where should the result be put?




                                               34
         Low-level problem steps (2)

• Suppose that we want to add two numbers.
   – Where is the first number?
      • Location 0201
   – Where is the second number?
      • Location 0202
   – What should we do with the numbers?
      • Add them
   – Where should the result be put?
      • Location 0203


                                             35
           Low-level problem steps (3)

• Suppose that we want to add two numbers.
   – Step 1
      • Fetch a copy of the contents of memory location 0201
        and bring the value to the CPU register 0
   – Step 2
      • Fetch a copy of the contents of memory location 0202
        and bring the value to the CPU register 1
   – Step 3
      • Add contents of CPU register 1 to CPU register 0.
   – Step 4
      • Move the value from CPU register 0 to memory location
        0203 (which replaces the value currently stored there!)


                                                                  36
               In Machine Code

 Command             Operands (data)
 00110011            11001001 00000000
 00110011            11001010 00000001
 01100010            00000000 00000001
 11000011            00000000 11001011




Each CPU type has
a set of commands,
each of which has
a given arbitrary
code number.


                                         37
            In Assembly Language (1)

       Command       Operands (data)
       Load          $0201, R0
       Load          $0202, R1
       Add           R0, R1
       Store         R0, $0203



• There is a direct correspondence between the machine codes of
  the previous page, and the elements of the assembly language.
   – example:
       Load  00110011



                                                              38
             In Assembly Language (2)

       Command        Operands (data)
       Load           A, R0
       Load           B, R1
       Add            R0, R1
       Store          R0, C

• The assembler will need a symbol table to know the actual
  locations of A, B, and C.
       Symbol         Actual location
       A              $0201
       B              $0202
       C              $0203

• This version is easier to move around in the computer’s main
  memory: just change the locations in the symbol table.
                                                                 39
           In a High-Level Language

      C = A + B

• The computer will still need a symbol table, but this is
  produced as part of compiling the program. The
  compiler will ask the operating system for suitable
  locations in memory.

      Symbol       Actual location
      A            $0201
      B            $0202
      C            $0203
                                                        40
The highs and lows of programming languages ...

• High-Level Language (HLL)
  – closest to natural language
  – words, numbers, and math symbols
  – not directly understood by hardware
  – ―portable‖ source code (hardware independent)

• Machine Language (lowest level)
  – least natural language for humans, most natural
    language for hardware
  – just 0s and 1s
  – directly understood by hardware
  – not portable (hardware dependent)
                                                      41
               The important point

• Everything stored in a computer – data, program
  instructions, etc. – eventually has to be stored as a
  collection of 0s and 1s.
   – Programs have to be converted to 0s and 1s using a
     compiler or interpreter.
   – Data of any type also has to be stored using 0s and
     1s.
   – Files essentially consist of a long set of 0s and 1s.
   – Internet communication is based on sending and
     receiving 0s and 1s.
• Humans don’t work well with long collections of 0s and
  1s, and so we need translation of programs, and
  (re-)formatting of data.
                                                        42

								
To top