ld a,$00 ;byte to be stored
ld (mem_1),a ;load into memory
ld (mem_2),a ; addresses 1-6
ld (mem_3),a
ld (mem_4),a
ld (mem_5),a
ld (mem_6),a
Two errors in this one also. The first is that
you can use your logical
operators to zero a just as easily as
ld a,$00 . Try
xor a . It will take a and a (itself...if you
haven't caught on yet) and if a bit is set in one and not the
other, then that bit will be set in the new a . Since
both bits will be the same because it is the same register, the
result will have no bits set in it, thus zero. The second
error is one of size. Every time you load a memory address
with a , you take three bytes. That's okay if we're
only going to do it one or two times but if we want to do it
four or more times to sequential memory addresses, then you want
to try using the block copy
instruction ldir to copy a byte that's been zeroed to the
following memory address, and then incrementing both to do it
again (the address copying from having been zeroed the previous
loop). This works great for zeroing temporary memory locations
at the start of your programs or clearing parts of the screen
for graphics.
Speed up the routine even more by waiting until you've loaded
bc with 6 before clearing (hl) .
When you load bc with 6 you are clearing
the Most Significant Byte (b ) because 6
is $0006 in Hexadecimal; so, we might as well
use that to clear (hl) while we have it!
| ld hl,mem_1 ;source
ld de,mem_2 ;destination
ld bc,6 ;how many bytes to copy
;clears c also
ld (hl),b ;what's going to be copied
ldir ;(hl) to (de); inc hl & de;
; dec bc; repeate till
; bc=$00000
| |