So I started a while ago doing some z80 asm, particulary to improve some project of mine Very Happy

I wanted then to divide HL by C at one point. So I read topic about division, and what I get so far is :

But it seems that the value return sometimes is 0 and I can't get why.
Do you have any idea ?
I won't have time to look at your code in depth until later this evening, but have you inspected the 16-bit by 8-bit division routines on the z80 Bits page to see how their method compares to yours? You'll have trouble finding routines as optimized as theirs, so I'd strongly consider using them unless you really want to reinvent them as a learning experience.
Mmmmmhhh didn't see this page. Seems pretty good.
.... It seems by the way that they use the same way for the division than I did :p

Strange that I have a bug then.

EDIT : finally spotted the bug. The first itération is not optimized as it have to be --' add hl,hl can't be removed.

EDIT2 : seems however, if c>160, it always return 0 Sad
So back with my question Razz

I need to optimze a particular piece of code, wich is abusing of jp :

        add     a, e
        jp      nc, _A
        jp      _End
        jp      p, _End

Basically, it do abs(a+e), but even with a overflow, it still get the correct result
(a=-64, e=-100, get a=164).

I need to optimize for speed, since this code is reiterated a lot of time. (and size don't really matter).
Thanks for help :]
It looks like the code needs more than optimization, it needs its logic corrected. It incorrectly negates the result in cases like 100+64, 100+-64, and -64+100. Basically, the current logic negates the result if S|C (sign or carry), but I believe the correct logic is S^V (sign xor overflow). So correct code might look like this, which, in a few minutes of thinking, I can't find any way to further optimize:


   add   a,e
   jp   po,_NoOverflow
   jp   m,_End
   jp   _Neg
   jp   p,_End
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum