M Web Magazine Issue 003 (Jun 5, 1997 - Sept 4, 1997)

Teach You, Teach Me (Part 3)

Back to Formatting, but…

SCRN004Þ ;Program demonstrates the library function COLLIB - Feb 1997 - ACB
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞFOR COL="WHITE","RED","BLUE","BLACK","MAGENTA","CYAN","GREEN","YELLOW" D SETFGCOL^COLLIB(COL) W !!,"COLOR ME NOW"
ÞQUIT

 

MSM/Student does provide a mnemonic that sets foreground and background colors, but this mnemonic is incorrect. MWM has contacted Micronetics and we have been told that the correction will appear in the next release of MSM. Although we didn’t have this mnemonic, we very soon created our own library for this type of formatting. It is called COLLIB and is listed below. This library has three procedure within it: SETBGCOL (set background color), SETFGCOL (set foreground color) and SETCOL. SETCOL takes two parameters specifying both the foreground and background colors. This library is listed below. All the procedures in this library refer to colors using their written name (in capitals at the moment).

COLLIBÞ; Color Library - ACB - Feb 1997
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
SETFGCOL(COL)Þ ;Sets the foreground color to that passed as a parameter. If an invalid color is specified the default will be white and a beep will be sounded - ACB - Feb 1997
ÞN I,SCOL
ÞS I=0 FOR SCOL="BLACK","RED","GREEN","YELLOW","BLUE","MAGENTA","CYAN","WHITE" Q:SCOL=COL S I=I+1
ÞI SCOL'=COL W /BEL S I=7
ÞW *27,"[",(30+I),"m"
ÞQ
Þ;*** EOR ***
SETBGCOL(COL)Þ ;Sets the background color to that passed as a parameter. If an invalid color is specified the default will be black and a beep will be sounded - ACB - Feb 1997
ÞN I,SCOL
ÞS I=0 FOR SCOL="BLACK","RED","GREEN","YELLOW","BLUE","MAGENTA","CYAN","WHITE" Q:SCOL=COL S I=I+1
ÞI SCOL'=COL W /BEL S I=0
ÞW *27,"[",(40+I),"m"
ÞQ
Þ;*** EOR ***
SETCOL(FCOL,BCOL)Þ ;Calls SETFGCOL and SETBGCOL with the two colors passed as parameters. Invalid entries are handled by the respective procedures - ACB - Feb 1997
ÞD SETFGCOL(FCOL),SETBGCOL(BCOL)
ÞQ

 

SCRN005 also makes use of COLLIB. This should help drive home the basic idea of having a library function. Write it once use it many times from different programs.

Both COLLIB and SCRN005 introduce us to an new instruction; NEW or as it appears in our program N. In order to explain this command we shall first have to understand how variables operate in M.

 

The Life of Variable

The life of a variable defines how long the variable remains in existence after it is created. If you’ve been playing around with M for some time, you’ve probably already come across the error <UNDEF> whenever you try to manipulate a variable that doesn’t exist.

Unlike other programming languages, a variable in M will last until M is shut down. To check this claim out, shutdown MSM and load it again. When you get the command prompt, type the command WRITE and you should get nothing. DO ^SCRN002 and after the program finishes, type WRITE again. What did you get? The value of variables as they were last set by SCRN002.

From this paragraph there are a couple of things to note:

This extended life can cause problems and a programmer must be aware of this. For example during a development session a program appears to work well, yet when it is tested elsewhere the same code gives errors. What these baffled people fail to notice is that during development certain variables are already defined.

We can solve this problem by using the command KILL or (K). This command takes three different varieties:

KILL<two spaces>

In this flavor KILL is kamikaze. It destroys all variables. Some programmers like to use this instruction at the beginning of their programs so as to ensure that nothing is defined upon commencement.

KILL <variable>,…,<variable>

Here KILL is specifically told to destroy only the variables specified. Other variables are not effected. One example for the use of such a command is to remove variables that hold sensitive data, once the data has been done with.

KILL (<variable>,…,<variable>)

Here the variables are enclosed in brackets. This type of instruction instructs KILL to remove all variables except those specified.

 

RTN006Þ ;Program demonstrates procedure calls - May 1997
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞK
ÞF I=1:1:100 D CURRLINE(I)
ÞQ
Þ;*** EOR ***
CURRLINE(LINE)Þ ; Displays the current line
ÞW !,"This is line ",LINE," (Press CTRL+C to terminate program)"
ÞS I=LINE-1
ÞW !,"The Line before is ",I,!
ÞQ

Try it!

Run the program shown here. Why is it that variable I never increments to two as instructed by the FOR command? Don’t forget, press CTRL+C to stop the program. Dry run the program paying special attention to what values I takes when the program is running.

The problem is that I in SHOWLINE, I is being decremented by 1 and when control returns to MAIN it is initiating every time. This is a common problem in programming and the solution lies in encapsulating variables. Basically this means that although we have used I in the two modules we want the program to treat both separately.

By NEWing the variable I in CURRLINE we are instructing the computer to store the current value of I, creating a new instance of it within CURRLINE. As CURRLINE comes to an end, the variable I created within it is destroyed and the original value restored. RTN007 is the modified version of RTN006 (and it works well).

RTN007Þ ;Program demonstrates procedure calls - May 1997
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html Þ;You are using this program at your own risk
Þ;
ÞK
ÞF I=1:1:100 D CURRLINE(I)
ÞQ
Þ;*** EOR ***
CURRLINE(LINE)Þ ; Displays the current line
ÞN I
ÞW !,"This is line ",LINE," (Press CTRL+C to terminate program)"
ÞS I=LINE-1
ÞW !,"The Line before is ",I,!
ÞQ

Some of you might be wondering what all the fuss is about. Couldn’t we have use J instead of I, or IA or something else? Definitely yes, but try to imagine a situation in which your program is a good few thousand lines long, calling three dozen library routines written by yourself and by others. Are you willing to memorize each and every variable? What happens if you didn’t notice that a variable is already in use, is the error variable-related or is it something else? The solution simply doesn’t hold in practice.

Also, we have observed that many of those who practice encapsulation normally prove to be better programmers in the long run. Since you must know what variables are being used inside the procedures, it normally encourages more planning than the quick and dirty approach of thinking and writing.

Before we conclude this section, one thing we would like to add is that parameters are automatically encapsulated in that they are automatically NEWed when the procedure becomes active.

What do you think will happen if you new a variable inside a procedure? Alter RTN007 and include a NEW I after the instruction S I=LINE-1.

Try It!

In MWM001 we told you that practice makes perfect. We repeat this here. Take situations you are familiar with and write modular programs for them. The more time you put into this, the more proficient you will become.

SCRN005Þ ;Program demonstrates the library function COLLIB - Feb 1997 - ACB
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞDO SETBGCOL^COLLIB("BLACK")
ÞWRITE /ED(2),/CUP(1,32),"SAY IT IN COLOR"
ÞREAD /CUP(5,1),"Enter your Colorful Quote: ",QUOTE:30
Þ;Check if the user input has timedout
ÞQUIT:('$TEST)!(QUOTE="")
ÞWRITE /CUP(7,1),"Enter Foreground Color"
ÞDO COLLIST SET FGCOL=COL
ÞWRITE /CUP(7,1),/ED(0),"Enter Background Color"
ÞDO COLLIST SET BGCOL=COL
ÞDO SETCOL^COLLIB(FGCOL,BGCOL) W /CUP(7,1),/ED(0),QUOTE
ÞREAD /CUP(20,1),"Press Any Key ",*ANS
ÞQUIT
Þ;*** EOR ***
COLLISTÞ ;Starting at Line 9 display the colors. The selected color is stored in the variable COL. Background is set to Black, but the color black is show on a white background so as to be visible.
ÞNEW I,CHOICE
ÞDO SETBGCOL^COLLIB("BLACK")
ÞWRITE /CUP(9,1)
ÞSET I=1
ÞFOR COL="BLACK","RED","GREEN","YELLOW","BLUE","MAGENTA","CYAN","WHITE" D SETFGCOL^COLLIB(COL) D  SET I=I+1
Þ.D:I#2=1 SETBGCOL^COLLIB("WHITE")
Þ.D:I#2=0 SETBGCOL^COLLIB("BLACK")
Þ.W /EL(2),I,". ",COL,!
Þ.Q
A10ÞREAD /CUP(20,1),"Enter Color: ",CHOICE
ÞI CHOICE=1 S COL="BLACK"
ÞE IF CHOICE=2 SET COL="RED"
ÞE IF CHOICE=3 SET COL="GREEN"
ÞE IF CHOICE=4 SET COL="YELLOW"
ÞE IF CHOICE=5 SET COL="BLUE"
ÞE IF CHOICE=6 SET COL="MAGENTA"
ÞE IF CHOICE=7 SET COL="CYAN"
ÞE IF CHOICE=8 SET COL="WHITE"
ÞE WRITE /BEL GO A10
ÞQUIT

DO is a very loaded command. In SCRN005 we discuss yet another method of how to group command. In label A10-5, we have a version of DO that will ultimately help us to uncompound lines. Some people criticize M because of its ability to put a very large number of commands on a single line. As we said in previous TYTM’s nothing stops a person from splitting lines up. With some commands such as FOR all instructions iterated by it must lie on the same line. This can pose a problem especially when we want to do lots of things once in the loop. We can solve this problem by putting some of the commands in a block underneath the FOR and invoking them with a DO followed by two spaces.

Let’s take a closer look at the following fragment of SCRN005

.
.
.
ÞFOR COL="BLACK","RED","GREEN","YELLOW","BLUE","MAGENTA","CYAN","WHITE" D SETFGCOL^COLLIB(COL) D   SET I=I+1
Þ .D:I#2=1 SETBGCOL^COLLIB("WHITE")
Þ .D:I#2=0 SETBGCOL^COLLIB("BLACK")
Þ .W /EL(2),I,". ",COL,!
Þ .Q
.
.
.

When the DO<two spaces> is encountered is processed commands on the lines below it. It knows which lines it must pass through because these lines commence with a period (.). It will continue down the block until it either encounters a QUIT or until it gets the the first line not having a period. Once the block ends, the instruction following the DO is processed (in our example, SET I=I+1).

Try rearranging the SCRN005 so that all instructions other than the FOR will be blocked.

Try it!

Alter the programs in MWM002 to operate in this manner.

Note to M Companies

The Source has been set up so that you can explain your wares better. As the name states, this section must help explain what a product does, and, more importantly, how it does what it does.
A tutorial (that should contain worked examples) is the type of information our readers expect. If you are interested in booking this section for the March 5, 1997 issue of MWM, drop us a note as soon as possible. Address your e-mail to mwm@mcenter.com with the subject set to MWM The Source. In your e-mail tell us, in a paragraph or two, a little bit about the product (only one) you are proposing together with a rough idea of what you have in mind for your presentation.
MWM will normally offer The Source on a first come, first served basis.

Our last program in this issue demonstrates two new mnemonics. /MSMSC saves the cursor position, while /MSMRC, restores the same. We will not comment any further and this program is quite simple to follow.

SCRN006Þ ;Program demonstrates some of the Mnemonic namespaces - Feb 1997 - ACB
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞW #
10ÞW /MSMSC
ÞW "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
ÞH 5
ÞW /MSMRC
ÞW "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
ÞW /MSMSC
20ÞR /CUP(20,1),"Another Line? (Y/N): ",ANS#1
ÞQ:ANS="N"!(ANS="n")
ÞI ANS'="Y"&(ANS'="y") W /BEL G 20
ÞW /MSMRC
ÞG 10

Goodbye

Our reviewers have noted that probably this is the most dense article to every appear in TYTM. We have in fact split this issue into parts to make it easier for you to assimilate the different parts. This is not the end of this fascinating and important topic. Next issue we will be delving on this subject even more. In the meantime, send us your problems and we’ll try to give you an answer in the shortest possible time.

 

Format it

 

The table below gives a list of the X3.64-1979 Mnemonic Namespace that comes with MSM. This is not the only one, but we are sure that it will provide all the controls you will need.

Mnemonic Description Notes
/BEL Ring Bell  
/BS Backspace Cursor  
/CBT(n) Backup n Tab Stops Tab equivalent to 8 characters
/CHA(n) Move Cursor To Column n  
/CHT(n) Forward n Tab Stops Tab equivalent to 8 characters
/CNL(n) Cursor Down n Lines  
/CPL(n) Cursor Up n Lines  
/CR Beginning Of Current Line  
/CTC(a,b,...) Define Tab Stop Position (columns)  
/CUB(n) Cursor Back n Columns  
/CUD(n) Cursor Down n Rows  
/CUF(n) Cursor Forward n Columns  
/CUP(row,col) Position Cursor at row, col  
/CUU(n) Cursor Up n Lines  
/DA(a,b,...) Set Attributes for foreground and background colors, blink, etc  
/DAQ(a,b,...) Define Area Qualification  
/DCH(n) Delete n Characters At Cursor character to the left.  
/DL(n) Delete n Lines At Cursor shift lines up  
/DSR(a,b,...) Device Status Request read using READ command  
/ECH(n) Erase n Characters At Cursor replacing with blanks  
/ED(n) Erase Display:

n=0: Cursor To End Of Display

n=1: Home To Cursor

n=2: Entire Display

 
/EL(n) Erase Line:

n=0: Cursor To End Of Line

n=1: Start Of Line To Cursor

n=2: Entire Line

 
/FF Cursor Down 1 Row  
/HPA(n) Cursor To Column n  
/HPR(n) Cursor Forward n Columns  
/HT Horizontal Tab  
/HTS Set Horizontal Tab Stop at current cursor position  
/HVP(r,c) Cursor To Row r And Column c  
/ICH(n) Insert n Blanks At Cursor  
/IL(n) Insert n Blank Lines At Cursor  
/IND Move Cursor 1 Column Down  
/LF Move Cursor 1 Line Down  
/MSMRC(n) Restore Saved Cursor Position  
/MSMRGN(n,m) Setup Scrolling Region  
/MSMRM(a,b,...) Reset Modes  
/MSMSC Save Current Cursor Position  
/MSMSM(a,b,...) Set Modes  
/NEL Move To Column 1 Of Next Line  
/NP(n) Advance n Pages Of Terminal Display Memory  
/PP(n) Backup n Pages Of Terminal Display Memory  
/RI Reverse Index (Up 1 Column)  
/RIS Reset To Initial State  
/RM(a,b,...) Reset Modes  
/SGR(a,b,...) Set Graphical Rendition  
/SM(a,b,...) Set Modes  
/TBC(a,b,...) Clear Tap Stops  
/VPA(r) Move To Row R At Same Column  
/VPR(n) Cursor Down n Lines at same Column position  
/VT Vertical Tab (Up 1 Column)  

Pass on our Web Address!

E&OE

Next Page

Up/TOC

1