At a savings of seven bytes! Very nice algorithm there; thanks, calc84. Smile
The only potential problem would be in it destroying A, but that can be saved with a push/pop.
Xeda112358 wrote:
And I was just going to post the NegAHL:

Code:

NegAHL:
  push af    ;10
  xor a      ;4
  sub l      ;4
  ld l,a     ;4
  sbc a,a    ;4    ;since c flag will be set, a will be FF
  sub h      ;4
  ld h,a     ;4
  ex (sp),hl ;19
  ld a,-1    ;7
  sub h      ;4
  pop hl     ;11

I hope I didn't make any mistakes :/

I don't think that works, because the top byte needs to take into account the carry from the middle byte, and you're just subtracting it from -1 unconditionally.
Dang, you are right. Zero won't work with that method as it returns FF000 which is not 0. :[ Does this fix it?

Code:

NegAHL:
  push af    ;10
  xor a      ;4
  sub l      ;4
  ld l,a     ;4
  ld a,0
  sbc a,h      ;4
  ld h,a     ;4
  ex (sp),hl ;19
  sbc a,a
  sub h      ;4
  pop hl     ;11

(12 bytes, again)
EDIT: Also, this is designed not to harm any other registers.
Yeah, I think that would work, though if you have a free register it would be much faster to back up A into it rather than using the stack. And if the register still needs to be preserved, pushing/popping it is still faster since you don't have an ex (sp),hl.
Ah, that is a much better idea. This saves 15 cycles, I believe:

Code:

  push bc    ;--
  ld b,a     ;4
  xor a      ;4
  sub l      ;4
  ld l,a     ;4
  ld a,0     ;7
  sbc a,h    ;4
  ld h,a     ;4
  sbc a,a    ;4
  sub b      ;4
  pop bc     ;10+11

(and still 12 bytes)
Thank you for your routines and optimizations.

Now I need a 16.8 division routine. I'll make it signed myself. I also searched for an algorithm on wikipedia and google, but I didn't really understand it...
I still need a fixed point 16.8bit square root routine. Does anyone know how this works?
Check out:
http://wikiti.brandonw.net/index.php?title=Z80_Routines:Math:Square_root

The one from z80 bits is pretty fast and small, i have no idea how it works though.
Thank you. I found an algorithm that will probably work.
I coded a square root routine, but it causes a crash (after 30 secounds of freeze), and I can't find out why.

This is my code:

Code:

FPsqrt:
  ;preforms the square root of ahl
  saveAHL(RoutineRam)

  xor a
  ld hl, 0

  set 3, h
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect1
    res 3, h
BitCorrect1:

  xor a
  set 2, h
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect2
    res 2, h
BitCorrect2:

  xor a
  set 1, h
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect3
    res 1, h
BitCorrect3:

  xor a
  set 0, h
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect4
    res 0, h
BitCorrect4:

  xor a
  set 7, l
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect5
    res 7, l
BitCorrect5:

  xor a
  set 6, l
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect6
    res 6, l
BitCorrect6:

  xor a
  set 5, l
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect7
    res 5, l
BitCorrect7:

  xor a
  set 4, l
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect8
    res 4, l
BitCorrect8:

  xor a
  set 3, l
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect9
    res 3, l
BitCorrect9:

  xor a
  set 2, l
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect10
    res 2, l
BitCorrect10:

  xor a
  set 1, l
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect11
    res 1, l
BitCorrect11:

  xor a
  set 0, l
  push hl
  call FPmul
  loadCDE(RoutineRam)
  cpAHLCDE
  pop hl
  jr nc, BitCorrect12
    res 0, l
BitCorrect12:
  xor a
  ret
Ok, this was really strange. I commented out the code of that routine by placing a #comment tag at the start and an #endcomment tag at the end, and kept moving the #comment tag down, testing it every time too see if the crash happened. After a while of testing, I could remove the #comment and #endcomment tags altogether witouth it crashing again, and I didn't make any modifications to the code.

I really don't understand this, but anyway, it works now.
Maybe it was a leftover bug from a previous test or program? That happens to me on occasion :/
I actually had this exact same problem with, of all things, TI-BASIC code. A for loop to do a math function was working incorrectly, so I added in a Pause command followed by an argument to check what the relevant values were equal to...and they were correct. The code ran fine. So I got rid of the pause, and it ran fine...then I ran the exact same program again...and that time, it messed up again!
  
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 2 of 2
» 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

 

Advertisement