I need a Z80 expert to tell me exactly what the following code does. It's short and simple, and just involves a little bit of math. However, I picked up Z80 exactly a few hours ago, and I can't seem to make sense of the how the flags work in relation to the mathematical operators. The comments that appear below may, of course, be very wrong.

Here's the challenge:

This code is taking the distance between two characters on screen and turning them into a destination coordinate. The return value of this function is whatever is in HL -- and will be interpreted as XXYY coordinates. Weird huh?

I can't imagine why they are perfoming an add hl, hl eight times in a row!

So, who here is smart enough with the Z80 to tell me what the mathematical significance of this peice of code is?

Here's the challenge:

2a12 67 ld h,a ; h = a 2a13 5f ld e,a ; e = a 2a14 2e00 ld l,#00 ; l = 00 2a16 55 ld d,l ;d = 00 2a17 0e08 ld c,#08 ; c = 08 ; loop start 2a19 29 add hl,hl ; hl += hl 2a1a d21e2a jp nc,#2a1e ; jump on overflow???? 2a1d 19 add hl,de ; hl += de 2a1e 0d dec c ; c-- 2a1f c2192a jp nz,#2a19 ; jump to loop start if c is not 1 2a22 c9 ret

This code is taking the distance between two characters on screen and turning them into a destination coordinate. The return value of this function is whatever is in HL -- and will be interpreted as XXYY coordinates. Weird huh?

I can't imagine why they are perfoming an add hl, hl eight times in a row!

So, who here is smart enough with the Z80 to tell me what the mathematical significance of this peice of code is?

This post has been edited by

**Dr. Ivo**: 03 April 2007 - 10:15 PM