FIN

From C64-Wiki
Jump to navigationJump to search

Note: This article describes the numeric FIN routine for converting strings to floating point numbers in BASIC-ROM.

Name: FIN
Description: Convert ASCII string to floating point number in floating point register FAC.
Entry point: $BCF3 / 48371
Passed arguments:
Accumulator: first character of the string to be read
carry flag: deleted if A contains a digit (ASCII $30...$39), set otherwise
Other: Address 122/$7A (Low-Byte) / 123/$7B (High-Byte) = Pointer to numeric string to be read
Return values:
Other: FAC = read number

FIN — also STRFLP[1] or ASCFLOAT[2] - reads in a numeric ASCII string and converts it into a floating point number, which is then stored in the Floating point register FAC. Since the string is read via the CHRGET routine, the start address of the string must be in the read pointer of this routine at the addresses 122/$7A (Low-Byte) and 123/$7B (High-Byte) are passed. Furthermore, the first character of the string must already be in the accumulator, and the carry-flag must be set if it is a digit - The easiest way to fulfill this is to call the routine CHRGOT immediately before entering FIN.

The call to FIN uses the memory area at addresses 93/$5D to 103/$67 as a buffer and moves the read pointer of the CHRGET routine to the first character that does not belong to the numeric input string.

Algorithm[edit | edit source]

Since FIN uses the routine CHRGET to read the string, FIN accepts and ignores spaces at any position and in any number. Otherwise, FIN expects an optional sign ("+" or "-"), zero or more digits, and then optionally a decimal point followed by zero or more digits. For a number in scientific notation, this can be followed by an exponent, i.e. the character 'E', optionally followed by a sign ("+" or "-") and by a number in the range [ -99; 99 ] consisting of zero or more digits.

  1. In the first step, FIN distinguishes whether the number to be read begins with a sign ("+" or "-") and, if there is a minus sign, sets the sign flag at address 103/$67 to the value 255/$FF.
  2. Then FIN reads and processes the digits of the string one after the other. For this purpose, starting with a starting value of FAC = 0, the content of FAC is increased tenfold for each character and the value of the newly read digit is added. If FIN encounters a decimal point, the decimal point flag in bit 7 of address 95/$5F is checked: If this flag was already set, then it is the second decimal point and the conversion is over, otherwise the flag and from this point onwards the number of decimal places is counted at address 93/$5D.
  3. If FIN encounters the character "E" when reading in, the string contains a number in scientific notation. If the sign of the exponent is negative, bit 7 is set to address 96/$60, then memory cell 94/$5E serves as a buffer for the exponent. For each digit, the value stored here is multiplied tenfold and the digit just read is added. If the exponent had already reached or exceeded the value 10 before this tenfold increase, an ?OVERFLOW ERROR is triggered for positive exponents, a value of -100 is assumed for negative exponents and thus a value of the read number of 0 forced.
  4. Now FIN negates the exponent if it had a negative sign according to memory cell 96/$60. The difference between the exponent and the number of decimal places is then calculated; it indicates how often the mantissa of FAC must now be multiplied by 10 (if the difference is positive) or divided by 10 (if the difference is negative) until FAC has the correct amount.
  5. Finally, the sign flag at address 103/$67 is checked and FAC is negated if the sign of the number was negative.

Runtime behavior[edit | edit source]

The running time of FIN is mainly determined by the number of digits in the string to be read and by the number of final multiplications by 10 or divisions by 10 (i.e. by the difference between the exponent of the scientific notation and the number of decimal places). The following diagram shows the calculation time for reading the numbers 1 to

Links[edit | edit source]

References[edit | edit source]