Many readers have called us about differences in 4K and 8K ROMs. You can use machine language (ML) to adapt programs for both computers or to regain lost uses, in particular, the masking function of 4K logical AND and OR.
ML functions A AND B and A OR B, unlike arithmetic, return numbers representing logical values. A and B are OPERANDS. ML recognizes two values, true (=1) and false (=0).
1 AND 1 is 1 1 OR 1 is 1 1 AND 0 is 0 1 OR 0 is 1 0 AND 0 is 0 0 OR 0 is 0
ML code compares bit positions in A and B to see if they are the same or different. AND returns true (or 1) for each bit position if both bits are 1, while OR returns true if at least one bit of the pair is 1. So AND returns false (or 0) if at least one bit is 0, and OR returns false only if both bits are 0.
BASIC converts the binary strings of 1s and 0s into decimal equivalents. Check the following binary results and their decimal equivalents:
00100000 = 50 AND 37 = 32 00110010 = 50 00100101 = 37 00110111 = 50 OR 37 = 55
BASIC 4K AND tests bits in SON OF BIG CHARACTERS (Feb.81) before SON makes them into big characters. For example, in line 110, Q AND B (=128) returns 128 (10000000 in binary) if Q contains a 1 in the 128 (or 2**7) binary position, 0 otherwise. (Remember the rightmost position is 2**0, so the eighth from the right is 2**7.) In 4K, you can test any bit position in a binary number for a 1 by typing its decimal equivalent AND one of the following decimal B operands:
BIT # B B
(from right) (Dec) (Bin)
7 128 10000000
6 64 01000000
5 32 00100000
4 16 00010000
3 8 00001000
2 4 00000100
1 2 00000010
0 1 00000001
AND and OR results from the 8K ROM, on the other hand, cannot mask bits. You don’t get decimal numbers made from logical bit comparisons; instead you get A or 0. A AND B returns A if B<> (doesn’t equal) 0, and 0 if B=0. A OR B returns 1 when B<>0, and A when B=0.
50 AND 37 is 50 50 OR 37 is 1 50 AND 0 is 0 50 OR 0 is 50
AND and OR on 8K do not return bit-by-bit true or false; they depend on A’s value and B’s logical value (B zero or nonzero).
As a result, SON OF BIG CHARACTERS must be modified for the ZX81 or 8K ROM, since AND no longer works as a bit tester. You can duplicate 4K AND in your 8K machine by POKEing USR routines into RAM.
Assembled ML routines from the 4K ROM to put AND and OR into an 8K machine appear for use with an ML monitor (see May 81,p.13). A decimal listing enables you to POKE them in by hand. (We assume you will load both AND and OR routines at the same time.) They differ from the original 4K ROM listing in that they return results to the BC register instead of to HL, and we added INIT and INIT2 so AND and OR would fetch operands from the appropriate locations.
Before LOADing these routines, you can prevent the computer from overwriting them by POKEing a new value into RAMTOP, the 8K’s way of knowing where the end of RAM is.
POKE 16388,224
POKE 16389,67
NEW (NL)
RAMTOP points to the first nonexistent RAM address; lowering RAMTOP fools the computer into thinking there is no more RAM space above RAMTOP since it fills RAM from the bottom up. The new RAMTOP is now 17376, the first byte you can use for USR routines. You can’t, however, SAVE your routines, because the computer doesn’t know they’re there.
Before you use 4K BASIC AND or OR, you must POKE A and B (your operands) into 8K memory so ML can load them into the appropriate registers. The following should appear before your USR call:
POKE 17376,A-256*(INT A/256)
POKE 17377,INT(A/256)
POKE 17378,B-256*(INT A/256)
POKE 17379,INT(A/256)
To run AND, use USR (17380); for OR, USR (17394). Begin POKEing the decimal listing at 17380.
4K BASIC AND and OR also use expressions as operands, but some rules change. More later.
DECIMAL LISTING
42 224 67 237 91 226 67 124 162 71 125 163 79 201 42 224 67 237 91 226 67 124 178 71 125 179 79 201
ASSEMBLY LISTING–4K LOGICAL AND & OR FOR 8K MACHINES
43E0 00080 ORG 43E0H ;ORIGIN PLUS ROOM TO SAVE A AND B
0002 00090 DEFS 2 ;SAVE 2 BYTES FOR A
0002 00095 DEFS 2 ;SAVE 2 BYTES FOR B
43E4 2AE043 00100 INIT LD HL,(43E0H) ;A OPERAND
43E7 ED5BE243 00110 LD DE,(43E2H) ;B OPERAND
43EB 7C 00120 AND LD A,H
43EC A2 00130 AND D
43ED 47 00140 LD B,A
43EE 7D 00150 LD A,L
43EF A3 00160 AND E
43F0 4F 00170 LD C,A
43F1 C9 00180 RET
43F2 2AE043 00190 INIT2 LD HL,(43E0H) ;A OPERAND
43F5 ED5BE243 00200 LD DE,(43E2H) ;B OPERAND
43F9 7C 00210 OR LD A,H
43FA B2 00220 OR D
43FB 47 00230 LD B,A
43FC 7D 00240 LD A,L
43FD B3 00250 OR E
43FE 4F 00260 LD C,A
43FF C9 00270 RET