Turbo Pascal MSX – lesson 3

da | Nov 18, 2023 | Programming, Turbo Pascal | 0 comments

MSX Turbo Pascal Column by Stefano Roperto (part 3)

Chapter 3

The data type defines the set of values that a variable can take on. The more complex data types that can be defined by the programmer are all derived from the standard types. The standard types that can be used in Turbo Pascal 3.0 are as follows:

Integer

The integer type can represent all signed integers that can be represented with two Bytes, therefore an integer type variable can contain values ranging from -32768 to 32767. It should be noted that the overflow of arithmetic operations on numbers integers is not detected. When operating with integers, it is up to the programmer to pay attention to the fact that the partial results of integer expressions must be kept within the valid range. for example, the expression 1000 * 100 /50 will not return 2000, because multiplication causes an overflow. In this case, the use of parentheses is necessary to force the precedence of the operators 1000*(100/50) = 1000 * 2 = 2000 will not produce any overflow. An Integer variable occupies two bytes of memory.

Byte

The Byte type is a subset of the Integer type and can contain values between 0 and 255. Bytes are therefore compatible with integers. It is possible to assign an Integer value to a byte type variable and vice versa (provided that it is included in the range 0..255), except when the byte type variable is passed as parameters. Additionally, bytes and integers can be used in the same expression. A variable of type Byte occupies one byte in memory.

Real

The real number range is 1 E – 38 to 1 E + 38 with a mantissa of up to 11 significant digits. An overflow during an arithmetic operation involving real numbers causes the program to abort and returns an error code, while an underflow will cause a result of zero. Although the real type is included among the standard scalar types, it should be noted that:
1) The Pred and Next functions cannot accept real arguments.
2) Real values cannot be used in array indexing.
3) Real values cannot be used to define the basic type of a set.
4) Real values cannot be used to control for and case statements.
5) Subsets of real numbers are not allowed.
Real values occupy 6 bytes in memory.

Boolean

A Boolean value can take on one of the logical values defined by the standard identifiers True and False. These are defined such that False < True. A boolean variable occupies one byte in memory.

Char

A Char value describes a character in the ASCII character set. Represented characters are sorted by their ASCII value, for example: 'A' < 'S'. The ordinal of character codes (ASCII) are between 0 and 255. A Char variable occupies one byte in memory.

Identifiers

Identifiers are used to indicate labels, constants, variables, programmer-defined types, procedures, and functions. An identifier can be 127 characters long, can contain letters and numbers, but cannot start with a number and cannot contain spaces for example:

A valid Abc valid (even if not very significant) Word valid String1 Valid ContatoreTempo Valid (use capital letters at the beginning of each word of a compound identifier, it increases readability) 1 name invalid (starts with a number) Name surname invalid ( contains a space)

Numerical constants

Numeric constants can be expressed in either decimal or hexadecimal and can be of type integer or real. Some examples of valid numeric constants:

integer type constants 1 valid 12324 valid -23 valid $ABC valid $123G invalid (G is not part of the hexadecimal digits) 1.0 valid if used as real, invalid if used as integer real type constants 1234.5678 valid -0.056 valid lE4 valid 4E -3 valid -1.2345678901E+10 valid 1 valid both if used as an integer and if used as a real

String constants

String constants are made up of a set of characters enclosed in two single quotes '. For example, the following strings are valid string constants:

'hello' 'in the middle of our life's journey' 'one a year' also allows you to print the superscript as an apostrophe '' represents an empty string '''' represents a string that contains only the character '

Control codes

Turbo pascal also allows you to insert control characters into strings. Codes preceded by the symbol # denote the ascii code of the control character, while those preceded by the symbol ^denotes the control character. For example:

#$1B is the ascii code of the escape character Similarly ^[ represents the escape character Examples of control characters are as follows #13#10 carriage return + line feed ^G^G^G^G beeps 4 times

The control codes can be concatenated to the string type to obtain the desired effect, for example if you print the 'test text' string #13#10 on the screen after displaying the characters between the quotes, the cursor will wrap. In any case, online it is possible to find the complete list of ASCII control characters which are the first 31 characters.

Comments

As in all programming languages, it is possible to insert comments in the listings. The Pascal turbo comes in 2 formats:
first format: {the first comment format opens with an open curly brace and closes with its closing curly brace}
second format: (* the second format is delimited by the round bracket and asterisk characters at the beginning of the comment and ends with the same inverted characters *)
with the first comment insertion format it is possible to insert some special instructions into the program such as the inclusion of an external code file or the so-called compilation directives or special instructions that enable or disable some features of the compiler:

Compiler directives {$I-} {$I INCLUDE. FIL} {$R-, B+, V-} (*$X-*)

As you can see, the compiler directives are inserted into the code with the same syntax as comments, so both comment formats are valid. We will see later the usefulness of activating or deactivating certain features of the compiler depending on the result we want to obtain and how to create, test and include an external file within our programs.

0 Comments

Submit a Comment

Your email address will not be published. I campi obbligatori sono contrassegnati *

Turbo Pascal MSX – Lezione 7

Turbo Pascal MSX – Lesson 7

FILES (part 1) Turbo Pascal provides a complete set of instructions for managing files. The FILE type is represented by a sequence of objects of the same type, the size of a file is not determined by the type, but a pointer is provided that moves...

Turbo Pascal MSX – Lesson 6

MSX Turbo Pascal Column by Stefano Roperto (part 6) Chapter 6 PROCEDURES AND FUNCTIONS Turbo Pascal, as we saw in chapter 4, provides the programmer with a complete collection of procedures and functions that make the language modern and complete. In...

Turbo Pascal MSX – Lesson 5

MSX Turbo Pascal Column by Stefano Roperto (part 5) Lesson 5 Loops and control instructions In turbo Pascal there are the usual iterative structures present in all high-level languages, the FOR DO loop and the REPEAT UNTIL and WHILE DO structures. ...

Turbo Pascal MSX – lesson 2

MSX Turbo Pascal Column by Stefano Roperto (part 2) Chapter 2 Introduction to the Turbo Pascal language We will now see how a program in Turbo Pascal is structured. We don't need to understand everything right now, we're just seeing the typical structure...