Page 1 of 2

Which program can you use to convert float into 5 bytes for ASM?

Posted: Tue Apr 09, 2024 7:03 pm
by funkheld
Hi good afternoon.
Another nice math trip for me old man in ASM and inline.

Which program can you use to convert float into 5 bytes for ASM and 5 bytes ih the float?

-----------------------------------
flt_g: .byte $84, $1c, $f5, $c2, $8f ; float 9.81
flt_time: .byte $83, $20, $00, $00, $00 ; float 5.0
flt_two: .byte $82, $00, $00, $00, $00 ; float 2.0
----------------------------------

Thanks.


demo from x16-doc.

Code: Select all

; calculate how far an object has fallen:  d = 1/2 * g * t^2.
; we set g = 9.81 m/sec^2, time = 5 sec -> d = 122.625 m.

CHROUT = $ffd2
FOUT   = $fe06
FMULTT = $fe21
FDIV   = $fe24
CONUPK = $fe5a
MOVFM  = $fe63

    lda  #4
    sta  $01         ; rom bank 4 (BASIC) contains the fp routines.
    lda  #<flt_two
    ldy  #>flt_two
    jsr  MOVFM
    lda  #<flt_g
    ldy  #>flt_g
    jsr  FDIV        ; FACC= g/2
    lda  #<flt_time
    ldy  #>flt_time
    jsr  CONUPK      ; ARG = time
    jsr  FMULTT      ; FACC = g/2 * time
    lda  #<flt_time
    ldy  #>flt_time
    jsr  CONUPK      ; again ARG = time
    jsr  FMULTT      ; FACC = g/2 * time * time
    jsr  FOUT        ; to string
    ; print string in AY
    sta  $02
    sty  $03
    ldy  #0
loop:
    lda  ($02),y
    beq  done
    jsr  CHROUT
    iny
    bne  loop
done:
    rts

flt_g:      .byte  $84, $1c, $f5, $c2, $8f  ; float 9.81
flt_time:   .byte  $83, $20, $00, $00, $00  ; float 5.0
flt_two:    .byte  $82, $00, $00, $00, $00  ; float 2.0
This is the asm-inline for basic:
---------------------------------------------
RESTORE ASM

READ Z
FOR P = 0 TO Z-1
READ A
POKE $a000+P,A
NEXT

SYS $a000

ASM:
DATA 73
DATA $A9, $04, $85, $01, $A9, $44, $A0, $A0, $20, $63
DATA $FE, $A9, $3A, $A0, $A0, $20, $24, $FE, $A9, $3F
DATA $A0, $A0, $20, $5A, $FE, $20, $21, $FE, $A9, $3F
DATA $A0, $A0, $20, $5A, $FE, $20, $21, $FE, $20, $06
DATA $FE, $85, $02, $84, $03, $A0, $00, $B1, $02, $F0
DATA $06, $20, $D2, $FF, $C8, $D0, $F6, $60, $84, $1C
DATA $F5, $C2, $8F, $83, $20, $00, $00, $00, $82, $00
DATA $00, $00, $00,
-----------------------------------------------

Re: Which program can you use to convert float into 5 bytes for ASM?

Posted: Tue Apr 09, 2024 7:16 pm
by desertfish
I use prog8 (it does it inside the compiler) but over at the Discord server, Wavicle had posted this Python code.
I haven't used it myself so can't immediately tell if or how it works

Code: Select all

def from_c64_float(num):
    excess_exp = (num >> 32)
    if excess_exp == 0:
        return 0.0
    exp = (excess_exp-0x80)-32
    sign = 1 if num & 0x80000000 == 0 else -1
    man= 0x80000000 + (num & 0x7fffffff)
    return float(sign * (2**exp) * man)

def to_c64_float(num):
    if num == 0:
        return 0
    sign = 0x80000000 if num < 0 else 0
    num_abs = abs(num)
    exp = math.floor(math.log(num,2))
    if 0 > (exp + 0x81) or (exp + 0x81) > 255:
        raise FloatingPointError(f"exponent {exp+0x81} is not in the range 0..255")
    man = round(num_abs * 2 ** (31 - exp)) & 0x7fffffff
    return(((exp + 0x81)<<32) + (sign) + man)
    

Re: Which program can you use to convert float into 5 bytes for ASM?

Posted: Tue Apr 09, 2024 7:22 pm
by funkheld
hello, thanks for info-math.

Can you please show where the Python program is located?

greeting

Re: Which program can you use to convert float into 5 bytes for ASM?

Posted: Tue Apr 09, 2024 8:19 pm
by desertfish
https://www.python.org/

but maybe I should add an option to prog8 to expose the conversion routines it has so you can do it using the prog8 command line :roll:

Re: Which program can you use to convert float into 5 bytes for ASM?

Posted: Tue Apr 09, 2024 8:26 pm
by funkheld
That would be nice with prog8 these routines.

thanks
greeting

Re: Which program can you use to convert float into 5 bytes for ASM?

Posted: Tue Apr 09, 2024 10:06 pm
by desertfish
Ok in the next release of Prog8 (10.3 once it is finished), you can do this


$ p8compile -target cx16 dummy -float2bytes "-9.87654321"

-9.87654321 in bytes on 'cx16': 132,158,6,82,44

$ p8compile -target cx16 dummy -bytes2float 132,158,6,82,44

floating point value on 'cx16': -9.876543208956718



(the "dummy" argument does nothing in this case, and looks a bit silly, but is required due to the way the compiler expects the arguments, and I don't want to change that all just for this niche feature)

Re: Which program can you use to convert float into 5 bytes for ASM?

Posted: Tue Apr 09, 2024 11:13 pm
by TomXP411
Here's the simple way:

10 X = 1234 20 X1 = POINTER(X) 30 FOR P = X1 TO X1+4 40 PRINT HEX$(PEEK(P));", "; 50 NEXT 60 PRINT

This actually reads the value out of memory , already encoded the way it's used in BASIC.

Re: Which program can you use to convert float into 5 bytes for ASM?

Posted: Wed Apr 10, 2024 4:30 pm
by desertfish
^ Clever!!!

I am so not used to the new basic commands like POINTER.

Re: Which program can you use to convert float into 5 bytes for ASM?

Posted: Wed Apr 10, 2024 5:33 pm
by funkheld
Hi, Thank You.
Looks good with the values for the float-ASM calculation.

greeting

Re: Which program can you use to convert float into 5 bytes for ASM?

Posted: Wed May 01, 2024 3:44 pm
by PaulForgey
If you are asking about taking a floating point value in textual form purely from assembly, use VAL_1 to take a string pointed at in X/Y length A to parse it into the facc. Then use MOVEMF to write a compacted 5 byte form of that into memory pointed at in X/Y.