Re: LZ: Loops


[Prev][Next][Index][Thread]

Re: LZ: Loops



On Wed, 25 Jun 1997 09:58:11 -0500 John Koch <john@imagin.net> writes:

>Question, how would I make a loop inside a loop that does this:
>you have a 6X6 grid, and you want to reverse all the pixels in it!
>
>in ti-basic it would be
>For(x,1,6
>For(y,1,6
>PtOn(5+x,9+y
>End:End
>
>that would put a 6X6 grid at 6,10, how would I do that in ASM?

If you didn't need it to be fast, you could just do a double
loop like this one:

            ld     b,6           ;Number of times to execute outer loop
outerloop:  ld     c,b           ;Back up "Y"
            ld     b,6           ;Number of times to execute inner loop
innerloop:  push   bc            ;Save counters
            add    b,5           ;Increase X by 5
            add    c,9           ;Increase Y by 9
            CALL_(pton)          ;In USGARD you can use "call &pton"
            pop    bc            ;Restore counters
            djnz   innerloop
            ld     b,c           ;Get "Y" back into B
            djnz   outerloop
            ...

You also need this routine somewhere:

pton:       ROM_CALL(FIND_PIXEL) ;Get pixel offset
            ld     de,$fc00
            add    hl,de         ;Point into video memory
            or     (hl)          ;A now contains modified screen byte
            ld     (hl),a        ;Move back to screen to update
            ret 

This is actually more like the BASIC code
:For(Y,6,1,-1
:For(X,6,1,-1
:PtOn(5+X,9+Y
:End:End
but it has the same effect.

If you need the routine to run fast, you should access the video
memory directly.  If you will be drawing blocks at all positions,
you could just make a 6x6 sprite and draw that with a regular
sprite routine.  

If you need to be drawing many blocks very quickly, it would be the
fastest if they were 8 pixels wide, allowing you to just do a single
byte write for each column.

If you will only be drawing at this specific location, try:

            ld     hl,$fc00+(6*16)   ;Starting address
            ld     b,6               ;Number of columns
boxloop:    ld     a,(hl)            ;Load existing byte
            or     %00000011         ;Set right 2 pixels
            ld     (hl),a            ;Save modified byte
            inc    hl                ;Next byte
            ld     a,(hl)            ;Load existing byte
            or     $11110000         ;Set right 4 pixels
            ld     (hl),a            ;Save modified byte
            djnz   boxloop

This is much faster, but only works on this specific position.


References: