Author |
Message |
|
Appelhorne
Newbie
Joined: 13 Nov 2003 Posts: 9
|
Posted: 13 Dec 2003 05:04:32 am Post subject: |
|
|
Hey i have a question about DJNZ
this code:
ld b, 8
label:
[...]
djnz label
how many times is this executed?
is it 8 times with
b = 8
b = 7
b = 6
b = 5
b = 4
b = 3
b = 2
b = 1
or nine times?:
b = 8
b = 7
b = 6
b = 5
b = 4
b = 3
b = 2
b = 1
b = 0
Thanks in forward for the help! |
|
Back to top |
|
|
sic
Advanced Newbie
Joined: 28 Jul 2003 Posts: 62
|
Posted: 13 Dec 2003 06:16:57 am Post subject: |
|
|
8 times. |
|
Back to top |
|
|
Justin W. Shattered Silence
Advanced Member
Joined: 24 May 2003 Posts: 429
|
Posted: 13 Dec 2003 10:12:47 am Post subject: |
|
|
Yes it is executed 8 times, the reason being this. When the 8 bit register a,b,c,d,e,h,or l becomes 0, it sets the zero flag.
djnz simply decrements the b register, then performs a jr nz,Label
If the value in b has not become 0 it jumps to the label defined by the djnz instruction.
jr nz = jr "not zero" . Also can be thought of as zero flag reset.
Therefore if you have a loop such as this.
ld b,2
Label:
halt
djnz Label
One the first cycle through the loop, b is decremented once leaving 1 in the register. Then because it is not zero it jumps back to "Label". Executes the loop code again but this time when it reaches the djnz, it decrements b, but this time it will set the zero flag because b has become zero. Thus it skips the jr nz part of djnz, and the loop is complete. |
|
Back to top |
|
|
Darth Android DragonOS Dev Team
Bandwidth Hog
Joined: 31 May 2003 Posts: 2104
|
Posted: 13 Dec 2003 01:10:18 pm Post subject: |
|
|
a djnz expands to something like:
ld c,8
label:
[...]
dec c
ld a,c
or a
jr nz,label |
|
Back to top |
|
|
Appelhorne
Newbie
Joined: 13 Nov 2003 Posts: 9
|
Posted: 13 Dec 2003 04:26:28 pm Post subject: |
|
|
I wanted to rewrite my astroidsbelt. I first had for each astroid separate code, like this:
;Ast1
ld b, 8
ld a, (bAst1Y)
ld l, a
ld a, (bAst1X)
ld ix, sprRock
call isprite
;Ast2
ld b, 8
ld a, (bAst2Y)
ld l, a
ld a, (bAst2X)
ld ix, sprRock
call isprite
;Ast3
ld b, 8
ld a, (bAst3Y)
ld l, a
ld a, (bAst3X)
ld ix, sprRock
call isprite
etc etc
I wanted to improve this and make it all in one routine so i rewrote astroidsbelt. But it doesn't seem to work. This is my code:
#include ti83plus.inc
#include mirage.inc
.org 9D93h
.db $BB, $6D ;compiled asmprgm token
ret
.db 1 ;MirageOS token
;MirageOS Icon
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
.db %00000000, %00000000
szDiscription:
.db "Door Jasper Smit ",0
:Astroids X's
bAstX = SafeRam1
;Astroids Y's
bAstY = bAstX + 8
;Astroid type
bAstT = bAstY + 8
xor a
ld (bAstX), a
ld (bAstX + 1), a
ld (bAstX + 2), a
ld (bAstX + 3), a
ld (bAstX + 4), a
ld (bAstX + 5), a
ld (bAstX + 6), a
ld (bAstX + 7), a
ld (bAstY), a
ld (bAstY + 1), a
ld (bAstY + 2), a
ld (bAstY + 3), a
ld (bAstY + 4), a
ld (bAstY + 5), a
ld (bAstY + 6), a
ld (bAstY + 7), a
blit:
;bcall(_grbufclr)
ld b, 8
blitloop:
push bc
blitast:
;Get astroid type
ld hl, bAstT - 1
ld c, 0
add hl, bc
ld a, (hl)
cp 0
jr Z, blitsmall
cp 1
jr Z, blitsmall
jr blitoncomming
blitsmall:
ld ix, sprSmall
jr blitnow
blitlarge:
ld ix, sprRock
jr blitnow
blitoncomming:
ld ix, sprShip
blitnow:
ld hl, bAstX - 1
add hl, bc
ld a, (hl)
ld hl, bAstY - 1
add hl, bc
ld l, (hl)
ld b, 8
;call isprite
pop bc
djnz blitloop
flip:
;call ifastcopy
CheckAst:
ld b, 8
CheckLoop:
ld c, 0
ld hl, bAstY - 1
add hl, bc
ld a, (hl)
inc a
ld (hl), a
cp 56
jr NZ, SkipReset
;Reset astroid
xor a
ld (hl), a
ld hl, bAstX - 1
add hl, bc
push bc
push hl
ld b, 87
call irandom
pop hl
pop bc
ld (hl), a
SkipReset:
djnz CheckLoop
;Loop again
jr blit
sprRock:
.db %00011000
.db %00111100
.db %01111110
.db %11111111
.db %11111111
.db %01111110
.db %00111100
.db %00011000
sprSmall:
.db %00000000
.db %00000000
.db %00010000
.db %00111000
.db %01111100
.db %01111100
.db %00111000
.db %00010000
sprShip:
.db %00011000
.db %00011000
.db %00111100
.db %00011000
.db %01011010
.db %11111111
.db %11100111
.db %11000011
.END
I read through the code for about 100 times, but i can't see anything wrong. I thought the problem was maby in the djnz, but it should work this way. Do you know what is wrong?? |
|
Back to top |
|
|
Justin W. Shattered Silence
Advanced Member
Joined: 24 May 2003 Posts: 429
|
Posted: 13 Dec 2003 08:06:49 pm Post subject: |
|
|
Darth Android wrote: a djnz expands to something like:
ld c,8
label:
[...]
dec c
ld a,c
or a
jr nz,label
Did you not read my above post Darth?
If you use a dec instruction on any of the following 8 bit registers a,b,c,d,e,h,or l
and the register becomes 0 as a result of the decrement, the zero flag is set.
Therefore, djnz would work like this.
ld b,5
Label:
[inner loop code]
dec b
jr nz,Label
While b does not equal zero, it will continue to jump to "Label". Once b becomes 0 it will set the zero flag and skip the "jr nz". |
|
Back to top |
|
|
Darth Android DragonOS Dev Team
Bandwidth Hog
Joined: 31 May 2003 Posts: 2104
|
Posted: 13 Dec 2003 08:49:10 pm Post subject: |
|
|
well, the comment after szDescription (Asteroid X's) had a : (colon) in front of it instead of a ; (semi-colon) |
|
Back to top |
|
|
sgm
Calc Guru
Joined: 04 Sep 2003 Posts: 1265
|
Posted: 15 Dec 2003 01:03:12 pm Post subject: |
|
|
Justin W. wrote: While b does not equal zero, it will continue to jump to "Label". Once b becomes 0 it will set the zero flag and skip the "jr nz".
DJNZ doesn't set the flags.
JFF, the MC68000 counterpart DBcc will loop until the register becomes -1 (or cc is true, but enough about that). |
|
Back to top |
|
|
Justin W. Shattered Silence
Advanced Member
Joined: 24 May 2003 Posts: 429
|
Posted: 15 Dec 2003 09:26:58 pm Post subject: |
|
|
Yes you are right about the flags being preserved sigma. I was explaining how it works relatively. I was simply giving a different perspective on it. |
|
Back to top |
|
|
|