Assembler “as-z80”

Use the shared Z80 assembler to write programs for the Spectrum. Place code in RAM and use the example below to write to the screen or ULA ports.

The ZX Spectrum 48K uses the same Z80 assembler plugin as the MITS Altair8800. Please refer to the as-z80 documentation for the complete assembler reference.

Compile from the command line

The assembler can be run independently:

  • on Linux:
    > bin/as-z80 [--output output_file.hex] [source_file.asm]
    
  • on Windows:
    > bin\as-z80.bat [--output output_file.hex] [source_file.asm]
    

ZX Spectrum specific notes

When writing assembly for the ZX Spectrum, keep the following in mind:

  • Programs should be assembled to start at address 0x8000 (32768) or higher, since addresses 0x0000–0x3FFF are ROM and 0x4000–0x5AFF are screen/attribute memory.
  • The ULA is accessed through port 0xFE (254). Writing controls border colour and audio; reading returns keyboard state.
  • The ROM BEEP routine is available at address 0x03B5 (if the 48K ROM is loaded).
  • The standard program entry point from BASIC is RANDOMIZE USR <address>.

First program

The distribution includes ZX Spectrum examples for the Z80 assembler. Open an example from the editor, compile it, and start execution at its ORG address. The examples use ORG 8000H, above ROM and display memory, so the compiled program can be loaded and run without replacing the system ROM.

For a new program, select the ZX Spectrum 48K computer, create an assembly file beginning with ORG 8000H, compile it, set the instruction pointer to 8000h, and run or single-step it in the emulator. Load a legal 48K ROM image in the memory settings first when the program calls ROM routines.

Example: Simple beeper tone

org 8000H

BEEPER_PORT equ 0FEH
BEEPER_ON   equ 10H    ; EAR bit on
BEEPER_OFF  equ 08H    ; EAR bit off

start:
    ld b, 255           ; number of half-waves

play_loop:
    ld a, BEEPER_ON
    out (BEEPER_PORT), a

    ld de, 200          ; half-period delay
delay1:
    dec de
    ld a, d
    or e
    jr nz, delay1

    ld a, BEEPER_OFF
    out (BEEPER_PORT), a

    ld de, 200
delay2:
    dec de
    ld a, d
    or e
    jr nz, delay2

    djnz play_loop

    halt

This site uses Just the Docs, a documentation theme for Jekyll.