ZX BASIC:Types

From BorielWiki
Revision as of 08:52, 20 June 2008 by Boriel (talk | contribs)
Jump to: navigation, search

Introduction

Although ZX BASIC was originally designed with ZX Spectrum in mind, it is a three-stage retargeable compiler. This means it should not be difficult to ZX BASIC to compile for other machines (E.g. Commodore 64) or current machines (PC) or even virtual machines like Java or .NET. Porting ZX BASIC to other Z80 architectures like Amstrad or MSX should be almost straightforward (only the library.asm should need some work to use different ROM routines).

Once said that, data types where designed for the Z80 platform. So, for example 64 bits integers are not implemented.

Types

There are 3 kinds of types:

Integers

Integers. Integers are a numeric type. They can be unsigned (their value is always 0 or positive) or signed (can take negative values). ZX Basic integer types sizes are 8, 16 and 32 bits. Unsigned types have the prefix U.

Byte

8 bit signed integer. Ranges from -128 to 127.

UByte

8 bit unsigned integer. Ranges from 0 to 255.

Integer

16 bit signed integer. Ranges from -32768 to 32767.

Uinteger

16 bit unsigned integer. Ranges from 0 to 65535.

Decimals

Decimals, as suggested, stores decimal numeric types. Their sizes are 32 bit for Fixed type and 40 bytes for Float one.

Fixed

32 bit Fixed Point decimal. First 16 bits are the integer part, whilst remaining 16 contains the decimal one. Ranges from -32767.9999847 to 32767.9999847 with a precision of 1 / 2^16 (0.000015 aprox.). Fixed points decimal are less precise than Floating ones, but much faster and requires less space (1 byte less). Also, their range is much limited. They're usually used on screen drawing when Floating point is too slow and decimal calculations are required.

Float

Floating point type is the same used in Sinclair BASIC. It uses 40 bytes (8 bits for exponent, 32 bits for mantissa). Read the ZX Spectrum manual or here.

To store the number in the computer, we use five bytes, as follows:

  1. write the first eight bits of the mantissa in the second byte (we know that the first bit is 1), the second eight bits in the third byte, the third eight bits in the fourth byte and the fourth eight bits in the fifth byte;
  2. replace the first bit in the second byte which we know is 1 by the sign: 0 for plus, 1 for minus;
  3. write the exponent +128 in the first byte. For instance, suppose our number is 1/10
    1/10 = 4/5x2-3

Strings

String types are used to store alphanumerical strings. Strings can contain up to 65535 characters, and they can change its size dynamically so, unlike other data types, their content is stored in a different memory area, called the heap.