A Fortran introduction to programming and computers, including Fortran IV

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

ESTaa

se7 7 5

ie

A

FORTRAN

INTRODUCTION

TO

PROGRAMMING| AND

COMPUTERS

INCLUDING

MARVIN

L.

FORTRAN

STEIN

and

WILLIAM

D.

MUNRO

Numerical Analysis Center Institute of Technology University of Minnesota Minneapolis, Minnesota

ACADEMIC New

York and London

PRESS

Iv

To Tama, Gene, Jerry, Douglas, Vicky, and Maggie

CopyricHT © 1966 BY ACADEMIC PRESS INC. All Rights Reserved. No Part of This Book May Be Reproduced in Any Form, by Photostat, Microfilm, or Any Other Means, without Written Permission from the Publishers.

ACADEMIC PRESS INC. 111 Fifth Avenue, New York, New York 10008 Umied Kingdom Edition published by

ACADEMIC PRESS INC. (LONDON) LTD. Berkeley Square House, London W.1 First Printing, March 1966 Second Printing, August 1966

LiprRaRY

OF

PRINTED

IN

CONGRESS

THE

SS

UNITED 4

Se

CATALOG

STATES

a

CARD

OF

NuMBER:

65-26409

AMERICA

Za

Q\\

This text is suitable for a one-semester course at the freshman level or higher. It is also suitable for a course in self-study, with a two-week orientation course for either students or faculty who simply want to use the computer. This approach we have found very successful, especially if auxiliary consultation is provided . by the computer center. Technical advances in computer design in recent years have made possible the development of digital computers (the “hardware’’) with capabilities far beyond those of their predecessors. Paralleling this advance there has been an almost greater one in the programs supplied with the computers (the ‘“‘software’’) to facilitate the development of these capabilities. These programs represent many years of experience and many different people interacting on and with each other, and they make possible the modern problem-oriented languages that permit us to give instructions to the machine in a fashion very similar to the way in which we are accustomed to think of them. One immediate result of these developments has been to make the modern computer available to anyone who takes the time to learn the simple rules, restrictions, and syntax of one of these languages. This is true to the extent that a knowledge of one of them will soon be as much an accepted part of the elementary training of an engineer as was the use of the slide rule a generation ago. The digital computer, however, will be an accepted tool to many more people than was the slide rule. It is the ‘purpose of this book to introduce its reader to one of these languages, that of Fortran. Although one of the major contributions of the new computer languages has been to develop a degree of universality that permits running the same program on two very different computers, no truly universal language has been developed. Many have gained a degree of acceptance that approaches this goal—such as Algol, which, to a certain extent has become a universal publication language. To be used, however, a language must be implemented with a compiling program designed to accept it. It is for this reason that we have chosen the language of Fortran for this book. Certainly, at least in the United States, no other language has as widely available the necessary programs to permit its use. Even the smallest installations with computers of ten-year-old design will have one or more Fortran compilers. The development of Fortran is a constant and continuing process in a field in which obsolescence is almost

PREFACE

iv

PREFACE

as constant. The rate of growth prohibits stagnation. The American Standards Association is currently working on a core body for the language that will probably eventually become reasonably definitive. There will always be variations in local dialect, however, to which the individual user must accustom himself. We have chosen to use a development that starts with what is essentially Fortran II and then goes on to give the common advances made to this date. The recent advances are by no means consistent, and we have used this fact to point out to the beginner that he must be aware of the particular properties of the compiling programs he uses. At the same time we have chosen only those parts of current usage that will undoubtedly become standardized with only minor modifications of what appears in the book, and we have put in the Appendix what appears to be the most likely final form for acceptance by the American Standards Association. Since the level to which we carry the material is about as much as could reasonably be absorbed at the beginning level, we have tried to avoid being out of date before the book is printed by including only the material that is reasonably fixed and adequate to launch the occasional user well along the way. It is possible for the kind of computer user to whom this book is directed to consider the computer as a

“black box” into which he feeds a correct sequence of instructions and gets an answer. It is also possible to drive a modern car without knowing what the gas gauge represents. Our feeling in both cases is that the work can be done more efficiently if at least a rudimentary knowledge of what is physically taking place is kept in mind by the user. For this reason we have included in Chapter 8 a brief introduction to the organization and functioning of a modern digital computer, as well as glimpses of a modern digital computer and the ultimate form of program or code which it accepts. I1 is possible to understand the basic Fortran material if we omit this part, but we feel that the user who understands enough to know why the restrictions are made will be able to use the computer more efficiently. To Richard Hotchkiss and Lawrence Liddiard our special thanks for many valuable suggestions and criticisms. We are also grateful to Janet Schuffenhauer and Amy Koepke for efficient and careful typing of the manuscript.

January, 1966

MARVIN

Minneapolis, Minnesota

WILLIAM

L. STEIN D. MUNRO

Preface iti 1,

INTRODUCTION

1.1 1.2 1.38 1.4

2.

Digital Computers and Programming Languages, 1 Registers, Addresses, Variables, 2 Operands of Arithmetic, 3 Accuracy of Computations and Ranges of Operands, 6 EXERCISES, 7 ;

ELEMENTS

OF FORTRAN

2.1 2.2 2.38 2.4 2.5 2.6 2.7 2.8

Introduction, 8 The Numbers in Fortran, 9 Symbolic Addresses and Variable Names, 10 Fortran Arithmetic Operations and Expressions, 12 The Arithmetic Assignment Statement, 14 The Structure of Fortran Programs, 18 Flow Charts, 18 Fortran Library Functions, 20

2.9

Simple Examples of Programs, 21 EXERCISES, 23

3.

INPUT AND

8.1 8.2 3.3 8.4. 8.5

4,

Introduction, 25 Information Units; Fields, 26 Format Statements, 28 Input and Output Statements, 33 Examples, 34 EXERCISES, 37

PROGRAM

~ 4.1 4.2 ~4.3 4.4. 4.6

OUTPUT

CONTROL

AND

ORGANIZATION

Control and Jump Statements, 39 Unconditional Transfer of Control, 40 Conditional Transfer of Control, 41 Subprograms, 46 Program Segmentation, 49 EXERCISES, 52

5.

EXTENDED

5.1 &.2

§.8 5.4. 5.5 6.6 &.7

MODES

OF ARITHMETIC

New Operand Types, 54 Complex and Double Precision Constants and Field Descriptions, 55 Mixing Operand Modes, 56 The Data Statement, 58 Some Examples, 59 Subprograms with Extended Operands, 60 Programmer-Defined Modes of Operation, 62 EXERCISES, 63

CONTENTS

vi

6.

CONTENTS

SOME

NONARITHMETIC

ASPECTS OF FORTRAN

Relational Operators, 65 Logical Variables, 66 Logical Operations, Expressions, and Statements, 67 Binary Content of Storage Registers, 71 Boolean or Masking Expressions and Statements, 73 EXERCISES, 75

PROGRAM

CHECKING

AND

EXECUTION;

MISCELLANY

Program Execution; Executive Programs, 77 Program Checking, 80 Some Miscellaneous Fortran Features, 82 EXERCISES, 85

8.

INTRODUCTION TO MODERN DIGITAL COMPUTING SYSTEMS

&.1 &.2 8.3

General Description of a Modern Digital Computer, 86 Functional Description of a Modern Digital Computer, 87 Coding, Programming, 94 EXERCISES, 103

APPENDIX

1. Operations, Symbolic Names, Q. Fortran Statements, 106 8. 4.

Numbers, 105

Fortran Library lunctions, 108 Permissible Characters and Codes, 109

Answers to Even-Numbered Exercises, 111 Index, 118

1.1

DIGITAL COMPUTERS LANGUAGES

AND

PROGRAMMING

The modern digital computer is basically a device for performing operations on operands represented by sequences of digits. Its primary arithmetic operations are those of addition, subtraction, multiplication, and division. It carries these out in a manner patterned after the way in which human beings carry out long hand computations; and, like its human counterpart, the computer is restricted to the use of finite sequences of digits. The computer must also have certain nonarithmetic capabilities that permit it to operate on operands, which, though numerically represented or coded, do not even relate to numbers. All of the operations, both arithmetic and nonarithmetic, that the computer can execute directly by means of its built-in operations, are of the most elementary types. More complex operations, such as integration or differentiation, must be formed in terms of aggregates of the basic operations that can be executed. The great speed of the modern computer permits highly complex combinations of the basic operations and, consequently, the execution of procedures and the solution of problems that have previously been beyond the reach of human beings. Without the creative intelligence of its human programmers, however, the computer’s speed alone would be inadequate. 1.1.1 Digital Machine Language. By the program of a digital computer we mean a specification as to precisely what operations it is to perform, on what operands, and in what order. The program takes the form of a series of digitally coded instructions and parameters. An instruction that the computer can accept directly is of a very specialized nature, being, in fact, of the same form as the numbers it operates on, a sequence of digits. The acceptable sequences of digits or instruction words to which a given computer has been

designed to react form what is called its machine language. Every program from which the computer ultimately operates is a machine language program— that is, a sequence of digits forming an aggregate of instruction words and numerical parameters. 1.1.2 Problem-Oriented Language. In the early days of digital computers it was necessary for a programmer to learn the language of his particular machine and to prepare his program in that language. Like the learning of any foreign language this required study and effort, and the use of the computer was restricted to the relatively few specialists who made the effort. Years of experience and the combined work of many people, however, have resulted in the creation of translating programs that permit the writing of computer pro-

1 INTRODUCTION

2

INTRODUCTION

grams in problem-oriented languages. These languages allow us to write programs in a form close to the written instructions we might give another trained person, and phrased in the terms in which we think of a problem. A program written in a problem-oriented language is in turn translated and compiled internally by the computer into the necessary machine language program, which it can accept directly. The program for carrying out this transformation is usually referred to as a compiler. The compiler itself accepts the symbols of the problem-oriented language as input data through the intermediary of a digital code, which permits replacement of each symbol by a designated numerical value. The compiler programs corresponding to a given problem-oriented language will vary greatly from one make of computing machine to another. These variations reflect differences in the machine languages and in other hardware, or physical, characteristics, as well as differences in the philosophies and perhaps the abilities of compiler authors. These differences in compilers may, in turn, be reflected in a so-called “universal” problem-oriented language itself, in terms of a series of local variations and restrictions which the programmer must learn and adhere to. They may even be reflected in terms of substantial discrepancies between versions of the same problem language. These are important points for the neophyte to keep in mind.

1.1.38 Fortran Language. The particular problemoriented language we consider here is Fortran. It is a relatively simple language, of wide use, particularly in the United States. The ‘universality’ of Fortran suffers from a series of machine-dependent restrictions of varying magnitudes as well as from the fact that the language has evolved over a period of time through a series of successively expanded versions. Unfortunately, prior agreement as to the nature and scope of the additions to the language was not obtained. Thus, there are a variety of different versions of Fortran, some with only subsets of the total capability of the language, others with full capability but differing in the form of the instructions required to initiate various facets of this capability. In order to determine the specific capabilities of the Fortran language he will use, and the specific local restrictions imposed on it, the reader will eventually need to read carefully the literature provided locally in the installation in which he will work. We believe that careful study of the material in this book will make this reading easy. The Fortran language we present here represents a powerful instrument for illustrating the ideas of programming, as well as for the implementation of programs; and aside from parochial variations of a relatively trivial nature, a

major portion of it is the language upon which the largest area of agreement presently obtains. In those cases where important distinctions between major versions of Fortran exist, we shall point them out. It is quite possible to use Fortran with almost no knowledge of the organization of a computer or the way in which it functions. It is, however, possible to use Fortran more efficiently and intelligently with at least a rudimentary knowledge of computers. We present an introduction to computer organization and operation in the final chapter of this book. Here we content ourselves with a sufficient description of the computer to clarify the basic ideas and terminology we will introduce. 1.2

REGISTERS,

ADDRESSES,

VARIABLES

Since the digital computer operates on operands represented by sequences of digits and also accepts its instructions as such sequences, it must have physical devices for holding and representing digits. These devices are called registers, and each register in the computer will at any given moment contain a sequence of digits or word, called its content. The registers of a. computer can be assigned to classes, which correspond to the various functions the computer system must perform. We will discuss these classes and the properties of the registers assigned to them in closer detail in the final chapter. At this point we wish to introduce and briefly discuss some ideas associated with a class of registers designed simply to store digits, which is referred to as the storage or memory of the computer. The storage registers of the computer will contain’ both the operands and the machine language instructions, each as sequences of digits. For the computer control to know which operand to select and which instruction to accept, it must be able to identify the registers in which they are contained. To achieve this end, each storage register is assigned an address. The address is merely a sequence of digits that uniquely defines the particular register with which it is associated. Thus, for example, in a machine equipped with 1000 storage registers, all three digit integers in the range 000 to 999 can serve as addresses, with each different integer of the range assigned as the identifying number of one, and only one, register. Inside of the computer, the address is an absolute digital sequence, called an absolute address. The ultimate machine language program must be organized on the basis of absolute addresses, and it must be able to distinguish between the address of a register and the content of a register. Because discrimination between address and content can be built into the compiler program, however, this distinction is not so important

1.3

externally. Consequently we may introduce and use, in place of absolute addresses, mathematically oriented notations for addresses, or symbolic addresses, with a dual purpose. We may use them as a symbol for the address of a register where a number is stored and as a symbol for the number itself. Since the content of a given register may vary during the running of a program, we may also use its symbolic address as a symbol for its variable content. Thus, we may use A to symbolize both the address of the register that contains the current value of the variable A and as a symbol for the variable A itself. For this reason we will use the terms symbolic address and variable interchangeably. For example, in a kinematical problem we may be dealing with the variables distance, rate, and time and, for convenience, assign the numerical values of these quantities to be stored, respectively, in registers with symbolic addresses DIST, RATE, and TIME. A compiler program translating into the language of the machine of the previous paragraph may assign to these symbolic addresses the respective absolute addresses 100, 101, and 102. Writing in a _problem-oriented language we need not and will not, in general, be aware -of this specific assignment, and we may write a problem-oriented language instruction such as “let RATE = DIST/TIME.” It should be remembered, however, that such a problem-oriented language instruction to the computer will come out in the resultant machine language program as an instruction to be interpreted something like “‘divide the content of the register whose address is 100 by the content of the register whose address is 102 and store the quotient at the register whose

address is 101.’’ Of course, the production of such machine language instructions is the job of the compiler, and a programmer employing problem-oriented language hardly ever, if at all, need be particularly concerned with them or their specific forms. When we assign a name to a variable, the implication is that we intend to operate on the values of that variable.

In order for this to be accomplished,

the

values must ultimately be placed in a register where they can be referred to. If we introduce the name of a variable and assign a storage register to hold that variable’s values, it makes sense to use the name of the variable as a symbolic address for its values. Conversely, if we assign a symbolic address to a register, we may regard that address as the name of a variable whose values are to be stored there. Other authors have, in the main, chosen to ignore the fact that the symbols of a variable can also be regarded as presenting a symbolic address, preferring to emphasize the variable point of view. In our opinion, the realization that variables result in values which are to be stored will make Fortran easier to use and to understand, and will

OPERANDS

OF

ARITHMETIC

3

clarify a number of otherwise obscure points—particularly those relating to the machine dependence of the language. Thus, in what follows, we will freely use the concepts of variable and symbolic address interchangeably, and the reader should have this well in mind.

1.3.

OPERANDS

OF ARITHMETIC

When writing a program in a problem-oriented language, we may expect to be permitted to make use of all of the admissible symbols of the language in describing and specifying the operands with which the computer is to deal. Since the admissible symbols will, in general, include digits, alphabetic characters, operational symbols, punctuation marks, and _ various “special characters,’’ operands may appear outside of the computer in such diverse forms as names of people, chemical formulas, or even as ordinary signed numbers with decimal points. Inside the computer all of these operands, regardless of their external form, must be represented by a sequence of digits. For nonnumerical operands this is accomplished by introducing a one-toone correspondence between the set of admissible symbols and a set of integers such that each symbol is uniquely associated with an integer of the set. Such a correspondence is frequently called a code. For numerical operands a conversion from the decimal system to the number base of a particular computer may be necessary. In later chapters we will discuss ' ways of writing programs that direct the transformation of external operands to proper internal form, and their storage and correct treatment in accordance with the original interpretation. At this point, however, we wish to emphasize that we must take a dual view of operands: the external view and the internal one. 1.3.1 Fixed-Point Operands and Arithmetic. When we do longhand arithmetic, we use sequences of decimal digits together with a decimal point and sometimes an algebraic sign to approximate the numbers we are dealing with. Such operands are often called fixedpoint decimal numbers. The actual arithmetic with fixed-point decimal numbers will break down into taking account of the rules of signs; the digit-bydigit addition, subtraction, multiplication, or division algorithms; and, finally, the correct placement of the decimal point. We handle the algebraic sign by application of a memorized set of rules, whereas we do the actual arithmetic on absolute values by using memorized tables of addition or multiplication. We operate on all numbers with the basic arithmetic we learned for integers, and, as the final step in a calculation, we locate the decimal point according to the proper rules.

4

INTRODUCTION

For example, if we wish to multiply 2.4 by 0.35, we multiply the integer 24 by the integer 35, using a memorized table, to get 840. We then count three places from the end and put in the point to get 0.840 because there are, respectively, one and two digits to the right of the decimal point in the two operands. When we add, subtract, and multiply whole numbers or integers, the results are also whole numbers. Moreover, if a and b are integers, we form division in the form a = qb + r, with r < b, and the quotient q and remainder r are whole numbers. Thus, we can arrange integral arithmetic so that all of the results will be integers. Accordingly, we can regard integers as an important special case of fixed-point numbers and arithmetic in which it is not necessary to keep track of the position of the decimal point; and we may, if we wish, drop the decimal point from the representation of fixed-point numbers when confining ourselves to the integral case. This is not to say that the fixed-point numbers, in general, may not take on integral values or that the results of arithmetic with such numbers may not have whole number values. We will generally discover this, however, by noting that after we have properly placed the decimal point, all digits to the right of it are zero. Thus, for example, 1.42 divided by 0.71 yields a quotient of 2.0, which is a whole number, but computing the check, we find 2.0 X 0.71 = 1.42. Inside the computer, addition, subtraction, multiplication, and division are carried out by being built into the circuitry of the arithmetic section, and to achieve simplicity of design, the decimal base, which we usually utilize externally, is often replaced by a different base. The most frequently used base is two, giving the binary system in which the integer two plays precisely the same role as the integer ten in the decimal system. Besides the possible change of base from decimal to binary, another difference between longhand arithmetic and computer arithmetic is concerned with the handling of the algebraic sign, which is frequently done internally by means of so-called complement arithmetic. However, the user of a problem-oriented programming language need have little concern with binary numbers, complements, and the internal processes of arithmetic in general, except possibly for one problem—that of proper placement of the base point. When we place fixed-point numbers inside the computer, we lose the flexibility that longhand arithmetic permits of locating the decimal point wherever it belongs in the sequence of digits. For internal arithmetic with fixed-point numbers, all internal operands will be treated as though an imaginary base point were at some fixed location in the computer register—for example, the right-hand end with all operands thought of as integers—and the circuits for arithmetic will be built to conform to this treat-

ment. With all fixed-point operands treated internally as though they were integers (or perhaps simple fractions) the programmer will have to record externally the actual positions of the base points and take these into account in properly interpreting the answer. This is usually accomplished by multiplying each operand by a scale factor or appropriate power of the base to convert it to an integer (or fraction) and keeping account of all scale factors in order to interpret the final result properly. Thus, in forming the product of 2.4 and 0.35 we apply the scale factor 10! to 2.4 to get 24 and the scale factor 10? to 0.35 to get 35. We then form 24 X 35 = 840. Since the final result has a scale factor of 10°, we unscale it by dividing 840 by 10% to get the correct result of 0.840. This general procedure is called scaling, and is carried out for fixed-point arithmetic externally by the programmer. It is tedious and often more difficult than this simple example indicates and requires the programmer to have a reasonable knowledge of the internal arithmetic processes of the machine. The special case of integral arithmetic is an exception. In this case all operands and results of arithmetic operations are integers and the scaling problem becomes trivial, since all numbers will then have a scale factor equal to one, which can be ignored. The problems of external scaling can be avoided by storing the scale factor internally in the storage register, with the fixed-point operand, and redesigning the arithmetic circuitry so that scaling is done automatically by operations on both the fixed-point number and its scale factor. The resultant operands are called floating-point numbers, and we will discuss them in the following section. To conclude this section we point out that in Fortran, unless we specifically indicate that external numbers are integers and are to be treated by integral arithmetic, external fixed-point numbers will always be converted internally to floating-point operands. Thus the programmer will have minimal scaling problems, if any. 1.3.2 Floating-Point Operands and Arithmetic. A floating-point number conforms to the so-called scientific notation in which an approximate value for the velocity of light might be given as 1.86 xX 105 miles per second. Outside the computer each floatingpoint operand consists of a fixed-point operand multiplied by an appropriate power of ten. Internally the most frequently used floating-point format is that of a. normalized fraction, in which the base point is considered to be immediately to the left of the first nonzero digit. This fraction is multiplied by a power of the base, which moves the base point to the correct position. Thus, a number N will be represented as N

=

nb!

1.4

ACCURACY

where n is a normalized fraction called the coefficient or mantissa and t is an integer called the true exponent or characteristic. Since the base b is implicit in the machine and so is understood, each floating-point number will be contained in the computer as an ordered

pair Nites (20;-)

in which n and ¢ will each be represented by a sequence of digits in a register, and usually the same register holds both. Arithmetic is carried out automatically to account for both the operations on the coefficient and the exponent. Example 1.1 In a decimal computer with 12 digits to a register, two are allocated to the exponent and the remaining ten are for the digits of the coefficient. The signs of the exponent and coefficient are handled separately. The numbers A = —1.23 and B = 0.0345 are stored as the equivalent of A=(—1230000000,-+01) =—0.123 X10! and B=(+3450000000,—01)=+0.345X107}

where we give in order the symbol, the internal form, and the value in conventional notation. For addition, to align the decimal point, the machine will convert B to (+0034500000, + 01) and form the sum with A, as A+B=(—1195500000,+-01) = —0.11955

For multiplication, equivalent of

the

machine

X10!= —1.1955

will

perform

the

AB =(—0.123)(+0.345)10110-!=(—0424350000,-++00)

Since the coefficient in the internal form must be normalized, the machine will then adjust the coefficient to normalize and the exponent to compensate. The result is AB =(— 4243500000, —01)= — 0.42435 X1071= — 0.042435

The fact that the internal form of the number makes use of a different base than ten need not concern the beginning programmer very much. He should be aware of it, however, in terms of the fact that the external decimal numbers he enters into the machine may be rounded off internally for the new base, and this in turn may cause the introduction of further round-off error. For example, the exact decimal number 0.2 would require an infinite sequence of binary digits to represent

it. The Fortran programmer will deal externally with both fixed-point and floating-point operands and with the special class of fixed-point operands designated as

OF

COMPUTATIONS

AND

RANGES

OF

OPERANDS

5

integers. Except for the specially designated integers, all of these will be represented internally as floatingpoint numbers. Thus, in practice, the Fortran programmer will deal internally with both kinds of operands, the fixed point for integers and the floating point for the representation of real numbers (including integral values) in general. Because of the different way these numbers are treated internally by the computer, a careful distinction must be made, which accounts for some of the rules and restrictions that we will introduce in the next chapter. 1.4

ACCURACY OF COMPUTATIONS RANGES OF OPERANDS

AND

Because only a finite number of digits can be used in the representations of numbers, all numbers in machine computations are confined to certain fixed ranges of values and almost all numbers are subject to roundoff errors. The magnitude of the error will be dependent on the number of significant digits retained. Thus, the decimal sequence 0.333333, with six significant digits, is a more accurate approximation to the number 4 than is 0.333, with only three significant digits. In a digital computer the number of digital positions allocated determines the maximum number of significant digits that can be retained. In turn, the choice of allocation will be a function of register size. We must also remember that numbers will be in the base utilized by the computer, and so their decimal equivalents may only be approximate. If a floating-point number is stored in a single register with some digits allocated to the coefficient and some to the exponent, the number of digits allocated to the coefficient determines the accuracy and the number assigned to the exponent determines the limitations on magnitude. The basic rules are the following:

(1) If C digital locations, exclusive of sign, are used to represent the coefficient, base b, then the normalized coefficient n must satisfy the conditions

ba Sales

ee”

or be zero, and the relative error will not exceed b-°-. (2) If # digital locations, exclusive of sign, are used for the exponent ¢, then ¢ must satisfy

lt} d-+ 6. If inadequate field width is provided, we will lose significant digits or perhaps an algebraic sign. In some compilers a special symbol such as an asterisk may

be printed to indicate such errors.

Example 3.3 A register contains the decimal integer —3456 and a second register the integer 78912. These are printed on output with a field description I4 as: 3456

Example 3.5 In a particular 80-column card, the first five columns are not used for purposes of input or output. The following 15 columns are divided into five integer fields of width three, and the next 54 columns are used for six decimal fields in which there will be three digits to the right of the point. The remainder of the card is not utilized. The Format statement takes the following form: s FORMAT (5X, 513, 6F9.3)

and

8912

In the one case the sign and in the other case the leading significant digit has been lost. We should also use caution, particularly on input, with a description that gives too wide a field.

Example 3.4 Two integers are punched in consecutive fields on a card. They are Field one | Field two

12345|

67898

If field one is described by I6 the number read in will be 123456.

3.3

99999 which precedes the word FORMAT in the statement, and is punched in card columns one through five.

FORMAT

STATEMENTS

The Format statement is one of the fundamental modes of address of the Fortran input-output language. It is used to describe the makeup of units of information. To specify the format of a unit of information, we write down the descriptions of all fields contained in the unit using the notation given in Section 3.2. The field descriptions are separated by commas and ordered in the normal sequence in which the fields will be scanned. This sequence is usually clear in any particular case and may generally be characterized by the term “left to right.” To complete the statement of format, we enclose the sequence of field descriptions in parentheses and preface the parenthesis by the word FORMAT. Accordingly, a Format statement appears as s FORMAT (Cy, Co, «++, Cx)

where C; (¢ = 1, 2, ..., &) are the field descriptions. Since Format statements will be referred to by other statements, and since a variety of Format statements may be needed in a program, we identify a particular one by assigning to it a unique statement number s. The statement number is an integer in the range 1 to

A Format statement by itself initiates no input or output. It is made merely for reference. To initiate input or output, the language has a variety of statements, each of which specifies a particular peripheral device to be utilized and gives an ordered list of addresses (in the form of variable names) from which output will be taken or to which input will be delivered. Format statements serve as references for input-output statements, which we define and examine in greater detail below; however, of more immediate concern is the way in which the list of addresses provided by an input or output statement interacts with the Format statement which it refers to. A Format statement presents an ordered list of fields and a description of the information contained in each field. The ordered list of fields provided by the Format statement is matched to the ordered list of variable names provided by the input or output statement. The name thus associated with each field is a symbolic address, which will specify the location to which the input from the field is to be delivered, or the location from which the output for the field is to be taken. Of course, the digits taken from the input medium and delivered to the named register, or taken from the named register and delivered to the output medium, must represent information of the type described by the corresponding field description. If this is not the case, an error will ensue. In matching the list of fields to the list of names, care must be taken in dealing with X fields and H fields. An X field can contain no input information. Neither can it receive output information. It merely provides a means of ignoring part of a unit of input information, or of not inscribing any data in part of an output unit. Thus, X fields are ignored in the matching of addresses and fields in the two lists. To understand why H fields receive special treatment in the matching process, we must amplify somewhat on their properties. In fact, the information in an H field is normally specified within the Format statement itself. For example, assume that the H field is to contain characters representing the table heading

3.3

ARCTANGENT THETA. This gives a 16-character Hollerith field including the space between words. Its description in the Format statement takes the following form:

1 6HARCTANGENT, THETA Here the characters 16H identify a Hollerith field of width 16, and the field information itself follows immediately. The symbol ,, which would not normally be present, has been included to call attention to the fact that the space is counted in the 16 characters. We see

that H-field

information

is actually a part of the

Format statement, and on input it will be stored as part of the Format statement. For output, these characters can then only be taken directly from storage within the Format statement and not from any storage register whose address is listed by the output statement. If a Hollerith field is referred to in a Format statement for an input operation, Hollerith data on the input medium will replace the Hollerith data in the Format statement and not in any listed register. Thus, H fields must be ignored as far as the matching of names and fields in the two lists is concerned. Example 3.6 Assume that an input statement specifies a list of 11 symbolic addresses or variable names and refers to the Format statement s of Example 3.5. In matching the list of 11 names to the fields listed in Format statement s, we ignore the initial no informa-

tion or blank field of width five. The following five fields of three-character integers are then stored in order in the first five locations listed by the input statement. The remaining six fields of nine characters are stored in order as floating-point numbers in the remaining six listed locations. We emphasize that on input, data in F fields, which are decimal numbers externally, become floating-point numbers internally. We note that in Example 3.5 the Format statement makes no reference to the field formed by the final six unused columns of the card. This was not necessary, since the left parenthesis signified the beginning of an information unit and the right the close. Thus, when the right parenthesis is encountered, the remainder of the card is ignored. The next unit, consisting of the next card in the deck, will be read provided that there are still unmatched names on the list furnished by the input statement. This is not the case in Example 3.6, and so the input operation terminates.

Example 3.7 Assume that a list of 22 symbolic addresses was specified in the input statement of ‘Example 3.5. In this case, the input will proceed as before, except that after the first 11 fields of informa‘tion defined by the Format statement have been

FORMAT

STATEMENTS

29

matched to addresses, the input routine will determine that, although the unit of information has been exhausted, there are still 11 unfilled locations in the list of addresses. The next unit must then be read and matched to the unfilled input locations. This is done according to the format initiated by the opening parenthesis immediately preceding the closing parenthesis that signals the end of the previous unit. Although there is only one opening parenthesis in the 'ormat statement under consideration, we have presented the rule in this general way so that it will also apply in the more complex cases to be introduced below.

Example 3.8 Assume that an output statement for card punching specifies a list of names of variables totaling some multiple n of 11 and refers to Format statement s of Example 3.5. The output routine will then punch n cards. The first five columns of each card will be blank as well as the last six. The five threecolumn fields beginning with column 6 will contain three digit integers, while the next six nine-column fields will contain decimal numbers with three places to the right of the decimal point. These values will be taken from the storage registers in the order in which their names appear on the output list. The first five registers listed should contain integers of correct magnitude and the next six, floating-point numbers of correct magnitude, and so on, alternately, until the list is exhausted. We note that on output in the form of I fields, the content of the addressed register is treated as a floatingpoint number even though the I field holds a decimal number. We have already pointed out that I fields go into floating-point form on input. At this point we may make some general observations.

(1) An input statement will cause the input routine to continue calling in new units from the peripheral device until the list of names specified by the input statement has been exhausted. Similarly, an output statement will cause the output routine to continue sending out new units to the peripheral device until the list of names furnished by the output statement has been exhausted. (2) The input or output routine will determine that all relevant fields in a unit have been matched with names when it encounters a closing parenthesis in the Format statement. It then stops if the list is exhausted. If the list is not exhausted, it goes to the next unit and continues matching fields and names in accordance with that part of the Format statement which begins with the opening parenthesis immediately preceding the right parenthesis that signaled the end of the previous unit.

30

INPUT

AND

OUTPUT

To this point we have introduced only a single opening and a single closing parenthesis. In what follows, we introduce extensions of the use of parentheses in order to be able to describe a number of different input-output units in a single Format statement. For this reason, we have stated observation (2) in a more general way than yet necessary. Before extending the use of parentheses, however, we will define another means of incorporating the description of more than one unit in a single statement, namely the slash symbol (/). We have seen that an input or output statement referring to a Format statement of the form s FORMAT (Ci, Co, > +, Cx)

causes the list of fields to be repeatedly matched against the list of addresses it supplies until the list of addresses is exhausted. As many units as necessary will be activated until this condition is met. All of these units will have the same structure since statement s defines the format of exactly one unit of information. To assist in defining the format of more than one unit of information with a single Format statement, we use the slash symbol. When this symbol appears in a Format statement, it is interpreted to mean that the current unit is terminated. If the list of addresses supplied by the input-output statement has not been exhausted, the next unit is then processed according to that part of the Format statement to the right of the slash.

Example

3.9

We have the following Format state-

ment: s FORMAT (3X, 15, 3X, 17, /, 18X, 3F10.4)

We wish to determine the effect of an input statement specifying cards and listing 100 addresses. We can regard the initial opening parenthesis as a signal to. the input routine to read in the first unit of information or, in this case, the first card. The format of this card is given by the field descriptions up to the slash. Accordingly, a five-character decimal integer is taken from columns 4 to 8 and stored at the first listed address, and a seven-character decimal integer is taken from columns 12 to 18 and stored at the second address of the list. The slash indicates the end of the unit represented by the first card. Since the list of addresses has not yet been used up, the next unit, that is, card, will be read in and processed according to the field description following the slash. This results in the three fixed-point four-decimal-place numbers in columns 19-28, 29-38, and 39-48 being stored as floatingpoint numbers at the next three addresses on the list. The closing parenthesis indicates the end of the unit.

Since the address list is not yet exhausted, the input routine returns to the opening parenthesis immediately preceding the closing right parenthesis and continues. The next two cards will be read and five numbers stored at the next five addresses of the input list, and so on until the address list is used up. This will result in the reading of 40 cards. Of course, at least 40 cards punched in the proper format should have been placed in the hopper of the card reader. We note in Example 3.9 that the input process will continue until the address list is exhausted. The Format statement will be used repeatedly until this condition is achieved. The repeated use of the Format statement generates a list of fields long enough to match the list of addresses given by the input statement. A corresponding remark holds for output as we see from the following example.

Example 3.10 We wish to determine the effect of an output statement listing 100 names of variables, specifying a line printer and referring to format s of Example 3.9. The resultant output will consist of 40 printed lines. The odd-numbered lines 1, 3, 5,..., 39 will contain a five-character and a seven-character integer. The even lines 2, 4, ..., 40 will contain three ten-character, four-decimal-place numbers. No integer will appear directly above or below a number containing a decimal point. This follows from a discussion similar to that of Example 3.9.

We note that in the last two examples the X fields were ignored in the process of matching the field list to the list of names. In Examples 3.9 and 3.10 we illustrated the use of the slash symbol to incorporate the description of more than a unit of information into a single Format statement. In the examples, the slash appeared in the statement with one information unit to its left and the description of the following unit to its right; that is, the slash appeared somewhere in the middle of the Format statement. We now consider the effect of a slash appearing at one end or the other of the statement, that is, with all field descriptions either preceding it or following it. In the latter case we have a statement of the following form: s FORMAT (/,- +)

We have not shown any more of the statement detail, since it is not relevant to the point we wish to make. To determine the effect of this Format statement, we must keep in mind that both the initial opening parenthesis and the slash call for the next unit, with the slash’s having the additional property of terminating the previous unit. Thus, in this case, the opening

3.3

parenthesis calls for a unit of information and the slash terminates this unit and calls for its successor. Since there are no fields described between the parenthesis and slash, the information unit called for by the opening parenthesis cannot be processed before termination by the slash, so it is ignored. If the Format statement is referred to by an input statement calling for card input, the first card in the deck will be read and disregarded. If the input statement calls for magnetic tape or paper tape, the first record, or block on tape, will be read, but then not used. If an output statement to cards refers to this format, the first card in the deck will be blank, that is, unpunched. If the output is to a line printer, there will be a blank line. The effect is like that of a carriage return on a typewriter. If the output is to tape, a record or block composed of blank codes will occur on the tape. We may also place more than one slash in succession in a Format statement. We may do this either by writing the exact number n of slashes we wish, or (with some compilers) by writing one slash preceded by the number n.f If this latter were done in the last Format statement, it would appear as follows:

s FORMAT (n/,- ++) The result would then be n blank lines, cards, or tape records on output, and the passing over of n cards or tape records on input. This is confirmed by an analysis similar to the one made above for a single slash. We note that the n blank, or unprocessed, units result from the combination of the initial opening parenthesis and the first n — 1 slashes. The unit called for by the nth slash is processed in the manner imposed by the field descriptions appearing immediately to the right of the slash. Similar reasoning leads us to conclude that n slashes, appearing with field descriptions between them and the parentheses on either side, will lead to n — 1 blank or unprocessed units of information. There is one less unit in this case because there is no opening parenthesis immediately preceding the first slash. The unit activated by the nth slash will be processed according to the part of the Format statement to the right of this slash. Conceivably, this could result in this unit’s also being ignored. We complete the discussion of the slash symbol with the case of the following Format statement. s FORMAT (- +, n/)

FORMAT

STATEMENTS

31

first n — 1 slashes. Because there is no information in the part of the Format statement between the right of the nth slash and the closing parenthesis, however, the additional unit activated by this slash will also be blank or unprocessed, for a total of n. We have seen that our notation permits us to avoid repetition; for example, nIw stands for n consecutive integer fields of width w, and, with some compilers, n/ stands for n consecutive slashes. Frequently we encounter the situation in which a sequence of the members of a Format statement will be repeated successively if written out at length. For example, we might have the requirement for +++, 15, 2X, 16, /, 15, 2X, 16, /, 15, 2X, 16, /, ++

to appear within a Format statement. We avoid unnecessary repetition in this case by the same device as before. We merely write

+++, 3[15, 2X, 16, /], «+> In general, we have the form

Ree tt

‘poe

where the content of the square brackets consists of the repeated part of the format. This notation is equivalent to writing the content of the brackets within the Format statement n consecutive times at the point the brackets appear in the statement. We have introduced brackets for this purpose in order to distinguish them from the parentheses we have previously employed, which play a vital and not yet completely explored role in determining the sequence in which the fields in a Format statement are matched to the address list and which also influence the activation of information units. The square brackets have no such functions, serving only to specify that part of the Format statement to which the repetition index n applies.t Unfortunately, in practice, we do not have a distinct code for a square bracket symbol available for input to the computer. Accordingly, all brackets and parentheses on actual input to the compiler will appear to be the same. The compiler will have no difficulty telling the two kinds of parentheses apart, and neither should we once we have attained some proficiency in the use of Fortran inputoutput language. At this early stage, however, we will continue to make the distinction.

Example 3.11

An input card is divided into a five-

We see that this is essentially the same as the case of n slashes in the middle of a Format statement and will result in n — 1 blank or unprocessed units due to the

digit-integer field, a five-column blank field and seven fields of ten columns each. The data for the seven tencolumn fields consist of numbers with four decimal

+ For many compilers that do not permit the notation n/ the game effect can be achieved by use of n(/). This is an example of the more general use of parentheses to be discussed.

+ In some Fortran compilers this distinction may not apply and these parentheses obey the rules previously stated as well as those given for inner parentheses in general.

————Le

32

INPUT

AND

OUTPUT

places to the right of the point. For purposes of processing external to the computer, the five data cards for each case have a blank card with a distinctive corner cut preceding them. All cards are placed in the card reader. We give a Format statement for reference when reading the data for any single case. 15 FORMAT (/, 4[15, 5X, 7F10.4/], 15, 5X, 7F10.4)

We can also use Format statement 15 as a reference to list the input data for each case. On output to printer this format provides for a blank line preceding the data. This spacing will improve the appearance of the listing. Spacing between the listing of input data and the listing of results can be provided by the Format statement used as a reference in listing results. The following, if used, yields two blank lines: 20 FORMAT (//,

+=)

Example 3.12 For each of a number of cases, we want output of the following format: 15 CHARACTERS

te 2EN34 s FORMAT (-- «(++ +))

We will have no difficulty in determining the effect of this or any other particular structure if we keep the following basic rules in mind: (a) The initial opening parenthesis always calls for activation of the next available unit of information. (b) The interior opening parentheses are to be ignored when encountered on scanning the Format statement from left to right. (c) A closing parenthesis calls for the scanning procedure to back up to the nearest preceding opening parenthesis and then resume from left to right, provided the address list is not used up. When encountered in this manner, an interior opening parenthesis has the same effect as an initial one; see (a).

HOLLERITH

4 Floating-Point Numbers

The 15 Hollerith characters identify the particular case and are read in with the input data for each case. The data is the same as that of Example 3.11, except that

the first card contains the identifying Hollerith information in columns 1 to 15, instead of being blank. Our Format statements for output reference can take the following form: 100 FORMAT (15H,

thesis. We may introduce other parentheses. For example, we may have a Format statement with the following structure, where we have numbered the parentheses for reference:

AAAAAAAAAAAAAA)

On applying (a), (b), and (c) to the last Format statement, we see that it will be scanned once from parenthesis 1 to parenthesis 3, and then repeatedly from parenthesis 2 to parenthesis 3 until the list of names is exhausted. A unit of information will be activated at parenthesis 1. The first time parenthesis 2 is reached, it will be ignored, but on each subsequent encounter, a new unit of information will be activated. Parenthesis 4 is never used and could be omitted; however, it seems tidier to have as many closing parentheses as opening parentheses in each Format statement.

200 FORMAT (//,4E17.10)

Format statement 100 contains 15 arbitrary characters, which are to be filled by an input statement for cards referring to it and providing no address list. This will read in the first card of the input deck. The remaining five cards may be read in with reference to the following Format statement: 300 FORMAT (4[15, 5X, 7F10.4,/], 15, 5X, 7F10.4)

Output to printer can be handled by two statements, the first referring to Format statement 100 with no address list and the second to Format statement 200 with an appropriate address list. We have seen how the parentheses are used in a Format statement for reference in determining the order in which field descriptions will be matched with the list of variable names and also for reference in determining when to activate the next unit of information. To this point we have introduced only a single opening parenthesis and a single closing paren-

Example 3.13 In a deck of data cards, the first has a ten-character integer in columns 1-10 to identify the particular experiment to which the data corresponds. The following data cards are all filled with five-character integers. We may read cards with reference to the following Format statement: 138 FORMAT (110, /, (1615))

When the address list has been used up, the scanning of the Format statement will stop, no matter how many or few times it has been perused, or where the process happens to be within the statement. The input or output operation, activated by the statement furnishing the list, is then finished. The controlling factor in this regard is the list and not the Format statement. Accordingly, a Format statement may have elements which, for a particular address list, are never used. This feature may enable us to utilize a single Format statement with a number of different input-output statements.

3.4

3.4

INPUT AND

OUTPUT

STATEMENTS

The Format statement is for reference only. Of itself, it does not cause any input or output to take place. This is the function of the input and output statements. Such statements initiate input or output from a specified peripheral device, for data, conforming to a specified format, which are to be loaded into or taken from storage registers whose names are listed in the statement. In this section we define input and output statements for a computing system having peripheral devices that include a card reader and punch, magnetic tape handlers, and a line printer. The permissible input and output statements will vary from system to system, depending on the complement of peripheral equipment present. The statements and their definitions are listed below. READ n, LIST

This statement causes data to be read from 80 column cards, punched and arranged according to Format statement n, into the registers whose addresses are in the list. PUNCH n, LIST



This statement causes data from registers, whose addresses are in the list, to be punched into 80 column cards in accordance with Format statement n. READ INPUT TAPE f, n, LIST

This statement causes data to be read from a magnetic tape, prepared in accordance with Format statement n and mounted on tape unit ¢, into registers whose addresses are in the list. WRITE OUTPUT TAPE ¢, n, LIST

This statement causes data to be written, in accordance with Format statement n on a magnetic tape mounted on tape unit ¢, from registers whose addresses are in the list. PRINT n, LIST

This statement causes data from registers with addresses in the list to be printed by the line printer in accordance with Format statement 7.

We also have statements to assist with the mechanics of dealing with magnetic tape, of which we give three examples: ENDFILE ¢

This statement causes an end of file mark to be written on the magnetic tape mounted on tape unit t.

INPUT

AND

OUTPUT

STATEMENTS

33

This indicates the end of a block of output units on the magnetic tape mounted on tape unit ¢. REWIND ?#

This statement causes the magnetic tape mounted on tape unit ¢ to be rewound to its initial point. BACKSPACE t

This statement causes the magnetic tape mounted on tape unit ¢ to be moved back one record or unit of information. In many of the later versions of Fortran, the list of input and output statements can be condensed to only two basic ones (although the previous statements are usually all retained). These are: READ (e, n), LIST WRITE (e, n), LIST

The function of these last two statements is the same as that of those listed above, except that all input statements are READ with the specific input equipment designated by the code e. In this way, READ (e, n) with the code e for the card reader is equivalent to READ, n. Similarly, WRITE (e, 7), with the value of e equal to the code for the card punch is equivalent to PUNCH n, and WRITE (e, n) with a code e for the printer is the same as PRINT n. In the following examples we will use the statements in the form given first, since they are more self-explanatory and since codes and equipment may vary. If the list in any of the above input and output statements is empty—that is, if no list is stated—only Hollerith information will be read in or out. In this case only the part of the Format statement up to the first field which is not an H field or X field will be used. We recall that Hollerith information is stored in the Format statement itself and not in any register addressed by the list. Since the addresses in the list of an input or output statement are names of variables, they must conform to the rules for naming variables. Formation of a list is straightforward within these rules if no subscripted address symbols are used. Arrays in lists may present difficulties, and we elaborate on some of these. We have two cases: (1) the name of the array is given without subscripts; (2) the name of the array is given with subscripts. If the name of the array is given in the list without subscripts, then the name stands for all elements in the array, in sequence, with subscript values running from the value of one to the highest value specified in the

34

INPUT

AND

OUTPUT

applicable Dimension statement. Frequently, the Dimension statement will reserve storage for the maximum number to be expected in any case, but in the particular case at hand the actual order of the array is only n, where n is less than the maximum. Here we assume 7 is a parameter of the problem denoted by N—that is, stored at symbolic address N. We recall that N cannot appear in the Dimension statement, which must use an actual number denoting the largest subscript value. Hence, if we state merely the name of the address array, too many addresses will be placed on the list. To avoid this, we introduce the subscripts explicitly and give their specific range. Thus, we might have the following statement: PRINT 100, A, B, (VECT@R(I), 1=1, N)

We assume that the value of N has already been established, and that Format statement 100 is appropriate. If the parameter n is not in storage, we must give its value explicitly as in the following example:

statement. Then the statement

PRINT 10, ((Al(l, J), 1=1, 2), J=1, 2) would result in the printing, in order, of O11,

21,

12,

A22

that is, the first column followed by the second. The statement

PRINT 10, ((A(I, J), J=1, 2), 1=1, 2) would print by rows in the form O11, G12, H21, A22

Example 3.15 We have a three-dimensional array with the element a;;* to be stored at address A(I, J, K). Each subscript takes values 1 and 2. We again assume an appropriate Format statement number 10. The statement READ 10, (((A(l, J, K), J=1, 2), K=1, 2), 1=1, 2)

PRINT 100, A, B, (VECT@R(I), 1=1, 20)

We may place more than one array depending on the dummy subscript I in parentheses. For example we might have a combination such as the following. PRINT 100, A, B, (VECT@RII), HECTOR(I), 1=1, 20)

The part of the list determined by the subscript must be enclosed in parentheses. Parentheses play a major role in defining the order of a subscripted array within a list, and we must pay particular attention to the way in which they are used when we have more than one subscript. For two subscripts, the corresponding part of the list may assume a form such as ((A(l, J), 1=1, 50), J=1, 50) and for three, a form such as the following. (A(t, J, K), 1=1, 10), J=1, 10) K=1, 5)

The rule for determining the order in which subscripts vary the fastest is “from the inside out.’ This corresponds to the order in which subscripts are varied when multidimensional arrays are transformed to linear arrays. Thus, the list given above for the twodimensional array specifies input or output by columns. If we want the matrix A(I, J) to be read in or out by rows, we should put it in the list as

would then read in eight elements, which are interpreted to be, in order 1

1

O11, 12,

2

11,

2

1

12, 21,

1

22,

2

2

%21, A22

On the other hand, the statement PRINT 10, (((A(1, J, K), 1=1, 2), J=1, 2), K=1, 2)

would print these elements in the order 1

1

1

1

2

2

2

2

O11, 21, %12, 22, %11, &21, 12, A22

This last sequence happens to correspond to the order of the absolute storage addresses in which the elements are stored, which is different from the order in which they were read in. Finally we may use fixed values of the subscript. Thus, the names VECT@R(5), A(2, 1), A(4, 3, 2) may appear in a list and be treated as single elements of the arrays. If the value of NN is established, we may write VECTOR(NN) in the list, and this is treated as a single element with the subscript assigned the value equal to NN. We may establish the value of NN in the same statement in which NN is used as a subscript, as follows: READ 100, NN, VECT@R(NN)

((A(I, J), J=1, 50), 1=1, 50)

In either event the elements will be stored by columns, as we have said in Chapter 2.

Example 3.14 We have a two-by-two matrix in storage with the element a;; at address A(I, J). We assume that statement 10 is an appropriate Format

3.5

EXAMPLES

We close our discussion of Fortran input and output with some examples. We consider first a simple case of a complete program including the necessary input and output statements, and then consider two examples of a more complicated nature.

3.5

Example 3.16 We write a program to read in the four elements of a determinant of order two, compute its value, and print the result.

PROGRAM DETVAL DIMENSIZN ALFA(2, 2) 10 FORMAT (4E14.7) READ 10, ALFA VAL=A(1, 1)*A(2, 2)—A(1, 2)*A(2, 1)

PRINT 10, VAL END END

Example 3.17 We wish to write a program for the solution of the system of n linear equations in n unknowns

having an initial ten-column Hollerith field, which identifies the current right-hand terms B;, the remainder of this card being unused. The rest of the deck consists of the number of cards required for the B; to be punched in the same way as the A,; defined in (3). (6) Cards such as those described repeated for each of the k cases.

Biwa =

1. 2; easy

n)

(1) The first three columns of the first card contain an integer k, which specifies the number of different sets of coefficients A,; to be furnished. The remainder of the card is not used. (2) The first ten columns of the second card contain Hollerith information to identify the coefficients A;; for the current case, and the first three columns of the next card give the current value of n. (3) The fourth card and the following (n?/4) — 1 or (n? — 1)/4 cards, as the case may be, contain the coefficients A,;, punched four to a card in 18-column E fields with nine digits to the right of the point. The leading eight columns of each card are used for a sequence number, which is not part of the input data. (4) The first three columns of the next card contain an integer m defining the number of decks of righthand terms that follow. The remainder of the card is not used. (5) There are m right-hand term decks that follow.

deck

are

that for all k cases we have n
A || (Aj+-W; || Z;-00304 || STOP

Only the information between the double lines is entered into the storage registers of the computer. The information to the left and right of the double lines is for the aid of the coder, reminding him of exactly where an instruction is to be located and of the effect of that instruction.

98

INTRODUCTION

TO

MODERN

DIGITAL

COMPUTING

The codes or programs given above, like those in Example 4.2, although correct, have certain undesirable features. They vary in length and are different for each different value of n. The varying storage requirements of the programs arise from the fact that separate instructions have been placed in storage to handle each individual item of data. For a typical problem such a coding technique would require an extremely large number of instructions—probably more than could be placed in storage at one time. Since the speed of the computer enables it to execute thousands of instructions per second, the technique of storing separate instructions to handle each item of data will lead to an uneconomical mode of operation, in which the computer will spend the majority of its time either stopped or loading in new sets of instructions. The coding technique that should ordinarily be employed consists of creating the instructions for handling each item of data, as it is needed, from a small set of prototype instructions. This technique requires that we place the data in storage systematically and with careful foresight. Systematic storage of data, such as provided in Fortran for arrays, allows repetitive use of a small body of instructions to create from a set of prototype instructions, as needed, the individual instructions for operating on given data items. Random placement of data invalidates this technique and will force the introduction of more instructions for operating on prototypes than would be required if individual instructions for each item were used. [Tor Example 8.5, the data have already been correctly arranged, and the program will be rewritten below in order to illustrate the proper technique. 8.3.2(a) Symbolic Addresses. Before we rewrite the vector addition code, it will be convenient to introduce some notation. When we begin to prepare a program, we rarely have a complete knowledge of all of the addresses involved. We may not know where certain instructions, parameters, and data will finally be located. We must, however, constantly refer to these quantities by address within the code. Thus, for machine language coding, we also introduce symbolic addresses, which are used in place of the actual machine or absolute addresses of a quantity. All of the symbolic addresses must eventually be reduced to machine

addresses. This last reduction is quite straightforward, though often tedious, and, in Fortran, is done by the compiler. We employ two kinds of symbolic addresses. One type is called a relative address and (as in an array) is symbolized by the notation a;. Here a is just a convenient tag, for which any other symbol could have

been used, and additional symbols may be required.

SYSTEMS

The address a; is defined to be equal to the address a) + 7. Thus, once an absolute address value is assigned to the symbol apo, all a; are immediately determined relative to ao. The second type of symbolic address notation is not as elegant as the first, but is, nevertheless, quite useful. It is known as the location-of address notation and symbolized by l(z). This symbol is interpreted to be the location of the quantity in its parentheses—in this particular example, z. In Fortran we simply use Z for both z and l(z), but here we need to distinguish between the address I(z) and the content z. We are at liberty to introduce for addresses any other symbols that may seem convenient. We ordinarily choose such symbols for mnemonic purposes, as we do in Fortran; however, it is well to keep in mind that symbolic addresses must eventually be converted to absolute machine addresses. This conversion can be accomplished by the machine itself if a suitable program of instructions is provided for this purpose. In order to avail ourselves of the machine conversion, we will have to sacrifice some freedom of choice of symbols in order to remain within some format dictated by the conversion program. Here we will utilize the two kinds of address symbols defined above and perform the conversion to absolute addresses ourselves. Example 8.6 As an example of the use of the symbolic addresses, we rewrite the first code of Example 8.5, using both kinds. We have for n = 2, with instructions starting at ao, ao

12

0

L(V)

Vi —

Qy

30

0

1(W,)

(A)

a2

20

0 1(Z)

A —

Vi

+

Wi

Z,; —> 1(Z1)

a3

120

L(V)

V27

a4

30

0

l(Wo)

(A)

a5

20

0 L(Zz)

A —

LZ. —

Ve —- W2

1(Zz)

ag || 76 0 00000 ||STOP If we assign a = 01000, /(V1) = 00100, 1(Wi) = 00200, and 1(Z;) = 00300, all other absolute addresses are determined, and the absolute program is the same as before.

In addition to simplifying the bookkeeping required in the first stages of a program, the use of symbolic addresses often helps us to visualize the purpose of each instruction. For instance, we see that the triplet of instructions,

12 0 U(V,),

30 0 1(W,),

20 0 1(Z;)

indicates very clearly the steps we need to take to form each component. Their Fortran equivalent might be Z(1) = V(I) + WI).

8.3

8.3.2(b) The Iterative Loop. Now let us turn to a second technique for writing the code for vector addition. Note that the instruction triplet for forming the ith component Z; is 12 0 00100 +72 — 1, 30 0 00200

+ 2 — 1,

20 0 00300 +2 — 1 while the triplet for forming the (2 + 1)st components Z i41 18

12 0.00100 + 3, 30 0 00200 + 3, 20 0 00300 + 7 Obviously, the instructions for forming Z;,; can obtained from the instructions for forming Z; addition of the parameter 00 0 00001. Thus, if instructions for forming Z, are written explicitly, of the other triplets can be obtained from them successive addition of this parameter.

be by the all by

Example 8.7 We write the vector sum code, creating the necessary instructions, as needed, from those for forming Z,. The reader is warned in advance that the code has the same defect as that in Example 4.3. ao || 12 0 00100 ||V: ~A a1 || 30 0 00200 || (A) = V; + Wi, 7 = 1 initially a2 || 20 0 00300 || Z; — 300 + 7 — 1,7 = 1 initially a3 || 12 0 ao a, || 30 0 1(1)

Set up (ao) for modification Increase 7 by one in instruction

as ae az ag ag aio an

Store modified instruction at ao Set up (a1) for modification Increase 7 by one in instruction Store modified instruction at a1 Set up (a2) for modification Increase 7 by one in instruction Store modified instruction at az Jump to repeat instructions for next value of 7

|| 20 || 12 || 30 || 20 || 12 ||30 || 20 a || 75

The 4 = three final

0 0 0 0 0 0 0 0

ao a (1) a ae 1(1) ae a

first three instructions form Z;, starting with 1. The next nine instructions change the initial so that they will form Z;,; when executed. The instruction provides for the control to go back to

ao in order to form Z 441.

An iterative loop of addresses consists of a sequence of addresses which the control consults repetitively in the execution of a code. The addresses ao, a1, ..., 12, in Example 8.7, form such a loop. The instructions making up the content of the registers whose addresses form the loop may remain constant, but ordinarily certain ones will change each time the control traverses the loop of addresses. In Example 8.7 the control has

CODING,

PROGRAMMING

99

no means of breaking out of, or exiting, from the loop. It will cycle through the loop indefinitely, performing the desired vector addition, and a large number of extraneous calculations, unless halted by a computer fault. A loop without an exit, such as formed by Qo, @1, ..-, Q12, 1S called a closed or infinite loop. When the data are properly arranged in storage, an effective and economical code can be written by employing the loop concept. In general, some of the instructions stored within the registers addressed in the loop will be prototype instructions for operating on the initial data items. Others will be housekeeping instructions which operate on the prototypes and their successors to create new instructions for handling succeeding sets of data items. As Example 8.7 illustrates, however, the housekeeping instructions must do more than this—they must also provide the loop with an exit. A simple and straightforward means of exiting from a loop is the combination of counting operations and a conditional jump. This is what takes place in a Fortran Do loop. For example, a parameter equal to n, the number of times the loop is to be traversed, can be stored. Then each time the loop is actually traversed this parameter can be reduced by one and compared to zero by means of the Zero jump. If the parameter has become zero, the instructions of the loop have been executed a sufficient number of times and the control exits from it by a jump; if not, the jump will be such as to cause the control to complete the loop one more time. We will redo the vector addition code to illustrate this exit technique. Example 8.8 an exit.

We write the loop of Example 8.7 with

a || 12 0 00100 || | i

3 ;pace

Form Z; beginning with z = 1

a3 || 12 0 U(n) a, || 31 0 U(1) as || 22 0 ai ag || 20 0 U(n) a7

Reduce count by one; if count exit from loop Store reduced count

zero,

12 0 ao

ag || 30 0 U(1) ag || 20 0 a

aio || 12 0 a ay || 30 0 1(1) 12

20

0

ay

13

12

0

a2

Modify instructions to form Z; for 7 increased by one

au ||30 0 1(1)

ais |20 0 ap

J

aig || 75 0 ao

Jump to repeat program

a7 ||76 0 00000

STOP

This might be the object program produced by the Do loop of Example 4.10 except for a defect, which we will note below.

100

INTRODUCTION

TO

MODERN

DIGITAL

COMPUTING

8.3.2(c) Prestoration, Entry Points. If the data and parameters are stored, absolute addresses assigned, and the initial instruction taken from the absolute address assigned to ao, the program of Example 8.8 will cause the computer to form the sum of two n-dimensional vectors and stop. However, this code is not acceptable. It preserves the data but does not preserve itself. Although the original data remain intact, repeating the program will not cause the vector addition to be repeated correctly because the instructions stored in registers a, a1, and a, and the parameter n are changed each time the loop is traversed. This defect can be remedied either by restoring these quantities to their initial values before stopping, or by prestoring them to their initial values before entering the loop. With the addition of prestoration or restoration, the program will perform the vector sum correctly every time the control begins executing instructions from register ao. Without prestoration or restoration, the program would have to be reloaded before it could repeat itself. If a program causes the computer to carry out the same calculation every time it is executed, its initial address will be called a proper entry point for the program. It is of the utmost importance that codes have proper entry points. Such codes will prove to be much simpler to check out and to run. Of the two methods—restoration and prestoration—the authors prefer the latter. One reason for this preference is that a program that requires restoration will have to be completed before initial values are restored. On the other hand, a prestored initial setting will always be correct, even though the program has been interrupted. The idea of prestoration is even more important if a code is used repeatedly as a part of a larger program. To provide an example of prestoration, the vector addition will be redone. In writing the code, we will assume that a parameter equal to one is stored, that the value n is stored in register 99999, and that parameters

Py>=12 0 00100 P,=30 P2=20

0 00200 0 00300

12 0 UE)

a3 || 20 0 ag

a ||12 0 UP.)

ate and store Z;, 7 = 1 initially

au || 12 0 l(index) ein |31.0

index b by one Decrease index

1)

ais ||22 0 aos au || 20 0 l(index) O15

12

Index = 0? If yes, jump to stop Index not zero; store index

0 ag

ais ||30 0 l(1) a7 || 20 0 ag

ieee Oke OS, 20

20

0

Modify instructions to form Z; for 7 [increased by one

ag

aa || 12 0 aro az || 30 0 (1)

as ||20 0 aio

| ~

J

aos ||75 0 ag azs ||76 0 00000 ae ||12 0 00100

Jump to form next component STOP Po

a7 ||30 0 00200

Py

aes ||20 0 00300

P,

a29

One

00

30

0 00001



Index location

This would more closely resemble the object program for Example 4.10, since the compiler will be designed to provide proper entry points. 8.3.2(d)

Code Assembly.

The vector addition code in

the form given in Example 8.9 could not be transmitted directly from the storage registers to the control section for execution. Before this is possible, all of the symbolic addresses in relative and location-of form must be reduced to absolute machine addresses. This is done in two stages: first the location-of symbols are changed to relative symbols; then ay is defined as an absolute machine address, and all relative addresses are reduced to absolute form by the formula a; = ao + 7. In Example 8.9, iP)

eto

L(Py)

=

27,

1(P2)

S23

(BID

l(Gndex)

Example 8.9 We write the vector sum loop with a proper entry point.

a2

ag || 12 0 00100 ag || 30 0 00200 ayo ||20 0 00300

l(one) = ag

are also available in storage.

ao || 12 0 1(Po) a | 20 0 as

SYSTEMS

) | Prestore Si

instructions

to value

=

=

30

The absolute machine codes corresponding to both a = 00500 and a) = 01000 are given in Example 8.10. The procedure of changing from symbolic to machine addresses is usually called assembly. Assembly is an operation that can be carried out by means of a computer program when appropriate format and symbolic address forms are employed in writing the code.

as || 20 0 ano

ag || 12 0 99999

\Set value of index ton

a7 ||20 0 U(index) ||{7 = 1

— 7+

1,

Example 8.10 We assemble the program of Example 8.9 with absolute addresses assigned as given above.

8.3

00500 00501 00502 00503 00504 00505 00506 00507 00508 00509 00510 00511 00512 00513 00514 00515 00516 00517 00518 00519 00520 00521 00522 00523 00524 00525 00526 00527 00528 00529 00530)

|| 12 0 00526 || 20 0 00508 || 12 0 00527 || 20 0 00509 || 12 0 00528 || 20 0 00510 || 12 0 99999 || 20 0 00530 || 12 0 00100 || 30 0 00200 || 20 0 00300 |} 12 0 00530 || 31 0 00529 || 22 0 00525 || 20 0 00530 || 12 0 00508 |} 30 0 00529 || 20 0 00508 || 12 0 00509 || 30 0 00529 || 20 0 00509 || 12 0 00510 || 30 0 00529 || 20 0 00510 || 75 0 00508 || 76 0 00525 || 12 0 00100 || 30 0 00200 || 20 0 00300 || 00 O 00001 = ——————

|| 01000 || 01001 || 01002 | 01003 || 01004 || 01005 || 01006 || 01007 || 01008 || 01009 |] 01010 || 01011 |} 01012 || 01013 || 01014 || 01015 |} 01016 || 01017 || 01018 || 01019 |} 01020 |) 01021 || 01022 || 01023 || 01024 || 01025 || 01026 || 01027 || 01028 || 01029 51101030)

|| 12 0 |] 20 0 || 12 0 |] 20 0 || 12 0 |] 20 0 || 12 0 || 20 0 |] 12 0 || 30 0 || 20 0 |] 12 0 || 31 0 || 22 0 || 20 0 || 12 0 || 30 0 || 20 0 || 12 0 || 30 0 || 20 0 |} 12 0 || 30 0 || 20 0 || 75 0 || 76 0 || 12 0 |; 30 0 || 20 0 |; 00 0 ———

01026 01008 01027 01009 01028 01010 99999 01030 00100 00200 00300 01030 01029 01025 01030 01008 01029 01008 01009 01029 01009 01010 01029 01010 01008 01025 00100 00200 00300 00001

We notice immediately that the vector addition code with entry point at 00500 is different from the one with entry point 01000. This illustrates a cardinal point: a code is a function of its operating locations and assumes, in general, different forms in different operating locations. Thus, an item of coding, operating in certain fixed registers, cannot be arbitrarily copied into other registers and then be expected to function correctly. In general, modification of the code will be necessary. 8.3.3 Indexing. In the examples of coding given above, we modified instructions by means of a constant parameter equal to one. This parameter can be used to increase or decrease the address part of an instruction by one. Any other incrementation or decrementation constants appropriate to a particular situation can be introduced and utilized in a similar way. In this technique of address modification, the modifier remains constant and the digits in storage represent the instruction change after each modification. This scheme requires us either to prestore or restore the modified instruction. Another drawback of this instruction modification technique is that it requires the use of the accumulator for housekeeping arithmetic, which imposes an added burden on the accumulator and frequently interferes with the essential problem arithmetic. The extra housekeeping instructions required

CODING,

PROGRAMMING

10i

tend to make the code cumbersome and lengthy. Accordingly, we seek a method of address modification that will minimize prestoration or restoration of instructions and will not disturb the flow of the problem arithmetic through the accumulator. This leads to the introduction of index registers and computer instructions for working with such registers. In the remainder of this section these registers and instructions will be discussed, and techniques illustrating their use will be demonstrated. 8.3.3(a) The 802 Index Registers. We will assume that the 802 is provided with ten auxiliary control registers of five-digit capacity, called index registers. With the Load Accumulator, Store Accumulator, Add, and Subtract instructions defined above, designator values 0, 1, ..., 9 are used to designate or address these ten index registers. In accordance with the particular designator value with which it is associated, an index register will be referred to as Index Register 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9. In this context the designator is called the index designator and assigned the symbol b. Used in this way, it becomes the address of the index register. The associated index register will be symbolized by B®, where b = 0,1, ..., 9. Index Registers B! to B® may contain any five digit number. However, Index Register B® can

contain only the value zero. Because of this, a value of b = 0 may require special consideration. The base address a specified in the instruction table above, is not, in general, the address utilized by the control in the execution of an instruction. In many cases, a different address related to the base address is utilized. This address will be called the effective or execution address. We define the effective address as follows:

effective address = base address + (B?)

= a+

(B?)

Ob e/ OS eee

We recall that (B°) = 0 at all times. It follows that whenever 6 = 0, the base address and the effective address

are

the

same.

We

illustrate

these

ideas

in

Example 8.11. Example 8.11 (B') = 00001, (B?) = 00005. occurs if we execute the pair of instructions

12 1.01000,

What

30 2 01000

Because of address modification, the computer will form in the accumulator the sum (01001) + (01005). This occurs because the effective addresses are, for the first instruction 01000-+1=01001

and for the second one 01000+5=01005

102

INTRODUCTION

TO

MODERN

DIGITAL

COMPUTING

Note that without indexing, we would have 12 0 01000,

30 0 01000

giving us twice the content of register 01000 in the accumulator. In the 802 the effective address is computed in specially provided control registers from a copy of the instruction taken from storage. Thus, the stored copy is unchanged and prestoration or restoration of it is unnecessary. Prestoration with index registers is ordinarily simpler to carry out than prestoration of instructions, since, frequently, the content of a single index register can be used to modify more than one instruction, as well as to count loop traversals. Thus, assuming the availability of instructions that will enable us to assign initial values to, and modify the content of, index registers without using the accumulator, we can carry out address modification without disturbing the flow of the problem arithmetic and without altering the coded instruction in the storage register. In doing this, we hold the instruction in storage constant and modify the modifiers, that is, the content of the index registers. This reverses the earlier procedure of modifying the instruction in storage and holding tne modifier constant. By also using index registers to carry out any counting necessary to perform the exits from loops, we can relieve the accumulator of its last housekeeping function, leaving it completely free to carry out the problem arithmetic. For certain operations it is convenient to think of the designator as specifying an extension or variation of the operation, since its function will be to modify the operation. In these cases, if we wish, we may think of the operation code and designator taken together as defining an extended operation code. We will not pursue this aspect of coding further.

8.3.3(b) The 802 Index Instructions. Before turning to examples of the use of index registers—that is, indexing—we must first introduce a few of the index instructions whose existence we assumed above. The reader should familiarize himself with the following instructions in order to understand the examples.

SYSTEMS

The Load tion from referenced copies the

Index instruction carries out this prestoraa part of the content of the storage register by address a. Specifically, the Load Index lowest order five digits (address part) of the instruction in a into the index register addressed by the designator b, or, symbolically,

lowest-order

five digits of (a) —

B?

Since (B°) is always zero, it is unnecessary to prestore the content of index register zero. A machine such as the 802 will typically have a number of other instructions of Load Index type. For example, instructions for transferring information out of index and into storage or arithmetic registers. We will not introduce such instructions here. Since we now propose to modify addresses by changing the value which we add to the base address rather than by changing the base address itself, we must have instructions to alter the index register content. The Index Jump instruction serves this purpose. The Index Jump tests the content of the index register addressed by the designator b. If (B®) = 0, the next instruction in the normal sequence will be executed. If (B®) ¥ 0, the value stored in Index Register B? is decreased by one and the next instruction is taken from the register whose address is given by a in the Index Jump instruction. Symbolically, for an instruction of the form 55 b a, If (B’) ¥ 0; (B’) —

1 — B®, jump toa

(B®) = 0, continue in sequence 8.3.3(c) Hxamples of Indexing. We close cussion of indexing with several examples.

Example 8.12

12-0

By

the

dis-

We wish to perform the instructions

320.0

9;

for 1 =

07-1)

J

oe

to transmit a block of information. We propose to do this by indexing and decreasing the content of an index register one step at a time. We assume that the value 99 is stored at register zero. We use the pair, 12 1 Bo 20 1 6) as the equivalent of the above. To load the index we use

53 1 00000 Instruction

Operation code

‘Load Index

53

Index Jump

55

| Designator

Address

At the outset the two instructions will be the equivalent of 12 0 Bog and 20 O 49, and as we successively decrease (B') by one it will become, in turn, equivalent to 12

Since the techniques of address modification now requires the prestoration of the content of an index register, we must have instructions for this purpose.

0

Bos

12 0 Boz

20

0

O98

20 0 do,

The original pair of stored instructions is not modified.

EXERCISES

103

For further examples of the indexing technique of looping and address modification we will redo the vector sum problem of Section 8.3.2. All of the previous assumptions will be retained. We will assume the parameter n specifying the number of components in the vector table to be in the address part of register 99999.

In Example 8.14 we used the Index Jump at a, to decrement the index value by one. This is in line with the policy of transferring as much of the housekeeping arithmetic as possible out of the accumulator. For more complicated operations on index values we may still have to use the conventional arithmetic registers.

Example 8.13 dexing, follows:

8.3.4 A Glimpse at Wider Horizons. In the foregoing discussion we have not said anything about the operands of arithmetic beyond the fact that they consisted of eight digits. For simplicity of presentation we have treated 802 words as if they consisted of eight decimal digits and have avoided questions of how negative

The code for a vector sum, with in-

ao ||53 1 99999 ||Set (B) = n EXIT | a |] 55 1 a3 az ||76 0 ay

If (B1) ¥ 0, decrement, jump to a; STOP

a: |12100100 |/Va4. >A

t=n—1,...,1,0

a ||30 1 00200 || (A) = Viai + Wisi = Ziy: t1=n —1,..., 1,0 as |}20 1 00300 |]Store Zis1 te Na— Ise. 10 ae ||75 0 a Close or

This could well be the object program compiled from the first Fortran program of Example 4.12. In Example 8.13 only the content of Index Register B needed to be prestored before entering the loop. This single prestoration replaces the prestoration of the count and the address parts of three instructions required by the previous technique of looping and address modification. Since the index is automatically decremented by unity, we do not need to store an advancer parameter as in the previous codes. The Index Jump operates so that the vector components are handled in the order n, n — 1, ..., 1. This makes no difference in the final result. If we wish to handle the components in the order 1, 2, ..., n, we can reverse the order in which components are stored. In Example 8.13, the Index Jump is placed at the beginning of the loop. It may also be placed at the end of the loop. However, in this case, we must decrease the initial value of the index by one to account for the fact that one traversal of the loop will have occurred before the index test.

numbers are to be handled and of scaling the results so that they can be properly represented by the allotted quota of digits. We have left such questions open for

the 802 in order not to interpolate into a discussion of the most elementary aspects of machine language coding a discussion of number representation and number systems. We have also omitted from consideration important concepts concerning nonarithmetic or logical operation, subroutines, input-output, and assembly and checkout. For the occasional user toward whom this book is directed, it will be sufficient to introduce the necessary elements of these topics from the point of view of the problem-oriented languages; and we have done so in the first seven chapters. The reader will do well, however, to remember the possibility of attaining utmost efficiency from his program by combining machine language coding and problem language coding into an integrated whole. If and when he progresses to computer usage on a large scale, he will want to consider this possibility carefully and to fill in the gaps in his knowledge of machine language programming.tf

EXERCISES All exercises in this chapter pertain to the 802.

Section 8.3.1(a)

Example 8.14

The following code is an example of the

vector addition program with the Index Jump at the end of the loop. We assume n ¥ 0. ao || 53 1 99999 || Set (B!) = a || 55 1 ae

(B!) =n-1

a2 || 12 1 00100 ||V; ~ A az ||30 1 00200 |}(A) = Vi + W; = Z:; 4+=n,n —1,..., 1

ax ||20 1 00300 ||Store Z; as ||55 1 ae If (B’) ¥ 0, decrement, jump to a ae ||76 0 ae STOP

1. How many digits are there in an 802 instruction word? What is their function? 2. What does each of the following mean as an instruction word? (a)

12 0 10000

(b)

20 0 10001

(c)

30 0 10002

+ To this end the reader may wish to consult the authors’ Computer Programming: A Mixed Language Approach. Academic : Press, New York, 1964.

104

INTRODUCTION

TO

MODERN

DIGITAL

COMPUTING

3. What effect would the three instructions 12 0 10000, 30 0 10001, 20 0 10002 have if executed in orred?

SYSTEMS

Section 8.3.3(a) 13. What is the result of the following instructions if (B*) = 00002 and (B?) = 00004?

Write a sequence of instructions to add (10000), (20000), (30000), and store the result at 40000. Section 8.3.2.

Write the necessary instructions to form the sum of two three-dimensional vectors stored at 10001, 10002, 10003, and 20001, 20002, 20003, respectively, and store the results at 30001, 30002, 30003.

Section 8.3.2(a) Write a sequence of instructions that would be equivalent to the Fortran statement

(a) (b)

14

15

What would happen if (B!) = 99999 and we give the instruction 12 1 00001?

Section 8.3.3(b) Suppose we wish to load the value b into B? for b = 1, 2, 3.

ao || 53 1 (ten) a, || 12 0 l(one)

a || 30 O L(one) a3 ||55 1 ae

7 To what Fortran statement might the following instructions correspond? 12 0 l(a)

30 2 20002 30 2 20000; 20 1 30000

Write instructions to achieve this. 16. In the following code, the computer control starts at an. What happens?

A=B+C+D

30 0 16) 30 0 1(2) 20 0 ly)

12 1 10000 12 1 10000;

ag || 76 0 00000

Section 8.3.3(c) 17. Use the Index Jump to write a program that will generate 5

yok

Section 8.3.2(b) Write a program to generate integers 1, 2, 3,..., n in order. Make a closed loop. Repeat Problem 8 with an exit after 10 integers have been formed. Repeat Problem 9 with an exit after n integers have been formed where n is stored at 99999.

k=1

18. Use the Index Jump to write a program that will generate

> 2k k=1

19 Redo Problem 17 for 5

> (2k — 1) k=l

Section 8.3.2(d) 11

For the following code, give possible assembled versions for a = 01000 and ay = 50000.

20

Redo Problem 18 for

Dek —¥) ao || 12 O 1(5) ai || 30 0 l(a) a2 ||20 0 l(answer)

a3 ||76 0 00000

12. Do Problem 11 for the code in the answer to Problem 8.

k=1

21 e The sum in Problem 20 is n®. Use this fact to write a program to generate

L k=1

1.

OPERATIONS, SYMBOLIC NAMES, NUMBERS

Operations. Expressions in parentheses are evaluated first from the innermost set outward to the outermost. Within parentheses or in expressions with no parentheses, the order in which operations is carried out is:

APPENDIX

Arithmetic:

1. Exponentiation (**) 2. Multiplication (*) and Division (/) 3. Addition (+) and Subtraction (—)

Logical: 1. .NOT. 2. .AND. 3. OR.

Within any class, operations are performed in order from left to right. Symbolic Names. Names of variables must consist of from one to six (or seven) alphabetical characters, or digits, or both, the first of which must be an alphabetical character. If listed in a Type statement, a variable name may begin with any letter. If the variable is classified implicitly as real or integer, then for integers the name must begin with I, J, K, L, M, or N and for reals any letter not one of these. Numbers.

1. Real constants appearing in expressions may take either the form of an F-field (for example 3.1416) or an E-field (for example, 0.31416E+1). The decimal point must appear. 2. Integral constants must be integers with no decimal point (for example, 12345). 3. Double Precision constants may appear as a D-field (for example, .3141592D+01) or as the analog of F-fields (for example, 3.141592D). For some compilers the final D is not required. 4. Complex constants appear as ordered pairs of reals (for example, (3.1416, 2.7183)). on. Logical constants may be either .TRUE. or .FALSE. 6. Boolean constants or masking constants may be octal integers identified by a final B (for example, 1234567B).

105

106

APPENDIX

2.

FORTRAN

STATEMENTS ee

oe

NONEXECUTABLE STATEMENTS ———E—e—e—e——eee———— Description

Form

Kind I

Program and Subprogram

———————_—_—_—_$_$_$_$—

PR@GRAM NAME

Identifies Program and Start.

NAME[X,, °*°, Xz] = E

Gives Name as the function defined by the arithmetic expression E. See local manual for special restrictions.

FUNCTI@N NAME[X,, ***, Xx]

Defines the beginning and gram. The name should variable names and must to define the value of the

SUBR@UTINE NAME[X,, «+, Xz]

Defines the beginning of a Subroutine subprogram. The parameter list contains both dependent and independent variables. Beginning letter of name is arbitrary.

C@MPLEX FUNCTIGN NAME[X,, °*°, Xx] D@UBLE PRECISION

FUNCTIGN

NAME[X,, © °°, Xx]

INTEGER FUNCTI@N NAME[X,, © ++, Xx] LOGICAL FUNCTIGN NAME[X,, «+ *, Xx] REAL FUNCTI@N NAME[X,, «++, Xx]

Mode Definition

Storage Allocation

reed

=

name of Function subproconform with the rules for be used in the subprogram function.

Same as Function subprogram statement, except that

the mode of the result is explicitly defined. Should be used for Complex, Double, and Logical. Need not be used for Real or Integer if first letter rule is used to define mode implicitly.

RETURN

Defines exit from a subprogram.

END

Defines the terminal subprogram.

EXTERNAL List

Permits the name of a Function appearing in the list to be used as a parameter in a subprogram.

C@MPLEX List or TYPE COMPLEX List D@UBLE PRECISI@N List or TYPE DOUBLE List INTEGER List or TYPE INTEGER List LOGICAL List or TYPE LBGICAL List REAL List or TYPE REAL List TYPE QTHER

Explicit definition of the mode of the variables appearing in the list. Most of the compilers using the word TYPE will accept the other form except for the TYPE OTHER statement which permits non-

DIMENSION List

Defines

point

of each

program

and

standard operands (see Chapter 7). Some compilers permit dimensioning in Type statement. Should be used for Complex, Double, and Logical. Need not be used for Real and Integer if implicit first letter rule is used. the maximum

value

for each

subscript

for

arrays in the list. The maximum value must be given as an integral constant except for the variable Dimension statement permitted in subprograms (see Chapter 7).

CBMMON List or

Input and Output

COMMON /B/List

Variables in the same relative positions in a Common list in programs and/or subprograms will occupy the same storage locations. The second form permits this allocation by blocks.

EQUIVALENCE|List 1], [List 2], ++

Identifies common each list.

DATA[V, = List], [V2 = List], -++ or DATA List/List/,

Assigns

List/List/

FORMAT(Fi,

storage locations for elements

in

the values in the List on the right to the

variables V; or to the variables in the List on the left. Implied Do loops are permitted (see Chapter 5). Fo, boned} F;]

Reference for input and output statements in terms of the field descriptions F; for the format of data. Should have a statement number attached.

rr

APPENDIX

EXECUTABLE

107

STATEMENTS

(NES denotes Next Executable Statement) Description Assignment

Sequence Control for alternate branches

Sequencing

Assigns the value of the arithmetic expression AE or the logical expression LE to the variable V. Called in the one case the Arithmetic Assignment statement, in the other the Logical Assignment statement. Assigns the appropriate form of the value of the arithmetic expression AE to each of the variables V;. Not available in some compilers.

NES

Unconditional transfer of control to executable statement numbered s.

Statement s

GS TD [sy SaaS sk], n

Computed Go To statement. If the integral variable n satisfies 1 < n $k, control accepts next the statement numbered s,. In some compilers the comma before n may be omitted.

Sn

GD TOD nfs, «++, sx] with

The Assigned Go To statement. If m is given the value s;, 1 Sik by a prior Assign statement, control accepts next the statement numbered s;. Comma and list may be omitted.

Si

Arithmetic If statement. If the arithmetic expression A is negative, zero, or positive, control is transferred as

A

0, 83 ES or NES 8, OF Se

executable one. For the second form, if L is true control goes to statement number s1, otherwise to statement number sz. Some compilers permit both forms.

IF OVERFLOW or

Many internal condition sensors are provided and tend to be machine dependent. See the local manual for details.

See local manual.

C@NTINUE

A do nothing instruction except when used in a Do loop to replace an illegal branch statement as the final statement of the loop.

NES or continue Do loop.

PAUSE or PAUSE n

Program halts or halts and prints n. Program continues if computer is restarted.

NES if restarted.

ST@P or STOP n

Program terminated or terminated with printing of n.

Program terminated.

DD si = ny, no, ng

All statements through that one numbered s are executed for i = m1, m1 + 13, °**, m + tn3 for m + tn3 S m. If nz is missing, n3 = 1.

NES

Read in cards according to Format statement s. Print according to Format statement s. Punch cards according to Format statement s. Read in or write out on equipment e according to Format statement s. Read in or write out on tape unit e according to Format statement s. In all cases input and output for formatted statements

NES

IF ACCUMULATOR

DVERFLOW, etc.

Indexed Loop Control

Input and Output

READ s;, List PRINT s, List PUNCH

s, List

READ [e, s] List WRITE [e, s] List READ INPUT TAPE e, s, List

WRITE GUTPUT TAPE e, s, List

after completion

of loop unless there is an internal branch.

is for the variables in the list. Unformatted input and output statements may be used for Hollerith fields and in some other situations. See local manual. EET TEEEESEEIEIEESEEEEEEEEEEEnES EEE EE

108

APPENDIX

3.

FORTRAN

LIBRARY FUNCTIONS

Note: Not all of the following functions are available with some compilers. There is also some variation in the symbolic names. Some compilers require a final F on the function name for integral and real arguments—for example, SQRTF in place of SQRT. Many of these compilers also identify integral operands by a prefix X instead of I, for example XABS instead of IABS for the absolute value. In general, the use of the final F and X instead of I is in compilers using the word TYPE in the Type statements. Many of the conversion functions from one mode to another can be achieved by an Assignment statement

Symbolic Name and Fortran Form

with compilers which permit sufficient mixed operation. Thus, the statements TYPE C@MPLEX Z

X=Z

would achieve the same result as X = REAL(Z) to evaluate X as the real part of Z. For available functions and names, consult the local manuals. In the following table z, 71, 22 are arguments and y =f(x) or y = f(a, x2) is the functional value returned by the subprogram. The arguments are Fortran expressions.

.

Number of Mathematical Function

Arguments

Type of Argument

Type of Function

sin x (x in radians)

1

Real Double Complex

Real Double Complex

cos x (x in radians)

1

Real Double Complex

Real Double Complex

TAN[X]

tan x (z in radians)

if

Real

Real

CTN[X]

ctn x (radians)

1

Real

Real

SEC[X]

sec x (radians)

1

Real

Real

CSC[Xx]

esc x (radians)

1

Real

Real

ASIN[X]

sin! x

1

Real

Real

AC@S[Xx]

cos"! x

1

Real

Real

ATAN[X]

tan“ x

1

Real

Real

ATAN2[X,, Xe]

tan! (2/22)

Real

DATAN[X]

tan“! x

DATAN2[X]

SIN[X] DSIN[X] CSIN[X]

C@s[x]

mode

DC@s[x] Cc@s[x]

tan} (2/22)

1 2

Double Double

Real Double Double

SINH[X]

sinh z

1

Real

Real

C@SH[X]

cosh x

1

Real

Real

TANH[X]

tanh x

1

Real

Real

CTNH[X]

ctnh x

1

Real

Real

SECH[X]

sech x

1

Real

Real

CSCH[X]

esch x

1

Real

Real

ASINH[X]

sinh™(z)

1

Real

Real

AC@SH[x]

cosh}(z)

1

Real

Real

ATANH[X]

tanh“!(2)

1

Real

Real

ACTNH[X]

ctnh™!(z)

1

Real

Real

ASECH[X]

sech™!(z)

1

Real

Real

SSS,

APPENDIX

109

ee Symbolic Name and Fortran Form

Mathematical Function

Number of Arguments

Type of Argument

Type of Function

ARE ee

ACSCH[X]

esch7!(z)

1

ee

Real

Real

e

Real Double Complex

Real Double Complex

loge z

Real Double Complex

Real Double

Complex

SQRT[X] DSQRT[x] CSQRT[X]

Real Double Complex

Double Complex

AL@G10[x]

Real

Real

Real Integer

Real Integer Double Real

EXP[X] DEXP[X] CEXP(x] AL@G[x] DLOG[x] CLOG[x]

ABS[x] 1ABS[X] DABS[X] CABS[X] AINT[X] INT[X] IDINT[X]

aay a

Integral part of z, that is [x]

Double Complex Real Real Double

AM@D[X,, Xo]

Mm

MSDIX,, Xo]

(Remainder)

[21VfZelae

Real

Real Integer Integer

Real Integer

Real Integer

AMAXO[X,, Xo, + +*] AMAX1[X,, Xo, **°] MAXO[X,, Xo, **°] MAX1[X,, Xo, +++] DMAX1[X,, Xo, °**]

max(a1, 22, ***)

Integer Real Integer Real Double

Real Real Integer Integer Double

AMINO[X,, Xo, °°] AMIN1[X,, Xo, °°] MINO[X,, Xo, -**]

min(21, £2, +++)

Integer Real Integer Real Double

Real Real Integer Integer Double

FIX[X]

Convert integer to real (floating point). Convert real to integer (fixed point).

Integer Real

Real Integer

SNGL[X]

Obtain most significant part.

Double

Real

SIGN[X,, Xe] ISIGN[X,, Xo] DSIGN[X,, X2]

|21 |(sign of 22)

Real Integer Double

Real Integer Double

DBLE[X]

Extend single to double precision.

Real

Double

REAL[X] AIMAG[X] CBNIG[X]

Real part of complex z. Imaginary part of complex z. Conjugate of complex z.

Complex Complex Complex

Real Real

CMPLX[X,, Xs]

y = 2 + te

Real

Complex

MIN1[X,, Xo, ***]

DMIN1[X,, Xz, °**]

FLDAT(X]

ww) Li

Complex

110

APPENDIX

4.

PERMISSIBLE CHARACTERS AND CODES

The permissible characters in Fortran and the codes generated on the punched card are given in Fig. A.1. Some compilers may permit an extended list, available by double punching or an extended keyboard.

BECTEP GH! JELAMOPORS TUVHSYED

[ill 111 | MPM

12]

S

eapAmap AAT apse Ve T Perwewsaasape seh dadsaeenn Manan wnw ria

1014

ESe See Soret sly

1415160718

a zen

ecetveatemen ss

006

BOE

Te

aaa

2223aaa 26 27 2a 3031 2230 35 95/373839404k42 43 alas48 7 aes e05158054 555687 8 50 6a 62 62 64) 65 67686070 71 779

CAAT AE TTA NENA

PAH EI

bran

A

a

PA

74 75 767778

79

AE 0h od

aleaan aa alyanale ro aon a gle aa ai a2 ae ne oeataaataeane

7 BY9 10 if 12/13 14 15 16 17 18-19 20]21 22 25 24/25 26 27 28)29 30 3) 32/33 34 35 36}5/ 38 29 ay 42 43 44/45 46 47 40/49 50 51 52/53 54 55 56/57 58 59 60/61 62 63 64165 66 67 63}69 70 71 72)73 74 75 oe 78 79 80

33999) 1 2.9.4)5 nn wo no

999 91999.193.3.3/339913

3313333 2395/53

a3

jl

|

4444444 acaeagal aaaleqaalag 4444444 4 COL ee ee & 7 8/9 1011 12)13 4 45 WE17 48 19 a0l24 22 23 24)25 26 27 28]29 90 31 32}39 4 35 36)37 3B 34 4O;41 42 43 42145 46 47 48149 50 51 52159 54 56 Sb)57 5B 59 60:St $2 62 G4)65 6B 87 58/9 29 71 72473 74 75 76)17 78 79 a0 DeOk 0)Js Od |mundo Ors Oluld 55/555 5/555. 955 5/555 5/5 55 51555 55 55 5/55 55/5 5 55/5 55515 55 51555 515 5 55/5995 |

6 6\6 6 6 6/6 6

an ao

$/6 666/56) 6/866 5/6666

OO

6666

SERS SES SIG CG CEG EGEGES

43.14.95 16)17 18 19 20/21 22 23 24)25 26 27 28/29 30 31 32193 34 35 36)37 38 39 40/41 42 43 44/45 46 47 48149 50 51 52 53 54 55 56)57 58 59 60 61 62 63 64/65 66 67 68)C9 70 74 72472 74 75 76)77 78 73 80

I Tiarteeai Peeining |Heaton Qiactiaed ate 7 Tae Flediedin| Ueada a Pa UN

UTR RUT UE

TAME EU

aL U

| 818 8 8 8! 5 N.C.CO.

9 1011 12]13 14 15 16]17 18 19 20[2) 22 23 24)25 26 27 28)29 30 31 32}33 34 35 36]37 3f 39 40)41 42 43 44/45 46 47 48}49 50 51 52]53 54 55 56/57 56 59 60/61 G2 63 64)65 GS 67 G8)69 7071 72]79 74 75 76)77 78 79 80 733727

Vig. A.1.

CHAPTER 2.

1.

(+.325, 2)+(+.5, —1) = (+.325, 2)+(+.0005, 2) = (+.3255, 2);

ANSWERS

(+.325, 2)+(—.1127, 4) = (+.00325, 4)+(—1127, 4) = (—.10945, 4) 4.

EVEN-NUMBERED

(4.325, 2)+(+.5, —1) = (+.65, 3); (—.625, —2)+(+.5,

—1) =

(—.1127, 4)+(+.5, —1) =

TO

(—1.25, —1) =

(—.125, 0);

PROBLEMS

(—.2254, 5)

6. 4