M Tutorial (Part 1) by Commencing with this issue, we now have two M tutorials per issue. This column deals with M running on character based interfaces, the other is a tutorial of MSM-Workstation (the native Windows 95 GUI implementation of the language). M is M irrelevant of the platform on which the language is being executed; and therefore the material presented here is very much the same so those who will be delving into the MSM-Workstation tutorial will get a kick start by going through this column. The programs in this tutorial can be run on the student version of Micronetics Standard M (MSM/Student). Regular readers of this column will be pleased to learn that Micronetics have extended the license of MSM/Student. In order to download a copy of this program check out Chris Bonnici's Download M section. Before you leave this page be sure to bookmark it so that you can find your way back with ease. For a free subscription to MWM click the Subscribe button on top. If you would like to know more about the host of products and services Micronetics provide, check out their web site at http://www.micronetics.com. Micronetics is listed in our Thank You page. Drop them an e-mail to let them know that you appreciate the fact that they are helping MWM. Back issues can be viewed by going to the the index. If you have any comments or questions we are here to help. Your input could be in the form of general comments giving us the green light, hitting the expand-topic button and even taking us back a step or two. No two people will generate the same solution to a problem. We welcome any code you send in, be it different approaches to what we present here or other code of your choice. All accepted submissions will be prominently displayed in MWM. Last time we delved into procedures (or as they are also called routines). In this issue we will be looking at their close cousins, functions. Click here to download this issue's routines
Intrinsic Function Last time we talked about user defined or extrinsic functions. Today we look at the functions that are available in M as standard; the intrinsic functions.
$ASCII(<string>[,<string position>]) or $A(<string>[,<string position>]) Within a computer, all symbols are stored at numbers. This is because by their very nature, modern computers operate exclusively in a binary world with the only valid digits being 0 and 1. In order for a computer to manipulate anything other than its numeric 2 digit world, it must reference a table to translate from the users world to its own. ASCII, which stands for American Standard Code for Information Interchange is the table (there are others though) exclusively used in the modern small and medium sized computers. The table spans 256 characters from 0 to 255. The characters 0-31 are controlled characters and are not normally printable. The characters up to 126 are normally found on the keyboard, while the characters 128-255 consist of a variety of language, math, currency and drawing symbols. The $A function converts a character to its numeric equivalent. It can take up to two parameters; the string and the position. The position is optional and if left out will result in the first character of the string being converted. If the character is null or the string position does not exist, $A will return a value of 1. Example 1 SET WORD="M WEB MAGAZINE" will give 77 Note that the last two numbers are 1. This is because the length of the variable WORD is 14 not 16. Example 2 W $A("M Web Magazine") Gives 77 as does W $A("M Web Magazine",1) Example 3 INTFN001Þ;Program
demonstrates intrinsic functions - Chris Bonnici - June 1997 The program above is a ASCII typewriter. At the bottom of the screen you get a prompt >. Anything you type will be displayed in ASCII format in the top part of the screen. If the screen fills up, it will clear. To exit the program dont type anything for two seconds. This will cause the READ instruction to timeout and return a null string, which gets translated to 1 in ASCII (whih should explain the Q:-1 below the read command. The rest of the code mainly handles the jumping of the cursor between input and output.
$CHAR(<ascii num1>[,<ascii num2> ]) or $C(<ascii num1>[,<ascii num2> ]) This function converts from the ASCII number to the character it stands for. This function does the opposite of $A as shown in the diagram. To specify more than one number at the same time separate them with commas.
Example 1 W $C(77,32,87,69,66,32,77,65,71,65,90,73,78,69) Will output M WEB MAGAZINE. If you recall we had converted the string M WEB MAGAZINE to ASCII in $A examples. INTFN002Þ;Program
demonstrates intrinsic functions - Chris Bonnici - June 1997 The program shown above shows how one could translate the ASCII character returned by the READ command when the read variable is preceded by an *. This could be useful for single letter prompts. INTFN003Þ;Program
demonstrates intrinsic functions - Chris Bonnici - June 1997 We modified INTFN001 two write the most basic form of encryption program. What the program does is add 1 to the read character before displaying it. The changed lines have been coloured differently. What is the character equivalent of 1. This is the number we got when we attempted to find the $A of null. Write a FOR loop to display the $C of the numbers 32 to 255. $C allows to access the symbols not available on a keyboard. The Try-it example above should display them. INTFN004Þ;Program
demonstrates intrinsic functions - Chris Bonnici - June 1997 INTFN004 is a very simple program that basically draws a little drawing using extended characters. Refer to the table on top (printing it will prove useful if you intend to use these characters frequently). INTFN005Þ;Program
demonstrates intrinsic functions - Chris Bonnici - June 1997 If you load INTFN005 into the M editor you might wonder how those codes got embedded directly into the code. The code is much more readable and simpler than the example INTFN004. To enter an extended character from the keyboard simply type in its ASCII number from the numeric keypad. Entering the number from the main keyboard will not work. This should work in practically any program both Windows and not (although in Windows, the mappings may be different from those presented in the table above). Another alternative to using the $CHAR function when outputting characters is to use the asterisk (*). For example W $C(253) and W *253 will product the same output. Write a currency conversion program. Dont forget to display the appropriate denominations.
Before we leave the $ASCII and $CHAR functions, I would like to mention briefly the characters between 0 and 31. We normally exclude them because they are non printing. For example ASCII 12 clears the screen and ASCII 7 sounds a bell (equivalent to W /BEL). You should know how to write a program that displays these numbers. |