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.

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
KliveProjectsfolder 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 StartWhat it does:
.model Spectrum48and.org #8000tell 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),Awrites it. HALTwaits for the next screen frame. The ZX Spectrum draws 50 frames a second, so 25 of them take half a second.DJNZcountsBdown from 8, so the colours go from 7 (white) to 0 (black), and then the program starts again.
123- The icon next to
code.kz80.asmmarks it as the build root: the file Klive builds. - Run builds the program, loads it into the machine and starts it.
- 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.

Klive did five things for you:
- It assembled the program. If it had found an error, it would have stopped here and shown the error in the editor.
- It restarted the machine and let the ROM reach its main loop.
- It paused the machine and copied the program’s bytes into its memory at
#8000. - It set the CPU’s program counter to the program’s first instruction.
- It let the machine run.
The Commands tab shows what happened:
1- The
runcommand compiledcode/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.