Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: Matrefeytontias on December 30, 2012, 02:11:28 pm

Title: [z80 ASM] How do 8.8 maths work ?
Post by: Matrefeytontias on December 30, 2012, 02:11:28 pm
Hi guys,

I already know how to do 16-bits multiplication and division in z80 ASM, but how to extend them to 8.8 fixed point ?

In fact there are two questions in one : how do 8.8 work (guess the title) and how do I implement them ?

Thanks by advance :)
Title: Re: [z80 ASM] How do 8.8 maths work ?
Post by: jacobly on December 30, 2012, 03:53:27 pm
Since 8.8 numbers are stored the same way as 16-bit integers, let x be the first value interpreted as an integer and y be the second value interpreted as an integer.  Then x/256 is the first value interpreted as 8.8 and y/256 is the second value as 8.8.  Additionally if z/256 is the result interpreted as an 8.8 value, then z mod 65536 is the result as a 16-bit integer.

z/256=x/256+y/256
z/256=(x+y)/256
z=x+y (Addition is exactly the same)
Spoiler For Example:
    add  hl,de

z/256=x/256-y/256
z/256=(x-y)/256
z=x-y (Subtraction is exactly the same)
Spoiler For Example:
    or   a
    sbc  hl,de

z/256=(x/256)*(y/256)
z/256=xy/65536
z=xy/256 (Multiplication result must be divided by 256)
Spoiler For Example:
    ld   a,l
    ld   c,h
    ld   hl,0
    ld   b,16
Loop:
    add  hl,hl
    rla
    rl   c
    jr   nc,Skip
    add  hl,de
    adc  a,0
Skip:
    djnz Loop
    ld   l,h
    ld   h,a
    ret

z/256=(x/256)/(y/256)
z/256=x/y
z=256x/y (First operand must be multiplied by 256)
Spoiler For Example:
    ld   bc,16*256
    ld   a,l
    ld   l,h
    ld   h,c
Loop:
    scf
    rl   c
    rla
    adc  hl,hl
    sbc  hl,de
    jr   nc,Skip
    add  hl,de
    dec  c
Skip:
    djnz Loop
    ret

z/256=√(x/256)
z=√x/√256*256
z=√x/16 (Square Root result must be divided by 16)
Spoiler For Example:
    ld   b,12
    ld   a,h
    ld   c,l
    ld   de,0
    ld   h,d
    ld   l,e
Loop:
    sub  $40
    sbc  hl,de
    jr   nc,Skip
    add  a,$40
    adc  hl,de
Skip:
    ccf
    rl   e
    rl   d
    sla  c
    rla
    adc  hl,hl
    sla  c
    rla
    adc  hl,hl
    djnz Loop
    ex   de,hl
    ret

Of course, these are all unsigned.  Changing to signed is almost identical to changing integer math to signed.

Edit: Mixed up h and l at the beginning of the multiply routine.
Title: Re: [z80 ASM] How do 8.8 maths work ?
Post by: Matrefeytontias on December 30, 2012, 04:03:42 pm
Oh okay, it's in fact a multiplication/division involving a 24-bits number which is either for the multiplication the result or for the division the first operand. That's it ?
Title: Re: [z80 ASM] How do 8.8 maths work ?
Post by: jacobly on December 30, 2012, 04:08:20 pm
Pretty much :)
Title: Re: [z80 ASM] How do 8.8 maths work ?
Post by: Matrefeytontias on December 30, 2012, 06:09:26 pm
It seems that your multiply routine doesn't work... I tried :

ld hl,$0180
 ld de,$0200
 call HLtimesDE88
 bcall DispHL


It displayed 2 instead of 3.
Title: Re: [z80 ASM] How do 8.8 maths work ?
Post by: jacobly on December 30, 2012, 07:11:54 pm
Fixed.
Title: Re: [z80 ASM] How do 8.8 maths work ?
Post by: Matrefeytontias on December 30, 2012, 07:21:20 pm
It works, thanks :) (I think I could correct myself but it's 1 am :P )
Title: Re: [z80 ASM] How do 8.8 maths work ?
Post by: blue_bear_94 on December 30, 2012, 08:43:44 pm
Try getting a 32-bit result, and take the middle 2 bytes.