MSX BASIC – lesson 13

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

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

Functions for working with strings (1st part)

In this episode I will talk about all those functions we need to work with strings; create them, modify them, cut them, search for a string within another and many other things. Since the topics I will cover are quite a few, I decided to divide it into 2 parts.

ASC
This function is used to know the ASCII code of the first character of the string.
Example: A$=”CIAO”: PRINT ASC(A$), will print the code of the first character of the A$ string, i.e. 67, which is the ASCII code of C. It does not matter how long the string is, it will always and only print the first. It is not mandatory to use a variable, as long as the character is enclosed in quotation marks:
PRINT ASC(“C”). We can save it in a numeric variable, since it returns a number: A=ASC(“C”).
Syntax: ASC(variable$) or ASC(“/”)

CHR$
Let's also say that it is the opposite of the function mentioned above. From the ASCII code it returns its correspondence. PRINT CHR$(67), will print the capital C on the screen. Unlike the ASC, it returns alphanumeric data, so if we want to use a variable to store it, it must be of this type: A$=CHR$(65). Remember that the ASCII code table is made up of 255 characters, we cannot exceed this last number, otherwise the MSX will report the error. It can also be used with non-decimal numbering, just add the suffix inside the quotes to make the MSX understand what type of numbering it is and the VAL function to transform it into numeric. Example: PRINT VAL(CHR$(“&H”+A$)), &H is the suffix for hexadecimal systems (I had already talked about this in the episode about Binary/octal and hexadecimal systems), it is a method used mostly when we want to load of data in RAM or VRAM, such as images, sprites or programs in ASM.
Syntax:CHR$(ASCII Code)

SPC
Function that creates empty spaces, as many as are inside the brackets after the function. It can only be used with PRINT. Example: PRINT “NAME”; SPC(10); "ADDRESS"
SYNTAX: PRINT SPC(NumberSpaces)

SPACE$
Create a string of white spaces. Unlike the previous one, it can be saved in a variable. The variable must be alphanumeric. It cannot be longer than 255 characters.
Syntax: A$=SPACE$(NumberSpaces)

INSTR
Search for a string in another, if this is found, it will return the number of its position. I'll explain better with an example. I will create 2 variables, one with my name and in the other I will insert more data, including my name. They are: N$=”ORAZIO”: 
F$=”MY NAME IS ORAZIO CACCIOLA”. The variable N$ will have to search in the other one, i.e. the F$. If this is found, it will return the position number of the first character. In the INSTR function we will put the variable to be searched first and then the search variable: INSTR(F$,N$). Since this will be true, it will give the number 16 as a result, in fact the O of HORACE in the variable F$ is the sixteenth character. It can be used directly with PRINT or inserted into a numeric variable. Strings can also be used instead of variables or both. If this is false, i.e. the search will not be successful, it will return the value 0. The INSTR function has another optional argument, placed at the beginning of the other 2 parameters. This is the character number from where you want the recharge to start. I take the previous example again: A=INSTR(5,F$,N$). He will start researching
starting from the fifth character, but the result will always be 16. But if we start the search from character 17 it will no longer find the N$ in F$ because it starts from character 16 and will give 0 as a result.
Syntax: INSTR([StartNumber],String1,string2)

LEN
Returns the number of characters of which the string or variable is composed. PRINT LEN(“HELLO”). The result is 4. Or next use it with a variable:
A$=”Hello everyone”: PRINT LEN(A$). Or again, we can insert it into a numeric variable: A=LEN(A$). Its result will always be the length of the string.
Syntax: LEN(Variable$)

STRING$
Returns a string of defined length of one character or ASCII code. Let's always take the same example. We want to display zero on the screen 10 times: PRINT STRING$(10,”0″) or PRINT STRING$(10,48). In the second argument we can use either the character we want to repeat several times, enclosed in quotation marks or its corresponding ASCII code . While the first argument is the amount of times we want to display it. We can also save it in an alphanumeric variable: A$=STRING$(10,48). The two topics must not exceed 255 characters and views.

And with this last feature, I close the first part of this episode.
Thank you all, see you next time.

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