Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming Instructor: Yong Tang Brookhaven National Laboratory Working on accelerator control 631-344-7022 (BNL Phone #)

Similar presentations


Presentation on theme: "Introduction to Programming Instructor: Yong Tang Brookhaven National Laboratory Working on accelerator control 631-344-7022 (BNL Phone #)"— Presentation transcript:

1 Introduction to Programming Instructor: Yong Tang Brookhaven National Laboratory Working on accelerator control 631-344-7022 (BNL Phone #) yntang@bnl.gov tangy@sunysuffolk.edu

2 2 Text Book: Learning Processing by Daniel Shiffman Publisher: Morgan Kaufmann Introduction to Programming

3 3 Do we need to buy the text book? It is recommended that you buy the book. However, you can survive without it. Introduction to Programming

4 4 Supply: USB drives to save files and transfer data. Very cheap when they are on sale. About $1 per GB, or even less. Every one should have one or two. Introduction to Computing

5 5 Course Web Pages: www2.sunysuffolk.edu/tangy  Outline  Announcements  Slides  Projects  Data files for projects and exercises  Answers to some exercises Introduction to Programming

6 6 Attendance Policy  Attending classes is very important  Firm but flexible policy  If you can not attend a class, call 344-7022 or email tangy@sunysuffolk.edu  Absences Grades Introduction to Computing

7 7 Grading Policy  Specified in the outline  In general, if one attends the classes, finishes lab work, exams and projects, one can pass the course  Pay attention to the attendance policy  Do not worry about grades, to learn something is more important Introduction to Computing

8 8 Objective  Learning computer programming. Please notice that the language processing is only a tool.  One should be able to write medium-size and relatively complex programs. Introduction to Programming

9 9 What are programs?  A program is a sequence of instructions written in computer languages to guide computers to perform tasks.  A computer follows the instructions exactly and precisely. --- an important observation of computer programming. Introduction to Programming

10 10 An example  To calculate the average of two numbers Input: get the two numbers Processing: calculate their average Output: display the result Introduction to Programming

11 11 Programming Languages  Programming Language analogy: Each computer has a native machine language (language L0) that runs directly on its hardware A more human-friendly language is usually constructed above machine language, called Language L1 Introduction to Programming

12 12 Programming Languages  Programming Language analogy: Each computer has a native machine language (language L0) that runs directly on its hardware A more human-friendly language is usually constructed above machine language, called Language L1 Introduction to Programming

13 13 Programming Languages  Programming Language analogy (Cont.) Programs written in L1 can run two different ways: Interpretation – L0 program interprets and executes L1 instructions one by one Translation – L1 program is completely translated into an L0 program, which then runs on the computer hardware Introduction to Programming

14 14 Programming languages  First generation: machine language: Consists of 0’s and 1’s The only language understood by computers Fast and efficient Very hard to program, read and understand. Used in the old times. Introduction to Programming

15 15 Programming languages  Second generation: assembly language: English words are used in a very cryptic way Needs an assembler to convert it to machine language Relatively fast and efficient Still hard to program, read and understand; but is doable. The core parts of OS’s are usually coded in assembly language Different hardware (CPUs) has different assemblers Introduction to Programming

16 16 Programming languages  Third generation: high level language: C, C++, Java, C#,VB, Fortran,… English-like language Developing programs by writing source code Source code (compiler or interpreter)  assembly language (assembler)  machine language Still fast and efficient for most tasks Relatively easy to program, read and understand Portable to almost all platforms. Introduction to Programming

17 Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, 2010.17 Translating Languages English: Display the sum of A times B plus C. C++: cout << (A * B + C); Assembly Language: mov eax,A mul B add eax,C call WriteIn t Intel Machine Language: A1 00000000 F7 25 00000004 03 05 00000008 E8 00500000

18 Specific Machine Levels

19 19 Compiler and Interpreter  Compiler converts the whole program from source code to machine code, generates an executable program. Source files  object files (.obj) A linker links all the object files and library functions  an executable program (.exe)  Interpreter converts the source code one line/block at a time and execute it. Advantage: easy to develop and debug the program Disadvantage: slow Introduction to Programming

20 20 Write a fast program  The slowest part of a program is I/O: Reduce I/O times as much as possible Use buffers to do I/O  A fast computer make a program running fast:  Fast CPU (32-bit vs. 64-bit)  Big memory (32-bit machine can access only 3.7 Gb)  Good video card if you like to play fast games Introduction to Programming

21 Units of Memory and Storage  Computer understands 0 and 1 only: on/off state, N/S pole, 0/5 volts…  1 (“on”) and 0 (“off”) are referred to as bits.  Eight bits is a byte. Two bytes represent a unique character (Unicode)

22 Units of Memory and Storage  Kilobyte (KB) = one thousand (1024) bytes  Megabyte (MB) = one million (1024 KB) bytes  Gigabyte (GB) = one billion bytes  Terabyte (TB) = one trillion bytes

23 Data Representations  Everything is a number in the computer memory or on storages Numbers  numbers Characters  numbers by ASCII Grey  numbers (0-255) Color  three numbers (R/G/B)

24 Data Representations  Every English character is represented by an ASCII number.  ASCII codes are defined by ANSI.  Two bytes for a character (Unicode)

25 Data Representations  Decimal number: base 10 --- 0,1,2,…,9  Binary number: base 2 --- 0,1  Octal number: base 8 --- 0,1,2,…7  Hexadecimal number: base 16 --- 0,1,2,…9,A,B,C,D,E,F

26 Binary Numbers  Digits are 1 and 0 1 = true 0 = false  MSB – most significant bit  LSB – least significant bit  Bit numbering

27 Binary Numbers  Each digit (bit) is either 1 or 0  Each bit represents a power of 2: Every binary number is a sum of powers of 2

28 Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, 2010.28 Translating Binary to Decimal Weighted positional notation shows how to calculate the decimal value of each binary bit: dec = (D n-1  2 n-1 )  (D n-2  2 n-2 ) ...  (D 1  2 1 )  (D 0  2 0 ) D = binary digit binary 00001001 = decimal 9: (1  2 3 ) + (1  2 0 ) = 9

29 Binary Addition  Starting with the LSB, add each pair of digits, include the carry if present.

30 Integer Storage Sizes What is the largest unsigned integer that may be stored in 20 bits? Standard sizes:

31 Hexadecimal Integers Binary values are represented in hexadecimal.

32 Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, 2010.32 Translating Binary to Hexadecimal Each hexadecimal digit corresponds to 4 binary bits. Example: Translate the binary integer 000101101010011110010100 to hexadecimal:

33 Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, 2010.33 Converting Hexadecimal to Decimal  Multiply each digit by its corresponding power of 16: dec = (D 3  16 3 ) + (D 2  16 2 ) + (D 1  16 1 ) + (D 0  16 0 )  Hex 1234 equals (1  16 3 ) + (2  16 2 ) + (3  16 1 ) + (4  16 0 ), or decimal 4,660.  Hex 3BA4 equals (3  16 3 ) + (11 * 16 2 ) + (10  16 1 ) + (4  16 0 ), or decimal 15,268.

34 Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, 2010.34 Character Storage  Character sets Standard ASCII(0 – 127) Extended ASCII (0 – 255) ANSI (0 – 255) Unicode (0 – 65,535)


Download ppt "Introduction to Programming Instructor: Yong Tang Brookhaven National Laboratory Working on accelerator control 631-344-7022 (BNL Phone #)"

Similar presentations


Ads by Google