Top Banner
Introduction to Programming Instructor: Yong Tang Brookhaven National Laboratory Working on accelerator control 631-344-7022 (BNL Phone #) [email protected] [email protected]
34

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

Jan 04, 2016

Download

Documents

Christina Lang
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Programming Instructor: Yong Tang Brookhaven National Laboratory Working on accelerator control 631-344-7022 (BNL Phone #) yntang@bnl.gov.

Introduction to Programming

Instructor:Yong TangBrookhaven National LaboratoryWorking on accelerator control631-344-7022 (BNL Phone #)[email protected]@sunysuffolk.edu

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

2

Text Book:Learning Processingby Daniel ShiffmanPublisher: Morgan Kaufmann

Introduction to Programming

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

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

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

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

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

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

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

6

Attendance Policy

Attending classes is very important Firm but flexible policy If you can not attend a class,

• call 344-7022 or • email [email protected]

Absences Grades

Introduction to Computing

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

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

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

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

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

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

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

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

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

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

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

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

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

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 oneTranslation – L1 program is completely translated

into an L0 program, which then runs on the computer hardware

Introduction to Programming

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

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

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

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

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

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

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

Translating LanguagesEnglish: Display the sum of A times B plus C.

C++: cout << (A * B + C);

Assembly Language:mov eax,Amul Badd eax,Ccall WriteInt

Intel Machine Language:A1 00000000F7 25 0000000403 05 00000008E8 00500000

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

Specific Machine Levels

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

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

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

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

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

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)

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

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

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

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)

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

Data Representations

Every English character is represented by an ASCII number.

ASCII codes are defined by ANSI.

Two bytes for a character (Unicode)

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

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

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

Binary Numbers

Digits are 1 and 0• 1 = true• 0 = false

MSB – most significant bit

LSB – least significant bit

Bit numbering

015

1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 0

MSB LSB

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

Binary Numbers Each digit (bit) is either 1 or

0 Each bit represents a power

of 2:

1 1 1 1 1 1 1 1

27 26 25 24 23 22 21 20

Every binary number is a sum of powers of 2

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, 2010.

Translating Binary to DecimalWeighted positional notation shows how to

calculate the decimal value of each binary bit:

dec = (Dn-1 2n-1) + (Dn-2 2n-2) + ... + (D1 21) + (D0 20)D = binary digit

binary 00001001 = decimal 9:

(1 23) + (1 20) = 9

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

Binary Addition

Starting with the LSB, add each pair of digits, include the carry if present.

0 0 0 0 0 1 1 1

0 0 0 0 0 1 0 0

+

0 0 0 0 1 0 1 1

1

(4)

(7)

(11)

carry:

01234bit position: 567

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

Integer Storage Sizesbyte

16

8

32

word

doubleword

64quadword

What is the largest unsigned integer that may be stored in 20 bits?

Standard sizes:

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

Hexadecimal Integers

Binary values are represented in hexadecimal.

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

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:

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, 2010.

Converting Hexadecimal to Decimal

Multiply each digit by its corresponding power of 16:

dec = (D3 163) + (D2 162) + (D1 161) + (D0 160)

Hex 1234 equals (1 163) + (2 162) + (3 161) + (4 160), or decimal 4,660.

Hex 3BA4 equals (3 163) + (11 * 162) + (10 161) + (4 160), or decimal 15,268.

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

Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, 2010.

Character Storage

Character sets• Standard ASCII (0 – 127)• Extended ASCII (0 – 255)• ANSI (0 – 255)• Unicode (0 – 65,535)