| 1 | .RP |
| 2 | ....TM 75-1271-8 39199 39199-11 |
| 3 | .TL |
| 4 | DC \- An Interactive Desk Calculator |
| 5 | .AU "MH 2C-524" 3878 |
| 6 | Robert Morris |
| 7 | .AU |
| 8 | Lorinda Cherry |
| 9 | .AI |
| 10 | .MH |
| 11 | .AB |
| 12 | DC is an interactive desk calculator program implemented |
| 13 | on the |
| 14 | .UX |
| 15 | time-sharing system to do arbitrary-precision |
| 16 | integer arithmetic. |
| 17 | It has provision for manipulating scaled fixed-point numbers and |
| 18 | for input and output in bases other than decimal. |
| 19 | .PP |
| 20 | The size of numbers that can be manipulated is limited |
| 21 | only by available core storage. |
| 22 | On typical implementations of |
| 23 | .UX , |
| 24 | the size of numbers that |
| 25 | can be handled varies from several hundred digits on the smallest |
| 26 | systems to several thousand on the largest. |
| 27 | .AE |
| 28 | .PP |
| 29 | .SH |
| 30 | .ND |
| 31 | .PP |
| 32 | DC is an arbitrary precision arithmetic package implemented |
| 33 | on the |
| 34 | .UX |
| 35 | time-sharing system |
| 36 | in the form of an interactive desk calculator. |
| 37 | It works like a stacking calculator using reverse Polish notation. |
| 38 | Ordinarily DC operates on decimal integers, but one may |
| 39 | specify an input base, output base, and a number of fractional |
| 40 | digits to be maintained. |
| 41 | .PP |
| 42 | A language called BC [1] has been developed which accepts |
| 43 | programs written in the familiar style of higher-level |
| 44 | programming languages and compiles output which is |
| 45 | interpreted by DC. |
| 46 | Some of the commands described below were designed |
| 47 | for the compiler interface and are not easy for a human user |
| 48 | to manipulate. |
| 49 | .PP |
| 50 | Numbers that are typed into DC are put on a push-down |
| 51 | stack. |
| 52 | DC commands work by taking the top number or two |
| 53 | off the stack, performing the desired operation, and pushing the result |
| 54 | on the stack. |
| 55 | If an argument is given, |
| 56 | input is taken from that file until its end, |
| 57 | then from the standard input. |
| 58 | .SH |
| 59 | SYNOPTIC DESCRIPTION |
| 60 | .PP |
| 61 | Here we describe the DC commands that are intended |
| 62 | for use by people. The additional commands that are |
| 63 | intended to be invoked by compiled output are |
| 64 | described in the detailed description. |
| 65 | .PP |
| 66 | Any number of commands are permitted on a line. |
| 67 | Blanks and new-line characters are ignored except within numbers |
| 68 | and in places where a register name is expected. |
| 69 | .PP |
| 70 | The following constructions are recognized: |
| 71 | .SH |
| 72 | number |
| 73 | .IP |
| 74 | The value of the number is pushed onto the main stack. |
| 75 | A number is an unbroken string of the digits 0-9 |
| 76 | and the capital letters A\-F which are treated as digits |
| 77 | with values 10\-15 respectively. |
| 78 | The number may be preceded by an underscore \*_ to input a |
| 79 | negative number. |
| 80 | Numbers may contain decimal points. |
| 81 | .SH |
| 82 | + \- * % ^ |
| 83 | .IP |
| 84 | The |
| 85 | top two values on the stack are added |
| 86 | (\fB+\fP), |
| 87 | subtracted |
| 88 | (\fB\-\fP), |
| 89 | multiplied (\fB*\fP), |
| 90 | divided (\fB/\fP), |
| 91 | remaindered (\fB%\fP), |
| 92 | or exponentiated (^). |
| 93 | The two entries are popped off the stack; |
| 94 | the result is pushed on the stack in their place. |
| 95 | The result of a division is an integer truncated toward zero. |
| 96 | See the detailed description below for the treatment of |
| 97 | numbers with decimal points. |
| 98 | An exponent must not have any digits after the decimal point. |
| 99 | .SH |
| 100 | s\fIx\fP |
| 101 | .IP |
| 102 | The |
| 103 | top of the main stack is popped and stored into |
| 104 | a register named \fIx\fP, where \fIx\fP may be any character. |
| 105 | If |
| 106 | the |
| 107 | .ft B |
| 108 | s |
| 109 | .ft |
| 110 | is capitalized, |
| 111 | .ft I |
| 112 | x |
| 113 | .ft |
| 114 | is treated as a stack and the value is pushed onto it. |
| 115 | Any character, even blank or new-line, is a valid register name. |
| 116 | .SH |
| 117 | l\fIx\fP |
| 118 | .IP |
| 119 | The |
| 120 | value in register |
| 121 | .ft I |
| 122 | x |
| 123 | .ft |
| 124 | is pushed onto the stack. |
| 125 | The register |
| 126 | .ft I |
| 127 | x |
| 128 | .ft |
| 129 | is not altered. |
| 130 | If the |
| 131 | .ft B |
| 132 | l |
| 133 | .ft |
| 134 | is capitalized, |
| 135 | register |
| 136 | .ft I |
| 137 | x |
| 138 | .ft |
| 139 | is treated as a stack and its top value is popped onto the main stack. |
| 140 | .LP |
| 141 | All registers start with empty value which is treated as a zero |
| 142 | by the command \fBl\fP and is treated as an error by the command \fBL\fP. |
| 143 | .SH |
| 144 | .SH |
| 145 | d |
| 146 | .IP |
| 147 | The |
| 148 | top value on the stack is duplicated. |
| 149 | .SH |
| 150 | p |
| 151 | .IP |
| 152 | The top value on the stack is printed. |
| 153 | The top value remains unchanged. |
| 154 | .SH |
| 155 | f |
| 156 | .IP |
| 157 | All values on the stack and in registers are printed. |
| 158 | .SH |
| 159 | x |
| 160 | .IP |
| 161 | treats the top element of the stack as a character string, |
| 162 | removes it from the stack, and |
| 163 | executes it as a string of DC commands. |
| 164 | .SH |
| 165 | [ ... ] |
| 166 | .IP |
| 167 | puts the bracketed character string onto the top of the stack. |
| 168 | .SH |
| 169 | q |
| 170 | .IP |
| 171 | exits the program. |
| 172 | If executing a string, the recursion level is |
| 173 | popped by two. |
| 174 | If |
| 175 | .ft B |
| 176 | q |
| 177 | .ft |
| 178 | is capitalized, |
| 179 | the top value on the stack is popped and the string execution level is popped |
| 180 | by that value. |
| 181 | .SH |
| 182 | <\fIx\fP >\fIx\fP =\fIx\fP !<\fIx\fP !>\fIx\fP !=\fIx\fP |
| 183 | .IP |
| 184 | The |
| 185 | top two elements of the stack are popped and compared. |
| 186 | Register |
| 187 | .ft I |
| 188 | x |
| 189 | .ft |
| 190 | is executed if they obey the stated |
| 191 | relation. |
| 192 | Exclamation point is negation. |
| 193 | .SH |
| 194 | v |
| 195 | .IP |
| 196 | replaces the top element on the stack by its square root. |
| 197 | The square root of an integer is truncated to an integer. |
| 198 | For the treatment of numbers with decimal points, see |
| 199 | the detailed description below. |
| 200 | .SH |
| 201 | ! |
| 202 | .IP |
| 203 | interprets the rest of the line as a |
| 204 | .UX |
| 205 | command. |
| 206 | Control returns to DC when the |
| 207 | .UX |
| 208 | command terminates. |
| 209 | .SH |
| 210 | c |
| 211 | .IP |
| 212 | All values on the stack are popped; the stack becomes empty. |
| 213 | .SH |
| 214 | i |
| 215 | .IP |
| 216 | The top value on the stack is popped and used as the |
| 217 | number radix for further input. |
| 218 | If \fBi\fP is capitalized, the value of |
| 219 | the input base is pushed onto the stack. |
| 220 | No mechanism has been provided for the input of arbitrary |
| 221 | numbers in bases less than 1 or greater than 16. |
| 222 | .SH |
| 223 | o |
| 224 | .IP |
| 225 | The top value on the stack is popped and used as the |
| 226 | number radix for further output. |
| 227 | If \fBo\fP is capitalized, the value of the output |
| 228 | base is pushed onto the stack. |
| 229 | .SH |
| 230 | k |
| 231 | .IP |
| 232 | The top of the stack is popped, and that value is used as |
| 233 | a scale factor |
| 234 | that influences the number of decimal places |
| 235 | that are maintained during multiplication, division, and exponentiation. |
| 236 | The scale factor must be greater than or equal to zero and |
| 237 | less than 100. |
| 238 | If \fBk\fP is capitalized, the value of the scale factor |
| 239 | is pushed onto the stack. |
| 240 | .SH |
| 241 | z |
| 242 | .IP |
| 243 | The value of the stack level is pushed onto the stack. |
| 244 | .SH |
| 245 | ? |
| 246 | .IP |
| 247 | A line of input is taken from the input source (usually the console) |
| 248 | and executed. |
| 249 | .SH |
| 250 | DETAILED DESCRIPTION |
| 251 | .SH |
| 252 | Internal Representation of Numbers |
| 253 | .PP |
| 254 | Numbers are stored internally using a dynamic storage allocator. |
| 255 | Numbers are kept in the form of a string |
| 256 | of digits to the base 100 stored one digit per byte |
| 257 | (centennial digits). |
| 258 | The string is stored with the low-order digit at the |
| 259 | beginning of the string. |
| 260 | For example, the representation of 157 |
| 261 | is 57,1. |
| 262 | After any arithmetic operation on a number, care is taken |
| 263 | that all digits are in the range 0\-99 and that |
| 264 | the number has no leading zeros. |
| 265 | The number zero is represented by the empty string. |
| 266 | .PP |
| 267 | Negative numbers are represented in the 100's complement |
| 268 | notation, which is analogous to two's complement notation for binary |
| 269 | numbers. |
| 270 | The high order digit of a negative number is always \-1 |
| 271 | and all other digits are in the range 0\-99. |
| 272 | The digit preceding the high order \-1 digit is never a 99. |
| 273 | The representation of \-157 is 43,98,\-1. |
| 274 | We shall call this the canonical form of a number. |
| 275 | The advantage of this kind of representation of negative |
| 276 | numbers is ease of addition. When addition is performed digit |
| 277 | by digit, the result is formally correct. The result need only |
| 278 | be modified, if necessary, to put it into canonical form. |
| 279 | .PP |
| 280 | Because the largest valid digit is 99 and the byte can |
| 281 | hold numbers twice that large, addition can be carried out |
| 282 | and the handling of carries done later when |
| 283 | that is convenient, as it sometimes is. |
| 284 | .PP |
| 285 | An additional byte is stored with each number beyond |
| 286 | the high order digit to indicate the number of |
| 287 | assumed decimal digits after the decimal point. The representation |
| 288 | of .001 is 1,\fI3\fP |
| 289 | where the scale has been italicized to emphasize the fact that it |
| 290 | is not the high order digit. |
| 291 | The value of this extra byte is called the |
| 292 | .ft B |
| 293 | scale factor |
| 294 | .ft |
| 295 | of the number. |
| 296 | .SH |
| 297 | The Allocator |
| 298 | .PP |
| 299 | DC uses a dynamic string storage allocator |
| 300 | for all of its internal storage. |
| 301 | All reading and writing of numbers internally is done through |
| 302 | the allocator. |
| 303 | Associated with each string in the allocator is a four-word header containing pointers |
| 304 | to the beginning of the string, the end of the string, |
| 305 | the next place to write, and the next place to read. |
| 306 | Communication between the allocator and DC |
| 307 | is done via pointers to these headers. |
| 308 | .PP |
| 309 | The allocator initially has one large string on a list |
| 310 | of free strings. All headers except the one pointing |
| 311 | to this string are on a list of free headers. |
| 312 | Requests for strings are made by size. |
| 313 | The size of the string actually supplied is the next higher |
| 314 | power of 2. |
| 315 | When a request for a string is made, the allocator |
| 316 | first checks the free list to see if there is |
| 317 | a string of the desired size. |
| 318 | If none is found, the allocator finds the next larger free string and splits it repeatedly until |
| 319 | it has a string of the right size. |
| 320 | Left-over strings are put on the free list. |
| 321 | If there are no larger strings, |
| 322 | the allocator tries to coalesce smaller free strings into |
| 323 | larger ones. |
| 324 | Since all strings are the result |
| 325 | of splitting large strings, |
| 326 | each string has a neighbor that is next to it in core |
| 327 | and, if free, can be combined with it to make a string twice as long. |
| 328 | This is an implementation of the `buddy system' of allocation |
| 329 | described in [2]. |
| 330 | .PP |
| 331 | Failing to find a string of the proper length after coalescing, |
| 332 | the allocator asks the system for more space. |
| 333 | The amount of space on the system is the only limitation |
| 334 | on the size and number of strings in DC. |
| 335 | If at any time in the process of trying to allocate a string, the allocator runs out of |
| 336 | headers, it also asks the system for more space. |
| 337 | .PP |
| 338 | There are routines in the allocator for reading, writing, copying, rewinding, |
| 339 | forward-spacing, and backspacing strings. |
| 340 | All string manipulation is done using these routines. |
| 341 | .PP |
| 342 | The reading and writing routines |
| 343 | increment the read pointer or write pointer so that |
| 344 | the characters of a string are read or written in |
| 345 | succession by a series of read or write calls. |
| 346 | The write pointer is interpreted as the end of the |
| 347 | information-containing portion of a string and a call |
| 348 | to read beyond that point returns an end-of-string indication. |
| 349 | An attempt to write beyond the end of a string |
| 350 | causes the allocator to |
| 351 | allocate a larger space and then copy |
| 352 | the old string into the larger block. |
| 353 | .SH |
| 354 | Internal Arithmetic |
| 355 | .PP |
| 356 | All arithmetic operations are done on integers. |
| 357 | The operands (or operand) needed for the operation are popped |
| 358 | from the main stack and their scale factors stripped off. |
| 359 | Zeros are added or digits removed as necessary to get |
| 360 | a properly scaled result from the internal arithmetic routine. |
| 361 | For example, if the scale of the operands is different and decimal |
| 362 | alignment is required, as it is for |
| 363 | addition, zeros are appended to the operand with the smaller |
| 364 | scale. |
| 365 | After performing the required arithmetic operation, |
| 366 | the proper scale factor is appended to the end of the number before |
| 367 | it is pushed on the stack. |
| 368 | .PP |
| 369 | A register called \fBscale\fP plays a part |
| 370 | in the results of most arithmetic operations. |
| 371 | \fBscale\fP is the bound on the number of decimal places retained in |
| 372 | arithmetic computations. |
| 373 | \fBscale\fP may be set to the number on the top of the stack |
| 374 | truncated to an integer with the \fBk\fP command. |
| 375 | \fBK\fP may be used to push the value of \fBscale\fP on the stack. |
| 376 | \fBscale\fP must be greater than or equal to 0 and less than 100. |
| 377 | The descriptions of the individual arithmetic operations will |
| 378 | include the exact effect of \fBscale\fP on the computations. |
| 379 | .SH |
| 380 | Addition and Subtraction |
| 381 | .PP |
| 382 | The scales of the two numbers are compared and trailing |
| 383 | zeros are supplied to the number with the lower scale to give both |
| 384 | numbers the same scale. The number with the smaller scale is |
| 385 | multiplied by 10 if the difference of the scales is odd. |
| 386 | The scale of the result is then set to the larger of the scales |
| 387 | of the two operands. |
| 388 | .PP |
| 389 | Subtraction is performed by negating the number |
| 390 | to be subtracted and proceeding as in addition. |
| 391 | .PP |
| 392 | Finally, the addition is performed digit by digit from the |
| 393 | low order end of the number. The carries are propagated |
| 394 | in the usual way. |
| 395 | The resulting number is brought into canonical form, which may |
| 396 | require stripping of leading zeros, or for negative numbers |
| 397 | replacing the high-order configuration 99,\-1 by the digit \-1. |
| 398 | In any case, digits which are not in the range 0\-99 must |
| 399 | be brought into that range, propagating any carries or borrows |
| 400 | that result. |
| 401 | .SH |
| 402 | Multiplication |
| 403 | .PP |
| 404 | The scales are removed from the two operands and saved. |
| 405 | The operands are both made positive. |
| 406 | Then multiplication is performed in |
| 407 | a digit by digit manner that exactly mimics the hand method |
| 408 | of multiplying. |
| 409 | The first number is multiplied by each digit of the second |
| 410 | number, beginning with its low order digit. The intermediate |
| 411 | products are accumulated into a partial sum which becomes the |
| 412 | final product. |
| 413 | The product is put into the canonical form and its sign is |
| 414 | computed from the signs of the original operands. |
| 415 | .PP |
| 416 | The scale of the result is set equal to the sum |
| 417 | of the scales of the two operands. |
| 418 | If that scale is larger than the internal register |
| 419 | .ft B |
| 420 | scale |
| 421 | .ft |
| 422 | and also larger than both of the scales of the two operands, |
| 423 | then the scale of the result is set equal to the largest |
| 424 | of these three last quantities. |
| 425 | .SH |
| 426 | Division |
| 427 | .PP |
| 428 | The scales are removed from the two operands. |
| 429 | Zeros are appended or digits removed from the dividend to make |
| 430 | the scale of the result of the integer division equal to |
| 431 | the internal quantity |
| 432 | \fBscale\fP. |
| 433 | The signs are removed and saved. |
| 434 | .PP |
| 435 | Division is performed much as it would be done by hand. |
| 436 | The difference of the lengths of the two numbers |
| 437 | is computed. |
| 438 | If the divisor is longer than the dividend, |
| 439 | zero is returned. |
| 440 | Otherwise the top digit of the divisor is divided into the top |
| 441 | two digits of the dividend. |
| 442 | The result is used as the first (high-order) digit of the |
| 443 | quotient. |
| 444 | It may turn out be one unit too low, but if it is, the next |
| 445 | trial quotient will be larger than 99 and this will be |
| 446 | adjusted at the end of the process. |
| 447 | The trial digit is multiplied by the divisor and the result subtracted |
| 448 | from the dividend and the process is repeated to get |
| 449 | additional quotient digits until the remaining |
| 450 | dividend is smaller than the divisor. |
| 451 | At the end, the digits of the quotient are put into |
| 452 | the canonical form, with propagation of carry as needed. |
| 453 | The sign is set from the sign of the operands. |
| 454 | .SH |
| 455 | Remainder |
| 456 | .PP |
| 457 | The division routine is called and division is performed |
| 458 | exactly as described. The quantity returned is the remains of the |
| 459 | dividend at the end of the divide process. |
| 460 | Since division truncates toward zero, remainders have the same |
| 461 | sign as the dividend. |
| 462 | The scale of the remainder is set to |
| 463 | the maximum of the scale of the dividend and |
| 464 | the scale of the quotient plus the scale of the divisor. |
| 465 | .SH |
| 466 | Square Root |
| 467 | .PP |
| 468 | The scale is stripped from the operand. |
| 469 | Zeros are added if necessary to make the |
| 470 | integer result have a scale that is the larger of |
| 471 | the internal quantity |
| 472 | \fBscale\fP |
| 473 | and the scale of the operand. |
| 474 | .PP |
| 475 | The method used to compute sqrt(y) is Newton's method |
| 476 | with successive approximations by the rule |
| 477 | .EQ |
| 478 | x sub {n+1} ~=~ half ( x sub n + y over x sub n ) |
| 479 | .EN |
| 480 | The initial guess is found by taking the integer square root |
| 481 | of the top two digits. |
| 482 | .SH |
| 483 | Exponentiation |
| 484 | .PP |
| 485 | Only exponents with zero scale factor are handled. If the exponent is |
| 486 | zero, then the result is 1. If the exponent is negative, then |
| 487 | it is made positive and the base is divided into one. The scale |
| 488 | of the base is removed. |
| 489 | .PP |
| 490 | The integer exponent is viewed as a binary number. |
| 491 | The base is repeatedly squared and the result is |
| 492 | obtained as a product of those powers of the base that |
| 493 | correspond to the positions of the one-bits in the binary |
| 494 | representation of the exponent. |
| 495 | Enough digits of the result |
| 496 | are removed to make the scale of the result the same as if the |
| 497 | indicated multiplication had been performed. |
| 498 | .SH |
| 499 | Input Conversion and Base |
| 500 | .PP |
| 501 | Numbers are converted to the internal representation as they are read |
| 502 | in. |
| 503 | The scale stored with a number is simply the number of fractional digits input. |
| 504 | Negative numbers are indicated by preceding the number with a \fB\_\fP. |
| 505 | The hexadecimal digits A\-F correspond to the numbers 10\-15 regardless of input base. |
| 506 | The \fBi\fP command can be used to change the base of the input numbers. |
| 507 | This command pops the stack, truncates the resulting number to an integer, |
| 508 | and uses it as the input base for all further input. |
| 509 | The input base is initialized to 10 but may, for example be changed to |
| 510 | 8 or 16 to do octal or hexadecimal to decimal conversions. |
| 511 | The command \fBI\fP will push the value of the input base on the stack. |
| 512 | .SH |
| 513 | Output Commands |
| 514 | .PP |
| 515 | The command \fBp\fP causes the top of the stack to be printed. |
| 516 | It does not remove the top of the stack. |
| 517 | All of the stack and internal registers can be output |
| 518 | by typing the command \fBf\fP. |
| 519 | The \fBo\fP command can be used to change the output base. |
| 520 | This command uses the top of the stack, truncated to an integer as |
| 521 | the base for all further output. |
| 522 | The output base in initialized to 10. |
| 523 | It will work correctly for any base. |
| 524 | The command \fBO\fP pushes the value of the output base on the stack. |
| 525 | .SH |
| 526 | Output Format and Base |
| 527 | .PP |
| 528 | The input and output bases only affect |
| 529 | the interpretation of numbers on input and output; they have no |
| 530 | effect on arithmetic computations. |
| 531 | Large numbers are output with 70 characters per line; |
| 532 | a \\ indicates a continued line. |
| 533 | All choices of input and output bases work correctly, although not all are |
| 534 | useful. |
| 535 | A particularly useful output base is 100000, which has the effect of |
| 536 | grouping digits in fives. |
| 537 | Bases of 8 and 16 can be used for decimal-octal or decimal-hexadecimal |
| 538 | conversions. |
| 539 | .SH |
| 540 | Internal Registers |
| 541 | .PP |
| 542 | Numbers or strings may be stored in internal registers or loaded on the stack |
| 543 | from registers with the commands \fBs\fP and \fBl\fP. |
| 544 | The command \fBs\fIx\fR pops the top of the stack and |
| 545 | stores the result in register \fBx\fP. |
| 546 | \fIx\fP can be any character. |
| 547 | \fBl\fIx\fR puts the contents of register \fBx\fP on the top of the stack. |
| 548 | The \fBl\fP command has no effect on the contents of register \fIx\fP. |
| 549 | The \fBs\fP command, however, is destructive. |
| 550 | .SH |
| 551 | Stack Commands |
| 552 | .PP |
| 553 | The command \fBc\fP clears the stack. |
| 554 | The command \fBd\fP pushes a duplicate of the number on the top of the stack |
| 555 | on the stack. |
| 556 | The command \fBz\fP pushes the stack size on the stack. |
| 557 | The command \fBX\fP replaces the number on the top of the stack |
| 558 | with its scale factor. |
| 559 | The command \fBZ\fP replaces the top of the stack |
| 560 | with its length. |
| 561 | .SH |
| 562 | Subroutine Definitions and Calls |
| 563 | .PP |
| 564 | Enclosing a string in \fB[]\fP pushes the ascii string on the stack. |
| 565 | The \fBq\fP command quits or in executing a string, pops the recursion levels by two. |
| 566 | .SH |
| 567 | Internal Registers \- Programming DC |
| 568 | .PP |
| 569 | The load and store |
| 570 | commands together with \fB[]\fP to store strings, \fBx\fP to execute |
| 571 | and the testing commands `<', `>', `=', `!<', `!>', `!=' can be used to program |
| 572 | DC. |
| 573 | The \fBx\fP command assumes the top of the stack is an string of DC commands |
| 574 | and executes it. |
| 575 | The testing commands compare the top two elements on the stack and if the relation holds, execute the register |
| 576 | that follows the relation. |
| 577 | For example, to print the numbers 0-9, |
| 578 | .DS |
| 579 | [lip1+ si li10>a]sa |
| 580 | 0si lax |
| 581 | .DE |
| 582 | .SH |
| 583 | Push-Down Registers and Arrays |
| 584 | .PP |
| 585 | These commands were designed for used by a compiler, not by |
| 586 | people. |
| 587 | They involve push-down registers and arrays. |
| 588 | In addition to the stack that commands work on, DC can be thought |
| 589 | of as having individual stacks for each register. |
| 590 | These registers are operated on by the commands \fBS\fP and \fBL\fP. |
| 591 | \fBS\fIx\fR pushes the top value of the main stack onto the stack for |
| 592 | the register \fIx\fP. |
| 593 | \fBL\fIx\fR pops the stack for register \fIx\fP and puts the result on the main |
| 594 | stack. |
| 595 | The commands \fBs\fP and \fBl\fP also work on registers but not as push-down |
| 596 | stacks. |
| 597 | \fBl\fP doesn't effect the top of the |
| 598 | register stack, and \fBs\fP destroys what was there before. |
| 599 | .PP |
| 600 | The commands to work on arrays are \fB:\fP and \fB;\fP. |
| 601 | \fB:\fIx\fR pops the stack and uses this value as an index into |
| 602 | the array \fIx\fP. |
| 603 | The next element on the stack is stored at this index in \fIx\fP. |
| 604 | An index must be greater than or equal to 0 and |
| 605 | less than 2048. |
| 606 | \fB;\fIx\fR is the command to load the main stack from the array \fIx\fP. |
| 607 | The value on the top of the stack is the index |
| 608 | into the array \fIx\fP of the value to be loaded. |
| 609 | .SH |
| 610 | Miscellaneous Commands |
| 611 | .PP |
| 612 | The command \fB!\fP interprets the rest of the line as a |
| 613 | .UX |
| 614 | command and passes |
| 615 | it to |
| 616 | .UX |
| 617 | to execute. |
| 618 | One other compiler command is \fBQ\fP. |
| 619 | This command uses the top of the stack as the number of levels of recursion to skip. |
| 620 | .SH |
| 621 | DESIGN CHOICES |
| 622 | .PP |
| 623 | The real reason for the use of a dynamic storage allocator was |
| 624 | that a general purpose program could be (and in fact has been) |
| 625 | used for a variety of other tasks. |
| 626 | The allocator has some value for input and for compiling (i.e. |
| 627 | the bracket [...] commands) where it cannot be known in advance |
| 628 | how long a string will be. |
| 629 | The result was that at a modest |
| 630 | cost in execution time, all considerations of string allocation |
| 631 | and sizes of strings were removed from the remainder of the program |
| 632 | and debugging was made easier. The allocation method |
| 633 | used wastes approximately 25% of available space. |
| 634 | .PP |
| 635 | The choice of 100 as a base for internal arithmetic |
| 636 | seemingly has no compelling advantage. Yet the base cannot |
| 637 | exceed 127 because of hardware limitations and at the cost |
| 638 | of 5% in space, debugging was made a great deal easier and |
| 639 | decimal output was made much faster. |
| 640 | .PP |
| 641 | The reason for a stack-type arithmetic design was |
| 642 | to permit all DC commands from addition to subroutine execution |
| 643 | to be implemented in essentially the same way. The result |
| 644 | was a considerable degree of logical separation of the final |
| 645 | program into modules with very little communication between |
| 646 | modules. |
| 647 | .PP |
| 648 | The rationale for the lack of interaction between the scale and the bases |
| 649 | was to provide an understandable means of proceeding after |
| 650 | a change of base or scale when numbers had already been entered. |
| 651 | An earlier implementation which had global notions of |
| 652 | scale and base did not work out well. |
| 653 | If the value of |
| 654 | .ft B |
| 655 | scale |
| 656 | .ft |
| 657 | were to be interpreted in the current |
| 658 | input or output base, |
| 659 | then a change of base or scale in the midst of a |
| 660 | computation would cause great confusion in the interpretation |
| 661 | of the results. |
| 662 | The current scheme has the advantage that the value of |
| 663 | the input and output bases |
| 664 | are only used for input and output, respectively, and they |
| 665 | are ignored in all other operations. |
| 666 | The value of |
| 667 | scale |
| 668 | is not used for any essential purpose by any part of the program |
| 669 | and it is used only to prevent the number of |
| 670 | decimal places resulting from the arithmetic operations from |
| 671 | growing beyond all bounds. |
| 672 | .PP |
| 673 | The design rationale for the choices for the scales of |
| 674 | the results of arithmetic were that in no case should |
| 675 | any significant digits be thrown away if, on appearances, the |
| 676 | user actually wanted them. Thus, if the user wants |
| 677 | to add the numbers 1.5 and 3.517, it seemed reasonable to give |
| 678 | him the result 5.017 without requiring him to unnecessarily |
| 679 | specify his rather obvious requirements for precision. |
| 680 | .PP |
| 681 | On the other hand, multiplication and exponentiation produce |
| 682 | results with many more digits than their operands and it |
| 683 | seemed reasonable to give as a minimum the number of decimal |
| 684 | places in the operands but not to give more than that |
| 685 | number of digits |
| 686 | unless the user asked for them by specifying a value for \fBscale\fP. |
| 687 | Square root can be handled in just the same way as multiplication. |
| 688 | The operation of division gives arbitrarily many decimal places |
| 689 | and there is simply no way to guess how many places the user |
| 690 | wants. |
| 691 | In this case only, the user must |
| 692 | specify a \fBscale\fP to get any decimal places at all. |
| 693 | .PP |
| 694 | The scale of remainder was chosen to make it possible |
| 695 | to recreate the dividend from the quotient and remainder. |
| 696 | This is easy to implement; no digits are thrown away. |
| 697 | .SH |
| 698 | References |
| 699 | .IP [1] |
| 700 | L. L. Cherry, R. Morris, |
| 701 | .ft I |
| 702 | BC \- An Arbitrary Precision Desk-Calculator Language. |
| 703 | .ft |
| 704 | .IP [2] |
| 705 | K. C. Knowlton, |
| 706 | .ft I |
| 707 | A Fast Storage Allocator, |
| 708 | .ft |
| 709 | Comm. ACM \fB8\fP, pp. 623-625 (Oct. 1965). |