# FIN

*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.

- 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.
- 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. - 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
is triggered for positive exponents, a value of -100 is assumed for negative exponents and thus a value of the read number of**?OVERFLOW ERROR***0*forced. - 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.
- 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]

- Disassembly von FIN/$BCF3 at All About Your 64
- CodeBase 64: Floating Point Math
- C64 BASIC & KERNAL ROM Disassembly by Michael Steil
- C64OS: Floating Point Math from BASIC