• Keine Ergebnisse gefunden

Decimal Conversion30

Im Dokument UPDATE NOTICE (Seite 158-164)

User Operations

2.13 Decimal Conversion30

Included among the string instructions are four for converting between binary and decimal. The binary is always a twos complement, double length binary integer in the format given in Q1.4: the magnitude is the 70- bit string in bits l-35 of the two words, bit 0 of the high order word is the sign, and bit 0 of the low order word is a copy of the sign but is never used in any operation. The decimal is a string of bytes representing decimal digits (the reader should be familiar with the general information and cau- tions about strings presented at the beginning of the previous section). To be capable of conversion to double length binary, a decimal string can have a maximum of twenty-two significant digits, although the string may be longer because of the presence of leading zeros or nonnumeric characters.

The decimal value corresponding to the binary maximum of 270 is 1 180 591 620 717 411 303 424.

The four instructions are for converting with offset or translation in the two directions. All are two-word instructions, where the first has the EXTEND code 123, and all use a block of accumulators. Decimal to binary uses five accumulators, and binary to decimal requires a block of six, but one within the block is not used.

2-98 User Operations

CVTBDO Convert Binary to Decimal Offset

I

123 A I X Y

0 89 121314 17 18 35

I:‘0 012 I 00 111 X 1 Y 1 Bits 9-12 = 0.

Lo+1 FILL

0 8 9 12 13 14 17 18 35

CVTBDT Convert Binary to Decimal Translated

1 123 A / X Y

0 89 I2 1314 I7 I8 3s

‘E-0

013 00 1 x Y Bits 9-l 2 = 0.

1

‘E-0+1 I I FILL

1

0 8 9 12 I3 I4 I7 In 35

Convert the magnitude of a double length binary integer into a decimal digit string, offset or translated. The integer is given and the string space defined by the contents of a block of six accumulators.

AC AC+1 AC+2 AC+3 AC+4 AC+5

DOUBLE LENGTH BINARY INTEGER

NOT USED

LNM 00

STRING LENGTH Bits 3-8 = 0.

STRING BYTE POINTER

0 1 2 9 3s

Determine the number of decimal digits required to convert the binary integer, and if this number is greater than the string length given by AC+3 bits 9-35, go on to the next instruction without affecting the string space or the accumulators in any way. 32 Note that the string length must specify a minimum of one digit byte even if the binary number is zero, for to repre- sent zero in decimal requires at least the digit “0” (a string with no bytes cannot represent anything - not even zero). If the converted integer will fit in the defined string space, continue as follows.

If the binary integer in AC,AC+l is not zero, set N, if it is less than zero, set

M

(minus). If the number of digits required is less than the given string length and

L

is 1, place the leading fill character from EO+l in the excess positions at the left in the string space. This action causes the result to be right justified. Clear AC+3 bits 9-35.

32 Caution: In the KLlO the N and M flags are set up first and may therefore be affected even by an instruction that is aborted because the binary integer is too large.

User Operations 2-99

Compute each decimal digit for a positive representation of the magni- tude of the binary integer (highest order first), and for each do one or the other of the following two operations depending on which instruction is being performed.

If the instruction is CVTBDO, add El to the computed digit algebrai- cally (bit 18 is the sign).

If the instruction is CVTBDT, for the digit substitute a byte from the right half of location E1+D in the translation table, where D is the value of the digit, unless this is the last digit in the conversion, in which case make the substitution from the right half of the location if M is 0, but from the left half if M is 1.

Place each offset or translated byte in the next position in the string space, compute the next digit, and continue as described above. When the conversion is complete - all digits computed, offset or translated, and de- posited - clear AC and AC+l, and skip the next instruction.

At the end the byte pointer points to the last byte deposited in the string space, and AC, AC+l, and AC+3 bits 9-35 all contain zero. If unused interior bits in the string are clear initially, they are left clear; otherwise unused interior destination bits are indeterminate. The source string is unaffected.

Notes. The translation table, which starts at EI, contains ten locations for the decimal digits, each with substitute bytes in both half words, but the left half is only for the final digit. This allows the program to use a different final byte for a decimal string converted from a negative number. Note that setting N is just to indicate that the number converted is not zero; the state of the flag has no effect on the execution of the instruction.

--

CVTDBO Convert Decimal to Binary Offset

123 A I X Y I

0 89 I2 13 14 17 18 35

L-0 010 1 00 1/[ x 1 Y Bits 9-12 = 0.

0 a 9 12 13 14 17 18 35

CVTDBT Convert Decimal to Binary Translated

r

0 123 89 A II I3 I 14 X I7 I8 Y 35 I

L-0 011 00 I x Y Bits 9-12 = 0.

0 6 9 12 I3 I4 17 18 35

Convert a decimal string, offset or translated, to a double length binary integer. A block of five accumulators is used for defining the decimal string and receiving the binary result.

2-100 User Operations

AC

CAUTION

It is up to the programmer to keep track of the size of the decimal number - the hardware runs no test on the string. If there are too many significant digits, the most significant part of the binary is lost, and the processor gives no indica- tion of it.

The conversion is regarded as complete only when all bytes of the decimal string have been processed without causing a termination or gen- erating a digit outside the range O-S. Upon completion negate the accumu- lated binary if M is 1, place the result (negated or not) in AC + 3,AC +4, and skip the next instruction.

At the end the byte pointer points to the last byte read from the deci- mal string, and AC bits 9-35 contain the number of unprocessed bytes left in the decimal string (if any). The string itself is unaffected.

The translation table starts at location EI, and since there are two functions per word, it contains 2”-l locations, where n is the number of bits in a byte. The address is generated by adding the left n - 1 bits of a byte to El.

Notes. CVTDBO always sets S immediately, but in CVTDBT its setting is controlled by the translation functions. Hence an instruction can skip over leading fill characters or nonnumeric characters preceding the decimal part of a string. If an interrupt or page failure occurs during this instruc- tion, the number of bytes yet to be processed is put in AC bits 9-35, and the partial binary accumulated so far is placed in AC + 3,AC + 4. Thus when the instruction resumes after an interruption with S set, it simply continues where the conversion left off, adding the next digit to ten times the binary previously saved. If the programmer wishes to preset S to add the decimal string to a significant binary base already in AC + 3,AC +4, he must be aware that the base is multiplied by ten before the first digit is added.

For a decimal string a&de, the evaluation procedure is (((a x 10 + b) x 10 + c) x 10 + d) x 10 + e which is equivalent to

exl +dxlO + c x 100 + b x 1000 + a x 10000

Of course the operations are all done in binary arithmetic.

Translation functions manipulate M, but the program can set it prior to either instruction to indicate the decimal string represents a negative number. N can also be preset or manipulated through the translation table, but its state has no effect on the execution of the instruction.

--

2-102 User Operations

For decimal strings with 4-bit digits, conversion can be done by CVTBDO or CVTDBO with a zero offset. But note that decimal bytes need not be four bits: they can be larger using any decimal code provided only that on conversion to binary they are in the range O-9 (o-1001 binary) after offset or translation.

In ASCII numeric strings, the bytes representing the digits are 60-71 octal. Conversion to ASCII decimal would be by CVTBDO with offset 60 (48 decimal), and CVTDBO with offset -60 would convert in the opposite direc- tion. Consider an ASCII string containing decimal numbers of various un- known lengths separated by semicolons (ASCII code 73). The program could convert all of these numbers to binary by specifying a constant, but suit- ably large, string length while giving a sequence of CVTDBOs with offset -60. Each conversion would terminate (nonskip) upon encountering a semi- colon, as its offset value is 11 decimal. Between conversions the program would have to store away the result and clear S by a sequence like this.

EXTEND AC,[CVTDBO O,-601

DMOVEM AC + 3,VALUEl

TLZ AC,700000

EXTEND ACJCVTDBO O,-601

DMOVEM AC + 3,VALUE2

TLZ AC,700000

Convert Store result

;Reset SNM

If there were very many numbers, the program would naturally use only one of the above sets of three instructions in a loop, along with some mecha- nism to change the storage address and test whether to reiterate. The pro- cedure cannot of course provide a negative result. If the same situation were handled by translation, the table would not actually start at El - it would run from El + 30 to El + 35.

User Operations 2-103

Im Dokument UPDATE NOTICE (Seite 158-164)