# Re: A85: @!#%! Code Part 2

PXGray@aol.com wrote:
>
> Here's more of my seemingly defective source code.  I am trying to initialize
> a portion of memory to use as a 2 dimensional array with all zeros, but it
> crashes the calc all the time.
>
> ARRAY = TEXT_MEM+5
>
>  call CLEARLCD
>  ld a,1
>  ld (CUR_POS),a

> Loop:
>  ld b,9
>  ld hl,(ARRAY)
>  ld a,b
>  push bc
>  ld b,0
>  ld c,a
>  pop bc
>  ld (hl),0
>  djnz Loop
> Loop2:
>  ld b,6
>  ld hl,(ARRAY)
>  push bc
>  ld b,0
>  ld c,9
>  pop bc
>  ld c,b
>  push bc
>  ld b,0
>  ld (hl),0
>  djnz Loop2
>
> -Gray

I read some onther people telling you of other ways to do this, but that
is not the same as telling you what you have done wrong here.  There are
several ways to do what you want and only you can deside what you like
best.  I assume you want to learn Z80 assembly language, ao here it
goes.

Your loops use the DJNZ instruction to decrement the loop count.  That
count
is stored in the B register.  You load B with 9 as the first line in
your
first loop, but THIS RELOADS THE LOOP COUNTER EVERY PASS!  You really
want
to load B before the label Loop: or Loop2:.

Your second loop has one more problem in addition to that.  In order to
use
B in you loop you push it onto the stack, but you have two PUSHs and
only
one POP.  First I must say that it is very dangerous to use the loop
count
for anything but loop pass counting!  To use the value in B is fine,
just
don't use B to hold anything but that value.  Using PUSHs and POPs is
like
using parenthesis in an equation, always be sure to count them so they
always are used in pairs.

Good Luck with learning to program in assembly.  It is the only language
that makes me feal as if I am really in control of my machine.

Tom Decker
KWM Electronics
1355 West 8040 South
West Jordan, UT
USA   84088

References: