computer science stuff, chapter 0

Upload: inky13112

Post on 09-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Computer Science Stuff, Chapter 0

    1/58

    1

    COSC 1410

    Chapter 0: Introduction

  • 8/8/2019 Computer Science Stuff, Chapter 0

    2/58

    2

    Contents

    Computer Organization

    Programming Languages Binary Numbers

    Compilers

  • 8/8/2019 Computer Science Stuff, Chapter 0

    3/58

    3

    COMPUTER ORGANIZATION

    A computer has the following functionalcomponents:

    CPU (Central Processing Unit)

    Control Unit

    ALU (Arithmetic and Logic Unit)

    Main Memory

    Secondary Memory

    Input Devices

    Output Devices

  • 8/8/2019 Computer Science Stuff, Chapter 0

    4/584

    Major Components of a Computer

    MainMemory

    SecondaryMemory

    OutputDevice

    InputDevice

    ControlUnit

    ALU

    CPU

  • 8/8/2019 Computer Science Stuff, Chapter 0

    5/585

    The Control Unit

    The control unit of the CPU controls alloperations of the computer.

    It works in a cycle. Each cycle fetches the next instruction of the program currently

    being executed,

    interprets (decodes) the instruction to determine whatshould be done,

    executes the instruction.

    This cycle is sometimes summarized asfetch/decode/execute.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    6/586

    CPU

    If the instruction being executed in this cycle requires 2numbers to be added together then the control unitcauses copies of the 2 numbers to be sent from thememory cells to the ALU.

    The control unit then sends a signal to the ALU telling itto perform the addition.

    If the next instruction executed says the resultingnumber should be stored in main memory then thecontrol unit sends signals that cause a copy of the

    number to be sent to and stored in some specifiedmemory cell.

    If the instruction being executed says to perform input oroutput then the control unit sends signals to the input or

    output devices causing this to be done.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    7/587

    Example: Fetch/Decode/Execute

    MainMemory

    SecondaryMemory

    OutputDevice

    InputDevice

    ControlUnit

    ALU

    CPU

    LOAD A

    LOAD B

    ADD

    STORE

    15 3

  • 8/8/2019 Computer Science Stuff, Chapter 0

    8/58

    8

    Example: Fetch/Decode/Execute

    MainMemory

    SecondaryMemory

    OutputDevice

    InputDevice

    ControlUnit

    ALU

    CPU

    LOAD A

    LOAD B

    ADD

    STORE 15

    3

  • 8/8/2019 Computer Science Stuff, Chapter 0

    9/58

    9

    Example: Fetch/Decode/Execute

    MainMemory

    SecondaryMemory

    OutputDevice

    InputDevice

    ControlUnit

    ALU

    CPU

    LOAD A

    LOAD B

    ADD

    STORE 15 + 3 = 18

  • 8/8/2019 Computer Science Stuff, Chapter 0

    10/58

    10

    Example: Fetch/Decode/Execute

    MainMemory

    SecondaryMemory

    OutputDevice

    InputDevice

    ControlUnit

    ALU

    CPU

    LOAD A

    LOAD B

    ADD

    STORE 18

  • 8/8/2019 Computer Science Stuff, Chapter 0

    11/58

    11

    The Control Unit

    The control unit uses

    a program counter to store the address of the next instruction

    to be fetched and an instruction register to store the current instruction that is

    being decoded and executed.

    The faster this basic cycle can be performed the faster

    the computer can execute a program.

    The speed of a computer is usually stated as somenumber of

    megahertz (million cycles per second) or

    gigahertz (billion cycles per second).

  • 8/8/2019 Computer Science Stuff, Chapter 0

    12/58

    12

    ALU

    The arithmetic and logic unit, ALU, isresponsible for performing

    Arithmetic calculations involving addition, subtraction,multiplication and division and

    Logical operations such as the test i < n.

    The ALU uses arithmetic registers to store thenumbers involved in a calculation or logical

    operation. The number of such registers variesfor different computers.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    13/58

    13

    The Main Memory

    The main memory is where the data and instructions ofthe program being executed are stored.

    Main memory consists of a large number of memorycells each of which can store a sequence of binary digits(bits) that represent an instruction or data value.

    The amount of memory necessary to store a single

    character is called a byte and memory size is usuallystated in terms of the number of bytes of memory(megabytes or gigabytes).

    Each byte consists of 8 bits (binary digits).

  • 8/8/2019 Computer Science Stuff, Chapter 0

    14/58

    14

    Memory Address

    The bytes of memory are numbered from 0 to n-1 wheren is the number of bytes in the main memory of thecomputer.

    This number, referred to as the address of the byte,serves to identify the memory location and is used tospecify which bytes of memory are being referred to in

    machine language instructions. Memory addresses are also used in some C++

    instructions. The information stored in memory (data or

    instructions) can be sensed by the computer withoutbeing changed.

    However if the computer stores new information inmemory, this destroys the old information that was in

    that position in memory.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    15/58

    15

    Main Memory

    010001000000010001010100

    0100011101100100 0110010101011100

    0000000000000000

    01000100

    0 202 203 208

    300 301 302 303

    n-2 n-1

    bit Byte

    WordAddress

  • 8/8/2019 Computer Science Stuff, Chapter 0

    16/58

    16

    Main Memory

    010001000000010001010100

    0100011101100100 0110010101011100

    0000000000000000

    01000100

    0 202 203 208

    300 301 302 303

    n-2 n-1

    Integer 1092 Character D

    An instruction

  • 8/8/2019 Computer Science Stuff, Chapter 0

    17/58

    17

    Addresses

    High-level programming languages like C++ use variablenames to refer to values stored in memory. This makesreferring to stored values much easier than having toremember an address.

    For example if a programmer in C++ declares a variable,say i, to be of type integer then the compiler associates

    this name with some bytes of memory, say bytes 202-203 in the above picture.

    Then instead of having to refer to its address, we canuse the variable name in the C++ program.

    There are however some C++ instructions that useaddresses (pointers) and you need to be familiar with thenotion of a memory address.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    18/58

    18

    Memory Addresses

    0000010000000000

    01010100

    01000111

    01100100

    01100101

    01011100

    00000000

    01000100

    01000100

    0

    201

    202

    203

    204205

    206

    207

    208

    n-1

    00000000

    00000000

    i = i + 1;

    print ch;

    1092

    D

    variable type Startingaddress

    i int 202

    ch char 208

  • 8/8/2019 Computer Science Stuff, Chapter 0

    19/58

    19

    Secondary Memory

    Secondary memory provides permanent and large-scale storage of information. The most commonsecondary storage devices are magnetic disks thatrecord information in a magnetic form. Floppy disks provide a cheap way of storing data and programs

    in a form that is easily transportable.

    Hard disks provide for much faster writing and or reading ofinformation on the disk, but are not easily transportable.

    CD-ROMs are another secondary storage device. They providestorage for a large amount of information represented by asequence of tiny pits recorded by laser. The ROM part of thename stands for Read Only Memory.

    Flash memory is still another increasingly popular form ofsecondary memory.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    20/58

    20

    Input Devices

    The input devices allow information to be inputted intothe computer.

    The most common input devices are the keyboard andmouse.

    The keyboard allows typed information (instructions or data) tobe inputted into the computer.

    The mouse allows input into the computer by moving the cursordisplayed on the monitor to a particular icon (picture) or wordand clicking one of the mouse buttons. This inputs to the

    computer a choice of some operation to perform.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    21/58

    21

    Output Devices

    The output devices allow information to beoutputted from the computer to the user. Themost common output devices are the monitorand printer. The monitor of a computer has a screen where

    information is displayed. When the user types

    information at the keyboard this information isdisplayed (echoed) on the monitor as well as beinginputted into the computer. When the computer doesoutput from the program that is currently being

    executed, this is also displayed on the monitor. A printer is used to print computer output on paper sothat the user can have a printed copy of theinformation.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    22/58

    22

    Representation of Information

    All information stored in the memory of a computer is insome binary form, i.e. a sequence of binary digits (bits).

    We will discuss some of the details for one type ofinformation, namely integers. We are used to writingnumbers in decimal.

    Decimal numbers are written as a sequence of decimal

    digits, 0-9. The position of a digit determines what itstands for. The right most digit is thought of as multipliedby 1, the next digit from the right is thought of asmultiplied by 10, the next by 100, etc. In other wordseach digit from right to left is multiplied by the next powerof 10.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    23/58

    23

    Example: Decimal Number

    0 0 0 0 3 8 5 1

    107

    106

    105

    104

    103

    102

    101

    100

    10000000

    1000000

    100000

    10000

    1000

    100

    10

    1

    For examplethe decimalinteger 3851stands for thenumber 1*1 +

    5*10 + 8*100+ 3*1000.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    24/58

    24

    Example: Decimal Number

    0 0 0 0 3 8 5 1

    107

    106

    105

    104

    103

    102

    101

    100

    00000000

    0000000

    000000

    00000

    3000

    800

    50

    1

    For examplethe decimalinteger 3851stands for thenumber 1*1 +

    5*10 + 8*100+ 3*1000.

    3851

  • 8/8/2019 Computer Science Stuff, Chapter 0

    25/58

    25

    Binary Numbers

    The binary number system represents an integer by asequence of bits, 0, 1.

    The right most bit is thought of as multiplied by 1, the

    next bit from the right is thought of as multiplied by 2, thenext by 4, etc.

    In other words each bit from right to left is multiplied bythe next power of 2.

    For example the binary integer 100101 stands for thenumber 1*1 + 0*2 + 1*4 + 0*8 + 0*16 + 1*32 which is 37in decimal.

    In a computer that stores integers in 2 bytes (= 16 bits)this number would be stored as 00000000 00100101.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    26/58

    26

    Example: Binary Number

    0 0 1 0 0 1 0 1

    27

    26

    25

    24

    23

    22

    21

    20

    128

    64

    32

    16

    84

    2

    1

    37

  • 8/8/2019 Computer Science Stuff, Chapter 0

    27/58

    27

    Binary Arithmetic

    Addition:

    0+0 = 0, with no carry,

    1+0 = 1, with no carry, 0+1 = 1, with no carry,

    1+1 = 0, with a carry of 1.

    Complement 0 -> 1

    1 -> 0

  • 8/8/2019 Computer Science Stuff, Chapter 0

    28/58

    28

    Binary Arithmetic

    + 0 1

    0 0 1

    1 1 10

  • 8/8/2019 Computer Science Stuff, Chapter 0

    29/58

    29

    Binary Arithmetic

    + 0 1

    0 0 1

    1 1 10

    0 1 1 0

    + 0 1 1 1

    011

    Carry over

    11 1 0

  • 8/8/2019 Computer Science Stuff, Chapter 0

    30/58

    30

    Negative Numbers

    There are several schemes to store negative integers.The most commonly used one on computers is twoscomplement notation.

    This notation has several advantages that are difficult toexplain in a brief discussion. The twos complementversion of a negative number is formed by finding thebinary number representation of the positive number.

    Then each bit is changed to its complement, i.e. each 1is changed to 0 and each 0 to 1. This gives the so calledones complement of the number.

    To get the twos complement version one adds 1 to this

    number.

  • 8/8/2019 Computer Science Stuff, Chapter 0

    31/58

    31

    Example: Negative Integers

    37

    Complementeach bit

    Add 1

    00000000 00100101

    11111111 11011010

    11111111 11011011

    2scomplement

    1scomplement

  • 8/8/2019 Computer Science Stuff, Chapter 0

    32/58

    32

    2s Complement

    This is the twoscomplement version of -37

    and is the way mostcomputers would store -37.

    Note that if we do anaddition of +37 and -37we do get 0.

    Note that 1 + 1 results in 0plus a carry of 1.

    00000000 00100101

    11111111 11011011

    00000000 00000000

  • 8/8/2019 Computer Science Stuff, Chapter 0

    33/58

    33

    2s Complement

    Problem with 1s Complement Zero is coded (000000)

    +3 (000011) and -3 (111100) should add up to zero,but it add up to 111111.

    So there are two zeros (000000 and 111111) in this

    system!

  • 8/8/2019 Computer Science Stuff, Chapter 0

    34/58

    34

    2s Complement

    000 001 010 011

    111 110 101 100

    0 1 2 3

    100 101 110 111

    101 110 111 000

    -3 -2 -1 -0

    Problem with 1s Complement Zero is coded (000000)

    +3 (000011) and -3 (111100) should add up to zero,but it add up to 111111, not 000000.

    So there are two zeros!

    D i l Bi

  • 8/8/2019 Computer Science Stuff, Chapter 0

    35/58

    35

    Decimal to Binary

    How do you convert a decimal integer into binary? One method is torepeatedly divide the decimal number by 2. The remainder of thedivision is the next bit from right to left. The quotient is then used inthe next division. For example

    Thus the binary number representation of the decimal number 37 is100101. If this is stored in 2 bytes=16 bits then the leading bits are

    all 0 giving the result 00000000 00100101.

    Number

    (Divide by 2)

    Quotient Remainder

    37/2 18 1

    18/2 9 0

    9/2 4 1

    4/2 2 0

    2/2 1 0

    1/0 0 1

    Stop

    Si l Fl h

  • 8/8/2019 Computer Science Stuff, Chapter 0

    36/58

    36

    Simple Flowchart

    p = 0 ?

    q = p/2

    r = p mod 2r is the next bit

    p=q

    Input p

    Outputthe bits

    no

    yes

    Bi I f ti

  • 8/8/2019 Computer Science Stuff, Chapter 0

    37/58

    37

    Binary Information

    To convert a negative decimal integer (such as -37) tothe twos complement version in binary one just convertsthe positive decimal integer (+37) to the binary form

    shown and then complements each bit and adds 1 asillustrated before.

    All other information is also stored in some binary form.

    For example a character like A is stored using charactercodes. The ASCII character code for A is 65. This isstored as a one byte binary integer. 01000001 (= 1*1 + 0*2 + 0*4 + 0*8 + 0*16 + 0*32 + 1*64 = 65)

    Floating point numbers are also stored in a binary form.Even machine language instructions are stored usingbinary codes.

    P i L

  • 8/8/2019 Computer Science Stuff, Chapter 0

    38/58

    38

    Programming Languages

    There are 3 classes of programming languages,

    machine languages,

    assembly languages and

    high-level languages.

    M hi L

  • 8/8/2019 Computer Science Stuff, Chapter 0

    39/58

    39

    Machine Languages

    Each computer has its own machine language which isvery detailed and specific for the exact details of thecomputer architecture.

    All instructions in a machine language are in binarycodes.

    Since machine languages are machine dependent,very detailed and use binary codes it is very difficult towrite programs in them.

    A mbl L g g

  • 8/8/2019 Computer Science Stuff, Chapter 0

    40/58

    40

    Assembly Languages

    Assembly languages are symbolic versions of machinelanguages.

    The instructions use symbolic codes rather than binary

    codes and thus are easier to remember. But assemblylanguages are still machine dependent and very detailedand thus difficult to use. As an example an assemblylanguage might have an instruction like

    Load R4,Xwhich means load the value of variable X into the register

    R4 in the ALU. In machine language this sameinstruction might be 00010111 0100 00100110 wherethe first 8 bits is a binary code standing for load and thenext 4 bits give the register number in binary and the lastbit sequence gives the address of the variable X inbinary.

    High Level Languages

  • 8/8/2019 Computer Science Stuff, Chapter 0

    41/58

    41

    High Level Languages

    High-level languages are machine independent andmuch less detailed than machine/assembly languages.

    This makes it easier to write programs and allows thesame program to be run on different computers. Mostlanguages have standards defined.

    There are many different high-level languages including

    Fortran, Cobol, Basic, C, C++, C# and Java. We will learn C++/C in this course.

    C

  • 8/8/2019 Computer Science Stuff, Chapter 0

    42/58

    42

    C

    C is a programming language that was developed in the1972 at Bell Labs by Dennis Ritchie and was initiallyused to implement the UNIX operating system.

    It has since gained wide use in scientific and systemsprogramming.

    It is a high-level language with some low-level featuressuch as bit manipulation that most high level languagesdo not have.

    A sample C program

  • 8/8/2019 Computer Science Stuff, Chapter 0

    43/58

    43

    A sample C program

    /* Hello World program */

    #include

    main()

    {

    printf("Hello World");}

    Exam1-0.cpp, Exam1-1.cpp

    From C to C++

    http://../CPP/ch01/exam1-0.cpphttp://../CPP/ch01/exam1-1.cpphttp://../CPP/ch01/exam1-1.cpphttp://../CPP/ch01/exam1-0.cpp
  • 8/8/2019 Computer Science Stuff, Chapter 0

    44/58

    44

    From C to C++

    C was later extended to C++ by adding many newfeatures related to object-oriented programming andimproving some other aspects of the language.

    Backward compatibility issue: (almost) all syntax of theoriginal C are included in the C++. A program written instandard C should compile with a C++ compiler (thereare exceptions in which you have to make some minormodification).

    C with Some C++

  • 8/8/2019 Computer Science Stuff, Chapter 0

    45/58

    45

    C++

    C with Some C++

    In this course you will learn the basic parts of C++ thatare common to C, procedural programming and someaspects of C++ that are new, including an introduction toclasses and object-oriented programming.

    C

    A sample C++ program

  • 8/8/2019 Computer Science Stuff, Chapter 0

    46/58

    46

    A sample C++ program

    /* Hello World program */

    #include

    using namespace std;

    int main()

    {

    cout

  • 8/8/2019 Computer Science Stuff, Chapter 0

    47/58

    47

    C++ Compilers

    When the computer is actually executing a program it isalways in the machine language of that computer.

    However it is difficult and tedious to program in machinelanguage. Most programs are written in some high-level,machine independent programming language such asC++.

    Before the computer can execute a program written insuch a language it must be translated into the machinelanguage of the computer.

    The Role of a Compiler

  • 8/8/2019 Computer Science Stuff, Chapter 0

    48/58

    48

    The Role of a Compiler

    High LevelLanguage

    MachineLanguage

    Computer

    Programmer

    Compiler

    The Role of a Compiler

  • 8/8/2019 Computer Science Stuff, Chapter 0

    49/58

    49

    The Role of a Compiler

    A compiler is a program that does the translation from ahigh-level language into the computer's machinelanguage. An instruction in C++ such as

    i = i + 1;that is C++'s way of saying to assign to variable i the

    current value of i plus 1 might be translated into severalmachine language instructions something like the

    following. Send a copy of the integer in bytes 202-203 to the ALU

    (assuming is value is stored in bytes 202-203) Add 1 to this value in the ALU

    Store the new value back in memory in bytes 202-203

    Steps of Compilation

  • 8/8/2019 Computer Science Stuff, Chapter 0

    50/58

    50

    Steps of Compilation

    A C++ program must be compiled before thecomputer can execute it.

    The C++ compiler first invokes a program called thepreprocessor that does such things as copy the file (which contains information about

    the input and output functions used in C++) into theC++ source file before compilation starts.

    The compiler next translates the instructions in a C++

    program into the machine language of the computeras suggested above.

    Finally it links in the code for functions such as theinput and output functions.

    Steps of Compilation

  • 8/8/2019 Computer Science Stuff, Chapter 0

    51/58

    51

    Steps of Compilation

    The resulting file contains machine language instructionsthat can be understood and executed by the computer.

    Files Involved

  • 8/8/2019 Computer Science Stuff, Chapter 0

    52/58

    52

    Files Involved

    A file containing a C++ program is called a C++ sourcefile (.cpp).

    After being compiled the resulting file is called anexecutable file (.exe).

    If a file is compiled, but not linked then the resulting file iscalled an object file (.obj).

    Steps of Compilation

  • 8/8/2019 Computer Science Stuff, Chapter 0

    53/58

    53

    Steps of Compilation

    Source File

    Object File

    Executable File

    Library File

    Translator Linker

    Variations

  • 8/8/2019 Computer Science Stuff, Chapter 0

    54/58

    54

    Variations

    The exact details of how to compile and execute a C++program vary slightly from system to system and youmust learn these from documentation about your systemor from someone who already knows the details. ForDev-C++ these are given in Appendix 1.

    Different programming languages may have different

    compilation process. The next slide show how a Javaprogram compiles and executes in an HP OpenVMSenvironment. Its just an example to show how

    complicated it can get. You dont have to understand thechart.

    HP OpenVMS (you dont have to know this)

  • 8/8/2019 Computer Science Stuff, Chapter 0

    55/58

    55

    p (y )

    Source: HP web Site

    Abstraction

  • 8/8/2019 Computer Science Stuff, Chapter 0

    56/58

    56

    In discussing C++ programs we will always describe howC++ instructions are executed by the computer.

    We act as if the computer directly executes the C++

    instructions. In fact a single C++ instruction may havebeen translated into several machine languageinstructions that are actually the instructions which areexecuted by the computer.

    However since we don't have to do the translation, weshould think entirely at the higher level and understandC++ programs in terms of C++ instructions.

    About Your C++ Compiler

  • 8/8/2019 Computer Science Stuff, Chapter 0

    57/58

    57

    p

    If you program in C++ you must have a C++ compiler totranslate your program into machine language before itcan be executed.

    You can obtain a free version of Dev-C++, the C/C++compiler used in the lab, by down loading it from thefollowing web site.

    http://prdownloads.sourceforge.net/dev-cpp/devcpp4960.exe

    See Appendix 1 for more details on how to down loadand use Dev-C++.

    Which C?

  • 8/8/2019 Computer Science Stuff, Chapter 0

    58/58

    58

    So, which version of the C are we learning?

    Mostly C,

    Some C++, especially those features that areimprovements of C,

    Minimum C++, just to get by,

    It is still important to know the old C because thereare a lot of the C codes out there.