Author Topic: loop in ASM ???  (Read 618 times)

0 Members and 1 Guest are viewing this topic.

Offline SkulleateR

  • 464 Plus
  • *****
  • Posts: 460
  • Country: de
  • Liked: 225
  • Likes Given: 129
loop in ASM ???
« on: 16: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 :


Code: [Select]
...
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 !

Offline SOS

  • Supporter
  • 464 Plus
  • *
  • Posts: 388
  • Country: de
  • Identity lost
    • index.php?action=treasury
  • Liked: 256
  • Likes Given: 276
Re: loop in ASM ???
« Reply #1 on: 16:07, 31 May 21 »
Does A get corrupted from the firmware call ?
Correct,
Place "PUSH AF" before the CALL & "POP AF" after

Offline SkulleateR

  • 464 Plus
  • *****
  • Posts: 460
  • Country: de
  • Liked: 225
  • Likes Given: 129
Re: loop in ASM ???
« Reply #2 on: 16: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 ?
« Last Edit: 16:23, 31 May 21 by SkulleateR »

Offline SOS

  • Supporter
  • 464 Plus
  • *
  • Posts: 388
  • Country: de
  • Identity lost
    • index.php?action=treasury
  • Liked: 256
  • Likes Given: 276
Re: loop in ASM ???
« Reply #3 on: 16:43, 31 May 21 »
Oh, 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


Offline Targhan

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.402
  • Country: fr
  • Liked: 1331
  • Likes Given: 195
Re: loop in ASM ???
« Reply #4 on: 16: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.
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

Offline SkulleateR

  • 464 Plus
  • *****
  • Posts: 460
  • Country: de
  • Liked: 225
  • Likes Given: 129
Re: loop in ASM ???
« Reply #5 on: 16: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:

Offline GUNHED

  • 6128 Plus
  • ******
  • Posts: 2.795
  • Country: de
  • Reincarnation of TFM
    • FutureOS - The quickest OS for the CPC and Plus
  • Liked: 1300
  • Likes Given: 3034
Re: loop in ASM ???
« Reply #6 on: 19:21, 31 May 21 »
Speed chart...
http://futureos.de --> Get the revolutionary FutureOS (Update: 2021.01.24)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.07.15)

Offline Animalgril987

  • Supporter
  • CPC6128
  • *
  • Posts: 218
  • Country: gb
  • Liked: 108
  • Likes Given: 276
Re: loop in ASM ???
« Reply #7 on: 20:48, 31 May 21 »
Always best to assume firmware calls will alter registers, unless otherwise stated. :D

Offline Animalgril987

  • Supporter
  • CPC6128
  • *
  • Posts: 218
  • Country: gb
  • Liked: 108
  • Likes Given: 276
Re: loop in ASM ???
« Reply #8 on: 20:51, 31 May 21 »
@SkulleateR : Good luck with the assembly language. It's a LOT harder than Basic (at least for me...) :D

Offline Animalgril987

  • Supporter
  • CPC6128
  • *
  • Posts: 218
  • Country: gb
  • Liked: 108
  • Likes Given: 276
Re: loop in ASM ???
« Reply #9 on: 20:56, 31 May 21 »
@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 :-\

Online eto

  • Supporter
  • 464 Plus
  • *
  • Posts: 430
  • Country: de
  • Liked: 272
  • Likes Given: 243
Re: loop in ASM ???
« Reply #10 on: 21:26, 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.

Offline zhulien

  • 6128 Plus
  • ******
  • Posts: 787
  • Country: au
  • aka Vorax
    • 8bitology
  • Liked: 319
  • Likes Given: 339
Re: loop in ASM ???
« Reply #11 on: 13: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:

Code: [Select]
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.
« Last Edit: 13:53, 03 June 21 by zhulien »

Offline roudoudou

  • 6128 Plus
  • ******
  • Posts: 1.122
  • Country: fr
    • urban exploration
  • Liked: 1553
  • Likes Given: 900
Re: loop in ASM ???
« Reply #12 on: 13:53, 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

Code: [Select]
repeat 16 : ldi : rend
use RASM, the best assembler ever made :p

I will survive

Offline redbox

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.788
  • Country: gb
    • redbox
  • Liked: 407
  • Likes Given: 287
Re: loop in ASM ???
« Reply #13 on: 16: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:

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

Offline roudoudou

  • 6128 Plus
  • ******
  • Posts: 1.122
  • Country: fr
    • urban exploration
  • Liked: 1553
  • Likes Given: 900
Re: loop in ASM ???
« Reply #14 on: 16:29, 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:

Code: [Select]
        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
use RASM, the best assembler ever made :p

I will survive

Offline redbox

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.788
  • Country: gb
    • redbox
  • Liked: 407
  • Likes Given: 287
Re: loop in ASM ???
« Reply #15 on: 16:34, 03 June 21 »
16 bits counters loops must be optimised!

Get it working first, then optimise!

Offline roudoudou

  • 6128 Plus
  • ******
  • Posts: 1.122
  • Country: fr
    • urban exploration
  • Liked: 1553
  • Likes Given: 900
Re: loop in ASM ???
« Reply #16 on: 16:49, 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 )
use RASM, the best assembler ever made :p

I will survive