Author Topic: [z80 ASM] How do 8.8 maths work ?  (Read 4090 times)

0 Members and 1 Guest are viewing this topic.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
[z80 ASM] How do 8.8 maths work ?
« 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 :)
« Last Edit: December 30, 2012, 02:11:53 pm by Matrefeytontias »

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: [z80 ASM] How do 8.8 maths work ?
« Reply #1 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.
« Last Edit: December 30, 2012, 07:11:25 pm by jacobly »

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] How do 8.8 maths work ?
« Reply #2 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 ?

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: [z80 ASM] How do 8.8 maths work ?
« Reply #3 on: December 30, 2012, 04:08:20 pm »
Pretty much :)

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] How do 8.8 maths work ?
« Reply #4 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.

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: [z80 ASM] How do 8.8 maths work ?
« Reply #5 on: December 30, 2012, 07:11:54 pm »
Fixed.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] How do 8.8 maths work ?
« Reply #6 on: December 30, 2012, 07:21:20 pm »
It works, thanks :) (I think I could correct myself but it's 1 am :P )

Offline blue_bear_94

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 801
  • Rating: +25/-35
  • Touhou Enthusiast / Former Troll / 68k Programmer
    • View Profile
Re: [z80 ASM] How do 8.8 maths work ?
« Reply #7 on: December 30, 2012, 08:43:44 pm »
Try getting a 32-bit result, and take the middle 2 bytes.
Due to dissatisfaction, I will be inactive on Omnimaga until further notice. (?? THP hasn't been much success and there's also the CE. I might possibly be here for a while.)
If you want to implore me to come back, or otherwise contact me, I can be found on GitHub (bluebear94), Twitter (@melranosF_), Reddit (/u/Fluffy8x), or e-mail (if you know my address). As a last resort, send me a PM on Cemetech (bluebear94) or join Touhou Prono (don't be fooled by the name). I've also enabled notifications for PMs on Omnimaga, but I don't advise using that since I might be banned.
Elvyna (Sunrise) 4 5%
TI-84+SE User (2.30 2.55 MP 2.43)
TI-89 Titanium User (3.10)
Casio Prizm User? (1.02)
Bag  東方ぷろの