Chapter 25 – Using Machine Code

Using Machine Code

Computers do not respond directly to BASIC, or any other higher level programming language. Instead, such languages are either interpreted or compiled into what is known as machine code, and it is this which is understood by the CPU. The kind of processor that is built into the computer determines the type of machine code that is used. The ZX Spectrum range contains a Z80 processor, and so one writes Z80 machine code when addressing the processor directly. Specifically for the ZX Spectrum Next, the CPU is an updated one called Z80N which contains a superset of the instructions found in the Z80.
This section is written mainly for those who understand Z80 machine code. If you do not, but would like to, you might choose to read a book about it. Suitable titles will be something along the lines of Z80 machine code (or assembly language) for the absolute beginner. If it also mentions one of the computers in the ZX Spectrum range, so much the better. You might also like to read online resources and find tools such as the Design-Design Zeus cross-assembler1 at: https://www.desdes.com/products/oldfiles/, or the z88dk suite which includes apart from a C compiler, also an assembler, at www.z88dk.org, and last but not least the specnext.com forums.

Rather than write the numerical values of a machine code program directly, people usually choose to use mnemonics, known as assembly language, which, although cryptic, is not too difficult to understand with practice. You can see the assembly language instructions understood by the ZX Spectrum Next's CPU in Appendix A.

For a computer to execute this code the program must be converted into a sequence of bytes – in this form it is called machine code. This translation is usually done by a computer, using a program called an assembler. There is no assembler built into the ZX Spectrum Next ROM, however, three, loadable ones are included in the System/Next™ distribution: Zeus, Odin and SPED kindly provided by Neil Mottershead and Simon Brattel for the first, Matt Davies for the second and César Hernández Bañó for the latter respectively. It is also possible do the translation yourself, but this can be a painstaking process.

Let's take as an example the program:

ld bc, 99
ret

This will load the BC register pair with 99 and then return. This translates into the four machine code bytes 1, 99, 0 (ld bc, 99) and 201 (ret). (If you look up codes 1 and 201 in Appendix A, you will find that 1 corresponds to ld bc, NN – where NN stands for any two-byte number; and 201 corresponds to ret.)

Using CLEAR to Make Space

Once you have written your machine code program, the next step is to load it into the computer's memory. You need to decide whereabouts in memory to locate it – the best thing is to make extra space for it between the NextBASIC area and the user-defined graphics.

If you enter the command:

CLEAR 65267

This will give you a space of 100 (for good measure) bytes starting at address 65268.To create the machine code program, you may run a NextBASIC program like this:

10 a=65268
20 READ n: POKE a,n

1 A cross-assembler is an assembler that runs on a different system than the one it produces code for. For example z88dk is usually executed on a PC running Linux or Windows Operating Systems.


ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.