me 171 : computer programming languageteacher.buet.ac.bd/mdmamun/hardware.pdf · introduction to...

39
ME 171 : Computer Programming Language 3.00 Credit Hours Course Teacher: Dr. Md. Mamun Introduction to computer hardware and its working principle; Programming logic, algorithms, and flowcharts. Introduction to structured programming; Overview of C and C++ programming languages: C and C++ fundamentals data types and expressions; Operators; Libraries and keywords; Statements; Control statements; Input and output systems. Pointers; Arrays; Functions; Strings; Object Oriented programming; Introduction to advanced programming.

Upload: trinhkien

Post on 17-Apr-2018

223 views

Category:

Documents


4 download

TRANSCRIPT

  • ME 171 : Computer Programming Language

    3.00 Credit Hours

    Course Teacher: Dr. Md. Mamun

    Introduction to computer hardware and its working principle;

    Programming logic, algorithms, and flowcharts.

    Introduction to structured programming;

    Overview of C and C++ programming languages: C and C++ fundamentals

    data types and expressions; Operators; Libraries and keywords;

    Statements; Control statements; Input and output systems.

    Pointers;

    Arrays;

    Functions;

    Strings;

    Object Oriented programming;

    Introduction to advanced programming.

  • Textbooks:

    1. Teach Yourself C Herbert Schildt, Osborne

    2. Computer Fundamentals Pradeep K. Sinha, BPB Publications

    [ Any suitable book on C programming ]

    C ,

    The C Programming Language Brian. W. Kernighan and Dennis M.

    Ritchie, Pearson Education

    Softwares:

    Codeblocks (used in the sessional)

    [ Any suitable compiler for C programming such as DevCPP, Visual C++, Turbo

    C++, Borland C++, Geany, GNU GCC, Cygwin, Xcode, etc.]

  • What is a Computer ? A computer is an electronic device, which can input, process

    (according to instructions provided), and output data.

    input processing output

    A computer is a machine that stores data, interact with devices, and execute programs (provides computing capabilities to its

    users).

    A computer is an electronic device that stores, retrieves, and

    processes data, and can be programmed with instructions. A

    computer is composed of hardware and software, and can exist in

    a variety of sizes and configuration ,e.g., Mainframe computer as large as an entire building, Supercomputer, Single Board Computer

    (SBC) such as Raspberry Pi, Smartphone, Tablets, PC on a Stick, etc.

    Programs

    (Instructions)

    Data Data

  • What is a Computer ? Note that, a computer System is:

    Computer

    System

    Very

    Powerful

    (Speed)

    Completely

    Stupid

    (No intelligence)

    AI Artificial Intelligence Extremely Sophisticated Programming

    by Human for Machines

    AI systems may precisely be called Expert systems such as IBMs

    Deep Blue (Chess expert), Apples Siri, Microsofts Cortana,

    Googles Google Assistant, Amazons Alexa, Samsungs BixBy, etc.

    (Personal Digital Voice Assistants).

  • Stupidity of a Computer System

    Mary saw a bicycle in the store window. She wanted it.- The

    computer cannot determine if Mary wanted the bicycle or the

    store. This requires the input of the nearly complete set of likes

    and dislikes of human beings.

    Napoleon died on St. Helena. Wellington was saddened.-

    Understanding these simple sentences become very complicated

    for the computer. First, the computer has to figure out that

    Napoleon was a person; that the persons have the unfortunate

    habit of dying; that death is irreversible and undesirable; that

    death, in turn, often triggers emotion; and that sadness is one of

    these emotions.

  • Computer Programming

    So we see, a computer can do almost nothing by itself.

    Every single smallest step, however silly may it look like, has to

    be given to the computer in the form of instructions to get

    something done by the CPU.

    Some popular computer programming languages are Fortran, C,

    C++, Python, Java, HTML, PHP, Javascript, Perl, Ruby, Matlab,

    etc.

    Like human languages, all computer programming languages

    dictates their own set of alphabets (characters), words (keywords),

    syntax (grammar), semantics (meaning).

    A programming language is a formal constructed language

    designed to communicate these instructions to the computer.

  • Computer Programming

    However, the most important component of programming, i.e. ,

    logical step by step instructions (simply LOGIC or more

    prominent term ALGORITHM) to get something done by the

    CPU remains the same for a particular problem irrespective of the

    language of instruction.

    The last component of programming is Data Structure, which

    dictates the method of manipulating the program data, i.e.,

    storage/retrieval/modification of data (types) in the memory.

    Therefore, learning computer programming for the first time using

    a particular language means learning its alphabets, keywords,

    syntax, semantics plus algorithm (the single most significant

    aspect) and data structure.

  • Computer Programming

    Once a person develops skills / expertise in any particular

    computer language, (s)he can easily switch to another language

    (only by learning its alphabets, keywords, syntax, semantics).

    For a particular problem, the same algorithm can be used in

    all the languages.

  • Major Components of a Computer System

    A computer system consists of two sub-systems: hardware and

    software.

    Hardware is the electronic and mechanical parts of a computer

    system.

    Software is the data and the computer programs of a computer

    system.

  • Hardware vs. Software

    Hardware Physical Components (CPU, Mainboard, RAM, HDD, Keyboard, Monitor, etc.)

    Software Logical Instructions (OS, User programs, User Data, etc.)

    Firmware Logical Instructions imprinted on

    physical Components (BIOS, UEFI, etc. installed in Mainboard)

    [Basic Input Output System (BIOS) or Universal Extensible Firmware Interface (UEFI) provides the start-up (booting) instructions which the

    CPU executes immediately after the computer is powered on.]

    Without instructions, the CPU cannot even smart-up itself!! (start itself)

  • Main Hardware Components of a Processing Unit:

    o CPU Central Processing Unit (Processor)

    o Main Memory (RAM Random Access

    Memory )

    o MainBoard (Motherboard)

    o Power Supply Unit (PSU)

    o Secondary Memory /Auxiliary Storage

    [Hard Disk (Mechanical/SSD), Flash Disk (eMMC, SD), Optical Disk (DVD) and/or other Storage Devices]

    Computer Hardware

  • CPUs

    CISC

    Type

    RISC

    Type

  • Primary Memory

    RAM Modules

  • Mainboard / Motherboard

    Switching Mode Power Supply

    (SMPS)

  • Processing Unit of a Modern Desktop

    Computer System

  • Hardware vs. Software CPU is the brain plus heart of the computer system. It renders the whole

    computing system live and useful. It does the fundamental computing within the

    system. It directly or indirectly controls all the other components. A single-core of a

    CPU consists of 3 units:

    1. The Arithmetic and Logic Unit (ALU).

    2. The Control Unit (CU).

    3. Registers (Memory).

    When a computer is switched on, the CPU continuously goes through a process

    called fetch-decode-execute cycle:

    The Control Unit fetches the current instruction from memory, decodes it and

    instructs the ALU (Arithmetic Logic Unit) to execute the instruction.

    The execution of an instruction may generate further data fetches from memory

    The result of executing an instruction is stored in either a register (CPUs own

    memory) or RAM

    A multi-core CPU consists of a multiple of these 3-unit-core in a single chip so that

    multiple fetch-decode-execute (multi-threading) can take place making computation

    faster and efficient.

  • Hardware vs. Software Thus CPU continuously transfers data to and from the RAM.

    Instructions/Data transfer to and from the CPU are done in units called

    word (16 bit).

    If a CPU can handle a maximum word-size of 32 bits (Dword-double

    word), the CPU is called 32-bit CPU.

    If a CPU can handle a maximum word-size of 64 bits (Qword-quad

    word), the CPU is called 64-bit CPU.

    In addition to the registers, a CPU also has cache memories which are

    used to store the instructions that are used by the CPU repeatedly or the

    instructions in the pipeline (next executable instructions). Usually the

    higher the amount of the cache memory, the better the CPU performs.

    But cache memories are very expensive.

  • Hardware vs. Software There are two types of CPU architecture* in wide use today:

    CISC (Complex Instruction Set Computer) Intel x86, AMD64,

    x86_64, Intel Xeon, Atom, etc. are CISC CPUs. These CPUs

    support many instructions (sophisticated ones as well) but are power

    hungry. These are used in regular Desktop, Laptop, Netbook

    computers for their huge existing user-base, system frameworks and

    software libraries.

    RISC (Reduced Instruction Set Computer) ARM (v5, v6, v7) CPUs are

    RISC ones. They support fewer instructions (often simpler ones) and are

    power saver. So they are used in smart phones, tablet computers, etc.

    [*CPU architecture means physical and logical design-base, i.e., number and layout of physical transistor circuits and logical

    instruction sets implemented in the CPU.]

  • Hardware vs. Software Mainboard is a sophisticated PCB (Printed Circuit Board). It

    mainly provides the required circuitry to connect different devices

    with the CPU, for example, RAM to the CPU. It contains electrical

    wires plus some special-purpose IC chips (chipsets) to facilitate

    the connectivity of different devices with the CPU. Mainboards

    also contain the startup instructions (BIOS ROM chip).

    [The CPU is connected to RAM and I/O devices by the bus. Connectivity by wires plus dedicated ICs which follow some

    communication protocols (a set of agreed-upon rules) are called

    busconnectivity. For example, Universal Serial Bus (USB) is used

    connect pen drives to the CPU, SATA bus is used to connect hard disk to

    the CPU, etc. Other buses are SCSI, RS-232, PCI-E, Firewire,

    Thunderbolt, etc.]

  • Main Hardware Components

    +

    Main Software Components:

    OS - Operating System

    [User Software Programs / Packages]

    +

    One Input device + One Output device

    [Keyboard/Mouse + Monitor/Printer]

    Basic Computer System

  • Computing Platform (Environment)

    The computing platform or environment consists of any existing

    suitable combinations the main hardware component, i.e., CPU

    plus the main software component, i.e., OS (Operating System).

    Examples are:

    Intel/AMD x86 CPU + Windows OS (32 bit)

    Intel x86_64/AMD64 CPU + Windows OS (64 bit)

    Intel CPU + Linux

    Intel CPU + Mac OS X

    ARM CPU + Linux

    ARM CPU + Android

    ARM CPU + iOS

  • Computing Platform (Environment)

    The basic knowledge of a suitable platform is necessary for a

    programmer.

    Because all that a CPU understands are

    patterns of 1s and 0s. i.e., Machine language (or Object Code)

    Any program written in any language (called Source Code) must be compiled into Machine language of a particular CPU type (CISC

    / RISC) for the CPU to understand and execute accordingly. Note

    that, Different CPU understand different machine languages (bit

    patterns).

    Different OSs provide different ready-to-be-used machine code

    (called API routines/Library) that are used by the programmer

    in their own program. API routines dont match among OSs.

  • Primary Memory (RAM) Primary memory is divided into a number of memory

    cells (bits) or bytes.

    A bit (binary digit) is the smallest storage unit within a computer.

    It is a tiny electrical circuit that can be in one of two states:

    A voltage high represented by the symbol 1

    A voltage low represented by the symbol 0

    Any system of symbols can be represented by bit or byte patterns. A byte is a chunk of 8 bits. OSs keep track of the RAM on byte

    count.

    Each RAM byte must have a unique unsigned integer address,

    i.e., no two RAM bytes can have the same address. Byte

    addresses are automatically assigned by the OS.

  • 16-byte RAM Logical State byte-1 byte-2 byte-3 byte-4

    Address 0000 0001 0010 0011

    Value 00101101 00101101 00101101 00101101

    byte-5 byte-6 byte-7 byte-8

    Address 0100 0101 0110 0111

    Value 00101101 00101101 00101101 00101101

    byte-9 byte-10 byte-11 byte-12

    Address 1000 1001 1010 1011

    Value 00101101 00101101 00101101 00101101

    byte-13 byte-14 byte-15 byte-16

    Address 1100 1101 1110 1111

    Value 00101101 00101101 00101101 00101101

  • A 4-bit CPU/OS can only keep track of 24 =16 byte of RAM.

    A 16-bit CPU/OS can only keep track of 216 =65,536 byte (64 KB) of RAM.

    A 32-bit CPU/OS can only keep track of 232 =4,294,967,296 4 billion (giga) addresses of RAM bytes, i.e., 4 GB of RAM

    Also storages such as HDDs are divided into 512-byte sectors with unique

    addresses. Since only 4 billion addresses are available, A 32-bit CPU/OS can

    only address 4 512 = 2 partition of HDD.

    This 4 GB RAM and 2 TB HDD limits are avoided by 64-bit CPUs and 64-bit

    OSs.

    A 64-bit CPU/OS can keep track of 264 =18,446,744,073,709,551,616 byte (18

    EB, i.e., 18 Giga GB) of RAM and 9 ZB (9 Giga TB) partition of HDD.

    Primary Memory

    In order to avail the 64-bit computing advantages, one must have both 64-bit

    CPU and 64-bit OS.

    [32-bit CPUs cannot run 64-bit OSs and 32-bit OSs cannot run 64-bit softwares even if the CPU is 64-bit.]

  • Primary Memory UNIT SYMBOL POWER

    OF 2

    Number of

    bytes

    Byte 0

    2 1

    Kilobyte KB 10

    2

    1,024

    Megabyte MB 20

    2

    1,048,576

    Gigabyte GB 30

    2

    1,073,741,824

    Terabyte TB 40

    2

    1,099,511,627,776

    Address

    Value

  • RAM is the main working area of the CPU. Each RAM byte has

    got two very important properties for the programmers to

    understand clearly. One is its address (location), another is its

    value (content).

    All that a CPU does is reading value(s) from the

    pointed (addressed) RAM byte(s), (process it

    internally using its registers) or writing value(s) to

    the pointed (addressed) RAM byte(s)

    Each bit of information (programming instructions/data) for

    processing by the CPU is fetched from the RAM or stored back to

    the RAM after/or during processing. Not only that, every devices

    communicates to the CPU via its reserved area inside RAM

    (allocated by the OS).

    Primary Memory

  • Since RAM is volatile, (i.e., it loses all the instructions/data once

    the power is off), secondary persistent storage devices such as hard

    disk, usb flash drives, etc. are used to store the programming

    instructions/data permanently.

    However, for any processing to be done by the CPU, all the

    programming instructions/data must be loaded into the RAM

    byte(s).

    Primary Memory

    Usually in programming, the values contained into the RAM bytes

    are of general interest to the programmer. However in order to

    point (instruct) the CPU to manipulate the value(s) of the desired

    RAM byte(s), its address is to be mentioned by the programmer.

  • Primary Memory This becomes clumsy for the programmers to remember the

    addresses of the RAM bytes to manipulate their values. So All the

    programming languages provide symbolic names for pointing to

    the RAM byte(s) called identifiers.

    An identifier points to the address of a particular RAM byte. A

    very common example of an identifier is a variable used in a

    program. A (named) variable is linked to a particular RAM byte by

    the OS (as designated by the compiler) during execution of the

    program. A programmer can easily mention the variable name and

    manipulate that particular RAM byte by the CPU.

    Identifiers other than variables do exist. Examples are, (named)

    constants, functions, labels, etc. (They point to RAM bytes)

    Since a variable is a symbolic link to RAM bytes address, it also

    has got the same two important properties: (i) address of that

    RAM byte and (ii) value of that RAM byte.

  • Software Software is the programs and data that a computer uses.

    Programs are lists of instructions for the processor

    Data can be any information that a program needs: character data, numerical data, image data, audio data, etc.

    Both programs and data are saved in computer memory in the same way.

    Computer software is divided into two main categories:

    1. Systems software

    2. Applications software

    System software manages computer resources and makes computers easy to use. Examples are Operating Systems, Antivirus softwares, Disk utility programs, Networking softwares, etc.

    An applications software enables a computer to be used to do a particular task.

  • Operating Systems

    The most important systems program is the operating

    system.

    It is a group of programs that coordinates the

    operation of all the hardware and software

    components of the computer system.

    It is responsible for starting application programs

    running and finding the resources that they need.

    Examples of operating systems are: Windows 10/8/7,

    Windows XP, Linux, Unix, Mac OS X, Android, iOS,

    etc.

  • Computer Software

    Types of software

    Hardware Abstraction

    Layer (HAL)

  • Hardware Abstraction Layer (HAL) OSs provide Hardware Abstraction Layer (HAL) to facilitate the usage of

    computer by the user/programmer. HAL removes the hardships to control

    the hardware at their lowest levels. HAL also prevents direct access to the

    hardware by the user/programmer.

    HAL consists of device drivers (called kernel) to control the hardware

    at their lowest level and a set of API libraries/routines/framework to the

    user at a higher level to use those hardware in an efficient way, thus

    increasing the productivity of the programmer.

    With the evolution of the OSs, the HAL is growing thicker* and creating

    more restrictions* for the user to directly access the hardware but

    providing more and more API libraries/routines/framework to increase

    productivity.

    [ *Android rooting or iOS jailbreaking refers to the circumvention (like creating a hole through the HAL layer) of these hardware as well as software restrictions.

    Rooting or jailbreaking allows the programmer to install 3rd-party software and make

    direct access to different hardware components of the smartphones and tablets. ]

  • Software

    Application Programs Systems Programs

    Word processors

    Game programs

    Spreadsheets

    Data base systems

    Graphics programs

    Web browsers

    Operating system.

    Networking system.

    Programming

    language software.

    Web site server.

    Data backup.

  • Computer Languages

    Computer language evolution

    All the existing Computer programming languages can be broadly

    categorized into three groups:

    Low Level Language (Machine Language) Consists of patterns

    of 1s and 0s. No conversion required. Readily understood and

    executed by the CPU. But very difficult to program. Moreover,

    machine language of one CPU architecture (x86) is not understood

    by another CPU architecture (ARM).

    Mid Level Language (Assembly Language or Symbolic

    Language) Consists of mnemonics (symbolic names for bit

    patterns). Easier to program. Must be converted into machine

    language for the CPU to understand and execute the program.

    High level Language Consists of human language like words

    and symbols. Much easier to program. Must be converted into

    machine language for the CPU to understand and execute the

    program. Examples are Fortran, C, C++, Java, Python, Matlab, etc.

  • Computer Languages

    Computer language evolution

    The only language understood by a

    computer is machine language.

    Note: (**General-purpose, Scientific, Database, Scripting,

    Structured/Functional/Procedural, OOP**)

  • Steps to Solve a Problem by Computer Programming

    Computer language evolution

    Generally four steps are followed to solve a real-world

    computational problem by using a computer programming language:

    Step 1: Problem Definition General statement of the problem

    or the purpose of what is/are to be done with available inputs and

    desired outputs.

    Step 2: Algorithm Development with appropriate Data

    Structure Exploring the process/methodology to solve the

    problem and resolving the process/methodology into groups of

    single step sequential/logical instructions (called psudocode)

    executable by the CPU using appropriate data structure.

    Step 3: Coding Developing the source code of the solution

    using a suitable computer programming language.

    Step 4: Compiling, Linking, Testing and Debugging the Code

    Converting the source code into machine/object code and test

    run the code, check the output and errors and debug (correct) errors

    in the code. Then recompile, retest and debug if necessary.

  • Software Modules Required for Programming

    Computer language evolution

    Four software modules are required to constitute a complete programming environment.

    Module 1: Text Editor A plain text editor is required to develop the source

    code. Examples Notepad, Wordpad, Notepad++, WinEdit, TextEdit, vi, vim,

    emacs, etc.

    Module 2: Compiler or Interpreter A compiler/interpreter is required to

    convert the source code into machine code. Examples - gcc, g++, tcc, vcc, etc.

    Module 3: Linker A linker program links the API routines from the OS or other

    3rd party library codes to the programmers object code. Example ld.

    Module 4: Debugger A debugger checks for syntactical/logical errors in the

    code. Example gdb.

    There are some software packages which provide all the four required software

    modules within a single user interface. These softwares are called IDE (Integrated

    Development Environment). Examples - Codeblocks, Dev-C++, Turbo C, MS

    Visual Studio, Xcode, Eclipse, etc.

  • Compiler vs Interpreter

    Computer language evolution

    Compiler Interpreter

    A compiler converts the entire source code into

    machine code at once before runtime.

    An interpreter converts one line of the source code at a

    time during runtime.

    A compiler creates permanent machine code

    (filename.o), so compiled program does not need

    the source code (filename.c) to run.

    An interpreter does not create permanent machine code.

    So an interpreted program needs its source code during

    each run.

    Compiled programs are platform dependent.

    Separate object files must be generated for each

    platform from the source codes. So programmers

    or users must have compilers for those platforms.

    Platform independent. Interpreters are in-built in most

    OSs and Web browsers, so no separate interpreter is

    required.

    Source code can be kept closed during

    distribution of the software.

    Source code must be open for the program to run. Some

    interpreters such as Java interpreter make an

    intermediate file called byte-code. This is not machine

    code, so byte-code still needs to be converted into

    machine code to run. But in this case, the source code

    can be kept closed.

    Compiled programs always run faster. So time

    critical programs such as games, scientific

    simulation softwares, etc. are distributed as

    compiled programs.

    Interpreted programs run slower. Since platform

    independent, these programs are used in online

    (internet) programs facilitating the same programs to

    run in PCs, tablets, smartphones independent of their

    platforms.

    Languages that use compilers are Fortran, C,

    C++, etc.

    Languages that use interpreters are Java, Python,

    Matlab, JavaScript, php, etc.