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

Common machine code sequences

Post by cmal »

The topic that [mention]TMD2003[/mention] started on his beginnings into learning machine code reminded me of my own tip-toeing into the mysteries of MC. The post has now morphed into an almost fully fledged machine code tutorial on it's own, which is a good thing. Any beginner can read that post and certainly learn a lot from it.

It gave me an idea on a this topic which might have been done before. When you start programming in assembly, you start to use common sequences of instructions to accomplish something. Or you might be debugging someone else's code and recognize a sequence of instructions which you've seen before. You might be so familiar with them that you almost skip over them without thinking.

For example the very common BC loop has probably been used by every Z80 programmer:

DEC BC
LD A,B
OR C
JR NZ, ...

Or if you see something like:
INC H
LD A,H
AND 07H
RET NZ
You know right away that you're most likely dealing with a display address

What are some of the other common sequences of instructions that you use a lot or see often in other people's code? Is there something that you see a lot of but you don't know why it's used? Or is there something you run into a lot and you have a small tweak to make it faster or shorter?
Nienn Heskil
Microbot
Posts: 134
Joined: Tue Jun 09, 2020 6:14 am
Contact:

Re: Common machine code sequences

Post by Nienn Heskil »

Sprites, definitely. The patterns for LD, OR/XOR and Masked are the same in about 99% cases, and instantly recognizable.

Block fill:
LD HL,ADDR
LD DE,ADDR+1
LD BC,LEN-1
LD (HL),VAL
LDIR

MSB address conversions Screen<->Attr.

R16+R8 arithmetics:
LD A,L
ADD C
LD L,A
ADC H
SUB L
LD H,A

Wait for keypress:
XOR A
IN A,(#FE)
CPL
AND #1F
JR Z,$-5

Also this sorta thing in loaders: RES 4,(IY+1) - set 'USR0' mode (although I think it's not sufficient on it's own). It's like a common superstition lol.
User avatar
Einar Saukas
Bugaboo
Posts: 3141
Joined: Wed Nov 15, 2017 2:48 pm

Re: Common machine code sequences

Post by Einar Saukas »

cmal wrote: Sat Oct 03, 2020 7:26 pm DEC BC
LD A,B
OR C
JR NZ, ...

[...] Or is there something you run into a lot and you have a small tweak to make it faster or shorter?
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)
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 »

Nienn Heskil wrote: Sat Oct 03, 2020 7:35 pm MSB address conversions Screen<->Attr.

R16+R8 arithmetics:
LD A,L
ADD C
LD L,A
ADC H
SUB L
LD H,A
This one is neat! Good to know. I would normally do something like this, but it zeroes the B register:
LD B,0
AND A ;clear carry flag
ADC HL,BC
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: Common machine code sequences

Post by Ast A. Moore »

cmal wrote: Sun Oct 04, 2020 12:19 am AND A ;clear carry flag
ADC HL,BC
The Z80 can do 16-bit addition without Carry. It’s the subtraction operation that can only do it with Carry.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
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 »

Ast A. Moore wrote: Sun Oct 04, 2020 12:43 am
cmal wrote: Sun Oct 04, 2020 12:19 am AND A ;clear carry flag
ADC HL,BC
The Z80 can do 16-bit addition without Carry. It’s the subtraction operation that can only do it with Carry.
Oh yes, you're right. It's subtraction that you need to clear the carry. Thanks Ast.
zup
Manic Miner
Posts: 211
Joined: Wed Jul 08, 2020 8:42 am

Re: Common machine code sequences

Post by zup »

Some dumb ones...

Code: Select all

ld iy,23610
im 1
ld hl,10072
exx
This mark the end of some loaders (i.e. some speedlock). They use iy as index to blocks, and restores it (and hl and interrupts) before passing control to the game.

If you break a program while loading and see iy and/or hl on different values thatn 23610 and 10072, it might worth a shot putting a conditional breakpoint.

Code: Select all

ld a,d
or e
jr z / jr nz / ret z / ret nz ... anything related to z flag.
On most cases, it signals the end of a loading loop (ROM, turbo, even feared Alkatraz loaders). On ROM derived loaders (i.e.: most turbo loaders), add cp1 / ret near that instructions.
Miktorius
Drutt
Posts: 6
Joined: Sun Oct 04, 2020 4:06 pm

Re: Common machine code sequences

Post by Miktorius »

Anybody that's done a bit of coding in other languages has probably come across the O'Reilly programming cookbook books.

For example:

PHP Cookbook
https://www.amazon.co.uk/PHP-Cookbook-S ... 876&sr=8-1

Python Cookbook
https://www.amazon.co.uk/Python-Cookboo ... 51&sr=8-12

et cetera

I wonder should we compile a ZX Spectrum Z80 coding cookbook for common and useful ZX Spectrum coding routines.

All it would take would be someone to set up a website with a simple form containing the following fields:

ROUTINE TITLE
ROUTINE DESCRIPTION (WHAT THE ROUTINE DOES)
CODE SAMPLE 1
CODE SAMPLE 2
CODE SAMPLE 3
NAME OF SUBMITTER (PERSON WHO SUBMITTED THE CODE SAMPLE)
EMAIL OF SUBMITTER

Along with a PHP backend script to capture all the info submitted by different people.
The info could be compiled online in a single website and later compiled into a PDF for free and also a book that people could buy from lulu.com

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?
User avatar
Einar Saukas
Bugaboo
Posts: 3141
Joined: Wed Nov 15, 2017 2:48 pm

Re: Common machine code sequences

Post by Einar Saukas »

ROUTINE TITLE: Dixel scrolling

ROUTINE DESCRIPTION: Scroll bitmap image 2 pixels sideways

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

Code: Select all

    ld hl,16384
    ld bc,24
loop:
    rr (hl)
    ex af,af'
    rr (hl)
    ex af,af'
    inc l
    djnz loop
    inc h
    dec c
    jp nz,loop
CODE SAMPLE 2: Scroll entire screen 2 pixels to the right (unrolled loop version)

Code: Select all

    ld hl,16384
    ld b,192
loop:
REPT 31
    rr (hl)
    ex af,af'
    rr (hl)
    ex af,af'
    inc l
ENDR
    rr (hl)
    ex af,af'
    rr (hl)
    ex af,af'
    inc hl
    dec b
    jp nz,loop
CODE SAMPLE 3: Scroll entire screen 2 pixels to the left (compact version)

Code: Select all

    ld hl,16384+255
    ld bc,24
loop:
    rl (hl)
    ex af,af'
    rl (hl)
    ex af,af'
    dec l
    djnz loop
    inc h
    dec c
    jp nz,loop
CODE SAMPLE 4: Scroll entire screen 2 pixels to the left (unrolled loop version)

Code: Select all

    ld hl,16384+255
    ld a,l
    ld b,24
loop:
REPT 32
    rl (hl)
    ex af,af'
    rl (hl)
    ex af,af'
    dec l
ENDR
    cp l
    jp nz,loop
    inc h
    dec b
    jp nz,loop
NAME OF SUBMITTER: Einar Saukas
User avatar
Einar Saukas
Bugaboo
Posts: 3141
Joined: Wed Nov 15, 2017 2:48 pm

Re: Common machine code sequences

Post by Einar Saukas »

Einar Saukas wrote: Sat Oct 03, 2020 11:05 pm
cmal wrote: Sat Oct 03, 2020 7:26 pm DEC BC
LD A,B
OR C
JR NZ, ...

[...] Or is there something you run into a lot and you have a small tweak to make it faster or shorter?
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
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: 711
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: 3141
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