lec 8 lang hardware_001

Upload: faiz-akram

Post on 14-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Lec 8 Lang Hardware_001

    1/55

    Computer Languages

    Machine Language A collection of binary numbers Not standardized. There is a different machine language for

    every processor family.

    Assembly Language- mnemonic codes that corresponds

    to machine language instructions. Low level: Very close to the actual machine language.

    High-level Languages- Combine algebraic expressionsand symbols from English High Level : Very far away from the actual machine language

    For example: Fortran, Cobol, C, Prolog, Pascal, C#, Perl, Java.

    1

  • 7/30/2019 Lec 8 Lang Hardware_001

    2/55

    Example of ComputerLanguages

    char name[40];printf("Please enter your name\n");scanf("%s", name);printf("Hello %s", name);

    push offset string "Please enter your name\n"(41364Ch)call dword ptr [__imp__printf (415194h)]

    add esp,4lea eax,[name]push eaxpush offset string "%s" (413648h)call dword ptr [__imp__scanf (41519Ch)]add esp,8lea eax,[name]push eaxpush offset string "Hello %s" (41363Ch)

    call dword ptr [__imp__printf (415194h)]add esp,8

    68 4C 36 41 00 FF 15 94 51 41 00 83 C4 04 8D 45 D850 68 48 36 41 00 FF 15 9C 51 41 00 83 C4 08 8D 45D8 50 68 3C 36 41 00 FF 15 94 51 41 00 83 C4 08

    C Source Code:

    Assembly Code:

    Machine Code:

    2

  • 7/30/2019 Lec 8 Lang Hardware_001

    3/55

    Compiler

    Compilation is the process of translating the source code(high-level) into executable code (machine level).

    Source file - A file containing the program code

    A Compiler turns the Source File into an Object File

    Object file - a file containing machine languageinstructions

    A Linker turns the Object File into an Executable

    Integrated Development Environment (IDE) - a program

    that combines simple word processing with a compiler,linker, loader, and often other development tools

    For example, Eclipse or Visual Studio

    3

  • 7/30/2019 Lec 8 Lang Hardware_001

    4/55

    C Programming

    An introduction

  • 7/30/2019 Lec 8 Lang Hardware_001

    5/55

    History The initial development of C occurred at

    AT&TBell Labs between 1969 and 1973. It is developed in 1972 by Dennis Ritchie

    The origin of C is closely tied to the

    development of the Unix operating system, It was named "C" because many of itsfeatures were derived from an earlierlanguage called "B", which according to

    Ken Thompson was a stripped-downversion of the BCPL (Basic CombinedProgramming Language).

    5

    http://en.wikipedia.org/wiki/AT&Thttp://en.wikipedia.org/wiki/Bell_Labshttp://en.wikipedia.org/wiki/Dennis_Ritchiehttp://en.wikipedia.org/wiki/Unixhttp://en.wikipedia.org/wiki/B_(programming_language)http://en.wikipedia.org/wiki/Ken_Thompson_(computer_programmer)http://en.wikipedia.org/wiki/BCPLhttp://en.wikipedia.org/wiki/BCPLhttp://en.wikipedia.org/wiki/Ken_Thompson_(computer_programmer)http://en.wikipedia.org/wiki/B_(programming_language)http://en.wikipedia.org/wiki/Unixhttp://en.wikipedia.org/wiki/Dennis_Ritchiehttp://en.wikipedia.org/wiki/Bell_Labshttp://en.wikipedia.org/wiki/AT&T
  • 7/30/2019 Lec 8 Lang Hardware_001

    6/55

    Programming

    Programming - scheduling, or performing a task or anevent.

    Computer Programming - creating a sequence of stepsfor a computer to follow in performing a task.

    Programming Language - a set of rules, symbols, andspecial words used to construct a computer program.

    Programming language rules consist of:

    Rules of Syntax which specify how valid instructions are writtenin the language.

    Rules of Semantics which determine the meaning of theinstructions (what the computer will do).

    6

  • 7/30/2019 Lec 8 Lang Hardware_001

    7/55

    A SIMPLE C PROGRAM

    The following program is written in the Cprogramming language.

    #include

    main()

    {

    printf ("Programming in C is easy.\n");

    }Sample Program Output:Programming in C is easy.

    _

    7

  • 7/30/2019 Lec 8 Lang Hardware_001

    8/55

    A NOTE ABOUT C PROGRAMS

    In C, lowercase and uppercase charactersare very important! All commands in C mustbe in lowercase.

    The C programs starting point is identified

    by the word main() This informs the computer as to where the

    program actually starts.

    The brackets that follow the keyword mainindicate that there are no argumentssupplied to this program (this will beexamined later on).

    8

    http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_105.htm
  • 7/30/2019 Lec 8 Lang Hardware_001

    9/55

    Contd

    The two braces, { and }, signify the beginand end segments of the program.

    The purpose of the statment :

    #include is to allow the use of the printfstatementto provide program output.

    Text to be displayed byprintf() must be

    enclosed in double quotes . The program has only one statement

    printf("Programming in C is easy.\n")9

    http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_011.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_011.htm
  • 7/30/2019 Lec 8 Lang Hardware_001

    10/55

    printf() is actually a function in C that is

    used for printing variables and text. Wheretext appears in double quotes "", it isprinted without modification.

    There are some exceptions however. Thishas to do with the\ and % characters.These characters are modifier's, and for

    the present the\ followed by the ncharacter represents a newline character.

    Contd

    10

    http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_056.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_056.htm
  • 7/30/2019 Lec 8 Lang Hardware_001

    11/55

    Another important thing toremember is that all C

    statements are terminatedby a semi-colon ;

    Contd

    11

  • 7/30/2019 Lec 8 Lang Hardware_001

    12/55

    Summary of major points

    program execution begins at main() keywords are written in lower-case statements are terminated with a semi-colon text strings are enclosed in double quotes

    C is case sensitive, use lower-case and try notto capitalise variable names \nmeans position the cursor on the beginning of

    the next line

    printf() can be used to display text to the screen the curly braces { } define the beginning andend of a program block

    12

  • 7/30/2019 Lec 8 Lang Hardware_001

    13/55

    CLASS EXERCISE1

    Q1.1. What is the output of following program ?

    #include

    main(){

    printf("Programming in C is easy.\n");

    printf("And so is Pascal.\n");

    }

    13

  • 7/30/2019 Lec 8 Lang Hardware_001

    14/55

    ANSWER 1.1

    Programming in C is easy.And so is Pascal.

    _

    14

  • 7/30/2019 Lec 8 Lang Hardware_001

    15/55

    Process of Program execution

    The diagram of program execution is given on next

    Page:----

    15

  • 7/30/2019 Lec 8 Lang Hardware_001

    16/55

    Fig 1.12Entering,Translating,and Runninga High-LevelLanguageProgram

    16

  • 7/30/2019 Lec 8 Lang Hardware_001

    17/55

    Q1.2 What is the output of following program ?

    #include

    main()

    {

    printf("The black dog was big. ");

    printf("The cow jumped over the moon.\n");

    }

    17

  • 7/30/2019 Lec 8 Lang Hardware_001

    18/55

    ANSWER 1.2

    The black dog was big. The cow jumped over the moon.

    _

    18

  • 7/30/2019 Lec 8 Lang Hardware_001

    19/55

    Q1.3Try and work out what the following

    program displays,

    #include

    main(){

    printf("Hello...\n..oh my\n...when do i stop?\n");

    }

    19

  • 7/30/2019 Lec 8 Lang Hardware_001

    20/55

    ANSWER 1.3

    Hello...

    ..oh my

    ...when do i stop?

    _

    20

  • 7/30/2019 Lec 8 Lang Hardware_001

    21/55

    21

    KEYWORDS Keywords are words reserved by C so

    they cannot be used as variables.Keywords

    auto double int struct

    break else long switch

    case enum register typedef

    char extern return union

    const float short unsigned

    continue for signed void

    default goto sizeof volatile

    do if static while

  • 7/30/2019 Lec 8 Lang Hardware_001

    22/55

    Variables & Data Types

    Declaring a variable reserves enoughbytes in memory to store value of thedeclared type.

    22

  • 7/30/2019 Lec 8 Lang Hardware_001

    23/55

    Contd

    C provides the programmer with FOURbasic data types

    These are:

    integercharacter

    float

    double

    23

    http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_006.htmhttp://gd.tuwien.ac.at/languages/c/programming-bbrown/c_006.htm
  • 7/30/2019 Lec 8 Lang Hardware_001

    24/55

    Contd

    The basic format for declaring variables is

    data_type var, var, ... ;

    where data_type is one of the four basictypes, an integer, character, float, ordouble type.

    24

  • 7/30/2019 Lec 8 Lang Hardware_001

    25/55

    DATA TYPE DESCRIPTION MEMORYREQUIRMENT

    int Integer quantity 2 bytes(-32767 ,32767)

    Long int Integer quantity 4 bytes

    float A number containing adecimal point

    4 bytes

    (1E-37 to 1E+37)

    char Any character 1 byte (-127 to 127)

    double A number containing adecimal point but here wecan have more significantfigures.

    8 bytes

    25

  • 7/30/2019 Lec 8 Lang Hardware_001

    26/55

    Contd User defined variables must be declared

    before they can be used in a program.

    Get into the habit of declaring variablesusing lowercase characters. Remember

    that C is case sensitive, so even though thetwo variables listed below have the samename, they are considered different

    variables in C. sum

    Sum

    26

  • 7/30/2019 Lec 8 Lang Hardware_001

    27/55

    Contd

    The declaration of variables is done after theopening brace ofmain(),

    e.g.

    #include

    main(){

    int sum;

    sum = 500 + 15;printf("The sum of 500 and 15 is %d\n", sum);

    }

    27

  • 7/30/2019 Lec 8 Lang Hardware_001

    28/55

    General Form of printf() function

    printf(,);

    can contain

    %d for printing integer values.

    %f for printing real values.%c for printing character values.

    28

  • 7/30/2019 Lec 8 Lang Hardware_001

    29/55

    scanf() function

    scanf(format specifier,& list of vaiables)

    Again format specifier is same as in case of printf().

    And & is address of operator.

    29

  • 7/30/2019 Lec 8 Lang Hardware_001

    30/55

    Some of the formatters forprintfare

    1. Cursor Control Formatters

    \n new line

    \t tab

    30

  • 7/30/2019 Lec 8 Lang Hardware_001

    31/55

    Contd

    2. Variable Formatters%d decimal integer

    %c character

    %s string or character array

    %f float

    %e double

    31

  • 7/30/2019 Lec 8 Lang Hardware_001

    32/55

    CLASS EXERCISE2

    Q2.1 What is the output of the following program?

    #include

    main()

    {int value1, value2, sum;

    value1 = 35;

    value2 = 18;

    sum = value1 + value2;

    printf("The sum of %d and %d is %d\n", value1, value2, sum);

    }

    32

  • 7/30/2019 Lec 8 Lang Hardware_001

    33/55

    ANSWER2.1

    The sum of 35 and 18 is 53_

    33

  • 7/30/2019 Lec 8 Lang Hardware_001

    34/55

    MORE ABOUT VARIABLES

    Variables must begin with a character orunderscore, and may be followed by anycombination of characters, underscores, or thedigits 0 - 9.

    The following is a list of valid variable names:summaryexit_flagI

    Jerry7Number_of_moves

    _valid_flag

    34

  • 7/30/2019 Lec 8 Lang Hardware_001

    35/55

    Contd You should ensure that you use

    meaningful names for your variables. Thereasons for this are:

    meaningful names for variables are self

    documenting (see what they do at aglance)

    they are easier to understand

    there is no correlation with the amount ofspace used in the .EXE file

    makes programs easier to read

    35

  • 7/30/2019 Lec 8 Lang Hardware_001

    36/55

    CLASS EXERCISE 3

    Q3.1 Why are the variables in thefollowing list invalid,

    value$sum

    exit flag

    3lotsofmoney char

    36

  • 7/30/2019 Lec 8 Lang Hardware_001

    37/55

    ANSWER3.1

    value$sum contains a $

    exit flag contains a space 3lotsofmoney begins with a digit

    char is a reserved keyword

    37

  • 7/30/2019 Lec 8 Lang Hardware_001

    38/55

    COMMENTS

    The addition of comments inside programsis desirable.

    These may be added to C programs byenclosing them as follows:

    /* bla bla bla bla bla bla */

    Note that the/* opens the comment fieldand */ closes the comment field.Comments may span multiple lines.

    38

  • 7/30/2019 Lec 8 Lang Hardware_001

    39/55

    Contd

    Comments may not be nested one insideanother. For e.g.

    /* this is a comment. /* this comment isinside */ wrong */

    In the above example, the first occurrenceof*/ closes the comment statement for theentire line, meaning that the text wrongisinterpreted as a C statement or variable,and in this example, generates an error.

    39

  • 7/30/2019 Lec 8 Lang Hardware_001

    40/55

    What Comments Are Used For

    Documentation of variables and theirusage

    Explaining difficult sections of code Describes the program, author, date,

    modification changes, revisions etc

    Copyrighting

    40

    PREPROCESSOR

  • 7/30/2019 Lec 8 Lang Hardware_001

    41/55

    PREPROCESSORSTATEMENTS

    Note that preprocessor statements beginwith a # symbol, and are NOT terminatedby a semi-colon.

    Preprocessor statements are handled bythe compiler (or preprocessor) before theprogram is actually compiled.

    In general, preprocessor constants arewritten in UPPERCASE

    41

  • 7/30/2019 Lec 8 Lang Hardware_001

    42/55

    Contd

    The define statement is used to makeprograms more readable.

    Consider the following examples:

    #define TRUE 1#define FALSE 0

    #define NULL 0

    #define AND &

    #define OR |

    #define EQUALS ==

    42

  • 7/30/2019 Lec 8 Lang Hardware_001

    43/55

    HEADER FILES

    Header files contain definitions offunctions and variables which can beincorporated into any C program by using

    the pre-processor#include statement. Standard header files are provided with

    each compiler, and cover a range ofareas, string handling, mathematical, dataconversion, printing and reading ofvariables.

    43

  • 7/30/2019 Lec 8 Lang Hardware_001

    44/55

    Contd

    To use any of the standard functions, theappropriate header file should be included.

    This is done at the beginning of the C

    source file. For example, to use thefunctionprintf() in a program, the line

    #include

    should be at the beginning of the sourcefile, because the definition forprintf() isfound in the file stdio.h

    44

  • 7/30/2019 Lec 8 Lang Hardware_001

    45/55

    Contd

    All header files have the extension .h andgenerally reside in the/include subdirectory.

    The use of angle brackets informs thecompiler to search the compilers include

    directory for the specified file. The use of the double quotes " around the

    filename inform the compiler to search in thecurrent directory for the specified file.

    #include "mydec.h"

    45

  • 7/30/2019 Lec 8 Lang Hardware_001

    46/55

    46

    The only characters required by the C ProgrammingLanguage are as follows:

    A - Za -z0 - 9

    space . , : ; ' $ "# % & ! _ {} [] () < > |

    + - / * =

    C character set

    46

    V i bl C t t d

  • 7/30/2019 Lec 8 Lang Hardware_001

    47/55

    Variables, Constants andKeywords

    Variables:-It is a name given to a memorylocation to store a value of a data that aprogram is working on.

    47

    R l f t ti V i bl

  • 7/30/2019 Lec 8 Lang Hardware_001

    48/55

    Rules for constructing Variablenames

    (1) A variable name is any combination of 1to 31 alphabets, digits or underscores butthere is some restriction on the length of

    variables that depends on compiler tocompiles. Do not create unnecessarilylong variable names as it adds to youreffort.

    (2) The first character in the variable namemust be an alphabet or underscore.

    48

  • 7/30/2019 Lec 8 Lang Hardware_001

    49/55

    Continued

    (3) No commas or blanks are allowed withina variable name.

    (4) No special symbol other than an

    underscore can be used in a variablename.

    49

  • 7/30/2019 Lec 8 Lang Hardware_001

    50/55

    50

    Keywords:

    Keywords are words reserved by C sothey cannot be used as variables.

    Keywords

    auto double int structbreak else long switch

    case enum register typedef

    char extern return union

    const float short unsigned

    continue for signed void

    default goto sizeof volatile

    do if static while

    50

  • 7/30/2019 Lec 8 Lang Hardware_001

    51/55

    Data Types.

    Type of value that a variable can hold iscalled data type of a variable.

    51

    continued

  • 7/30/2019 Lec 8 Lang Hardware_001

    52/55

    52

    continuedDATA TYPE DESCRIPTION MEMORY

    REQUIRMENT

    int Integer quantity 2 bytes(-3276732767)

    Long int Integer quantity 4 bytes

    float A number containing adecimal point

    4 bytes

    (1E-37 to1E+37)

    char Any character 1 byte (-127 to 127)

    double A number containing adecimal point but herewe can have moresignificant figures.

    8 bytes

    52

  • 7/30/2019 Lec 8 Lang Hardware_001

    53/55

    53

    Declaringvariable

    In order to use a variable in a C program, youmust first declare it.

    You declare a variable with a declaration.

    int x=5; As in the example shown above, a declaration of

    a variable consists of the variables type followed

    by the variables name and then a semicolon.

    There must be whitespace between the type andthe name.

    It is also possible to declare multiple variables

    on one line, as well see later.53

  • 7/30/2019 Lec 8 Lang Hardware_001

    54/55

    Constants

    A constant is an entity that doest change

    its value while a variable is an entity thatmay change.

    54

  • 7/30/2019 Lec 8 Lang Hardware_001

    55/55

    Types of C constants

    (a) Primary constants. i.e. integer, real andcharacter constants.

    (b) Secondary constants. i.e. Array, pointer,

    structure, union and enum etc.

    55