Turbo Pascal MSX – Lesson 4

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

MSX Turbo Pascal Column by Stefano Roperto (part 4)

Lesson 4

Instruction set

Reserved words are an integral part of Turbo Pascal. They cannot be redefined, nor used as identifiers or variable names. Turbo Pascal is basically the standard Pascal language revisited by Borland inc. Reserved words preceded by the * symbol are not part of the standard Pascal language and indicate the language extensions implemented by Borland for memory management, for the Overlay mechanism, for string management and for inserting assembler instructions directly into the code

*external Nil *shl and file not *shr Array forward *overlay
*string begin for of then case function or type const
goto packed to div *inline Procedure until do if program
var downto in record while else label repeat with end
mod set *xor

In addition to reserved words, Turbo Pascal provides a number of identifiers, procedures, functions, constants, predefined types, and variables. Below is the complete list of all the elements of the language. Most of these are not difficult to understand, in any case we will see them all as we continue through the course. Some of these elements are related to files such as BlockRead or SeekEof, others are used for string manipulation such as Length or Concat, still others such as GetMem or MemAvail for memory management. Furthermore, as you can see, the standard types such as integer, byte or char and some constants such as Pi which is worth 3.14 are defined here...

Addr Delay Length Real
ArcTan Delete Ln Release
Assign EOF Lo Rename
Aux EOLN LowVideo Reset
AuxInptr Erase Lst Rewrite
AuxOutPtr Execute LstOutPtr Round
BlockRead Exit Mark Seek
BlockWrite Exp MaxInt Sin
Boolean False Mem SizeOf
BufLen FilePos MemAvail SeekEof
Byte FileSize MemW SeekEoln
Chain FillChar Move Sqr
Char Flush New Sqrt
Chr Frac NormVideo Str
Close GetMem Odd Next
ClrEOL GotoXY Ord Swap
ClrScr Halt Output Text
With HeapPtr Pi Trm
ConInPtr Hi Port True
ConOutPtr IOresult PortW Trunc
Concat Input Pos UpCase
ConstPtr InsLine Pred Usr
Copy Insert Ptr UsrInptr
Cos Int Random UsrOutPtr
CrtExit Integer Randomize Val
CrtInit Kbd Read Write
DelLine KeyPressed ReadLn WriteLn

As you can see, the set of procedures and functions useful for programming is sufficiently broad, but the fact remains valid that it is possible to further expand the language by defining your own routines which can be saved in external files to then be used in your own programs. So let's see in detail the structure of a Turbo Pascal program and when we encounter them in the examples, the syntax of the language's instructions.

As we mentioned in the second chapter, Turbo Pascal has a very tidy structure. Each program is divided into precise sections and in each of these sections precise constructs are used. Let's see them in detail:

  1. program header. Every program in Pascal begins with the header

               Program ProgramName(parameters);
               Program CalculationProbability(input,output);
               Program Shooter(score,lives);

The parameters are not mandatory but reflect standard Pascal syntax.

  1. Statements
    1. Label declaration or Label
    2. Declaration of constants o Const
    3. Type declaration o Type
    4. Declaration of variables o Var
    5. Statement of procedures and functions o Procedures And Function

 

Let's now look at the declarative section in detail.

The labels or label they are used in unconditional jumps or with the GOTO instruction (which actually makes little sense in a structured language). After the keyword Label, labels are indicated separated by a comma and concluded by a semicolon. Obviously also in labels, the rule of using meaningful names should apply for the benefit of the readability of the code.

Label Output, 10, Aexuoi, 1addf,label;

In the program, you then insert the label at the desired point followed by a colon:

program use labels;
label     Exit;
var x,y:integer;
begin
for x:=1 to 10
begin
y:=y+x;
if (y<100) goto Exit;
end;
end;
exit: writeln(y);
end.

The goto statement in the for loop causes the program to exit the loop according to the condition before it reaches the end. Incidentally, turbo Pascal provides the Exit statement that does the same thing more elegantly.

 Constants are defined using the keyword Const they are used to define constant values, i.e. which never change during the program and the assigned value indicates their type in the same way as we saw in chapter 3 of the course. They may appear to be the same as the variables, but in reality there are significant differences. For example, a variable defined within a procedure or function is not visible (usable) outside the function or procedure in which it is declared, furthermore a constant cannot be modified so it is safer than a variable , but let's see some examples:

const
rate = 3.56; {this is a constant of type real}
unit = 'Meter'; {this is of type string}
address =$F5; {this is an integer expressed in hexadecimal}

usage in a program should be clear:
 
program useconstant;
const    coefficient = 3.5;
var
x:integer;
y:real;
begin
for x:=1 to 10
begin
y:=y+x*coefficient;
writeln(y);
end;
end.

If in the software specifications, the coefficient should change, just change the value of the constant, without looking for every occurrence within the program. The Turbo Pascal environment provides some predefined constants including

Pi                         {whose value is 3.1415926536E+00}
Maxint                 {whose value is 32767}
False                   {whose value is the boolean value for false}
True                    {whose value is the boolean value for true}

Note that the compiler's definition of it is False < True.

The definition of types o Type it is an important feature in programming in Pascal, it allows you to define complex types, starting from primitive types (char, integer, real. In the Type section you declare variables to which a type is assigned, furthermore various data types are provided by the compiler as well as to the standard types. Let's see some examples:

Type
Number = integer;
name = string[50];
EnumDay = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
Next, in the variable declaration section we can use the types defined here the same way we use standard types, like this:

Var
Ordinal:Number;
person:name;
DaysOfTheWeek:EnumDay;
and use them in the program.

Finally here we are at the definition of the subprograms, which we have already introduced previously, Procedures and Functions. The definition of a procedure or function follows exactly the same model as the program, the only differences are the following, instead of the word program the word is used procedures or function and the sections will find their place within the procedure label, const, type, var, procedure and function. This, as already mentioned, is a very important feature because it allows you to divide the program into smaller parts, i.e. to break down the problem into smaller ones and with the opportunity to pass and obtain parameters as results. But let's analyze in detail the differences between a function and a procedure and take a closer look at their definition and use in a program.

A procedure, as mentioned, is defined by the word procedures. For example:

procedures UpdateSpritePosition(pattern : byte, CoordX, CoordY:integer)
labels…
const…
type…
var…
procedures…
function… {Yes! A procedure, just like a function, can define other procedures and functions internally}
 
begin
instructions that vary;
the values of X and Y;
of the sprite;
model number;
end;

In the main program the procedure will then be used, as if it were a normal program instruction:

program game;
labels…
const
enemy=10;
player = 1;
ball=3;
type…
var
sprite:byte
X,Y : integer


Begin


{somehow calculates sprite position values}
X:=expression;
Y:=expression;
{and updates them}
Sprite:=enemy; {here we use the value of a constant}
UpdateSpritePosition(sprite,X,Y);
{somehow calculates sprite position values}
X:=expression;
Y:=expression;
{and updates them}
Sprite:=ball;
UpdateSpritePosition(sprite,X,Y);


Rest of the program;
end.

It is immediately clear how dividing the program into smaller parts makes programming more effective. The procedure can be called at any point in the program with the parameters relating to the action we need to perform. Obviously, being a procedure, as well as a function, a separate program that performs a single simple action, it is the programmer's task to validate the input data in order to avoid errors during the execution of the program. This is especially important for functions. Functions are declared like procedures, with the difference that they return a value in the name. For example, a function that converts celsius degrees into kelvin degrees would have the following structure:
function Celsius2Kelvin(degrees:real):real
{here too you can declare constants, types, variables, procedures, functions}
Const
Coeff = 273.15;
begin
Celsius2Kelvin:=degrees+coeff;
end;
the difference between a procedure and a function, therefore, is that the procedure takes parameters as input and performs an action, while the function always returns a value and must be part of an expression, for example

temperature := Celsius2Kelvin(15);
k:=5+(Celsius2Kelvin * 15)-(2*4+4) mod 15;
write
Celsius2Kelvin(15)
without the value being used in an expression or assignment statement, it will cause an error.
Therefore, a procedure is a portion of the program that performs an action, while a function is a portion of the program that returns a value of a specific type.

From the next lesson onwards, we will begin to delve deeper into the language. We have laid the foundations, now we will go a little faster, with some slightly more complex examples so as to introduce the new topics, directly by programming.

 

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 3

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 guys...

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...