Writing my own pre-emptive multitasker in Turbo Pascal on our x86 as a self-taught teenager was a core memory thing.
I recall spending a few days mulling over the exact sequence of instructions to save the state of the previous task without clobbering the flags or any registers.
The result was that I could register functions (procedures) as tasks with their own little stack, and it would switch preemptively between them in a round-robin fashion.
I'm not familiar with Z80 asm but from what I can gather it looks very similar to what I had. I was running in real mode so also had very limited resources for each task, and a hardcoded upper limit on the number of tasks.
While I'm wildly more productive these days, I kinda miss how not having internet made accomplishments so much greater. It's like walking up a mountain on your own vs taking a tour bus to the summit.
I did something similar as a teenager using DOS Microsoft C to implement the Amiga's exec.library and other bits and pieces on the x86. I managed to get preemptive multitasking working in 8086 real mode, along with a serial and tty device. Later on I implemented bits and pieces of dos.library in an emulator that let me run some m68k Amiga shell binaries (like the Aztec C compiler) from a Linux command line. That was invaluable practice for learning about different execution contexts, interrupts, locking and such for Linux kernel programming. I had even written a very simple C compiler with a friend that I used to mangle C code into endian swapping code that ran natively on my 486 so that the Amiga code could call into native code to do Linux syscalls and such.
The experience of figuring these things out was tonnes of fun! There's nothing like following threads of assembly with a debugger or disassembler in the Amiga's ROM to get a better idea of how the code worked. And since systems were so much smaller in the 1980s, a single person really could understand virtually everything about the system with enough time and effort.
The biggest challenge for me was that the ROM Kernel Manuals were very expensive back then, so I wasn't able to get copies until far too late in my Amiga years (with Commodore being in its death throes).
Motorola and Intel were great back then as they would ship out printed copies of all the documentation for various CPUs and support chips for free upon request!
Good times!
Ah, that brings back memories. I was using Intel iRMX on a commercial system in the mid 80s, and wanted to write a multitasking program on my IBM XT.
So I wrote a pre-emptive multitasking iRMX clone in C and a bit of assembler. Ended up using it to develop a mildly successful POS system running on a single PC with several VT100 terminals attached.