• Keine Ergebnisse gefunden

I SUBRN EXIT

Im Dokument 2 COMPUTERS AND DATA PROCESSING (Seite 193-200)

Next Oper- Data Instruction ation Address Tag Address

RAA NEXTSUBRN

RAL AOOOO

{Now. Irrelevant)

xxx xxxxx 0000

Tag Explanatlon {

"NEXT" ~ LA.A.; jump to ~ubroutine

{

Return to the A address given by

C(LA.A.) (Here the sequences of x's indIcate the operation and data address can be anything. LA.A. stands for index accumulator A.)

In the MIDAC's typical three-address instruction logic, one instruction, the "file" (FI) operation, performs the same function as the instruction labeled JUMP for the modified IBM 650.

Loca-tion JUMP

Cl {3 'Y

Oper-Address Oper-Address Oper-Address ation

·EXIT 001 SUBRN

FI

AOOOO BOOOO COOOO AD

SUBRN (Irrelevant)

EXIT 000 001 (000)

Explanation

{" (JUMP

+

1)" to 'Y position of EXIT,jump to SUBRN

Note. If program parameters (variables needed in the subroutine) are required, they are generally stored in (1) the accumulator and other p::>sitions, except in the Wheeler entry method, and (2) registers following the JUMP instruction in the main program. In the latter case, the sub-rJutine entries or the subroutines themselves must be altered in an obvious fashion.

Table Look-Up. In many cases, it is desired to find the value of a function stored in a table. Since the process of finding an inner product described above obviously requires looking up ai (and bi ) in a table with

PROGRAMMING AND CODING 2-143 argument i, a similar procedure can also be used, as long as the arguments occur at equal intervals. The flow diagram for one approach is shown in Fi6. 16; no actual coding is included. Here the argument is x at equal

~_i_~_[X_I_AX_]---,I---~)---t1

Y -Y;,

I--~---IS

FIG. 16. Table look-up of y = f(x).

intervals Ax,and the function values are Yi. Again [ ... J means "integral part of." The result will be the value in the table corresponding to [xl.

The table look-up instruction on the IBM 650 (see Sect. 6) provides 11

similar technique using only one instruction on the IBM 650. Several hardware restrictions render this instruction less useful, but it is still a very powerful device.

A binary table look-up procedure may often prove most efficient when an equal interval table or table look-up operation is not available. Suppose there exist 16 arguments, xo, ... , XI5 in a table. A value of x is given, and it is desired again to find the approximation Y

=

f(x) from a table of Yi(i = 0, ... , 15). The flow diagram is shown in Fig. 17. Again, no coding is included.

This process may obviously be recorded in a recursive (loop) structure.

The number of comparisons in this process is C(N) ~ log2 N, where N is the number of elements in the table.

(Portion of di:lgram omitted)

FIG. 17. Binary table look-up procedure.

2-144 DIGITAL COMPUTER PROGRAMMING

Programming with Secondary Storage

Since secondary storage varies from digital computer to computer, it is difficult to give specific rules for its usage. The various devices which have been attached to general purpose computers as secondary storage include:

(1) magnetic drums, (2) magnetic tape units, and (3) large-scale random access devices (bin type magnetic tape units, magnetic disks, large-size drums).

All these devices employ magnetic methods of recording, and are there-fore storage of a general nonvolatile nature. They nevertheless have the capacity for malfunctions; dust on a magnetic surface, improper relay closure, etc., may cause an incorrectly read or written digit. It is therefore necessary, if satisfactory reliability or built-in checks are not available, to include programmed checks, usually by using storage-summing techniques, to guarantee proper performance. These techniques are described below under Integrated Systems (see Sect. 10).

Some magnetic drum systems are integrated into the high-speed storage unit; here the only programming requirement is to provide economy of performance either by minimal latency programming or an interlace feature. (See Ref. 103.) Use of magnetic drums in this fashion causes no basic problems. Drum equipment used as a secondary storage, however, entails a scheduling problem that generally can be solved exactly only by a computer itself, a procedure which has not been followed. Simpler methods of approximate solution are needed. Programs for such hierarchy transfer are discussed in Sect. II.

The use of magnetic tape units is very dependent on the presence or absence of built-in checking, ability to read both forward and backward, presence of fixed or variable block length. The reader is urged to consult Sect. 6, and then the various manufacturer's operation manuals or reports (see Refs. 34, 50, 54, 93, 103, and 148) for a fuller discussion of the instruc-tions that govern magnetic tape equipment.

Large-scale, so-called random access storage, as embodied in tape bin storage (see Ref. 119) and magnetic disk storage (see Ref. 118) basically require methods of mapping call words of long digit length (for example, inventory parts numbers) into a smaller number of digits giving the address in the random access storage. A parts number, ten digits in length, may correspond to a five-digit address in a random access unit. How can a unique correspondence be made? Certainly if there are more than 100,000 different parts, this is impossible; but if there are fewer than that, some method of randomization may allow an almost one-to-one mapping from the set of parts numbers (scattered thinly throughout the entire ten-digit range) into the set of storage addresses (most of which would be used),

PROGRAMMING AND CODING 2-145 One popular technique is a variation of the so-called mid-square procedure (see Ref. 74), to produce (in this case) the desired five-digit address.

EXAMPLE. Suppose an inventory parts number were 1122305151. The twenty-digit product of the number with itself is:

12595688518611232801

L....-J

If one uses digits 8 through 12 to provide a five-digit address, one obtains 85,186. With high probability, out of a group of 100,000 parts numbers each with ten digits, no two of them will have the same set of five mid-square digits. If more than one number does have a duplicate address under this mapping, the address can be tagged as an "exception" and either a second mid-square process based on the center ten digits of the resultant square, or another group of five digits in the square, may be used to gener-ate a new address, which again can be tested for duplications, etc.

Sorting and Merging. One primary problem that involves the use of secondary storage is the problem of rearrangement of input data in an ordered fashion. This problem can occur on one hand in assembly pro-grams where symbolic addresses are to be arranged in an easily entered, ordered list or, on the other hand, in any sort of business file maintenance problem where inquiries or changes that are not externally ordered in sequence are to be compared with a main file. Goldstine and von Neumann (see Ref. 19, Part II, Vol. II) developed the first theoretical analysis of two of the main methods of information rearrangement and compared the use of a general purpose digital computer for these purposes with standard punched card equipment, with some advantage in favor of the former.

Later studies, as listed in Seward's dissertation (Ref. 140), produce a better

"informational advantage" as far as use of a general purpose digital com-puter is concerned, but still indicate that this present machine structure is far from dominant in such performance.

There are two general classes of information rearrangement:

1. Merging. The act of taking two (or more) previously numerically increasing (or decreasing) ordered sequences of numerical information and combining them in one numerically increasing (or decreasing) se-quence.

2. Sorting. The act of taking an arbitrarily ordered sequence of (nu-merical) information and arranging it in a numerically increasing (or de-creasing) sequence. Since alphabetical information in a computer is most often encoded in some numerical form that is ordered analogous to the position in the alphabet, these definitions also cover merging and sorting of alphabetical and other nonnumerical information.

Such blocks of information (called items) are usually sorted with respect to a key, a sequence of one or more symbols (digits) which are pertinent to

2-146 DIGITAL COMPUTER PROGRAMMING

the position of the information in the sequence. In this discussion, it will be assumed, without loss of generality, that the key is numeric and the ordering desired is generally increasing.

Use of Main Storage in Sorting. If the blocks of information to be sorted each contain few enough computer words, many of them may be stored in the high-speed storage of a computer. During the sorting process, the relative values of the keys (usually located at the beginning of a block) may be used to exchange entire blocks. A simpler and often more effie lent process, however, is to move only the addresses of the blocks rather than the blocks themselves after a comparison of the keys has been made. Thus, if n blocks of m words are to be sorted, to be stored in nm positions, space must be also left for n addresses, which will be shuffled into an order corre-sponding to the order into which the blocks should be moved. After this process of rearranging the n addresses is completed, the corresponding blocks may then be read out onto secondary storage in the proper sorted order. This technique may obviously be extended to use of magnetic drums as well, since they in general have a relatively small access time.

Sorting Methods

There are two main types of sorting: (1) digital sorting and (2) merge sorting.

Digital Sorting. This method uses successive digits (or groups of digits) in the key to arrange the sequence being sorted into an ascending order. This is the method usually used on punched card equipment, where the information is passed through the sorter one time for each digit in the key, and the cards are collected in one of a number of output units (10 in a decimal sorter) at the end of each pass. If one starts at the least significant digit and proceeds up~vard in sequence, ordering the entire stack by digits after each pass, the entire process requires d passes, where d is the number of digits in the key.

The logical extreme of the digital sort technique is the so-calle~ address-sorting technique on a stored-program computer. If, for example, in a decimal computer single words are to be sorted on a two-digit key, each value of which is to appear only once, this key may be used as an index to modify a storage address for each word in turn. Thus, if the resultant storage block for ordered information is in locations 1900 to 1999, and if the two-digit key is 65, the machine word corresponding should be sent to location 1965. Even if the information being sorted is in larger blocks, if the keys are unique and occur densely within the entire possible range of key values, a similar technique may be used, either with the blocks them-selves or with their addresses. Duplicate keys, if very few occur, may be handled by signals designating an exceptional case.

PROGRAMMING AND CODING 2-147 With magnetic tape units, (decimal) digital sorting may be achieved by reading from one tape and storing the output on one of ten tapes, each corresponding to a possible digit of the key. With two banks of ten-tape units, the previous output can be used as the next input, with the next successive significant digit being sorted on in order.

The time required for such a digital sort is T

=

t X n,

where T is total time, t is time for one passage of the entire information through the storage, and n the number of digits, or as Seward (Ref. 140) has noted, approximately

T = NA[logr R]

where N is the number of items being sorted, A the access time to read or write items in the storage, the range of the key is from 0 to R, and r is the base or radix used in representing the key.

Sorting by Merging. This technique is that recommended by Goldstine and von Neumann for internal sorting. Sorting by merging consists in taking two or more ordered groups of items and merging them into one ordered group (usually called a string). Figure 18 shows an example of such a merging process.

Goldstine and von Neumann (Ref. 19) have discussed merging in detail in the case where information is stored in the main storage. The flow diagram is shown in Fig. 19. In this case, an item Xi consists of a one-word

Merged

Ordered St . Ordered

String rrg

String NO.~2-( No.2

1 ~ 3 2

3 ~ 7 _ _ _ _ _ _ 8

7 - 8~

11 ~ 11 _ _ _ _ _ _ 13

12

~ ~~::===---=

14

21 14 -17

~17~~

22

21 24

25_______ 22 ~

---=

28

26~ ~~

- - -_________

3 0 . 26

~

- - - - 28 32

31 _ _ _ _ _ _ ~30~

~31~35

32 35

FIG. 18. Example of sorting by merging.

Yes

The bOX-;..!wk...:-Xi71

~WOUld

consist of:

Wl_U

qi+1

and similarly for Wk _ yi+1 and Zr _ W r

FIG. 19. Merging two strings of m and n item<:: each of order p.

~

..j:Io.

00

o (i)

=i

»

r-()

o

~

"

C --i m

;;:0 -0

;;:0

o

(j) :;::c

»

~

~

Z

(j)

PROGRAMMING AND CODING 2-149 key Xi followed by p other words (p is the order of the item). A string S will consist of n items, where n is the length of the string. A string would then contain n (p

+

1) words. The flow diagram describes the merging of two strings S = (Xl, X2, .. " Xn) (of n items) and T = (yI, y2, .. " ym) (of m items) to produce an ordered string R

=

(ZI, Z2, Z3, .. " Zn+m) where each Zi is one of the previous X's or V's such that the keys Xi and yi are now arranged in increasing order. The string Xi would be given by Xi = (Xi, UI i, .. " Upi ), yi by yi = (yi, VI i, .. " vpi), and Zi by Zi = (Zi, WI i, •• " wpi ).

In the general case of merging two strings from two tapes into a third string on a third tape, there would need to be further storage boxes in-cluded in the flow diagram.

Sorting of an arbitrarily ordered string of length N can now be accom-plished by successive merging. This can be accomaccom-plished as follows:

1. Each pair of items in sequence is merged by considering each a string of length one to form a string of length two.

2. Each pair in sequence of strings of length 2v(v = 1, ... ) is merged, using the merging process described above, to yield a merged string of length 2,,+1.

3. When 2,,+1 ~ N and there is only one string, the process is complete.

The fact that N is not exactly equal to 2" can be disregarded by con-sidering the remaining r" = N - 2" elements as a separate string which mayor may not be merged at each stage of the process.

An example of the merge-sort process is given for a general string of 35 items (here merely keys) in Fig. 20.

A non ordered string of items {A 1, A 2, A 3, •• " AN}, each of order p, to be sorted may be manipulated by the flow diagram of Fig. 21. Here the merge routine of Fig. 19, with parameters m and n, is the heart of the process. There will be two indices involved, v, indicating the number of overall mergings that have been completed, and w, the number of mergings of strings of length 2" that have been completed for this value of v. To use the subroutine, the main program furnishes values m", and n"" which are used as m and n in the merge routine, and the addresses of Xl, yI, and ZI.

The resulting merged sequence {Zk} is stored beginning at the address of Xl. (The manipulation of these addresses is not included in the merge routine, for the sake of simplicity, but it is an obvious extension of the flow diagram of Fig. 21.)

For the worst possible case, when the keys are present in exactly the reverse order, the number of comparisons required for sorting n items, C (N), which may be considered a measure of the amount of effort needed to sort using this method, can be shown to be bounded from above:

C(N) ~ N log2 N

Im Dokument 2 COMPUTERS AND DATA PROCESSING (Seite 193-200)