This the multi-page printable view of this section.Click here to print.

Return to the regular view of this page.

Clear the Screen

Example code to clear the Spectrum’s screen

Table of Contents

This code snippet will clear the standard screen on all versions of the Spectrum.

It's broken down into three sections:

  1. Clear's the pixel data
  2. Reset the screen's colour data, in this instance to White text on Black Background
  3. Sets the screen border to black
; *************************************************************************** ; Clear the Spectrum Screen ; ; Example 1 ; ; Author: Peter Mount, Area51.dev & Contributors ; URL: https://area51.dev/sinclair/asm/screen/getcharaddr/ ; *************************************************************************** ; ; Clear the Spectrum screen, setting the border to black and white text on ; a black background ; ; On Exit: ; A Undefined ; BC Undefined ; DE Undefined ; HL Undefined ; clearScreen: ; Clear the pixel data ld hl,DISPLAYFILE ; Start of screen memory ld (hl),&00 ; Set first byte to 0 ld de,DISPLAYFILE+1 ; Destination as next byte ld bc, 6144 ; Number of bytes to copy ldir ; Copy from hl to de bc times ; Clear the colour attributes ld (hl),&07 ; White text black Background ld bc, 767 ; Number of bytes to copy, attr size -1 ldir ; Clear the attributes ld a, 0 ; Set border to black ld (BORDCR),a ; set OS copy of colour out (&fe),a ; set hardware colour ret

Important note here:

We copy 6144 bytes in the first LDIR not 6143 which we would normally do if we want to erase just the DISPLAYFILE.

Using 6144 bytes will cause the first byte of DISPLAYATTR to be set to 0 which is fine here as we want HL and DE to be pointing to DISPLAYATTR ready for the second LDIR operation.

This could, in theory cause the top left character to flicker briefly as it's been set to 0 (BLACK foreground and background). If that's an issue then set the BC to 6143 and increment HL and DE after the LDIR as in the next example:

; *************************************************************************** ; Clear the Spectrum Screen ; ; Example 2 - does not corrupt the top left characters colour attributes ; ; Author: Peter Mount, Area51.dev & Contributors ; URL: https://area51.dev/sinclair/asm/screen/getcharaddr/ ; *************************************************************************** ; ; Clear the Spectrum screen, setting the border to black and white text on ; a black background ; ; On Exit: ; A Undefined ; BC Undefined ; DE Undefined ; HL Undefined ; clearScreen: ; Clear the pixel data ld hl,DISPLAYFILE ; Start of screen memory ld (hl),&00 ; Set first byte to 0 ld de,DISPLAYFILE+1 ; Destination as next byte ld bc, 6143 ; Number of bytes to copy ldir ; Copy from hl to de bc times inc hl ; Move HL and DE forward 1 byte inc de ; so they point to DISPLAYATTR ; Clear the colour attributes ld (hl),&07 ; White text black Background ld bc, 767 ; Number of bytes to copy, attr size -1 ldir ; Clear the attributes ld a, 0 ; Set border to black ld (BORDCR),a ; set OS copy of colour out (&fe),a ; set hardware colour ret

There are faster but longer methods of clearing the screen which I won't go into here. For most purposes the first example above will work for most purposes other than time critical games.