News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_SkulleateR

loop in ASM ???

Started by SkulleateR, 14:02, 31 May 21

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

SkulleateR

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 !

SOS

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

SkulleateR

#2
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 ?

SOS

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


Targhan

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.
Targhan/Arkos

Arkos Tracker 2.0.1 now released! - Follow the news on Twitter!
Disark - A cross-platform Z80 disassembler/source converter
FDC Tool 1.1 - Read Amsdos files without the system

Imperial Mahjong
Orion Prime

SkulleateR

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:

GUNHED

Speed chart...
http://futureos.de --> Get the revolutionary FutureOS (Update: 2023.11.30)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

Animalgril987

Always best to assume firmware calls will alter registers, unless otherwise stated. :D

Animalgril987

@SkulleateR : Good luck with the assembly language. It's a LOT harder than Basic (at least for me...) :D

Animalgril987

@GUNHED  Thanks for the timings ;D
However, I can't make out what the ASCII art at the top of the page is supposed be :-\

eto

Quote from: Animalgril987 on 18:51, 31 May 21
@SkulleateR : 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.

zhulien

#11
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.

roudoudou

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
My pronouns are RASM and ACE

redbox

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


roudoudou

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
My pronouns are RASM and ACE

redbox

Quote from: roudoudou on 14:29, 03 June 2116 bits counters loops must be optimised!

Get it working first, then optimise!

roudoudou

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 )
My pronouns are RASM and ACE

Powered by SMFPacks Menu Editor Mod