CPCWiki forum

General Category => Programming => Topic started by: SkulleateR on 14:02, 31 May 21

Title: loop in ASM ???
Post by: SkulleateR on 14:02, 31 May 21
Ok, poking a bit around here and I really don't get it ....


I'll try to do a loop like this :



...
ld a,20


loop:


ld hl,1
ld de,0
call &BBED
dec a
cp 0
jp nz,loop
ret



Well, it does what it should do (plot at relative coordinates 0,1) but then it runs forever and never quits the loop ? Does A get corrupted from the firmware call ? HELP !
Title: Re: loop in ASM ???
Post by: SOS on 14:07, 31 May 21
Quote from: SkulleateR on 14:02, 31 May 21Does A get corrupted from the firmware call ?
Correct,
Place "PUSH AF" before the CALL & "POP AF" after
Title: Re: loop in ASM ???
Post by: SkulleateR on 14:19, 31 May 21
ok thx  :picard:


Oh, btw .... whats faster in this case ? using the stack or copy A to mem to preserve it, like LD (&2500),A ?
Title: Re: loop in ASM ???
Post by: SOS on 14:43, 31 May 21
Quote from: SkulleateR on 14:19, 31 May 21Oh, btw .... whats faster in this case ? using the stack or copy A to mem to preserve it, like LD (&2500),A ?

cp 0  => "or a" its faster
jp nz,loop  => jr nz,loop is faster

I'm not sure about the Cycle-Costs of
loop:
ld hl,1
ld de,0
ld (&2500),a
call &BBED
ld a,(&2500)
dec a
ld (&2500),a
cp 0
jp nz,loop
ret

vs. the Push-Pop-Version.
But i will bet, the Push/Pop is faster and generates much more better readable & shorter code.
When you do this much more often, in bigger programs, you can sometimes get lost of memory need - waste memory

IMHO my first approch will more like:

ls b,20
loop:
ld hl,1
ld de,0
push bc
call &BBED
pop bc
djnz loop
ret

You need also PushPop, take a look here, which registers are untouched:
http://k1.spdns.de/Vintage/Schneider%20CPC/Das%20Schneider%20CPC%20Systembuch/z160.htm#S

Title: Re: loop in ASM ???
Post by: Targhan on 14:50, 31 May 21
On a sidenote, you don't need the CP 0 after the DEC A, as the latter already sets/resets the Z flag. Fortunately, doing LD (&xxx),a does not modify this flag, so this is safe.
Title: Re: loop in ASM ???
Post by: SkulleateR on 14:55, 31 May 21
Already did a loop with "djnz" now, looks good thanks ...


@Thargan


I will try to memorize that, I'm like 3 hours into ASM now and I had NO clue before ... think I'm learning fast  :laugh:
Title: Re: loop in ASM ???
Post by: GUNHED on 17:21, 31 May 21
Speed chart...
Title: Re: loop in ASM ???
Post by: Animalgril987 on 18:48, 31 May 21
Always best to assume firmware calls will alter registers, unless otherwise stated. :D
Title: Re: loop in ASM ???
Post by: Animalgril987 on 18:51, 31 May 21
@SkulleateR (https://www.cpcwiki.eu/forum/index.php?action=profile;u=3124) : Good luck with the assembly language. It's a LOT harder than Basic (at least for me...) :D
Title: Re: loop in ASM ???
Post by: Animalgril987 on 18:56, 31 May 21
@GUNHED (https://www.cpcwiki.eu/forum/index.php?action=profile;u=2029)  Thanks for the timings ;D
However, I can't make out what the ASCII art at the top of the page is supposed be :-\
Title: Re: loop in ASM ???
Post by: eto on 19:26, 31 May 21
Quote from: Animalgril987 on 18:51, 31 May 21
@SkulleateR (https://www.cpcwiki.eu/forum/index.php?action=profile;u=3124) : Good luck with the assembly language. It's a LOT harder than Basic (at least for me...) :D


Agreed AND diasagreed at the same time. ;-)


Some things are utterly complicated if you are doing them in Assembler but others are so much simpler. I just replaced a relatively complex calculation in Basic by an Assembler routine. It's much much (much) faster, smaller AND simpler.
Title: Re: loop in ASM ???
Post by: zhulien on 11:49, 03 June 21
google unrolling loops, it takes a more ram but depending what you are doing is the fastest option. 


for something that you know you want always a fixed number of iterations unrolling them can be worth consideration.


eg:

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed

ld hl,1
ld de,0
call &bbed


Note: some assemblers let you put multiple instructions on a same line which isn't really standard for assembler but makes such unrolling more easy on the eyes.
Title: Re: loop in ASM ???
Post by: roudoudou on 11:53, 03 June 21
Quote from: zhulien on 11:49, 03 June 21

Note: some assemblers let you put multiple instructions on a same line which isn't really standard for assembler but makes such unrolling more reasonable.
Many assemblers let you use REPEAT / REND directive in order to repeat instructions or blocks. You may nest them by the way

repeat 16 : ldi : rend
Title: Re: loop in ASM ???
Post by: redbox on 14:24, 03 June 21
Worth noting here there will come a time when you want to loop something more than 255 times.

For this, you can use a 16-bit loop counter:


        ld      bc,1000
loop:
        push    bc
        ; do something
        pop     bc
        dec     bc
        ld      a,b
        or      c
        jp      nz,loop

Title: Re: loop in ASM ???
Post by: roudoudou on 14:29, 03 June 21
Quote from: redbox on 14:24, 03 June 21
Worth noting here there will come a time when you want to loop something more than 255 times.

For this, you can use a 16-bit loop counter:


        ld      bc,1000
loop:
        push    bc
        ; do something
        pop     bc
        dec     bc
        ld      a,b
        or      c
        jp      nz,loop

16 bits counters loops must be optimised!
https://wikiti.brandonw.net/index.php?title=Z80_Optimization#Looping_with_16_bit_counter
Title: Re: loop in ASM ???
Post by: redbox on 14:34, 03 June 21
Quote from: roudoudou on 14:29, 03 June 2116 bits counters loops must be optimised!

Get it working first, then optimise!
Title: Re: loop in ASM ???
Post by: roudoudou on 14:49, 03 June 21
Quote from: redbox on 14:34, 03 June 21
Get it working first, then optimise!
with a macro, you do it once, then it's always optimised (when it works  :P )
Powered by SMFPacks Menu Editor Mod