Close

Space-saving implementation of operators

A project log for One kilobyte Tiny BASIC for the 8080 and Z80

BASIC for the 8080 and Z80, fits in 1K of memory, runs BASIC games on a 2K system. Features similar to Palo Alto Tiny BASIC.

willstevenswill.stevens 01/25/2024 at 08:530 Comments

The + - = <> < <= > >= operators are all implemented using the following 23 bytes of code:

GTSub:
  ; Swap operands and fall through
  XCHG
LTSub:
  DCX D
LTESub:
  ; Swap operands and fall through
  XCHG
GTESub:
  RST_NegateDE
  DAD D
	
  MOV A,H
  RAL
	
  DB 11h; LXi D opcode to swallow next byte

EqualSub:
  RST_CompareHLDE ; returns Z iff HL=DE
  CMC
  DB 3eh ; MVI A opcode to swallow next byte
	
NotEqualSub:
  RST_CompareHLDE; returns Z iff HL=DE
  LXI D,1
  RNC
  DCX D
  RET

AddSub:
  DB 3eh ; opcode for MVI A, to eat next byte
SubSub:
  RST_NegateDE
  ;Add DE to HL and keep in DE
  DAD D
  XCHG
	
  RET

This uses a lot of code sharing, and saves space at the expense of speed. The order of the operator …Sub: labels corresponds to operator precedence, so if these are all in the same 256 byte page then the low order byte of the label address effectively encodes operator precedence.

Discussions