# GIVAYF

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

 Name: GIVAYF Description: Calculate signum function of floating point register FAC. Entry point: \$B391 / 45969 Passed arguments: Accumulator: High-Byte of the integer Y register: Low-Byte of the integer Return values: Other: FAC = Integer converted to floating point

The ROM routine GIVAYF — sometimes also INTFAC[1] or INTFLP [2] - converts a signed 16-bit integer into a floating point number. The integer must be transferred in the accumulator (High-Byte) and in the Y register (Low-Byte), the resulting floating point number is stored in the floating point register FAC.

GIVAYF not only changes the floating point register FAC, but also sets the data type flag at address 13/\$0D to 0, corresponding to the data type "numeric". This step is unnecessary when implementing your own numeric routines and can be omitted by starting the GIVAYF routine at address 45973 / \$B395.

## Algorithm

GIVAYF first writes the integer into the most significant bytes of the mantissa of FAC, then pads the mantissa with zero bytes and sets the exponent byte to 144/\$90, corresponding to an exponent of 16 (without Excess). The value of FAC is therefore equal to the value of the first two mantissa bytes, and FAC can be viewed as a non-normalized floating point number. However, negative values are still represented in two's complement. GIVAYF therefore jumps to the ROM routine FSUB at the point where negative results are inverted and the sign is set to "negative" before FAC is normalized.

### Runtime behavior

The running time of GIVAYF is mainly determined by the computing time for the inversion (in the case of negative integers) and for the subsequent normalization. For positive integers it is between 120 bars for the number 16384 (\$4000) and 339 bars for the number 1 (\$0001), for negative numbers it is between 182 bars for the number -32768 ( \$8000) and 427 bars for -1 (\$FFFF). If GIVAYF is called with the register contents A = 0 and Y = 0, the running time of the routine is 227 cycles.

On the Commodore 64, one system clock corresponds to around one microsecond (μs).