Common machine code sequences
Common machine code sequences
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?
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?
-
- Microbot
- Posts: 134
- Joined: Tue Jun 09, 2020 6:14 am
- Contact:
Re: Common machine code sequences
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.
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.
- Einar Saukas
- Bugaboo
- Posts: 3167
- Joined: Wed Nov 15, 2017 2:48 pm
Re: Common machine code sequences
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)
Re: Common machine code sequences
This one is neat! Good to know. I would normally do something like this, but it zeroes the B register: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
LD B,0
AND A ;clear carry flag
ADC HL,BC
- Ast A. Moore
- Rick Dangerous
- Posts: 2641
- Joined: Mon Nov 13, 2017 3:16 pm
Re: Common machine code sequences
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.
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.
Re: Common machine code sequences
Oh yes, you're right. It's subtraction that you need to clear the carry. Thanks Ast.Ast A. Moore wrote: ↑Sun Oct 04, 2020 12:43 amThe Z80 can do 16-bit addition without Carry. It’s the subtraction operation that can only do it with Carry.
Re: Common machine code sequences
Some dumb ones...
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.
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.
Code: Select all
ld iy,23610
im 1
ld hl,10072
exx
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.
Re: Common machine code sequences
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 What's the general interest level in the community?
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 What's the general interest level in the community?
- Einar Saukas
- Bugaboo
- Posts: 3167
- Joined: Wed Nov 15, 2017 2:48 pm
Re: Common machine code sequences
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 SAMPLE 2: Scroll entire screen 2 pixels to the right (unrolled loop version)
CODE SAMPLE 3: Scroll entire screen 2 pixels to the left (compact version)
CODE SAMPLE 4: Scroll entire screen 2 pixels to the left (unrolled loop version)
NAME OF SUBMITTER: Einar Saukas
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: 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: 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: 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
- Einar Saukas
- Bugaboo
- Posts: 3167
- Joined: Wed Nov 15, 2017 2:48 pm
Re: Common machine code sequences
Ops! My math was wrong, it should be B=n%256 and C=(n+255)/256Einar Saukas wrote: ↑Sat Oct 03, 2020 11:05 pmDJNZ ...
DEC C
JR NZ, ...
Same size but much faster. However you need to initialize BC differently: B=n%256 and C=1+(n/256)
Re: Common machine code sequences
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.Einar Saukas wrote: ↑Sun Oct 04, 2020 11:05 pmOps! My math was wrong, it should be B=n%256 and C=(n+255)/256Einar 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)
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
Re: Common machine code sequences
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.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 What's the general interest level in the community?
Re: Common machine code sequences
Here's a small one-liner that's commonly used:
XOR A
instead of:
LD A,0
XOR A
instead of:
LD A,0
Re: Common machine code sequences
Code: Select all
and a,#0F
cp a,10
sbc a,#69
daa
ah, yep, this converts low 4 bits of A to printable hex digit.
Re: Common machine code sequences
A couple of good links for this kind of thing (to get started)
(That I have bookmarked, plus the associated parent URLs)
http://z80-heaven.wikidot.com/optimization
http://z80-heaven.wikidot.com/math
https://wikiti.brandonw.net/index.php?title=Z80_Optimization
https://wikiti.brandonw.net/index.php?title=Z80_Routines:Math:Division
(That I have bookmarked, plus the associated parent URLs)
http://z80-heaven.wikidot.com/optimization
http://z80-heaven.wikidot.com/math
https://wikiti.brandonw.net/index.php?title=Z80_Optimization
https://wikiti.brandonw.net/index.php?title=Z80_Routines:Math:Division
CLEAR 23855
- Einar Saukas
- Bugaboo
- Posts: 3167
- Joined: Wed Nov 15, 2017 2:48 pm
Re: Common machine code sequences
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 SAMPLE 2: Scroll entire screen 4 pixels to the right (unrolled loop version)
CODE SAMPLE 3: Scroll entire screen 4 pixels to the left (compact version)
CODE SAMPLE 4: Scroll entire screen 4 pixels to the left (unrolled loop version)
NAME OF SUBMITTER: Einar Saukas
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: Select all
ld hl,16384
ld b,192
xor a
loop:
REPT 31
rrd
inc l
ENDR
rrd
inc hl
djnz loop
Code: Select all
ld hl,16384
ld bc,24
loop:
dec l
rld
djnz loop
inc h
dec c
jp nz,loop
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
Re: Common machine code sequences
It is not useful for most of people, because in Russian, so via help of google translate:
https://translate.google.com/translate? ... %23codeopt