As it turns out, DE is supposed to hold a number. RAM $CAAB is used to determine how many more times the thing loops.

Through a suggestion from the chat room to use a debugger, I managed to stumble upon what I did miss: while I did guess that I was missing something with the Carry Flag, I didn't guess that, as it turns out, 00:2A42's Carry Flag setting is preserved up to the next Rotate instruction. This allows the new BC to actually hold something sooner or later. A little more fiddling around with the debugger and it turns out that the whole thing turns out to be one thing: division. As it turns out, when $CAAB is finally 0, DE should have its final result being what it was divided by old BC, and BC should be holding the remainder.

Well, now this topic is unnecessary. And apologies for posting in the wrong forum. I saw "Script Help" and figured that's where I should post. Ah well.

**November 09, 2011, 11:12:08 pm - (Auto Merged - Double Posts are not allowed before 7 days.)**

Well, let's do an example, since somebody questioned this on PM:

DE = 100, BC = 20, HL = CAAB, CAA9 = 20, CAAA = 0

(Spoiler tags for length.)

**Spoiler:** 00:2A38 3611 ld [hl],$11 <-- DE = 100, BC = 20, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 17

00:2A3A 010000 ld bc,$0000 <-- DE = 100, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 17

00:2A3D 21ABCA ld hl,$CAAB <-- DE = 100, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 17

00:2A40 CB13 rl e <-- DE = 200, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 17

00:2A42 CB12 rl d <-- DE = 200, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 17

00:2A44 35 dec [hl] <-- DE = 200, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 16

00:2A45 281A jr z,$2A61 <-- DE = 200, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 16

00:2A47 CB11 rl c <-- DE = 200, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 16

00:2A49 CB10 rl b <-- DE = 200, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 16

00:2A4B 2B dec hl <-- DE = 200, BC = 0, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16

00:2A4C 2B dec hl <-- DE = 200, BC = 0, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 16

00:2A4D 79 ld a,c <-- A = 0, DE = 200, BC = 0, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 16

00:2A4E 96 sub [hl] <-- A = 236, DE = 200, BC = 0, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A4F 4F ld c,a <-- A = 236, DE = 200, BC = 236, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A50 23 inc hl <-- A = 236, DE = 200, BC = 236, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A51 78 ld a,b <-- A = 0, DE = 200, BC = 236, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A52 9E sbc [hl] <-- A = 255, DE = 200, BC = 236, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A53 47 ld b,a <-- A = 255, DE = 200, BC = 65516, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A54 3008 jr nc,$2A5E <-- A = 255, DE = 200, BC = 65516, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A56 2B dec hl <-- A = 255, DE = 200, BC = 65516, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A57 79 ld a,c <-- A = 236, DE = 200, BC = 65516, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A58 86 add a,[hl] <-- A = 0, DE = 200, BC = 65516, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A59 4F ld c,a <-- A = 0, DE = 200, BC = 65280, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A5A 23 inc hl <-- A = 0, DE = 200, BC = 65280, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A5B 78 ld a,b <-- A = 255, DE = 200, BC = 65280, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A5C 8E adc a,[hl] <-- A = 0, DE = 200, BC = 65280, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A5D 47 ld b,a <-- A = 0, DE = 200, BC = 0, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16 (Carry On)

00:2A5E 3F ccf <-- A = 0, DE = 200, BC = 0, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16

00:2A5F 18DC jr $2A3D <-- A = 0, DE = 200, BC = 0, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 16

This didn't seem to do anything, but look again. DE has been increased. Let's keep the loop while tracking DE and CAAB:

CAAB = 17, DE = 100

CAAB = 16, DE = 200

CAAB = 15, DE = 400

CAAB = 14, DE = 800

CAAB = 13, DE = 1600

CAAB = 12, DE = 3200

CAAB = 11, DE = 6400

CAAB = 10, DE = 12800

CAAB = 9, DE = 25600

CAAB = 8, DE = 51200

Now let's do the loop again, with that last line, because double 51200 is going to exceed 65535.

**Spoiler:** 00:2A3D 21ABCA ld hl,$CAAB <-- A = 0, DE = 51200, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 8

00:2A40 CB13 rl e <-- A = 0, DE = 51200, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 8

00:2A42 CB12 rl d <-- A = 0, DE = 36864, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 8 (Carry On)

00:2A44 35 dec [hl] <-- A = 0, DE = 36864, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A45 281A jr z,$2A61 <-- A = 0, DE = 36864, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A47 CB11 rl c <-- A = 0, DE = 36864, BC = 1, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 7

00:2A49 CB10 rl b <-- A = 0, DE = 36864, BC = 1, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 7

00:2A4B 2B dec hl <-- A = 0, DE = 36864, BC = 1, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7

00:2A4C 2B dec hl <-- A = 0, DE = 36864, BC = 1, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 7

00:2A4D 79 ld a,c <-- A = 1, DE = 36864, BC = 1, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 7

00:2A4E 96 sub [hl] <-- A = 237, DE = 36864, BC = 1, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A4F 4F ld c,a <-- A = 237, DE = 36864, BC = 237, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A50 23 inc hl <-- A = 237, DE = 36864, BC = 237, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A51 78 ld a,b <-- A = 0, DE = 36864, BC = 237, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A52 9E sbc [hl] <-- A = 255, DE = 36864, BC = 237, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A53 47 ld b,a <-- A = 255, DE = 36864, BC = 65517, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A54 3008 jr nc,$2A5E <-- A = 255, DE = 36864, BC = 65517, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A56 2B dec hl <-- A = 255, DE = 36864, BC = 65517, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A57 79 ld a,c <-- A = 237, DE = 36864, BC = 65517, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A58 86 add a,[hl] <-- A = 1, DE = 36864, BC = 65517, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A59 4F ld c,a <-- A = 1, DE = 36864, BC = 65281, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A5A 23 inc hl <-- A = 1, DE = 36864, BC = 65281, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A5B 78 ld a,b <-- A = 255, DE = 36864, BC = 65281, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A5C 8E adc a,[hl] <-- A = 0, DE = 36864, BC = 65281, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A5D 47 ld b,a <-- A = 0, DE = 36864, BC = 1, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7 (Carry On)

00:2A5E 3F ccf <-- A = 0, DE = 36864, BC = 1, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7

00:2A5F 18DC jr $2A3D <-- A = 0, DE = 36864, BC = 1, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 7

Well, now BC is changed, so we may actually do something. Let's compare BC, DE, and CAAB for now.

BC = 0, DE = 51200, CAAB = 8

BC = 1, DE = 36864, CAAB = 7

BC = 3, DE = 8192, CAAB = 6

BC = 6, DE = 16384, CAAB = 5

BC = 12, DE = 32768, CAAB = 4

Oh, BC is going to exceed 20, so let's see what happens again.

**Spoiler:** 00:2A3D 21ABCA ld hl,$CAAB <-- A = 0, DE = 32768, BC = 12, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 4

00:2A40 CB13 rl e <-- A = 0, DE = 32768, BC = 12, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 4

00:2A42 CB12 rl d <-- A = 0, DE = 0, BC = 12, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 4 (Carry On)

00:2A44 35 dec [hl] <-- A = 0, DE = 0, BC = 12, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 3 (Carry On)

00:2A45 281A jr z,$2A61 <-- A = 0, DE = 0, BC = 12, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 3 (Carry On)

00:2A47 CB11 rl c <-- A = 0, DE = 0, BC = 25, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A49 CB10 rl b <-- A = 0, DE = 0, BC = 25, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A4B 2B dec hl <-- A = 0, DE = 0, BC = 25, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A4C 2B dec hl <-- A = 0, DE = 0, BC = 25, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A4D 79 ld a,c <-- A = 25, DE = 0, BC = 25, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A4E 96 sub [hl] <-- A = 5, DE = 0, BC = 25, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A4F 4F ld c,a <-- A = 5, DE = 0, BC = 5, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A50 23 inc hl <-- A = 5, DE = 0, BC = 5, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A51 78 ld a,b <-- A = 0, DE = 0, BC = 5, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A52 9E sbc [hl] <-- A = 0, DE = 0, BC = 5, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A53 47 ld b,a <-- A = 0, DE = 0, BC = 5, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A54 3008 jr nc,$2A5E <-- A = 0, DE = 0, BC = 5, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A5E 3F ccf <-- A = 0, DE = 0, BC = 5, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 3 (Carry On)

00:2A5F 18DC jr $2A3D <-- A = 0, DE = 0, BC = 5, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 3 (Carry On)

00:2A3D 21ABCA ld hl,$CAAB <-- A = 0, DE = 0, BC = 5, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 3 (Carry On)

00:2A40 CB13 rl e <-- A = 0, DE = 1, BC = 5, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A42 CB12 rl d <-- A = 0, DE = 1, BC = 5, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 3

00:2A44 35 dec [hl] <-- A = 0, DE = 1, BC = 5, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A45 281A jr z,$2A61 <-- A = 0, DE = 1, BC = 5, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A47 CB11 rl c <-- A = 0, DE = 1, BC = 10, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A49 CB10 rl b <-- A = 0, DE = 1, BC = 10, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A4B 2B dec hl <-- A = 0, DE = 1, BC = 10, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A4C 2B dec hl <-- A = 0, DE = 1, BC = 10, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A4D 79 ld a,c <-- A = 10, DE = 1, BC = 10, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A4E 96 sub [hl] <-- A = 246, DE = 1, BC = 10, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A4F 4F ld c,a <-- A = 246, DE = 1, BC = 246, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A50 23 inc hl <-- A = 246, DE = 1, BC = 246, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A51 78 ld a,b <-- A = 0, DE = 1, BC = 246, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A52 9E sbc [hl] <-- A = 255, DE = 1, BC = 246, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A53 47 ld b,a <-- A = 255, DE = 1, BC = 65526, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A54 3008 jr nc,$2A5E <-- A = 255, DE = 1, BC = 65526, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A56 2B dec hl <-- A = 255, DE = 1, BC = 65526, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A57 79 ld a,c <-- A = 246, DE = 1, BC = 65526, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A58 86 add a,[hl] <-- A = 10, DE = 1, BC = 65526, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A59 4F ld c,a <-- A = 0, DE = 1, BC = 65290, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A5A 23 inc hl <-- A = 0, DE = 1, BC = 65290, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A5B 78 ld a,b <-- A = 255, DE = 1, BC = 65290, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A5C 8E adc a,[hl] <-- A = 0, DE = 1, BC = 65290, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A5D 47 ld b,a <-- A = 0, DE = 1, BC = 10, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2 (Carry On)

00:2A5E 3F ccf <-- A = 0, DE = 1, BC = 10, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A5F 18DC jr $2A3D <-- A = 0, DE = 1, BC = 10, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A3D 21ABCA ld hl,$CAAB <-- A = 0, DE = 1, BC = 10, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A40 CB13 rl e <-- A = 0, DE = 2, BC = 10, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A42 CB12 rl d <-- A = 0, DE = 2, BC = 10, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 2

00:2A44 35 dec [hl] <-- A = 0, DE = 2, BC = 10, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A45 281A jr z,$2A61 <-- A = 0, DE = 2, BC = 10, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A47 CB11 rl c <-- A = 0, DE = 2, BC = 20, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A49 CB10 rl b <-- A = 0, DE = 2, BC = 20, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A4B 2B dec hl <-- A = 0, DE = 2, BC = 20, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A4C 2B dec hl <-- A = 0, DE = 2, BC = 20, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A4D 79 ld a,c <-- A = 20, DE = 2, BC = 20, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A4E 96 sub [hl] <-- A = 0, DE = 2, BC = 20, HL = CAA9, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A4F 4F ld c,a <-- A = 0, DE = 2, BC = 0, HL = CAA0, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A50 23 inc hl <-- A = 0, DE = 2, BC = 0, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A51 78 ld a,b <-- A = 0, DE = 2, BC = 0, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A52 9E sbc [hl] <-- A = 0, DE = 2, BC = 0, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A53 47 ld b,a <-- A = 0, DE = 2, BC = 0, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A54 3008 jr nc,$2A5E <-- A = 0, DE = 2, BC = 0, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A5E 3F ccf <-- A = 0, DE = 2, BC = 0, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 1 (Carry On)

00:2A5F 18DC jr $2A3D <-- A = 0, DE = 2, BC = 0, HL = CAAA, CAA9 = 20, CAAA = 0, CAAB = 1 (Carry On)

00:2A3D 21ABCA ld hl,$CAAB <-- A = 0, DE = 2, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 1 (Carry On)

00:2A40 CB13 rl e <-- A = 0, DE = 5, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A42 CB12 rl d <-- A = 0, DE = 5, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 1

00:2A44 35 dec [hl] <-- A = 0, DE = 5, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 0 (Zero On)

00:2A45 281A jr z,$2A61 <-- A = 0, DE = 5, BC = 0, HL = CAAB, CAA9 = 20, CAAA = 0, CAAB = 1 (Zero On)

Well there we go. DE and BC were, as expected, 5 and 0 respectively, the rounded down answer and remainder of 100/20.

That should prove my point.