• AVR Freaks

AnsweredHot!How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-point

Page: < 12 Showing page 2 of 2
Author
Oblivion
Starting Member
  • Total Posts : 54
  • Reward points : 0
  • Joined: 2008/10/05 00:08:00
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2019/11/20 02:28:21 (permalink)
0
"... or you can try Olin's MPASM preprocessor PREPIC:"

Interesting. Seems like this very problem is exactly why Olin made that tool.

Yeah, I figured I can't be the only one who ran into this problem. Using that tool would probably make real numbers in ASM possible. ... however ... At that point I'm accepting the use of community/third party tools. To admit that, I would also have to accept other tools as viable. Then, a vary large door has been opened...

GPASM exists, and is open source. So why not rewrite that to do what I REALLY want? But, if I'm going to use GPASM, why not use the whole GPUTILS suite? At that point, gotta' figure that SDCC would also be on the table, and is an ostensibly better choice. (That project sphere desperately needs maintainers/curators, BTW.)

All in all, I'd ultimately end up slipping all the way down the slope, and falling on using C. In which case, I'd almost for sure also be saying bye bye to PIC, and hello to STM32 family. (The NUCLEO boards, in particular, are looking mighty tasty right now.)
#21
HolyPaps
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2020/03/19 14:40:58
  • Location: 0
  • Status: offline
Re: How to make MPASM (et al.) convert a real number to a number format? e.g. Qm.f Fixed-p 2020/03/23 11:21:31 (permalink)
0
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
#22
Page: < 12 Showing page 2 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5