MSX BASIC – lesson 7

da | Nov 20, 2023 | MSX BASIC | 0 comments

“MSX BASIC” COLUMN by Orazio Cacciola (part 7)

Mathematical, relational and logical operators

Today I will talk about the various operators that are fundamental in our Basic MSX programs.
The mathematical operators, which we all know and use in our daily lives, such as addition (+), subtraction (-), multiplication (*) and division (/), are the fundamental basis for our calculations.
PRINT 21+32; or PRINT 50-25; or PRINT 25*4; to finish PRINT 50/2.
By doing this, we will have our immediate results and how to use a calculator, but by inserting them into our programs, we can combine even more operations, also using them with variables. Remember that multiplications and divisions will take precedence over additions and subtractions. But this problem can be overcome by inserting the calculations that we want to give precedence inside the round brackets.

To give an example: 100 A=52*(25-10) + (20/2) + 22.
Let's examine the line above: first everything inside the brackets will be calculated: A=52*15+10+22. Then it will calculate in order of sign priority:
A=780+10+22 and finally the remainder, since the operators are all sums.
Remember that everything in brackets will be calculated in reading order and not in sign priority. In cases where within the same parenthesis there will be a sum or subtraction and a division or multiplication and then the sign priority rule will apply. You can also use parentheses within parentheses for even more complex calculations.

In addition to the 4 operators we know, there are 3 others that are very useful for our calculations.

\ (reverse split); using this operator will give the result only of the integer, eliminating the decimal part. Example: PRINT 18\4, the result will be 4
^ (Exponentiation) Calculates the power of 2 numbers or variables. PRINT 2^3. Result will be 8.
MOD; Returns the remainder of an integer division. Taking the example above:
PRINT 18 MOD 4, the result is 2. To give a simple example to show one of the many ways to use it, I will print on the screen only the even numbers in a variable that will be increased by one number at a time.

10 A=0 20 A=A+1: IF A MOD 2 = 0 THEN PRINT A 30 GOTO 20

Although I haven't talked about the IF/THEN conditioning function yet, the program is very simple to understand. I'm saying that if the remainder is zero then the number is even so it has to print it, otherwise it's odd, because the remainder will be 1 and won't print.

Relation operators compare 2 numbers or strings and return –1 if this is true, 0 if it is false. They are mainly used with IF/THEN, in fact, you will understand better when I talk about the latter. For now let's understand what they are for.

Relation Operator = Equal < Less > Greater <> Different <= Less equal >= Greater equal

Logical operators are similar to relation operators, they compare 2 values that are true or false. These are also widely used with the IF/THEN statement. They are: AND, OR, I will compare the 2 variables with the operators to show their results.

Potrebbe essere un'immagine raffigurante il seguente testo "X XY XANDY XORY XEQVY 11 XIMPY IMP 1 XXORY 1 1 1 0 1 1 01 0 0 0 0 1 1 00 0 0 1 0 1 1 1 0"
AND will be true only if the 2 results are true
OR will be true if 1 of the 2 or both results are true
EQV will only be true if they are both true or false
IMP will be true in all cases except when the former is true and the latter is false
XOR will be true only when one of the 2 is true.
NOT is the only unitary operator. It only operates on one topic, and is easy to understand. If it is false it becomes true and vice versa if it is true it becomes false.

Before saying goodbye, I wanted to say that the column will be on holiday for a few weeks, we will see each other again at the beginning of September.
Thanks again to everyone and see you soon.

0 Comments

Submit a Comment

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

LEZIONE 26 – INTERVAL ON, RND ED ALTRI

LESSON 26 – INTERVAL ON, RND AND OTHERS

Good morning everyone, In this episode I will talk about an important interrupt, especially for games, when we want to make an enemy suddenly appear or a useful object, but also to create a stopwatch or a clock and many other things. The COMMAND exploits the...

LEZIONE 26 – INTERVAL ON, RND ED ALTRI

LESSON 24 – THE DATA MANAGED BY THE MSX (PART 3)

Kind regards to everyone. Today we will close this topic, I will talk about direct or random access files. But first, as I mentioned in the last episode, I will give a small explanation about the program. First of all, the first thing to say is that I used a command which...