40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Y'know, other stuff, Sinclair related.
Post Reply
User avatar
PeterJ
Site Admin
Posts: 6879
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by PeterJ »

Did anyone back this on Kickstarter? Mine arrived today.

I'm a bit disappointed to be honest. The section on the Next is all rather vague, and the Next versions of the scrolling attribute code seem to use lots of ldd and ldi without any explanation as to why it needs to be different for the Next. I also feel the the listings would look much clearer in a good clear monospace font. The machine code loader and a few other listings are printed in what looks like a a Sinclair Spectrum font, but it's not consistent.

I'm still glad I backed it, but it may end up on eBay!
✓ PROCESSED
User avatar
Mpk
Dynamite Dan
Posts: 1008
Joined: Tue Feb 09, 2021 8:10 am

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by Mpk »

I backed it, but mostly so I can casually leave it lying about to impress people with my machine code knowledge.

I do get a Next soon, allegedly, so maybe I'll use it to resurrect one of my stalled projects.
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by ParadigmShifter »

I wasn't impressed by the original edition to be honest and don't have a Next anyway ;)

You'd be better learning machine code from a decent online tutorial anyway like Jonathan Cauldwell's or ChibiAkumas, both of which are very good. Obviously Rodney Zaks' book for Z80 coding in general.

Other sites I bookmark:

http://z80-heaven.wikidot.com/instructions-set for what the instructions do
https://map.grauw.nl/resources/z80instr.php for instruction timings
User avatar
PeterJ
Site Admin
Posts: 6879
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by PeterJ »

Thanks @ParadigmShifter,

I know the basics of assembly language (although nothing like to your level), I just backed it to add to my library and not really as a learning tool. Jonathan's book is good but does have some errors.

I learnt to my basic level with Cracking the Code by John Wilson, The Kobrasoft Assembler Guide, First Steps in Z80 Assembly Language by Darryl Sloan, and the same authors YouTube videos on creating a simple connect 4 game. The Z80 instruction guide from @Joefish is also a useful resource.

I've tried ChibiAkumas, but he needs to do something about the readability of his website IMHO. He maybe a great coder, but the design stinks. (Sorry).
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by ParadigmShifter »

Was more directed to Mpk I think.

I wouldn't say the routines in the original were very good though from memory, maybe they have updated them. I think they made them as short as possible since it was originally assumed you'd type them all in using a hex editor or somesuch.

Does the next stuff use a load of LDD/LDI instead of LDIR/LDDR or something? Cos multiple LDI/LDD is faster than LDIR/LDDR so it's probably optimised for speed rather than size? Although using the stack to copy would be faster still I expect (requires interrupts off or you know no interrupt will happen when you do it).
User avatar
PeterJ
Site Admin
Posts: 6879
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by PeterJ »

ParadigmShifter wrote: Mon Dec 11, 2023 10:02 pm Was more directed to Mpk I think.
OK! My bad.
Does the next stuff use a load of LDD/LDI
Yes it does. I can't really show an example of the code as it's still for sale and they say 'personal use only', but one of the Next specific scroll routines is using 32 x ldd (I've used LDIR before - and very powerful it is too), but not ldd or ldi.
User avatar
uglifruit
Manic Miner
Posts: 703
Joined: Thu Jan 17, 2019 12:41 pm
Location: Leicester
Contact:

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by uglifruit »

I concur about disappointment in the formatting. It seems a bit of a missed opportunity to make something rather nice to read that *would* be a useful tutorial. Obviously it's based upon the book from way back, but given it's a new edition there could have been more care taken over the page breaks (which are sometimes in awful places), and things like when the decimal opcodes need to be potentially changed it doesn't say what the assembly should be changed to.

eg. Page 75

Code: Select all

Comments
This routine is useful for highlighting areas of text or graphics. To scroll the top
22 lines only, change the 224* to 160.

Machine Code Listing
Label    Assembly language    Numbers to be entered
          ld hl, 22560              33 32 88
          ld de, 22528              17 0 88
          ld bc, 736                1 224* 2
....which is very unhelpful. The comment really should've said that it'd be ld bc,672 surely.

Incidentally, that monospaced codeblock is probably nicer to read than in the book!
CLEAR 23855
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by ParadigmShifter »

32xLDD = 32x16 T-States = 512 T-States
32 repeats of LDIR is 31x21 T-States (BC not 0) + 16 T-States = 667 T-States so ~30% slower?

Assuming Next chip has same timings as a Z80 (I dunno if it does or not).

One routine will use LDI and the other will use LDD I expect since when copying data you want to go backwards for scrolling in one direction and forwards for the other, since the buffers overlap (I expect).
User avatar
PeterJ
Site Admin
Posts: 6879
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by PeterJ »

uglifruit wrote: Mon Dec 11, 2023 10:13 pm there could have been more care taken over the page breaks (which are sometimes in awful places)
Agreed. There is one listing which is complete apart from 'ret' which is on the subsequent page!
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by ParadigmShifter »

uglifruit wrote: Mon Dec 11, 2023 10:13 pm I concur about disappointment in the formatting. It seems a bit of a missed opportunity to make something rather nice to read that *would* be a useful tutorial. Obviously it's based upon the book from way back, but given it's a new edition there could have been more care taken over the page breaks (which are sometimes in awful places), and things like when the decimal opcodes need to be potentially changed it doesn't say what the assembly should be changed to.

eg. Page 75

Code: Select all

Comments
This routine is useful for highlighting areas of text or graphics. To scroll the top
22 lines only, change the 224* to 160.

Machine Code Listing
Label    Assembly language    Numbers to be entered
          ld hl, 22560              33 32 88
          ld de, 22528              17 0 88
          ld bc, 736                1 224* 2
....which is very unhelpful. The comment really should've said that it'd be ld bc,672 surely.

Incidentally, that monospaced codeblock is probably nicer to read than in the book!
Well it's assuming you're not using an assembler (probably not a good assumption these days).

672 is 160 + 2 * 256 and 736 is 224 + 2 * 256 of course though.

Agree it should have said change it to ld bc, 672 if you have an assembler though.

EDIT: Anyway you are better off understanding what the code is doing and what you need to change in the assembly to make it do something a bit different. 736 is 23*32 and 672 is 21*32, so if you understand why those numbers are there you can change it to scroll any number of lines you choose (in fact you can write a routine that takes the number of lines as a parameter and work out BC yourself, and change it so it does not scroll the top N lines, whatever you like). That will be more instructive than just using the routines "as is".
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by ParadigmShifter »

PeterJ wrote: Mon Dec 11, 2023 10:07 pm OK! My bad.



Yes it does. I can't really show an example of the code as it's still for sale and they say 'personal use only', but one of the Next specific scroll routines is using 32 x ldd (I've used LDIR before - and very powerful it is too), but not ldd or ldi.
With an assembler you should use

Code: Select all

    REPT 32
    ldd
    ENDR
(some assemblers may use DUP instead of REPT and maybe something for ENDR in that case too) rather than copy/pasting LDD 32 times anyway :)

EDIT: Pretty sure posting code snippets is fine for learning purposes (the original edition is available as a PDF on this site after all) as long as you don't paste the whole book or large parts of it.

I think someone mentioned there was a Czech version of the book where they improved some of the routines as well (another thread here). They still weren't "the best" way of doing things though tbh... But definition of "the best" can vary depending on whether speed or size is more important...
Last edited by ParadigmShifter on Mon Dec 11, 2023 10:47 pm, edited 2 times in total.
User avatar
PeterJ
Site Admin
Posts: 6879
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by PeterJ »

Thanks @ParadigmShifter,

The book includes a Basic machine code loader, so as you assumed earlier, it's not written with in assembler in mind (The same as the original).
User avatar
uglifruit
Manic Miner
Posts: 703
Joined: Thu Jan 17, 2019 12:41 pm
Location: Leicester
Contact:

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by uglifruit »

PeterJ wrote: Mon Dec 11, 2023 10:38 pm The book includes a Basic machine code loader, so as you assumed earlier, it's not written with in assembler in mind (The same as the original).
...which sort of makes the new edition, with it's next code that *does* assume you've an assembler somewhat confused as to who it is aimed at.
CLEAR 23855
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by ParadigmShifter »

Well you should use an assembler (and develop targeting an emulator and produce a WAV file from a TAP if you want to test on real hardware), it's just mad not to do that ;)

Surely the Next has an assembler built in or a decent one available? And hopefully a debugger? Although if there is no debug break like on a real spectrum that might be an issue lol ;)

Writing assembly without a debugger is very painful indeed...
User avatar
ParadigmShifter
Manic Miner
Posts: 671
Joined: Sat Sep 09, 2023 4:55 am

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by ParadigmShifter »

From what I remember from the original book:

Some of the routines were relocatable and some weren't. Non-relocatable code is usually a bit quicker than relocatable code (since all the jumps have to be relative). You can just let the assembler handle the memory layout and labels if you use one, so making code relocatable is never an issue with an assembler (unless you are doing something like merging 2 assembled files together?). Aligning data (and occasionally code) can give speed benefits as well so relocatable code can be nerfed by that too.

The "replace these numbers" stuff was bad if they didn't give the actual assembly to replace as well.

I think they did give an overview of how each algorithm worked though which was ok... that's the most important bit.

I expect they also preferred smaller code than faster code to avoid tedium when typing them in.

I'm not sure if they even checksummed the data you had to type in if you went down that route? And using decimal rather than hex is a bit of a pain really as well...
tubs74
Drutt
Posts: 1
Joined: Sun Sep 10, 2023 2:29 pm

Re: 40 Best Machine Code Routines for the ZX Spectrum - 4th Ed

Post by tubs74 »

I've purchased this book as well, and I do have the original.

My assumption was that it was the original book re-edited, with a couple of new chapters, so it is as I expected but with a nicer feel to the paper etc - I think it's slightly better reprint than the "Spectrum Machine Language for the Absolute Beginner", which I think is a printed PDF rather than a redesign.

I'm nothing more than a MC dabbler, I've not run any of the routines so can't say if they are good. I suspect they might be useful as a reference (some of the routines are quite specialised - renumbering basic programs for example).

Supercharge your spectrum looks to have a few more interesting routines.

The biggest roadblock, I think is that they're not a guide on how to program MC with modern applications or helpers, as they are under the assumption all you have is a Spectrum.
Post Reply