Autor Tema: 16bit unsiged + 8bit unsigned  (Leído 4455 veces)

0 Usuarios y 1 Visitante están viendo este tema.

theNestruo

  • Karoshi Lover
  • ***
  • Mensajes: 236
    • Email
16bit unsiged + 8bit unsigned
« en: 22 de Junio de 2012, 10:11:53 pm »
Hi!
A snippet for the beginners like me ;)

To add an 8 bit value (a) to a 16 bit register (hl) I used to do this:
Código: [Seleccionar]
; hl += a
ld d,0
ld e,a
add hl,de
That takes 22 cycles and destroys an additinoal register (de).

There is a way to do the addition without using any additional register, slightly faster (20 cycles) but 1 byte longer:
Código: [Seleccionar]
; hl += a
add a,l
ld l,a
adc a,h
sub l
ld h,a
Note that bc register or de register can be used instead of hl.
theNestruo."Old BASIC programmers never die; they GOSUB but never RETURN."

nanochess

  • Karoshi Lover
  • ***
  • Mensajes: 141
  • Programando algo buenísimo :)
    • nanochess.org
Re: 16bit unsiged + 8bit unsigned
« Respuesta #1 en: 24 de Junio de 2012, 07:09:26 pm »
Very optimized  :). Well done!
Mira mis juegos MSX/Colecovision/Atari/Intellivision http://nanochess.org/retro_es.html, y sígueme en Twitter http://twitter.com/nanochess

theNestruo

  • Karoshi Lover
  • ***
  • Mensajes: 236
    • Email
Re: 16bit unsiged + 8bit unsigned
« Respuesta #2 en: 24 de Junio de 2012, 09:03:34 pm »
Oh, not mine; just found on the internet. Can't remember where; maybe here.
Strangely, it is not in the classical "Z80 maths" pages ??? That's why I put it here.
theNestruo."Old BASIC programmers never die; they GOSUB but never RETURN."

Mortimer

  • Karoshi Lover
  • ***
  • Mensajes: 216
Re: 16bit unsiged + 8bit unsigned
« Respuesta #3 en: 24 de Junio de 2012, 10:45:55 pm »
But, What about the extra M1 cycle? In a MSX, both routines take 25 cycles, so the advantage is reduced to don't destroy aditional registres at price of one byte longer.

theNestruo

  • Karoshi Lover
  • ***
  • Mensajes: 236
    • Email
Re: 16bit unsiged + 8bit unsigned
« Respuesta #4 en: 25 de Junio de 2012, 10:56:19 am »
You're right. I'm a newbie; I forgot about the M1 cycle :(
Anyway, it's still useful if other registers cannot be detroyed (the push/pop pair would make the code longer and slower) or if the operation to accomplish is bc+=a or de+=a.
theNestruo."Old BASIC programmers never die; they GOSUB but never RETURN."