Hello,

I don't know if this still could be useful for you, but coincidentally a month ago I made a MPASM macro for carrying out "Real numbers to IEEE574" conversion. Using this macro, you can initialize, for example, the variable myvar with the number 3.14159265 as @FLOAT 3,14159265 ,8,myvar.... Does all the calculations in compiling time, generating only 8 lines of code max.

@FLOAT MACRO int,frac,nfrac,f ;Single Precision Float initializer

VARIABLE float = 0 ;Stores IEEE754 representation of int.frac in f:f+3

IF int || frac ;nfrac = number of decimal digits. Rounds up LS bit.

VARIABLE a = int, c = frac, d = nfrac

VARIABLE n = 1, i = 0, exp = 127

IF a < 0

VARIABLE a = -a

VARIABLE exp = exp | (1 << 8)

ENDIF

IF c && d

WHILE d

VARIABLE n = n*10

VARIABLE d = d - 1

ENDW

IF !a

VARIABLE exp = exp - 1

WHILE !((c * 2) / n)

VARIABLE exp = exp - 1

VARIABLE c = c*2

ENDW

VARIABLE c = (c * 2) % n

ENDIF

WHILE c && i < 23

IF (c * 2)/ n

VARIABLE float = float | (1 << (22 - i))

ENDIF

VARIABLE c = (c * 2) % n

VARIABLE i = i + 1

ENDW

VARIABLE i = ((c * 2) / n != 0)

ENDIF

WHILE a > 1

VARIABLE i = float & 1

VARIABLE float = float >> 1 | (a & 1) << 22

VARIABLE a = a >> 1

VARIABLE exp = exp + 1

ENDW

VARIABLE float = (exp << 23 | float) + i ;Round-up

ENDIF

@MOVLF float,f,3

ENDM

@MOVLF MACRO k,f,n ;Moves up to 32-bit literal to little-endian RAM addr.

VARIABLE i = n ;"k" cannot be an address label on relocatable mode.

WHILE i+1

IF (k >> 8*i) & 0xFF

IF i == n || (((k >> 8*i) & 0xFF) != ((k >> 8*(i+1)) & 0xFF))

MOVLW ((k >> 8*i) & 0xFF)

ENDIF

MOVWF f+i

ELSE

CLRF f+i

ENDIF

VARIABLE i = i - 1

ENDW

ENDM

P.D.: A "@" is in front of the macros' labels so I don't get to mix them with CPU instructions.

post edited by HolyPaps - 2020/03/23 11:39:32