Characters:
Characters, by ASCII definition, are simply integers between 0 to 255, inclusive. Characters take up one word of storage. The ASCII value of the character is stored in the least significant byte. The most significant byte is not used by Pascal and should be ignored.
15 . . . . . . 8 7 . . . . . . 0 <== 16 bits
unused ASCII |
Example: the character "A" has an ASCII value of 65 (hexadecimal 41). The binary representation is:
MSB x x x x x x x x 0 1 0 0 0 0 0 1 LSB
<---- not used----> 4 (hex) 1 |
Characters can be passed either as actual parameters (by value) or as Var parameters (by address).
Strings:
A string is a packed array of characters that can be from one to 256 bytes long. The first byte of a string always contains a number from 0 to 255; this number indicates the length of the string. One character is stored per byte, and the string ends on a word boundary--that is, if the last character in the string is the first byte of a new word, the other byte of the word is also reserved and not used by the string.
Each character of the string can be accessed in a packed array of characters; you cannot, however, access the length byte (the 0th element). Doing so causes the message "Value Range Error" to be displayed.
Example: The string "ABCD" has a length of 4. It looks like this:
S[4] S[3] S[2] S[1] S[0]
MSB 01000100 01000011 01000010 01000001 00000100 LSB
"D" "C" "B" "A" 4 |
Pascal always passes strings by address, since strings' lengths may vary.
Pointers:
Address pointers are unsigned integers that occupy 1 word of storage. Their format is identical to that of integers, except that their values may range from 0 to 65535. The value of a pointer, in this implementation of Pascal, is the memory address of the object being described.
Example: The address of AN0 (one of the annunciator ports) is hex C058 (49240 decimal). This address is stored as:
MSB 1 1 0 0 0 0 0 0 0 1 0 1 1 0 0 0 LSB
<--------> <--------> <--------> <-------->
C 0 5 8 |
Pointers, like integers, may be passed by value or by reference (as a Var parameter).
Long Integers:
Long integers are a special type of variable, first defined at UCSD as part of their extensions to the Pascal language. They are primarily used to handle calculations involving numbers which (a) cannot be represented accurately in floating point (real) format, and (b) are too large to store in integer format.
Long integers are stored in BCD (binary coded decimal)--one digit per nybble. One entire word is reserved for the sign of the long integer, and the variable must end on a word boundary. Four digits can be contained in one word, so the smallest definable long integer takes up two words of memory. The numbers are padded with leading zeroes when necessary to fill up the last word. The sign is 0 if positive and 255 if negative. (One byte is used for the sign.)
To illustrate the structure of long integers, let's take a specific example: the long integer -123456 takes 3 words: one for the sign, and two for the digits (since they are stored in multiples of 4). This long integer is stored in the following format:
<---- each digit is one nybble --->
MSB 6 5 4 3 2 1 0 0 0 0 F F LSB
<- word -> <- word -> < sign word > |
A long integer should always be passed by address, since its length depends on its definition.
Booleans:
The Boolean (binary) variable can have two values: TRUE and FALSE. Booleans are most commonly used in determining yes/no conditions, such as equality or set inclusion. Boolean variables are stored in one word, though only the LSB (least significant bit) is used. TRUE is represented by a 1; FALSE is represented by a 0.
MSB 15 . . . . . . 8 7 . . . . . . 0 LSB
Boolean |
UCSD Pascal does not allow direct printing of Boolean variables. For example:
Program PrintBoolean;
Var A: boolean;
Begin
A := FALSE;
Writeln (A); (* this is illegal *)
If A = FALSE Then Writeln ('FALSE') Else Writeln ('TRUE'); (* this is correct *)
End.
Booleans are most efficient in packed arrays, where each bit of the word is utilized. DrawBlock is probably the best-known example of this use. An excellent example of the use of boolean packed arrays is in the GrafDemo program on the Apple Pacal diskette APPLE3.
Boolean variables may be passed by value or by address.
Other Types:
In addition to all the above standard types, Pascal allows the programmer to define a wide variety of non-standard variable types. Probably the most popular example of this is the SET.
A set is an arbitrary collection of elements with each element assigned an ordinal position (that is, represented by a number). Each element of the set is represented by a name; you may choose any word for this name, except for (a) words reserved by Pascal, and (b) other variable definitions already in use. Each name is then associated with one bit in the data definition, beginning with bit 0. The set is stored in memory as a series of bits identified by the ordinal position of the element in the type definition. A set must end on a word boundary: for example, 17 elements would take up 2 words, even though only one bit of the second word is actually used.
Example:
Type Colors = (Red,Green,Blue,Yellow,Black,White);
ColorSet = Set of Colors;
is a set of colors. Red occupies position 0, and white has position 5.
<----------one word----------->
MSB 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 LSB
W B Y B G R
h l e l r e
i a l u e d
t c l e e
e k o n
w |
Sets may be passed either by address or by value, with certain restrictions. See p. 203 of the Pascal reference manual for details.
In general, complex record types consist of one or more standard types, each stored as described above. For the last word on Pascal data types, read Niklaus Wirth's Report in "User Manual and Report" by Jensen and Wirth.
References:
--Apple Pascal Reference Manual, by Apple Computer Inc. 1979.
--Apple Pascal Language Reference Manual, by Apple Computer, 1980.
--Apple Pascal Operating System Reference Manual, by Apple Computer, 1980.
--Programming in Pascal, by Peter Grogono, Addison Wesley, 1978.
--User Manual and Report, by Kathleen Jensen and Niklaus Wirth,Springer-Verlag, 1974.