Perfect functions

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: https://technical.swissmicros.com/decoders/dm42/

You can then copy/paste the listing and post it in "code" tags.
Post Reply
whuyse
Posts: 198
Joined: Thu Dec 21, 2017 1:23 pm

Perfect functions

Post by whuyse »

A 'perfect' function is one that mimicks the built-in functions:
  • save X in LastX
  • take the n arguments from the stack, compute the result in X
  • roll down the stack and replicate reg T n-1 times
These small routines will alow you to turn any of your programs into 'perfect functions'.

Call XEQ "PF1", "PF2" or "PF3" for a function of 1, 2 or 3 args returning a single argument

Code: Select all

In: A: program name
    stack: as needed for program
XEQ "PFn"
  n:    1 2 3
Out: T: T T T
     Z: Z T T
     Y: Y Z T
     X: result
     L: lastX

00 { 76-Byte Prgm }
01▸LBL "PF1"
02 R↓
03 LSTO "Y"
04 R↓
05 LSTO "Z"
06 R↓
07 GTO 00
08▸LBL "PF2"
09 R↓
10 R↓
11 LSTO "Y"
12 R↓
13 GTO 01
14▸LBL "PF3"
15 R↑
16 LSTO "Y"
17▸LBL 01
18 LSTO "Z"
19▸LBL 00
20 LSTO "T"
21 R↓
22 LSTO "L"
23 ASTO ST L
24 XEQ IND ST L
25 STO ST L
26 RCL "T"
27 RCL "Z"
28 RCL "Y"
29 RCL "L"
30 X<> ST L
31 END
Example: make Z^YMOD behave like a built-in function:

"Z^YMOD"
1111 ENTER
73 ENTER
55 ENTER
31
XEQ "PF3"

results in:

T: 1111
Z: 1111
Y: 1111
X: 26
L: 31

Example: to have MOD behave like the builtin % (and thus keep Y), do:

Code: Select all

01 LBL "MOD"
02 MOD
03 END
25 ENTER
2
"MOD"
XEQ "PF1"

Hope you find it useful,
Werner
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
Post Reply