Calculator Community > ASM

[z80] 32 bit by 16 bits division and 32 bit square root

<< < (3/7) > >>

Streetwalrus:

--- Quote from: jacobly on April 12, 2013, 07:52:33 am ---You only have one exx in the loop, so every time through the loop uses different registers.  It should work if you add an exx at the beginning of the loop.

--- End quote ---
I forgot to copy it but I actually have it.

jacobly:
In that case you probably have an overflow problem.  Try either using a 48-bit temp value or putting

--- Code: --- jr nc,$+7
or a
sbc hl,de
jr $+7

--- End code ---
after adc hl,hl.

Streetwalrus:
Should I leave the extra sbc hl,de ?

Edit : It doesn't fix anything...
Edit 2 : My code actually worked I was just displaying without the small r in axe. :P

fghsgh:
I know this is old (and I'm sorry for sending you all a notification), but I happened to need a 32 div 16 routine today, so I made one:
This divides HLIX by BC
The result is stored in HLIX, the remainder in DE
BC is unmodified
A is 0
it doesn't use any other registers or RAM

--- Code: ---div32_16:
 ld de,0 ; 10
 ld a,32 ; 7
div32_16loop:
 add ix,ix ; 15
 adc hl,hl ; 15
 ex de,hl ; 4
 adc hl,hl ; 15
 or a ; 4
 sbc hl,bc ; 15
 inc ix ; 10
 jr nc,cansub ; 12/7
  add hl,bc ; 11
  dec ix ; 10
cansub:
 ex de,hl ; 4
 dec a ; 4
 jr nz,div32_16loop ; 12/7
 ret ; 10

--- End code ---
I'm not saying this is the fastest possible, but at least it does the job. This takes 4094 T-States in the worst case, 3582 in the best case, which is between .6 and .7 ms at 6 MHz.
I could add some comments to this, but it's more difficult to explain what is going on than to just program it.
Also, note that this doesn't check if BC is 0 so you will get junk as an answer in that case.
I might also write a 32-bit square root routine if it is still needed.

EDIT: an optimized version can be found further in this topic.

Xeda112358:
Thanks! I am on mobile, so not much help, but a few quick optimizations I see are changing those Inc/dec ix to use ixl instead (the bottom bit is 0 and 1 at those points, so no worry of overflow) and you don't need the `or a` before the sbc since the add before it is always pushing out a 0 bit (so leaving carry reset).

Do you prefer speed optimizations or size optimizations, btw?

Thanks for the work, I actually visit this thread semi-frequently!

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version