<<

HOW THINGS WORK

If a computer used an address of one , it would have a main store of 256 Binary with addresses from 0 to 255 (decimal) or 00 to FF (hex). A 2-byte address would allow addresses from 0 Arithmetic to 65,535 or 0000 to FFFF, a 3-byte address 0 to 16,777,215 or 000000 to FFFFFF, and so on. Nowadays, com- Neville Holmes, University of Tasmania puters usually have addresses larger than this. However, a one-byte address will serve here to illustrate logical arithmetic. The main operation of logical arith- metic is . Addition is a series of steps starting with an augend and The basic aspects of computer an addend. For simplicity, we use an 8- byte to illustrate the process, arithmetic are how although 32-bit and 64-bit words are are represented and the more common today. Each step computes three new quan- operations performed on tities: an augend, a carry, and an those representations. addend. The steps use all n of the operands in parallel (here n = 8). The new augend bit is 1 if and only if (denoted iff) one and only one of the igital technology works so sidered as divided into nibbles of two incoming bits is a 1 (that is, iff cor- well because at the heart of hexadecimal digits—hex digits for responding bits of the augend and digital representation there short. Table 1 lists binary digits and addend are 01 or 10). The new carry bit are only a few basic compo- their hexadecimal equivalent. is 1 iff both incoming bits are 1. The D nents. Nowadays, these basic For clarity, the hex digits beyond 9 adder forms the new addend by shift- components usually are binary digits, are sometimes spoken as able, baker, ing the carry bits one position to the left bits for short, called binary because charlie, dog, easy, and fox. and putting a 0 in the right-most bit they are designed to stand for only The value that any byte stores can position. The adder repeats the three two different values, conveniently then be shown as a pair of these hex computations of a step (new augend, called zero and one. A stored or trans- digits. Strings of bytes can represent carry, and addend) until all the carry mitted bit’s state can deteriorate quite anything you need represented digitally. bits are 0. Figure 1 illustrates the exam- badly before a processing device will Being able to represent anything de- ple of adding 85 and 103 (decimal). be mistaken in deciding which of the pends on having adopted a convention two possible values is the original. for representing things of that . Quirks Most modern computers store the The earliest and still quite popular The time taken for a straightfor- data uniformly in blocks in their main class of thing to be represented is num- ward addition depends on the store. The blocks are numbered serially bers, though properly speaking these of steps needed, which can vary so that each block has its own num- are not things but properties of things. widely. However, shortcuts can re- ber—its address. Thus, each block has The numerical operations that a com- move this dependency. two aspects: its address and its content. puter carries out on numbers as num- When adding two numbers, the Addresses are a simple sequence of ser- bers are together called its arithmetic. high order carry bit could be a 1. This ial numbers. The content located at a is simply lost in shifting. The effect is specific address is the value of its bits, ADDRESS ARITHMETIC like the hour-hand of a clock passing usually eight in number and called a Working with a computer’s data through 12, except that in the hexa- byte, divided into two groups of four addresses requires only the simplest decimal example the clock would have contiguous bits called nibbles. arithmetic, called address arithmetic 256 hours labeled 00 to FF. As each bit can independently be or logical arithmetic. The arithmetic is Circuitry could be provided for sub- either 0 or 1, a byte can hold 28 or 256 simple because there is only a limited traction, but it’s simpler to add the different values overall. There are too number of addresses and these are complement. For a hexadecimal clock, many of these to conveniently give a unsigned—that is, never negative— this hinges on 1 back, say, being the name to each, so bytes are usually con- and start at zero. same as 255 forward, and vice versa.

90 Computer Table 1. Hexadecimal digits. Step Operand Binary Hex Decimal

Binary Hex Binary Hex Add decimal 85 + 103 Augend 01010101 55 85 0000 0 1000 8 Addend 01100111 67 103 0001 1 1001 9 New augend bit is 1 iff incoming bits are 01 or 10 0010 2 1010 A 1a Augend 00110010 32 50 0011 3 1011 B Carry bit is 1 iff incoming bits are 11 0100 4 1100 1b Carry 01000101 45 69 0101 5 1101 D New addend is now the left-shifted carry with 0 fill 0110 6 1110 E 1c Addend 10001010 8A 138 0111 7 1111 F Repeat the three substeps until the carry is all zeroes 2a Augend 10111000 B8 184 2b Carry 00000010 02 2 For the binary representation of a 2c Addend 00000100 04 4 byte, the complement is its bitwise Repeat the three substeps until the carry is all zeroes complement with a one added in to 3a Augend 10111100 BC 188 the lowest position. 3b Carry 00000000 00 0 Logical arithmetic is error free. An Finished because carry is all zeroes; answer is in the last augend address can be invalid, but only because the main store is not big Figure 1.Address arithmetic example.The arithmetic is simple because there is only a enough to hold a value at every possi- limited number of addresses and these are never negative. ble address, a fault that virtual addressing eliminates. Example Binary Hex Decimal

INTEGER ARITHMETIC (a) 85 + 39 = 124 When possible values are quantita- Augend 01010101 55 85 tive and not indicative, the arithmetic Addend 00100111 27 39 must be able to handle negative num- Result 01111100 7C 124 bers and to multiply and divide with (b) 85+ (–39) = 46 them. Augend 01010101 55 85 An early method of representing Addend 11011001 D9 –39 negativity was to use the leading bit as Result 00101110 2E 46 a simple arithmetic sign, but this raised (c) (–85) + 39 = –46 the ambiguity of having two different Augend 10101011 AB –85 zeroes, one negative and one positive. Addend 00100111 27 39 The usual method nowadays is to Result 11010010 D2 –46 offset the values in such a way that the (d) (–85) + (–39) = –124 arithmetic is not directly concerned Augend 10101011 AB –85 with negativity, but the leading bit Addend 11011001 D9 –39 nevertheless acts as a negative sign. Result 10000100 84 –124 Thus the values for a single byte binary integer range from -128 (80) to Figure 2.Integer addition examples.The values for a single byte binary integer range -1 (FF) and from 0 (00) to 127 (7F). from -128 (80) to -1 (FF) and from 0 (00) to 127 (7F). The four examples of addition in Figure 2 illustrate this, showing that and magnitude are typically also pro- results in an apparently negative num- the operation is practically the same vided in a computer’s instruction . ber, then the correct sum is too long as for logical arithmetic, even though for the result register to hold. This is the meanings have been changed. Quirks called an overflow and must be sig- is carried out by adding Just as for address arithmetic, the naled so that a program can deal with the negation of the subtrahend. results are exact if they will fit into the this exceptional result. An apparent is carried out by space provided for the result in the nonnegative result from adding two repeated addition, and by result register. However, because the negative numbers is also an overflow. repeated subtraction. Shortcuts are values are no longer cyclic, the result Negation and magnitude of the lowest used to speed up these operations. of an addition can be longer than that representable number will also cause Simple operations such as negation space. If adding two positive numbers an overflow, which will need to be

June 2007 91 HOW THINGS WORK

called the “hidden” bit. The signifi- Sign Exponent Significand Binary Decimal cand is termed “normalized” because 0 1000 0000 01000000. . . 0 +1.01 2 128-127 (1 + .25) 21 = 2.5 its arithmetic value is always less than 1 1000 0001 11000000. . . 0 –1.11 2129-127 –(1 + .5 + .25) 22 = –7 2 but not less than 1. 0 0111 1111 00000000. . . 0 +1 2127-127 1 In floating-point multiplication the exponents are added and the signifi- Figure 3.Examples of scaled values.The most significant bit is the sign of the number (0 cands multiplied. The exponent and indicates positive,and 1 indicates negative).The next eight bits are the exponent in base significand of the result might need 2,expressed as an integer.The 8-bit exponent value is biased by +127,which makes the slight adjustment to bring the integer representable range –127 to +128.The significand is termed “normalized”because its part for the hidden bit back to 1 arithmetic value is always less than 2 but not less than 1. before the leading 23 fraction bits of the product’s significand, perhaps with signaled. In the example using eight When the unexpected proved all too , are stored in the result. bits, –128 cannot be negated because frequent, users built scaling into their Division is handled in much the same +128 is larger than the largest positive programs but, because this was very way, but using subtraction on the number representable in eight bits slow, scientific computers soon did exponents and full division (without (+127). their scaling in hardware. Rather remainder) on the significands. While a program can satisfactorily unfortunately, such arithmetic and the Addition and subtraction are quite deal with an occasional overflow, sim- representation of scaled values came complex because the values of the ple multiplication would overflow far to be called floating-point arithmetic exponents must be used to adjust the too often to be tolerated. The usual way and numbers. The adjective semilog- alignment of the two significands to deal with this is to couple two result arithmic is sometimes used for clarity. before the addition or subtraction can registers to provide a double-length A floating-point number has three take place. For further explanation product. It’s then up to the program to parts: the base b, the scale or expo- of floating-point arithmetic, see en. use the two halves appropriately. nent e, and the significand s. The value wikipedia.org/wiki/floating_point. Integer division is more complicated represented is s be. The exponent still. First, while the quotient, as an and the significand are variable in Quirks integer, usually can be accommodated, floating-point representations, but the The most significant quirk of scaled the division will leave a remainder, so base is fixed. Scaled values are printed arithmetic is the loss of exactness. two result registers are needed. out using a base of 10, so 45E6 rep- Basic laws of arithmetic no longer Second, the quotient can in effect resents 45,000,000 and 4.5E–6 rep- hold. For example, overflow when the divisor is zero or resents 0.0000045. Internally, most when the lowest representable integer computers use binary floating-point (a + b) – a = a + (b – a) = b is divided by negative one. arithmetic and representation in which the base is 2. in exact arithmetic but, because the SCALED ARITHMETIC IEEE Standard 754 for Binary significand of any result needs to be In scientific and engineering com- Floating-Point Arithmetic specifies the truncated or rounded in floating-point puting, and divisions format of floating-point numbers for arithmetic before it is stored, the result are as frequent as and sub- both single-precision (32-bit) and of (a + b) – a might not be the same as tractions. Repeated multiplication, as double-precision (64-bit) representa- that of a + (b – a). Such errors can in polynomial evaluation, is common. tions. For simplicity, we consider only accumulate significantly when a pro- Such computation requires scaling. the single-precision format. The stan- gram carries out trillions of floating- Before electronic computers became dard says that the most significant bit point operations. Sophisticated use of available, scientists and engineers com- is the sign of the number (0 indicates arithmetic can avoid this prob- monly used slide rules and positive, and 1 indicates negative). lem, but this requires the type of tables for multiplication and division, The next eight bits are the exponent rounding to be selectable so that it can and they did scaling mentally or with in base 2, expressed as an integer. But preserve the interval properties. the help of pencil and paper. Although because the number’s true exponent Because the significand is normal- Konrad Zuse’s early computers used must be allowed to be positive or neg- ized, there is no straightforward way automatic scaling, later machines, such ative, the 8-bit exponent value is to represent zero. A tweak is needed. as John von Neumann’s IAS computer, biased by +127, which makes the rep- Once this tweak is provided, the var- did not. This forced the programmer resentable range –127 to +128. The ious possible results of division by to anticipate what scaling would be remaining 23 bits are the mantissa, zero need tweaks in turn to represent needed, although the IAS machine also called the significand. Those 23 them. For example, different repre- used 40-bit numbers to protect against bits are used as fraction bits appended sentations are needed for 0 ÷ 0, 1 ÷ 0 the unexpected. to an implied integer of 1, sometimes and –1 ÷ 0. The arithmetic’s various

92 Computer operations must be able to handle all called is crucial. of scaled arithmetic from integer arith- these special values in combination Validated numerics can provide metic. This isolation means that pro- with each other and with ordinary val- solutions to most scientific computa- grammers must make decisions about ues. To handle these special cases, tions with certainty. Prominent among which to use when coding programs, exponent values of all zeroes and all its techniques is a sophisticated appli- and making such decisions is not ones are reserved to signal special val- cation of interval arithmetic, arith- always easy. Also, they might need to ues such as 0, denormalized numbers, metic that works with paired values write several versions of a computa- infinity, and not-a-number. Thus, for that specify the bounds within which tional function for different represen- ordinary arithmetic the exponent an entity’s value must lie. Control of tations of its arguments. An early actually only has a range of –126 rounding type ensures that values stay Burroughs mainframe computer in to +127. within their bounds. Mathematicians which an integer represesentation Floating-point arithmetic is not only can design algorithms so that conver- would switch to scaled rather than sig- still subject to overflow when a result gence of intervals is proof that the naling an overflow implemented such becomes too large to represent, but a solution is completely valid. Complete an arithmetic. result also can be too small to repre- arithmetic can greatly speed up con- sent, an exception called underflow. vergence, and it can induce conver- gence that would not be possible with urther discussion of these possi- COMPLETE ARITHMETIC traditional floating-point arithmetic. bilities can be found in “Com- Traditional floating-point arithmetic F posite Arithmetic” (Computer, tolerates the introduction of error, but Quirks Mar. 1997, pp. 65-73). Ulrich the errors tend to accumulate in unpre- While complete arithmetic can Kulisch’s Advanced Arithmetic for dictable ways. In the past, providing greatly reduce the incidence of over- the Digital Computer: Design of longer and longer representations less- flows and underflows, it cannot com- Arithmetic Units (Springer-Verlag, ened the error, but this is a losing bat- pletely eliminate them. Valid results 2002) provides a description of com- tle as computers become faster and can be too large or small to be repre- plete and interval arithmetics and their faster and problems larger and larger. sented even in a complete result regis- implementation. ■ The result of a large scientific compu- ter—for example, in the unlikely event tation might now need many trillions of repeated exponentiation of extreme Neville Holmes is an honorary research of floating-point operations. values. Such invalidity is not of prac- associate at the University of Tasmania’s Such computations typically include tical concern, however, because it can School of Computing. Contact him at focused subsections traversing very be completely avoided in its most par- [email protected]. large arrays of values to arrive at only ticular use—the ubiquitous scalar or a few results such as the sum of prod- dot product. A more significant prob- ucts. The truncation error within such lem is the inability to fit extreme com- Computer welcomes your submis- subsections can be unpredictably plete results into standard floating- sions to this bimonthly column. large, but it can be eliminated by using point formats. For additional information, or to a result register large enough to keep Large arrays of complete results suggest topics that you would like the complete result, which is exact. need large amounts of storage space to see explained, contact column With computers as capacious as and time to store and fetch, although editor Alf Weaver at weaver@cs. today’s, that exact result can then be using a variable-length format for virginia.edu. kept as is for intermediate results, external storage of complete results pressing it into floating-point format could greatly reduce both the space only for final results. and time needed. Nevertheless, pro- Complete arithmetic was available grams need to keep down the number more than 20 years ago as a special of times they convert complete results Join the feature for an IBM mainframe com- to floating-point format. puter, but was later removed from An arithmetic and representation IEEE sale. Perhaps it was before its time, as such as symmetric-level indexing, implementation in a microprogram which compares to semilogarithmic Computer Society was relatively slow. arithmetic somewhat the way semi- online With today’s integrated circuitry, logarithmic compares to integer, can complete arithmetic has become quite eliminate overflow and underflow. at practical and has been satisfactorily The drawback is the severe complex- implemented on special chips. Its ity of arithmetic operations. widespread adoption is overdue as its Perhaps a more serious problem www.computer.org/join/ support for a branch of computation with binary arithmetic is the isolation

June 2007 93