ZX Format #01: For Programmers: Introduction to Assembly Language

Assembler in pictures for "dummies" and not only. Author: Mikhail Spitsin.  

We are starting a series of articles on assembler for beginners, with the aim of encouraging people to study this language. In these articles, we will talk about the structure of a computer, graphics, sound (48 and 128), and, of course, about assembler itself. So...

1. COMPUTER PROCESSOR
Just like a human, any computer has a brain; the brain of the computer is the processor. So what is the "brain" of our Speccy? The "brain" of the Spectrum is the Z80 processor from ZILOG. The Z80 processor is a pseudo-sixteen-bit processor. How to understand this?
The thing is that each processor can understand numbers of a certain bit depth. For example, an eight-bit number is represented as a combination of eight binary zeros and ones and can denote any integer from 0 to 255. Each bit of such a number is a power of two, for example, the binary number 01100110 corresponds to the decimal 102, and to convert from decimal to binary, you can do it like this: 2^7*0 + 2^6*1 + 2^5*1 + 2^4*0 + 2^3*0 + 2^2*1 + 2^1*1 + 2^0*0 = 102.
Let's continue. The Z80 has twenty-eight-bit registers (a register is a cell inside the processor where numbers can be stored) and all registers are combined into pairs, each of which is called a "register pair". Here we have reached the answer to our question, - 8+8=16, therefore, a register pair gives a sixteen-bit number. Now about why Speccy is not just sixteen-bit, but "pseudo" that.
So, we need registers to perform arithmetic operations on them, namely addition and subtraction. Unfortunately, our little Speccy cannot divide and multiply, and it is not even worth mentioning any more "terrifying" operations.
So, the processor can add/subtract both registers and register pairs, i.e., perform arithmetic operations on eight and sixteen-bit numbers, and since registers have only eight bits (this is not IBM), the processor is "pseudo-sixteen-bit". Here is a table of all registers with their purposes:


╔═══════╦══════════════════════╗
║ Register║ Main purpose ║
╠═══════╬══════════════════════╣
║ A ║ 8-bit accumulator ║
║ F ║ Flag register ║
║ B ║ 8-bit counter ║
║ BC ║ 16-bit counter ║
║ DE ║ Receiver register ║
║ HL ║ 16-bit accumulator ║
║ IX ║ Index register ║
║ IY ║ System index ║
║ I ║ Interrupt vector ║
║ R ║ Regeneration register ║
║ SP ║ Stack pointer ║
║ PC ║ Program counter ║
╠═══════╩══════════════════════╣
║ Note: ║
║ 8-bit - eight-bit ║
╚════════════════════════════╝


2. SIMPLE COMMANDS
Now let's look at the simplest operations of assembler, using BASIC as an example, which I hope you are familiar with. The first thing to do to learn assembler is to choose an "assembler shell" in which you will work. I can recommend TASM128 if you have a ZX128, or, for ZX48, ZEUS ASSEMBLER.
So, let's get started:


BASIC ASSEMBLER
10 LET A=1 LD A,1
20 STOP RET


The command "LET" is equivalent to the command "LD", if you type and run it, then the number one will be stored in register "A". Upon the command "RET", there will be a return to the calling program (if the program was launched from BASIC, then the command "RET" will return control to BASIC).
You can store numbers in all registers except for "F" and "PC", for example:


LD A,1 ; 1-->A
LD B,15 ; 15-->B
LD HL,16384 ; 16384-->HL
LD IX,50000 ; 50000-->IX


But usually, the programmer does not have enough registers, so they have to use the computer's memory, for example:


BASIC ASSEMBLER
LET A=PEEK 23556 LD A,(23556)


After executing this command, the number stored in the specified memory cell will be in "A", and considering that in this cell BASIC stores the code of the last pressed key, this command is not without practical meaning.
There are also reverse commands used:


BASIC ASSEMBLER
POKE 23607,A LD (23607),A


But often you have to read/write the value of a cell whose address is calculated or taken from a table. In such cases, the following commands are used:


WRITE READ
LD (BC),A LD A,(BC)
LD (DE),A LD A,(DE)
LD (HL),0 LD E,(HL)


Note: I want to point out that there is no command for directly writing a number to a memory cell, nor for writing from one cell to another.
And now I want to present a program that prints any message (it is analogous to the "PRINT" statement). The program uses the "CALL" command to call a ROM procedure, this command can be compared to the BASIC command "GO SUB", for example:


BASIC ASSEMBLER
10 GO SUB 999 CALL M1
.... ....
999 LET A=1 M1 LD A,11000 RETURN
RET


Here "M1" is a label, which will be replaced by the corresponding address when assembling the program.

EXAMPLE OF A PROGRAM
After the semicolon in assembler, just like after REM in BASIC, comments are written.


; Program for printing text
; messages.
-= PRINT TEXT =-
ORG 30000
ENTSTART LD A,1 ; Open channel 1
CALL #1601; channel for printing.
LD DE,TEXT; Address of the text
LD BC,LEN ; Length of the text
CALL #203C; Print text
RET
TEXT DEFB 22 ; Code "AT"
DEFB Y,X ; Print position
DEFM "HELLO, EVEREBODY!"
LEN EQU 20 ; LEN=20
Y EQU 10 ; Y=10
X EQU 5 ; X=5


This program is analogous to the BASIC line: 10 PRINT AT 10,5;"HELLO, EVEREBODY!"
In the next issue, I will talk about arithmetic commands and comparison commands, and provide an example of a small game.
See you!

Contents of the publication: ZX Format #01

  • IS-DOS
    IskraSoft introduces IS-DOS Classic 4.0 with advanced text editing features, a disk utility, a hard drive controller for ZX Spectrum, and a program debugger.
  • IS-DOS - Владимир Елисеев
    Overview of IS-DOS for ZX Spectrum, highlighting its advantages over TR-DOS. Discussion on installation, usage, and file management features of IS-DOS. Introduction to system utilities and interface similar to Norton Commander.
  • IS-DOS - Владимир Елисеев
    The article provides a concise guide to the IS-DOS system disk, listing all essential files with explanations. Files are sorted alphabetically and grouped by directories for easy navigation. The guide focuses on a basic overview of files and commands without extensive descriptions.
  • IS-DOS - Владимир Елисеев
    Discussion on the UniColor standard for color settings in IS-DOS programs, its implementation, and advantages for user customization.
  • IS-DOS - Владимир Елисеев
    Discussion of IS-DOS window interface features. Implementation of window system in IS-DOS and its advantages. Example programs for creating windows and frames.
  • IS-DOS - Владимир Елисеев
    Description of the internal workings of the gmen.com program for IS-DOS, focusing on device management and menu handling with detailed source code and comments.
  • Hardware - Vladimir Larkov
    Discussion on modifying Scorpion ZS-256 hardware for compatibility with specific software. Describes blocking port #1FFD to run incompatible programs. Provides technical details for implementation.
  • Железо - Nemo
    Discussing hardware braking devices for ZX Spectrum. Focus on creation and impact of brakes on overclocked processors. Warning about potential side effects.
  • Железо
    Discussion of the impracticality of repairing old computers and the economic sense in replacing them with modern hardware. The article highlights challenges in DIY repairs and suggests alternative solutions. Emphasizes the benefits of upgrading to newer, more reliable components.
  • Железо
    Discussion of new peripheral developments for Scorpion ZS 256, including an innovative universal controller and MIDI interface. Highlights enhancements in keyboard and mouse controllers and expansion of built-in software capabilities. Offers details on professional ROM extension and turbo mode for improved performance.
  • Игрушки - Ruster
    A detailed walkthrough of the game Crystal Kingdom Dizzy, including solutions to puzzles and tips for progressing through the story.
  • Игрушки - Павел Рындин
    Description of the logic game 'Bulls and Cows', its mechanics and gameplay. Explains levels of difficulty and the goal to guess a number with minimal attempts.
  • Игрушки - Вячеслав Медноногов
    Discussion on the nuances of 'UFO: Enemy Unknown' game, focusing on alien species, their tactics, and gameplay strategies. Details on game mechanics and future sequel 'UFO-2: Devils of the Abyss'. Includes programming insights and development history.
  • Игрушки - SParker
    Overview of games and software on the Saint Petersburg market in 1995.
  • Интервью
    Vyacheslav Mednonogov shares his journey as a programmer, from assembling his first Spectrum-48 to developing notable games like 'UFO. Enemy Unknown'. He describes challenges and insights in game development and the rise of Russian game localization. His experiences highlight the evolution of programming and software distribution in the 1990s.
  • Информация
    Introduction to ZX-Format, a new electronic magazine for ZX Spectrum enthusiasts, focusing on games, programming, hardware, and community interaction.
  • Конкурс
    Discussion on organizing a new competition for the magazine. Readers are invited to submit their theme ideas. Selected themes will be featured in future issues.
  • Конкурс
    ZX Format announces a competition for the best mission created using the mission editor for the game Laser Squad. Submissions are accepted by mail or in person. Winner receives a box of 3M disks and the next issue for free.
  • Отдохнём - Килат Амор
    A programmer's day filled with dreams, gaming, and casual conversations. Vasetchkin balances ambition with relaxation, embodying the programmer's spirit. Reflects on the demands and enjoyment of a programmer's life.
  • Mailbox - Максим Романов
    Discussion on hardware and software offers in St. Petersburg by LOGROS and Create Soft, including detailed insights on Kempston Mouse interface.
  • Mailbox - SParker
    Compilation of cheat codes for ZX Spectrum games by Sergey Pavlov. Techniques for extra lives and immortality. Not recommended for fans of fair play.
  • Mailbox
    Description of submission guidelines for letters and articles to the magazine, including formatting and submission preferences.
  • Премьера - Алексей Щекин
    Gameplay of Lines game on ZX Spectrum, similar to Color Lines. Move balls to form lines of the same color. Scoring and controls are described.
  • Программистам - Дмитрий Рудовский
    Introduction to advanced BASIC programming techniques for ZX Spectrum 48, highlighting its advantages over 128 BASIC. Plans for future articles include service programs and language extensions. The article serves as a guide for experienced programmers.
  • Программистам - Михаил Спицын
    Introduction to assembly language on ZX Spectrum. Explanation of Z80 processor structure, registers, and basic commands. Includes example programs for learning.
  • Программистам - Андрей Седов
    Mission editor for Laser Squad by Andrey Sedov allows fans to create new missions. Includes user-friendly interface with minimal controls. Offers features like object editing, price adjustments, and sprite assignment.
  • Various
    History of Amiga computer development, its creators, and key features. Evolution from arcade games to a leading home computer. Amiga's market success and technological advancements.
  • Разное
    Review of upcoming ZX Spectrum software products with demos. Highlights include adventure games, RPG trials, and strategic tactics. Several well-known developers contribute to these releases.