• 沒有找到結果。

計算機程式

N/A
N/A
Protected

Academic year: 2021

Share "計算機程式"

Copied!
38
0
0

加載中.... (立即查看全文)

全文

(1)

任課教師 : 諶玉真

助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 :

1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman

2. Fortran 90 for Engineers and Scientists Larry R. Nyhoff and Sanford C. Leestma 3. Fortran 90 Programming

T. M. Ellis; Ivor R. Philips and Thomas M. Lahey

計 算 機 程 式

(2)

Chapter 1

Introduction to Computers

and the Fortran Language

(3)

•The computer is the most important invention of the 20th

century. It affects our lives profoundly in very many ways. •Computers control our telephone and electric power

systems, run our microwave ovens and other appliances, and even control the engines in our cars. Almost any

business in the developed world would collapse night if it were suddenly deprived of its computers.

•Considering their importance in our lives, it is almost possible to believe that the first electronic computers

(4)

•A computer is a special type of machine that stores information and performs mathematical calculations

on that information at speeds much faster than human beings can think.

•A program, which stored in the computer’s memory, tells the computer what sequence of calculations are required and which information to perform the lations on.

•Users mean that people who use computers to plish certain tasks and programmers indicate people who program computers so that things can be done by the users.

(5)

•It is important to realize that computers do not think as we understand thinking; they merely follow the steps contained in their programs.

•When a computer appears to be doing something clever, it is because a clever person has written the program that it is executing.

•The collective creativity of humans allows the computer to perform its “miracles”.

•This class will help you learn how to write programs of your own so the computer will do what you want it to do.

(6)

The Computer

The major components of the computer are the central processing unit (CPU), main memory, secondary

(7)
(8)

The CPU

•The central processing unit is the heart of any computer. It is divided into a control unit, an arithmetic-logic unit

(ALU), and internal memory.

•The control unit within the CPU controls all the other parts of the computer, and the ALU performs the actual mathematical calculations.

•The internal memory within a CPU consists of a series of memory registers used for the temporary storage of intermediate results during calculations.

(9)

•The control unit of the CPU interprets the instructions of the computer program. It also fetches data values from input devices or main memory, stores them in the mory registers, and sends data values from memory registers to output devices or main memory.

•For example, if a program says to multiply two numbers together and save the result, the control unit will fetch the two numbers from main memory and store them in registers. Then it will present the numbers in the ters to the ALU along with directions to multiply them

and store the results in another register. Finally, after the ALU multiplies the numbers, the control unit will take the result from the destination register and store it back into main memory.

(10)

Main and Secondary Memory

•A computer has two major types of memory: main or

primary memory, and secondary memory.

•Main memory usually consists of semiconductor chips. It is very fast and relatively expensive. Data that is stored in main memory can be fetched for use in 100 conds or less on a modern computer.

•Because it is so fast, main memory is used to temporarily store the program currently being executed by the puter, as well as the data that the program requires.

(11)

•Main memory is not used for the permanent storage of

programs or data. Most main memory is volatile, meaning that it is erased whenever the computer’s power is turned off.

•Secondary memory consists of devices that are slower and less expensive than main memory. They can store much more information for much less money than main memory can.

•Most secondary memory devices are nonvolatile. They retain the programs and data stored in them whenever the computer’s power is turned off.

(12)

•Typical secondary memory devices are hard disks, floppy disks, and tape drives.

•Secondry storage devices are normally used to

store programs and data that are not needed at the moment, but which may be needed some time in the future.

(13)

Input and Output Devices

•Data is entered into a computer through an input device and is output through an output device.

•The input devices are keyboards, scanners, and phones, ….

•Output devices permit us to use the data stored in a computer. The output devices are monitors, printers, plotters and speakers, … etc.

(14)

Data Representation in a Computer

•Computer memories are composed of millions of dual switches, each of which can be ON or OFF, but not at a state in between.

•Each switch represents one binary digit (also called a bit); the ON state is interpreted as a binary 1, and the OFF

state is interpreted as a binary 0.

•A single switch can represent only the numbers 0 and 1. Since we obviously need to work with numbers other than 0 and 1, a number of bits are grouped together to sent each number used in a computer.

(15)

•When several bits are grouped together, they can be used to represent numbers in the binary (base 2)

number system.

•The smallest common grouping of bits is called a byte. A byte is a group of 8 bits that are used together to present a binary number.

•The byte is the fundamental unit used to measure the capacity of a computer’s memory.

•The next larger grouping of bits in a computer is a word.

A word consists of 2, 4 or more consecutive bytes that are used to represent a single number in memory. The size of a word varies from computer to computer.

(16)

The Binary Number System

In the familiar base 10 number system, the number 12210 is

)

10

2

(

)

10

2

(

)

10

1

(

122

10

2

1

0 In the binary number system

5

)

2

1

(

)

2

0

(

)

2

1

(

101

2

2

1

0

7

)

2

1

(

)

2

1

(

)

2

1

(

111

2

2

1

0

(17)

Note that three binary digits can be used to represent 8 possible values: 0 (=0002) to 7 (=1112). In general, if n bits are grouped together to form a binary number, then they can represent 2n possible values.

values

possible

296

4,294,967,

bytes)

(4

bits

32

values

possible

65,536

bytes)

(2

bits

16

values

possible

256

byte)

(1

bits

8

Typically, half of all possible values are reserved for presenting negative numbers, and half of the values are reserved for representing positive numbers.

32767

to

32,768

bytes)

(2

bits

16

127

to

128

byte)

(1

bits

8

(18)

Computer Languages

•When a computer executes a program, it executes a

string of very simple operations such as load, store, add, substract, and multiply.

•Each operation has a unique binary pattern called an

operation code (op code) to specify it.

•The program that a computer executes is just a string of op codes ( and the data associated with the op codes) in the order necessary to achieve a purpose.

•Op codes are collectively called machine language, since they are the actual language that a computer

(19)

•Unfortunately, we humans find machine language very hard to work with.

•We prefer to work with English-like statements and braic equations that are expressed in forms familiar to us, instead of arbitrary patterns of zeros and ones.

•We like to program computers with high-level languages. •We write out our instructions in a high-level language and then use special programs called compilers and linkers

to convert the instructions into the machine language that the computer understands.

(20)

•Programmers use many different high-level languages, with different characteristics.

•Some of them are designed to work well for business

problems, while others are designed for general scientific use. Still others are especially suited for applications like operating-systems programming.

•It is important to pick a proper language to match the blem that you are trying to solve.

•Some of the high-level computer languages in use today are Basic, C, COBOL, Fortran and Pascal.

(21)

•Fortran is the preeminent language for general scientific computers.

•It has been around in one form or another for more than 50 years and has been used to implement everything

from computer models of nuclear power plants to aircraft design programs …etc.

•The language is especially useful for numerical analysis

and technical calculations.

•In addition, Fortran is the dominant language in the world of supercomputers and massively parallel computers.

(22)

A Brief History of Fortran

•One of the first high-level languages to gain widespread acceptance was FORTRAN (FORmula TRANslation), which was developed for the IBM 704 computer by John Backus and a team of 13 other programmers at IBM over a 3-year period (1954-1957).

•In the years that followed, other computer manufacturers developed FORTRAN compilers for their machines.

Several of these provided extensions and variations of

(23)

•Programs written for one machine could not be used on a different machine without modification.

•Over time, users developed large collections of FORTRAN programs, and the cost of converting all these programs whenever a new computer was installed became tive.

•To remedy these problems, efforts were made to standize FORTRAN so that programs were portable, which means they can be processed on several different machines with little or no alteration.

•One of the earliest standard versions appeared in 1966, and in 1977 another revision appeared, known as

(24)

•While the changes introduced in FORTRAN 77 were significant, it did not solve all of the problems in earlier versions, nor did it include many of the new features that were appearing in newer programming languages. •Consequently, work began almost immediately on a new standard version; in 1991, an extensive revision

known as Fortran 90 appeared, and compilers to support this new version of Fortran have been developed.

•The American National Standards Institute (ANSI), which establishes standards for programming languages,

decided that during the transition period there should be two American standards for Fortran, FORTRAN 77 and

(25)

•However, International Standards Organization (ISO)

groups decided that Fortran 90 will be the only national Fortran standard.

•Fortran 90 has standardized earlier versions and

provides a base from which newer versions of the guage will be developed.

•In fact, another ANSI committee is already at work preparing a new version that will make it possible to

design Fortran programs that will execute efficiently on massively parallel computers and that incorporate

modern programming techniques such as oriented programming.

(26)

Programming with FORTRAN

• FORTRAN : a combination of the words FORmula TRANslator

(1) high-level

(2) English-like language

(3) Engineering-oriented

• It is very easy to read and understand a Fortran program.

(27)

An example

program example

! This program is to calculate the summation ! and average of two variables, a and b.

real :: a, b, sum, avg read (*,*) a, b

sum = a+b

avg = (a+b)/2.

write(*,*) a, b, sum, avg end program example

(28)

 Earlier versions of FORTRAN have rather strict rules about where statements and comments can be placed. However, Fortran 90 allows considerably more flexibility in the program format.

• The main rules that must be followed are

(1)A line may have a maximum of 132 characters.

(2)A line may contain more than one statement, provided the statements are separated by semicolons (;).

program example

real:: a,b,sum,avg; read *, a,b

sum=a+b; avg=(a+b)/2.0; print *,a,b,sum,avg

Program Format

(29)

(3)An ampersand (&) must be placed at the end of each line that is to be continued to the next line. At most 39 continuation lines are permitted.

For example avg = (a+b)/2.  avg = & (a+b)/2.  avg = & (a+b)& /2.

(30)

(4)Any characters following an exclamation (!) and running to the end of the line form a comment. program example

! This program is to calculate the summation ! and average of two variables, a and b.

real:: a,b,sum,avg read *, a,b

sum=a+b

avg=(a+b)/2.

print *, a,b,sum,avg end program example

•Comments may also be attached to statements.

(31)

(5)If a statement requires a statement label, this label must precede the statement and be separated from it by at least one blank. Statement labels must be integers in the range 1 to 99999.

. . goto 1000 . . 1000 sum=a+b avg=(a+b)/2. . .

(32)

Types of Data Stored in Memory

•Three common types of data are stored in a computer’s memory: character data, integer data, and real data.

•Each type of data has different characteristics and takes up a different amount of memory in the computer.

Character data

•The character data type consists of characters and symbols. A typical system for representing character data in a non-Oriental language must include the

(33)

•The 26 uppercase letters ‘A’ through ‘Z’.

•The 26 lowercase ‘a’ through ‘z’.

•The 10 digits ‘0’ through ‘9’.

•Miscellaneous common symbols, such as “,(){} !~@#$%^&*

•Any special letters or symbols required by the language, such as áçñ.

Since the total number of characters and symbols required to write non-Oriental languages is less than 256, it is tomary to use 1 byte of memory to store each character.

(34)

Integer data

•The integer data type consists of the positive integers, the negative integers, and zero. The amount of memory devoted to storing an integer will vary from computer to computer, but it will usually be 1, 2, 4, or 8 bytes. The most common type of integers in modern computers are 4-byte integers.

•Since a finite number of bits are used to store each value, only integers that fall within a certain range can be represented on a computer.

(35)

•Usually, the smallest number and the largest number that can be stored in an n-bit integer is

1

1 -n 1 -n

2

Value

Integer

Largest

2

-

Value

Integer

Smallest

•For a 4-byte integer, the smallest and largest possible value are -2,147,483,648 and 2,147,483,647, tively.

•Attempts to use an integer larger than the largest ble value or smaller than the smallest possible value result in an error called an overflow condition.

(36)

Real data

• The integer type has two fundamental limitations:

1. It is not possible to represent numbers with fractional parts(0.25, 1.5, 3.14159 etc) as integer data.

2. There are not enough bits available to represent integers or very small negative integers.

• To get around these limitations, computers include a

real or floating-point data type.

• The real data type stores numbers in a type of tific notation.

(37)

•We all know that very large or very small numbers can be most conveniently written in scientific notation.

•For example, the speed of light in a vacuum is about 299,800,000 meters per second. This number is easier to work with in scientific notation:

m/s

8

10

998

.

2

mantissa exponent

(38)

•The real numbers in a computer are similar to the tific notation above except that a computer works in the base 2 system instead of in the base 10 system.

•Real numbers usually occupy 32 bits (4 bytes) of puter memory, divided into two components: a 24-bit mantissa and an 8-bit exponent.

參考文獻

相關文件

• The memory storage unit is where instructions and data are held while a computer program is running.. • A bus is a group of parallel wires that transfer data from one part of

• When paging in from disk, we need a free frame of physical memory to hold the data we’re reading in. • In reality, size of physical memory is

 To write to the screen (or read the screen), use the next 8K words of the memory To read which key is currently pressed, use the next word of the

– Transfers operating system from mass storage to main memory. – Executes jump to

• The memory storage unit holds instructions and data for a running program.. • A bus is a group of wires that transfer data from one part to another (data,

• An algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output.. • An algorithm is

We first define regular expressions with memory (REM), which extend standard regular expressions with limited memory and show that they capture the class of data words defined by

It costs >1TB memory to simply save the raw  graph data (without attributes, labels nor content).. This can cause problems for