Machine Code Counter

Developer(s): Stan Piotrowski
Date: 1984
Type: Program
Platform(s): ZX81, TS 1000

A 35-byte machine-code routine that counts upward in the middle of the screen, writing each digit straight into the display file, until you press a key. Stan Piotrowski gives it next to a four-line BASIC program that does the same job, to show how much faster machine code is.

Type RAND USR 16514: the download already holds the bytes in the line 1 REM, and a key press stops the count. Lines 10-70 are the article’s general-purpose POKE loader (RUN, then enter the bytes one at a time). BASIC.p is the four-line BASIC counter, for comparison. Use 16K: the routine writes to the full-size display file.

As printed, byte 60 is labelled DEC A; 60 is INC A, which is what makes the count go up. The LD DE,381 bytes are printed as 17,124,1 (that is 380), which only moves the counter one column.

Published in Sinc-Link early 1984 (Vol. 2 No. 1) (Machine Code Programming (part 1)).

Related Articles

Machine language programming tutorial.

Image Gallery

Source Code

Listing 1 (BASIC version):
10 LET A=1
20 PRINT AT 10,14;A
30 LET A=A+1
40 GOTO 20

Listing 2 (loader):
1 REM XXXX....etc  (35 X's)
10 LET A=16514
20 INPUT B
30 POKE A,B
40 SCROLL
50 PRINT A;",";PEEK A
60 LET A=A+1
70 GOTO 20

Listing 3 (machine code):
42,12,64                    LD HL, (16396)   :display file
                                              location
17,124,1                    LD DE, 381       :screen centre
25                          ADD HL,DE
126            CHECK        LD A,(HL)        :A contains the
                                              character at
                                              that location
254,0                       CP 0             :is it blank?
32,2                        JRNZ +2          :No. It contains
                                              a number so jump
                                              forward 2 bytes
62,28                       LD A,28          :Yes. A now contains
                                              the zero digit
60                          DEC A            :LET A=A-1
254,38                      CP 38            :is A larger than
                                              the code for
                                              digit "9"?
32,5                        JRNZ +5          :No. Jump ahead
                                              5 bytes
54,28                       LD (HL),28       :Yes. Print a
                                              zero
43                          DEC HL           :move display
                                              back 1 space
24,239                      JR -17           :jump back 17
                                              bytes to CHECK
119                         LD (HL),A        :print the digit
                                              A contains
205,187,2                      CALL 699      :keyboard scan
62,255                      LD A,255
189                         CP L             :check for no
202,130,64                  JP Z, 16514       key pressed
201                         RET              :if a key is
                                              pressed, return
                                              to Basic & stop.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.