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

All aspects of programming on the Commander X16.
funkheld
Posts: 322
Joined: Mon Feb 26, 2024 11:11 am

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

Post 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,
-----------------------------------------------
Last edited by funkheld on Tue Apr 09, 2024 7:24 pm, edited 1 time in total.
User avatar
desertfish
Posts: 1096
Joined: Tue Aug 25, 2020 8:27 pm
Location: Netherlands

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

Post 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)
    
funkheld
Posts: 322
Joined: Mon Feb 26, 2024 11:11 am

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

Post by funkheld »

hello, thanks for info-math.

Can you please show where the Python program is located?

greeting
User avatar
desertfish
Posts: 1096
Joined: Tue Aug 25, 2020 8:27 pm
Location: Netherlands

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

Post 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:
funkheld
Posts: 322
Joined: Mon Feb 26, 2024 11:11 am

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

Post by funkheld »

That would be nice with prog8 these routines.

thanks
greeting
User avatar
desertfish
Posts: 1096
Joined: Tue Aug 25, 2020 8:27 pm
Location: Netherlands

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

Post 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)
TomXP411
Posts: 1781
Joined: Tue May 19, 2020 8:49 pm

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

Post 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.
User avatar
desertfish
Posts: 1096
Joined: Tue Aug 25, 2020 8:27 pm
Location: Netherlands

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

Post by desertfish »

^ Clever!!!

I am so not used to the new basic commands like POINTER.
funkheld
Posts: 322
Joined: Mon Feb 26, 2024 11:11 am

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

Post by funkheld »

Hi, Thank You.
Looks good with the values for the float-ASM calculation.

greeting
PaulForgey
Posts: 17
Joined: Tue Mar 12, 2024 3:12 am

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

Post 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.
Post Reply