• Keine Ergebnisse gefunden

CODING AND PROGRAMMING A DIGITAL COMPUTER

Im Dokument CONTROL ENGINEERING (Seite 85-93)

3-1. Introduction

Recapitulation. The previous chapter has given the basic foundations necessary for a detailed discussion of how to direct a computer to perform desired computations. A code was defined as a list of instructions and numbers, the instructions being the specific means by which a person tells the computer what to do. The fundamental concept that instruc-tions are written in terms of addresses, the contents of which are the numbers to be operated upon, was emphasized. We described the pro-cedure by which the instructions are called into the instruction register, one instruction at a time and in sequence; and we indicated how the opera-tions are performed by the computer. The use of the binary number system inside the computer and of some kind of shorthand for coding purposes was discussed briefly. We described the processes involved in transforming numbers written on paper into binary pulses inside the computer's memory. And it was noted that a similar process is involved in the reverse transformation from pulses in the computer's memory to typed numbers on a working paper outside the computer.

Importance of Coding to the Engineer. It is important for the digital-computer engineer to be well versed in coding and programming because, first, such knowledge is essential for a clear understanding of the prin-ciples of digital computers; second, occasions often arise when the com-puter engineer will have to run a problem of his own associated with his engineering duties; third, such knowledge is essential in maintaining an existing computer or checking out a new computer; and, finally, the engineer must be capable of evaluating various designs with respect to the final use and application of the computer.

Material Directed to Engineering Needs. The present chapter is the first of three chapters devoted exclusively to programming and coding.

This chapter is concerned mainly with an introduction to coding and instruction formats. The next chapter (Chap. 4) is concerned with sub-routines and the basis of instruction definitions. The final chapter of this group (Chap. 5) considers the principles of automatic programming.

These chapters are directed to the digital-computer engineer and the problems he will face in his work. The engineer must grasp the funda-mental ideas involved, and his training cannot be restricted to a single type of computer instruction format. He must understand the principles

60

SEC. 3-1] CODING AND PROGRAMMING A DIGITAL COMPUTER 61 behind the definitions of many kinds of instructions, so that he may design new ones and evaluate proposed ones. He must also understand the basis for automatic programming so that he may use or write such programs intelligently in solving engineering problems or in performing computer maintenance. Programming and coding are therefore a funda-mental aspect of computer engineering.

Manner of Presentation. In successive sections of this chapter we consider instructions that include explicitly first four addresses, then only three, then two~ and finally only one address. We begin with the four-address system because this format includes all the necessary ingre-dients of an instruction. Then, by omitting one address in the instruction format, we arrive quite naturally at a three-address system, and so forth.

As each address is omitte<;l from the instruction format, it must be compensated for by corresponding additional computer circuitry or by using several instructions where one sufficed before. In this way the advantages and disadvantages of various basic instruction formats are clearly observed.

Examples will show that, the smaller the number of addresses per instruction, the greater the ingenuity and work required of the coder, the larger the list of instructions that must be available to the coder, and the longer the equivalent codes. On the other hand, as the number of addresses per instruction is increased, the number of possible addressable memory locations is decreased and the words are made longer. In order that the material be as simple as possible at first. Only sequences of instructions and decisions are illustrated. This leaves the student free to concentrate on the fundamental properties and uses of the instruc-tion formats. Considerainstruc-tion of those most important and fundamental programming techniques of loops (i.e., recursions, iterations) and sub-routines is left to Chap. 4. In that chapter also, a large number of more complicated and specialized instruction or operation types are considered in detail. We believe that the student will more easily understand the principles of programming if consideration of these some~

what sophisticated ideas is postponed. Any discussion of ~oding and programming must be preceded by a study of number systems, as will be found in the next sections of this chapter.

Stages in Coding. As will be seen, there are five stages in coding.

First the computations to be performed must be clearly and precisely defined. The over-all plan of the computations is diagramed by means of a so-called" flow chart." The second stage is the actual coding.

It is often best to write a code in terms of a symbolic language first, for then changes are easily made. N umbers are assigned to the symbols, and the final code is prepared. In the third stage some procedure ii:l

used to get the code into the memory of the computer. The fourth stage consists in "debugging" the code, i.e., detecting and correcting any errors. The fifth and final stage involves the running of the code on the computer and tabulating the results. In this chapter and the next only the first two stages are considered. Techniques for the- fourth stage are described in Chap. 5.

Philosophy of Coding. As will become abundantly clear in the follow-ing sections, a sfollow-ingle error in one instruction invalidates the entire code.

Hence coding is an exacting technique, requiring attention to details without losing sight of the over-all plan. Learning to code must be developed from within a person. Following through each instruction of the examples given in this chapter is important, but it is not sufficient.

The student must code problems by himself if he is to become aware of all the detailed thinking and reasoning associated with the coding technique. The method of teaching in this text is through example.

The 3tudent should try to do the examples himself, using the text only as a guide to the methods involved.

3-2. Number Systems: Conversion

Number Systems and the Radix. Preliminary to any discussion of coding should come an analysis of various number systems. In Sec. 2-4 we noted that 8,675 really means 8 X 103

+

6 X 102

+

7 X 101

+

5 X

10°, where 100 = 1. Since decimal numbers are expanded as illustrated in powers of ten, we say that the decimal number system has a radix of ten.

This concept can be generalized to define number systems based on any positive-integer radix q: if a number N is based on radix q, then it can be expanded as

for integer N 2:: 1, or

.for 0 < N

<

1, where ao, aI, . . . , a_I, a_2, are nonnegative inte-gers each less than q. Of course, for noninteger N

>

1, the expansion contains both the positive and negative powers,

+

a2q2

+

aIq1

+

aoqO

+

a_1q-1

+

a_2q-2

+ . . . +

a_mq-m For example,

8,675.8675 = (8 X 103)

+

(6 X 102)

+

(7 X 101)

+

(5 X 10°)

+

(8 X 10-1)

+

(6 X 10-2)

+

(7 X 10-3)

+

(5 X 10-4)

that is, a3 = 8, a2 = 6, al = 7, ao = 5, a_I = 8, a_2 = 6, a-3 = 7, and a_4 = 5. Even though we are most familiar with the system for-q= ten, -history records ancient civilizations that used q = six, q = twelve, and

SEC. 3-2] CODING AND PROGRAMMING A DIGITAL COMPUTER 63 even q = sixty! For our purposes we are most interested in q = 2 (binary system), q = 8 (octal system), and q = 16 (sexadecimalt system).

First note that for radix q the number system must involve q symbols.

For example, for q = ten, the ten symbols are 0, 1, 2, 3,

4-,

5, 6, 7, 8, and 9; for q = 2, the 2 symbols are

°

and 1; for q = 16, the 16 symbols most often used are 0,1,2,3,4-,5,6,7,8,9, A, B, C, D, E, andF. Then, if N = anqn

+

an_Iqn-1

+ .• . . +

a2q2

+

alql

+

aoqO, where the ai are of course chosen from the q symbols, the number is conventionally written by juxtaposition of the coefficients ai as follows: N = anan_l . . . a2alaO.

If N = a_Iq-1

+ ... +

a'-mq-m, then the number is usually written by juxtaposition of the ai : preceded by ·[.J-that is, .a_Ia_2 . . . a_m •

For noninteger N

>

1 we would have an . . '. aO.a-1 . . . a_m • With this in mind, counting in the four different systems mentioned above is accomplished as in Table 3-1.

Conversion from One Number System to Another. Using number sys-tems based on different radices is like talking in different languages.

Just as one can translate one language into another, so also one can

"convert" from one radix to another. For ex~mple, in Table 3-1 num-bers on the same row are equivalent-that is, I

28 (decimal) = 11100 (binary) = 34 (octal) = 1 C (sexadecimal) The binary, octal, and sexadecimal equivalents of the decimal numbers from 0 to 15 should be memorized.

In order to derive a systematic method for converting from one num-ber system to another, we' make us~ of the well-known result that if Nand q are positive integers then there always exists a unique (non-negative) integer r less than q and a unique integer S such that

N r

- = S

+-q q O<r<q

Let us apply this to convert an integer number with radix p, say N p, to its equivalent with radix q, say N q. In other words, we wish to deter-mine the nonnegative integers ao, aI, . . . ,an, each less than q, such that

Observe that

Np " ' ao

- = anqn-l

+ . . . +

a2ql

+

alqo

+

-q , q

Integer' Fraction

So

+

ro q

Since the quotient and rema,inder are each unique, equating the fractional t Also less correctly called hexadecimal.

Decimal Binary. Octal Sexadecimal (q = 10) (q = 2) (q = 8) (q = 16)

0 0 0 0

1 1 1 1

2 10 2 2

3 11 3 3

4 100 4 4

5 101 5 5

6 110 6 6

7 111 7 7

8 1000 10 8

9 1001 11 9

10 1010 12 A

11 1011 13 B

12 1100 14 C

13 1101 15 D

14 1110 16 E

15 1111 17 F

16 10000 20 10

17 10001 21 11

18 10010 22 12

19 10011 23 13

20 10100 24 14

21 10101 25 15

22 10110 26 16

23 10111 27 17

24 11000 30 18

25 11001 31 19

26 11010 32 1A

27 11011 33 IB

28 11100 34 1C

29 11101 35 1D

30 11110 36 1E

31 11111 37 1F

32 100000 40 20

33 100001 41 21

34 100010 42 22

35 100011 43 23

36 100100 44 24

64

SEC. 3-2] CODING AND PROGRAMMING A DIGITAL COMPUTER 65 parts we have ao

=

ro and So

=

anqn-l

+ ... +

a2ql

+

alqo.

Divid-ing So by q and usDivid-ing our above results, we obtain

whence al = rl, and so forth.

Hence the systematic conversion method is as follows: Divide Np by q;

the remainder is ao. Divide the resulting quotient by q; the remainder is at. Divide the resulting quotient by q; the remainder is a2; etc. As an example, we have converted 28 (decimal) into binary (q = 2) in Table 3-2. Hence,

28 (decimal) = 11100 (binary)

TABLE 3-2. DECIMAL TO BINARY CONVERSION (N p ;::: 1) Remainders

2~

2/14 o = ao 2~ o = al 2@. 1 = a2 21~ 1 = a3

0 1 = a4

The work can be more conveniently arranged from right to left, as follows:

Divide by 2

t Place remainder

o - 1 - 3 - 7 - 14 - 28~

J t t t t

1 1 1 0 0 Remainders

As another example, let us convert 28 (decimal) to octal (q = 8),

o

~3 ~28~

l l

3 4 Remainders whence the converted octal number is 34.

To convert 28 (decimal) to sexadecimal (q = 16),

0~1 ~28~

1

a

Hence the converted sexadecimal number is

1a.

[Here, of course, 28 - 16 = 12 (decimal) =

a

(sexadecimal) from Table 3-1.]

N ext consider 0

<

N p

<

1. We wish to find nonnegative integer

'coefficients a_I, a_2, . . . ,a_m, each less than q, such that Np = a_Iq-1

+

a_2q-2

+ ... +

a_mq-m'

Here qN p = a_I

+

a_2q-1

+ . . . +

a_mq-m+1

=

U-I

+

V-I

where U-I is the integral part and V-I is the fractional part of qN p; hence and

Again whence

U-I = a_I

V-I = a_2q-l+ . . .

+

a_mq-m+1

qV-:-I = a_2

+

a_3q-1

+ . . . +

a_mq-m+2 = U-2

+

V-2

U-2 = a_2, etc.

The conversion method thus is as follows: Multiply Np by q; the integral part is a-I. Multiply the resulting fractional part by q; the integral part is a_2; etc.

As an example, we have converted 0.28 (decimal) to binary (q = 2) m Table 3-3. Hence 0.28 (decimal) = .01000111 . . . (binary). The

TABLE 3-3. DECIMAL TO BINARY CONVERSION (0 < Np < 1) Integral parts

0.28

X 2

a_I = 0 .56

X 2

a_2 = 1 .12

X 2

a_3 = 0 .24

X 2

a_4 = 0 .48

X 2

a_5 = 0 - -.96

X 2

a_6 = 1 .92

X 2

a_7 = 1 - -.84

X 2

a_8 = 1 .68

work can be more conveniently arranged from left to right, as follows:

Multiply by 2 Place integral !

part

2 X 0.28 --) .56 --) .12 --) .24 --) .48 --) .96 --) .92 --) .84 ~ .68

! ! ! ! ! ! ! !

Integral parts 0 1 0 0 0 1 1 1

SEC. 3-2] CODING AND PROGRAMMING A DIGITAL COMPUTER 67 Note that the process does not necessarily end, as it must for the inte.,.

gral numbers, but may be carried out to any number of significant figures. Conversion to the octal and sexadecimal systems is similarly accom plished.

For nonintegral numbers greater than unity the integral part and the fractional part are done separately as just described.

Note that in converting from binary to octal (or sexadecimal) we divide, or multiply, the binary number by binary 100 (or 1000), which is equivalent to shifting the fraction point to the left, or to the right, by three (four) binary places. Hence for binary to octal conversion we start at the point and count off the binary bits in groups of three, to the left and to the right. Then we write the octal equivalent for each of these groups of three bits. For example, if the binary number is 11100.110100011, the octal equivalent is

3 011

4.6 100.110

4 3 100 011

To convert to sexadecimal, we count off similarly by groups of four.

For the same binary number the sexadecimal number is 1

0001

C.D 1100.1101

1 0001

(See Table 3-1 for binary-octal and binary-scxadecimal equivalents.) The converse will of course hold.

Converting from Binary to Decimal. Another conversion method, par-ticularly useful for converting from binary to decimal, is based on the following factorization, illustrated for n = 4:

N = a4q4

+

a3q3

+

a2q2

+

alql

+

aoqO

= (((a4q

+

a3)q

+

a2)q

+

al)q

+

ao

If N is a binary integer, then q = 2 and the a's are either 0 or 1, but a4, the most significant coefficient, must be 1. Hence, to convert N to decimal, start out with 1, and double it, obtaining 2; then add ag, and double the result; add a2, and double the result; add aI, and doub~e

the result; finally add ao. For example,

1 0 1 1 1 (((2

+

0)2

+

1)2

+

1)2

+

1

= ((4

+

1)2

+

1)2

+

1

= (10

+

1)2

+

1

= 22

+

1 = 23

The method is to accumulate the result while calculating, and the work

from left to right might look like this:

Similarly for N such that

1

Double/, /Add

1 0 1 1 1 = 23

l/'l/ l / l /

2 4 10 22

N = a_Iq-1

+

a_2q-2

+

a_3q-3

+

a_4q-4 we have the factorization

If N is given in binary, then q = 2 and the a's are either 0 or 1. The decimal conversion is calculated by starting with the least significant unit (for example, a_4 in our case), working toward the left as follows:

Add a-3 to .5, and halve the result; add a_2, and halve the result; add a_I, and halve the result. For example,

.1011 = %(1

+

%(0

+

}2(1

+

0.5))

= %(1

+

Y2(0

+

0.75»

= %(1

+

0.375) = 0.6875

Of course the method is to accumulate while calculating, and the work from right to left might be arranged as follows:

Im Dokument CONTROL ENGINEERING (Seite 85-93)