Karoshi MSX Community

Desarrollo MSX => Rutinas - Snipets => Mensaje iniciado por: theNestruo en 22 de Junio de 2012, 09:11:53 pm



Título: 16bit unsiged + 8bit unsigned
Publicado por: theNestruo en 22 de Junio de 2012, 09: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:
; 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:
; 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.


Título: Re: 16bit unsiged + 8bit unsigned
Publicado por: nanochess en 24 de Junio de 2012, 06:09:26 pm
Very optimized  :). Well done!


Título: Re: 16bit unsiged + 8bit unsigned
Publicado por: theNestruo en 24 de Junio de 2012, 08:03:34 pm
Oh, not mine; just found on the internet. Can't remember where; maybe here (http://stackoverflow.com/questions/2247906/z80-how-to-add-16-and-8-bit-registers).
Strangely, it is not in the classical "Z80 maths" pages ??? That's why I put it here.


Título: Re: 16bit unsiged + 8bit unsigned
Publicado por: Mortimer en 24 de Junio de 2012, 09: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.


Título: Re: 16bit unsiged + 8bit unsigned
Publicado por: theNestruo en 25 de Junio de 2012, 09: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.