Memscan

Date: 1986
Type: Program
Platform(s): TS 2068

Memscan is a machine code utility that scans a region of memory searching for a specific byte value, POKEd into RAM starting at address 65500 via a DATA loader. The routine uses Z80 registers: HL holds the base address, B counts rows, C counts columns, and D/E track position within the scan grid. It iterates through 27 bytes per row (register B initialized to 27) and one column (C initialized to 1), comparing each byte against a target value using the CP instruction (opcode 190/0xBE). When a match is found, a subroutine at address 0x2662 (38/0x26 in the DATA) is called. The final 34 bytes of machine code are preserved with SAVE “memscan” CODE 65500,34 for standalone reloading.


Program Analysis

Program Structure

The program is a two-phase loader/executor. Lines 1030 form a FOR/READ/POKE loop that writes 34 bytes of Z80 machine code into RAM at addresses 65500–65533. Line 110 then transfers control to that code via RANDOMIZE USR 65500. Line 115 is a safety STOP that would catch a fall-through if the machine code returned unexpectedly. Line 120 saves only the machine code block, not the BASIC loader itself.

DATA Block Disassembly

The 34 DATA bytes, loaded from lines 40100, disassemble to the following Z80 instructions:

OffsetHexMnemonicNotes
021 00 00LD HL,0x0000Base address (low byte, high byte = 0,0)
30E 01LD C,1Outer loop counter (1 pass)
506 1BLD B,27Inner loop counter (27 bytes per row)
73E 00LD A,0Target byte value to search for
9C5PUSH BCPreserve loop counters
10E5PUSH HLPreserve current pointer
11BECP (HL)Compare A with byte at HL
1228 03JR Z,+3Jump if match found
14CD 3E 26CALL 0x263ECall ROM routine on match
17E1POP HLRestore pointer
1823INC HLAdvance to next byte
1923INC HLAdvance again (stride of 2)
20C1POP BCRestore counters
2104INC BIncrement inner counter (unusual — counts up)
223E 9CLD A,0x9CLoad terminal value 156
24B8CP BCompare B with 156
2520 ECJR NZ,-20Loop back if not done
270CINC CIncrement outer counter
283E FFLD A,0xFFLoad terminal value 255
30B9CP CCompare C with 255
3120 E4JR NZ,-28Outer loop back
33C9RETReturn to BASIC

Notable Techniques

  • The FOR/READ/POKE pattern is a standard BASIC machine code loader idiom, placing code at the top of a 65535-byte address space to avoid interference with BASIC workspace.
  • The inner loop uses an ascending counter checked against 156 (0x9C), and the outer loop counts up to 255 — an unusual count-up-to-terminal-value structure rather than the more common DJNZ decrement-to-zero.
  • HL advances by 2 on each iteration (two consecutive INC HL instructions), suggesting the scan steps through word-aligned or two-byte-stride memory locations rather than every byte.
  • The CALL 0x263E target is a ROM address; on the TS2068 this falls in the ROM region and would invoke a built-in routine (potentially related to display or output).
  • The initial LD HL,0x0000 sets the scan base to address 0, meaning it scans from the very start of the address space.
  • SAVE ... CODE 65500,34 at line 120 preserves exactly the 34 bytes of machine code, omitting the BASIC loader — this is a common space-saving distribution technique.

Bugs and Anomalies

The JR Z,+3 at offset 12 skips the CALL when a match is not found — that is, the logic is inverted: the ROM routine is called only when the byte does not match the target, and the scan moves on silently when it does match. This may be intentional (e.g., logging non-matching addresses) or may represent a logic inversion bug in the original listing. Additionally, the initial target byte is hardcoded as 0 and the base address is hardcoded as 0x0000 in the DATA; customizing the scan would require either modifying the DATA values or POKEing the relevant bytes after loading.

Content

Appears On

Library tape from the Sinclair Computer Users Society (SINCUS).

Related Products

Related Articles

Related Content

Image Gallery

Memscan

Source Code

    1 REM memscan    ZX-Appeal                       Sept. 1986
   10 FOR x=65500 TO 65533
   20 READ y: POKE x,y
   30 NEXT x
   40 DATA 33,0,0,14,1
   50 DATA 6,27,62,0,197
   60 DATA 229,190,40,3,205
   70 DATA 62,38,225,35,35
   80 DATA 193,4,62,156,184
   90 DATA 32,236,12,62,255
  100 DATA 185,32,228,201
  110 RANDOMIZE USR 65500
  115 STOP 
  120 SAVE "memscan"CODE 65500,34

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

People

No people associated with this content.

Scroll to Top