Binary Representation

Author(s): Jon Bobst
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v3 n1 (Volume 3, Issue 1)
Date: January 1982
Pages: 7

Knowing how to manipulate individual bits in your ZX80 is essential for efficient Machine Code programmming, so knowing what bits and bytes are is essential also.

Mechanically, each address in the ZX80’s memory contains 8 bits of information (1 byte): 8 “switches” either “ON” or “OFF”. In binary representation, or the number system your computer counts in (see Feb.81 p.10), each switch means 1-ON or 0-OFF with these values:

BIT #  :  7  6  5  4  3  2  1  0
BYTE   :  0  0  0  0  0  0  0  0 = 0
VALUES:128 64 32 16  8  4  2  1
Ex.    :  0  0  0  1  0  1  0  1 =21
Ex.    :  1  0  1  0  1  0  1  0 =
                          170 (or -86)

In 1 byte (8 bits), you can store a positive number from 0 to 255, or a negative number up to 127 if using the left-most bit (#7) as the “sign” bit: 0=positive and 1=negative. The Z80 uses bit 7 for the sign, so any time you PEEK an adddress holding a negative value, it is displayed in 2’s complement. That is why our second example is 170 or -86, and not -42. (More on 2’s complement next month, or see any Z80 programming book, like Spracklen’s Z80 and 8080 Assembly Language Programming or Zaks‘ Programming the Z80.–AZ)

You can see that having only 8 bits per address rather limits the numerical value in 1 address. So, the Z80 microprocessor accesses 2 bytes or addresses, one after the other, for numbers greater than 255. This means that, in its simplest form (4K ROM), the ZX80 can store numbers from -32767 to +32767 like this:

BIT LOW-ORDER          BIT HIGH-ORDER
      VALUE                  VALUE
 7    128     x256=     15 (for sign)
 6     64     x256=     14      16384
 5     32     x256=     13       8192
 4     16     x256=     12       4096
 3      8     x256=     11       2048
 2      4     x256=     10       1024
 1      2     x256=      9        512
 0      1     x256=      8        256
      255       +               32512

255+32512=32767

This chart may explain why, to get a large value, you must multiply one of 2 PEEKed pair addresses by 256.

Next month: BINARY ADDITION & SUBTRACTION