๐Ÿ“œ โฌ†๏ธ โฌ‡๏ธ

Programming on the calculator

Hello. I think many of you have heard of programmable calculators (and some even used them). Oddly enough, here I did not find a single article telling about such an interesting thing, and therefore I decided to fill this gap and talk about the basics of programming on calculators.

Some time ago I found in my pantry the old โ€œ Electronics MK-61 โ€, which belonged to my dad. Naturally, I could not miss such a chance to learn not quite the "standard" programming on a calculator. (In case you do not have a programmable calculator, you can download an emulator here )

Memory


First of all, you need to understand how the memory is arranged in this calculator. The user has 4 stack registers: X, Y, Z, T. The contents of register X are always shown on the screen (in the calculation mode), the input of numbers also goes into it. In essence, after turning on the calculator, the stack memory looks like this:

T 0 Z 0 Y 0 X 0 

')
To push something onto the stack, use the [โ†‘] key. It copies the contents of register X into register Y, the value of register Y in Z, Z in T, and the value found in T is lost. That is, if before pressing the [โ†‘] button in the registers there were values

 T 5 Z 8 Y 14,5 X 6 
,

then after the values โ€‹โ€‹will be

 T 8 Z 14,5 Y 6 X 6 


In addition, there is a command that allows cyclically shifting all the values โ€‹โ€‹of the registers: T in Z, Z in Y, Y in X, X in T. To do this, press [F] [.] .

The [] command swaps the contents of the X and Y registers.

The [CX] command erases the contents of register X.

In addition to the stack memory, the calculator has 15 addressable registers (RG0-RGE). To work with them, use the keys [Xโ†’] and [โ†’X] . The first command places the contents of register X into the appropriate addressable register. For example, the sequence of commands [4] [Xโ†’] [0] places the number 4 in the RG0 register. The second command, as you already guessed, copies the contents of the addressable register into the X register. That is, [โ†’X] [0] will put X number 4.

It is important to remember that after turning off the calculator, the values โ€‹โ€‹of all registers are erased.

Calculations


The main thing you need to know before starting the calculations is that the MK-61 uses reverse Polish notation (ARF) . If we need to perform any unary operation, it is performed on the number in register X. However, the values โ€‹โ€‹of other registers do not change. Binary operations are performed on the contents of the registers Y and X (in that order). The result value is placed in X, Z is placed in Y, T is copied to Z. The previous value of register X is placed in the service register X1. Thus, if we need to calculate the banal 2 + 3 (in the arrester 2 3 +), then we need to press the keys on the calculator: [2] [โ†‘] [3] [+] . In this case, the values โ€‹โ€‹of the registers change as follows:

Before all operations:

 T 8 Z 14,5 Y 6 X 0 


After pressing the keys [2] [โ†‘] [3] :

 T 14,5 Z 6 Y 2 X 3 


After pressing [+] :

 T 14,5 Z 14,5 Y 6 X 5 


Let's try to do a little more complex calculations, for example, (15 + 2/5) * 7 + 10. In the arrester this expression can be written as: 15 2 5 / + 7 * 10 +. In order to calculate it on the calculator, we must press the keys: [15] [โ†‘] [2] [โ†‘] [5] [/] [+] [7] [*] [10] [+] .

Programming


So, well, this is more interesting :). To enter the programming mode, press [F] [] . 00 appears on the display. This indicates the current command number. In general, the program in MK-61 is a sequence of commands necessary to solve the problem. This is usually arithmetic operations, numbers, sometimes special codes of cycles and branching. In total, the program may consist of no more than 105 commands, which are numbered from 00 to 99. To enter a command, you must press the corresponding key. Here is the table of matching codes and keys of the calculator:



In programming mode, you can see the last 3 entered operations. for example
02 01 0E 06 on the display means:
  1. 06 - address of the next command entered
  2. 0E, 01, 02 - three consecutive commands located respectively at addresses 03, 04, 05.

The basics

Let us analyze the simplest problem of finding the area of โ€‹โ€‹a circle using the formula ฯ€r 2 . In order for the calculator to solve this problem, we enter the following commands in the programming mode ( [F] [] ) (we assume that the radius of the circle is in the RG1 register):

 //    โ€”  ,    00 [โ†’X] [1] //    RG1   X (61) 01 [F] [*] //     (22) 02 [F] [+] //   X  (20) 03 [*] //   X  Y (12) 04 [/] //     (    ) (50) 


That's all. Now we have to switch to the calculation mode with the command [F] [/-/] , and then we need to press the [/] key to go to the beginning of the program. Put the number 5 in the register RG1 ( [5] [Xโ†’] [1] ) and press [/] to start execution. After the calculator has passed all the steps, the number 78,539815 should appear on the screen - the area of โ€‹โ€‹a circle with a radius of 5.

Unconditional and conditional transitions

With the help of the [] command (51) you can perform an unconditional perezod (goto) at the desired address. To do this, in programming mode, press [] and then two digits of the address we want to go to. When the calculator reaches this operator while executing the program, it will continue execution from the command at the specified address. For example:
 ... 10 [F] [-] // 21 11 [] // 51 12 [4] [2] //   (42) ... 42 [+] // 10 

In this case, after step 12, the calculator will go straight to step 42.

The conditional transition is much more useful. To implement the conditional transition, there are 4 commands: [X >= 0] , [X < 0] , [X = 0] and [X != 0] . Using these commands, the contents of register X are checked for the condition. If the condition is not met, control passes to the address specified after the operator, otherwise (if the condition is met) the address is ignored and the program continues to function normally. For example:
 ... 09 [F] [*] // 22 10 [F] [โ†] // "if (X == 0)" (5E) 11 [4] [2] //  ,   ,    42 (42) 12 [+] //  ,  X = 0 (10) ... 42 [4] // 04 


Cycles

Cycles can be implemented using conditional jump commands, but, in addition, LK-L3 commands ( [F] [โ†’X] , [F] [Xโ†’] , [F] [] and [F] [] ). These commands operate on the contents of the registers RG0-RG3, respectively. Each time the loop command is executed, 1 is subtracted from the contents of the corresponding register and compared with zero. If the contents of the register are not equal to zero, a transition occurs to the address written after the cycle command, if equal, then a transition occurs to the command following the cycle transition address. To make it clearer, let's look at an example. We will assume the factorial of the number in register X.

 //  RG0    ,  RG1 . 00 [Xโ†’] [0] //   X  RG0 -    (40) 01 [1] //  1  X (01) 02 [Xโ†’] [1] //    RG1 -  (41) 03 [โ†’X] [1] //       (61) 04 [โ†’X] [0] //        (60) 05 [*] //   (12) 06 [Xโ†’] [1] //     (41) 07 [F] [โ†’X] // L0 -         .   ... (5) 08 [0] [3] // ...     03... (03) 09 [/] // ... -  (50) 


Conclusion


Of course, this article is not a complete guide to programming on the MK-61. If you are interested in learning more about this topic, I recommend reading the instructions (pdf, 6 MB) for this wonderful calculator.

Links

Source: https://habr.com/ru/post/111099/


All Articles