Skip to Content
Get StartedYour first assembly program

Your first assembly program

On this page you create a project, write a Z80 assembly program that cycles the ZX Spectrum’s border through its eight colours, and run it.

Create a project

Open the dialog

Choose File › New Project…, or click Create a Klive Project in the explorer. The Create a new Klive project dialog opens.

Fig. 6.1A new ZX Spectrum 48K project
The Create a new Klive project dialog for a ZX Spectrum 48K project

Fill it in

  • Machine type: ZX Spectrum 48K.
  • Project Template: default, a project with one Klive Z80 assembly file.
  • Project folder: leave it empty, and the project goes into the KliveProjects folder in your home folder.
  • Project name: BorderColours.

Click Create. Klive creates the project and opens it.

Write the program

The project has one source file, code/code.kz80.asm. Open it from the explorer, replace its contents with this program, and save it with Cmd+S (Ctrl+S on Windows and Linux):

; Border colours - the program of Get Started, steps 5 and 6. ; It shows the eight border colours, half a second each, forever. .model Spectrum48 .org #8000 Start: ei ; HALT needs interrupts to wake up ld b,8 ; eight colours: 7 (white) down to 0 (black) NextColour: ld a,b dec a ; B is 8..1, the colour is 7..0 out (#fe),a ; the border colour is bits 0-2 of port #FE ld e,25 ; wait 25 frames (half a second) Wait: halt ; wait for the next frame dec e jr nz,Wait djnz NextColour jr Start

What it does:

  • .model Spectrum48 and .org #8000 tell the assembler which machine the code is for and where in memory it goes.
  • The ZX Spectrum sets its border colour from the lowest three bits of the byte written to port #FE. OUT (#FE),A writes it.
  • HALT waits for the next screen frame. The ZX Spectrum draws 50 frames a second, so 25 of them take half a second.
  • DJNZ counts B down from 8, so the colours go from 7 (white) to 0 (black), and then the program starts again.
Fig. 6.2The program, ready to run
The program in the IDE's editor123
  1. The icon next to code.kz80.asm marks it as the build root: the file Klive builds.
  2. Run builds the program, loads it into the machine and starts it.
  3. Debug does the same with the debugger on. You will use it on the next page.

Run it

Click Run on the build bar, or press F5 in the IDE. The border starts changing colour.

Fig. 6.3The program is running
The emulator with the border changing colour

Klive did five things for you:

  1. It assembled the program. If it had found an error, it would have stopped here and shown the error in the editor.
  2. It restarted the machine and let the ROM reach its main loop.
  3. It paused the machine and copied the program’s bytes into its memory at #8000.
  4. It set the CPU’s program counter to the program’s first instruction.
  5. It let the machine run.

The Commands tab shows what happened:

Fig. 6.4The build's report
The Commands tab after the program was built and started1
  1. The run command compiled code/code.kz80.asm, then injected and started the code. (The build bar’s buttons run the same commands you can type at the prompt.)
💡

Try a change: replace ld e,25 with ld e,5 and run the program again. The colours now change ten times a second.

Next, debug your program.

Last updated on