Common machine code sequences

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
cmal
Manic Miner
Posts: 630
Joined: Fri Jun 05, 2020 1:05 am
Location: California

Re: Common machine code sequences

Post by cmal »

Einar Saukas wrote: Sun Oct 04, 2020 11:05 pm
Einar Saukas wrote: Sat Oct 03, 2020 11:05 pm DJNZ ...
DEC C
JR NZ, ...

Same size but much faster. However you need to initialize BC differently: B=n%256 and C=1+(n/256)
Ops! My math was wrong, it should be B=n%256 and C=(n+255)/256
This is a great improvement. If you compare the two methods, you're basically using half the amount of T-states (13 for djnz) for most iterations instead of 26 for every iteration. Do a bit of loop unrolling on top of that and you'll really cut down on time. Thanks for sharing.

Traditional method:
DEC BC 6
LD A,B 4
OR C 4
JR NZ, ... 12
total 26 t-states

Faster method:
DJNZ ... 13
DEC C 4
JR NZ, ... 12

total 29 t-states, but only 13 on most iterations
User avatar
cmal
Manic Miner
Posts: 630
Joined: Fri Jun 05, 2020 1:05 am
Location: California

Re: Common machine code sequences

Post by cmal »

Miktorius wrote: Sun Oct 04, 2020 8:38 pm
I think this could be a very interesting project and particularly worthwhile for:
(1) people coming to learn speccy Z80 for the first time (me!)
(2) people interested in preserving the age-old wisdom of ace coders who actually programming top spectrum games back in the 80s (and also more recently)

Any thoughts?

P.S. I could probably set up the above described website :D What's the general interest level in the community?
I like the idea and I don't think such a site exists today. There are other Z-80 sites with routines and stuff but not Spectrum-specific from what I've seen.
User avatar
cmal
Manic Miner
Posts: 630
Joined: Fri Jun 05, 2020 1:05 am
Location: California

Re: Common machine code sequences

Post by cmal »

Here's a small one-liner that's commonly used:

XOR A

instead of:

LD A,0
User avatar
ketmar
Manic Miner
Posts: 713
Joined: Tue Jun 16, 2020 5:25 pm
Location: Ukraine

Re: Common machine code sequences

Post by ketmar »

Code: Select all

  and  a,#0F
  cp   a,10
  sbc  a,#69
  daa
prolly the only snippet i've ever seen with "daa". ;-)

ah, yep, this converts low 4 bits of A to printable hex digit.
User avatar
Bedazzle
Manic Miner
Posts: 305
Joined: Sun Mar 24, 2019 9:03 am

Re: Common machine code sequences

Post by Bedazzle »

Miktorius wrote: Sun Oct 04, 2020 8:38 pm I wonder should we compile a ZX Spectrum Z80 coding cookbook for common and useful ZX Spectrum coding routines.
Wiki that can live forever seems a good choice for this.
User avatar
uglifruit
Manic Miner
Posts: 703
Joined: Thu Jan 17, 2019 12:41 pm
Location: Leicester
Contact:

Re: Common machine code sequences

Post by uglifruit »

CLEAR 23855
User avatar
Einar Saukas
Bugaboo
Posts: 3144
Joined: Wed Nov 15, 2017 2:48 pm

Re: Common machine code sequences

Post by Einar Saukas »

ROUTINE TITLE: Quaxel scrolling

ROUTINE DESCRIPTION: Scroll bitmap image 4 pixels sideways

CODE SAMPLE 1: Scroll entire screen 4 pixels to the right (compact version)

Code: Select all

    ld hl,16384
    ld bc,24
    xor a
loop:
    rrd
    inc l
    djnz loop
    inc h
    dec c
    jp nz,loop
CODE SAMPLE 2: Scroll entire screen 4 pixels to the right (unrolled loop version)

Code: Select all

    ld hl,16384
    ld b,192
    xor a
loop:
REPT 31
    rrd
    inc l
ENDR
    rrd
    inc hl
    djnz loop
CODE SAMPLE 3: Scroll entire screen 4 pixels to the left (compact version)

Code: Select all

    ld hl,16384
    ld bc,24
loop:
    dec l
    rld
    djnz loop
    inc h
    dec c
    jp nz,loop
CODE SAMPLE 4: Scroll entire screen 4 pixels to the left (unrolled loop version)

Code: Select all

    ld hl,16384
    ld b,24
    xor a
loop:
REPT 32
    dec l
    rld
ENDR
    inc l
    dec l
    jp nz,loop
    inc h
    djnz loop
NAME OF SUBMITTER: Einar Saukas
User avatar
Bedazzle
Manic Miner
Posts: 305
Joined: Sun Mar 24, 2019 9:03 am

Re: Common machine code sequences

Post by Bedazzle »

uglifruit wrote: Mon Oct 05, 2020 12:55 pm A couple of good links for this kind of thing (to get started)
It is not useful for most of people, because in Russian, so via help of google translate:
https://translate.google.com/translate? ... %23codeopt
Post Reply