-
Notifications
You must be signed in to change notification settings - Fork 1
Example macros
43615 edited this page Dec 19, 2023
·
46 revisions
Many of these are not optimal in terms of length, but serve as understandable explanations of different features. Try to read them in detail and understand what's happening!
For a more general introduction, especially for use as a calculator: Wikipedia article about dc. Many GNU dc scripts will still work, but most algorithms have significantly easier possible implementations.
I also have a repo of more advanced programs.
- Clear screen:
[clear]\
on Linux &c,[cmd /c cls]\
on Windows - Leave only the top object on the stack:
zRz1-C
orSacLa
- Infinite loop with constant memory usage (CPU stress test):
[lax]salax
- Sum the entire stack:
[+]z2-X
- Average the entire stack:
zSa[+]la1-XLa/
- Absolute value:
[_1*]s|d0>|
- Has to use a register for the helper macro, leaves it there. Use
d0>|
on successive runs.
- Has to use a register for the helper macro, leaves it there. Use
- Round down (positive only):
d1+%
- Rounding is a side effect of all commands that take integers.
- Factorial (assuming positive integer):
d1-dSa[d1-]rX[*]LaX
- Recreate number (to apply W change):
{"x}
- Crude "benchmark" of CPU speed (lower=better):
[[timens]"]2Xr-P
- "Advanced" prompt (shows current params and prints stack on enter):
[WOIK{["[ ]+n]4X}[> ]n?fl?x]S?l?x
- leftpad(str, len, ch) without registers:
3Rrdg_3R-_3Rr*r+
- Random character in the ASCII printable range:
95N32+a
- 20-char password:
[][95N32+a+]20X
- 20-char password:
- Random UUID:
{16o[][ON"+]32X}8~[4~]3X[[-]r++]4X
- 2 chars shorter than a UUID!
- Allocate ~30G of memory with the shortest macro possible:
@9;a
- Check whether the thread on reg
a
has finished, runb
if yes:Za0MaZa!=b
- Assuming it should yield a result on its stack.
These can probably be optimized a lot.
A hashmap stores registers with arbitrary numbers. Each register is a stack of RegObjs. Each RegObj has an associated array. Each array cell can store a string, which can be manipulated char-wise.
- save(byte, reg, pos, idx, chr):
S4S3S2_1@9+dS1,Zl2[[[]l1,S]l2l1,Z-1+X]S0!<0[l1,b1@9 1+,B]l1,Zl2-1-Xl3l1,;gl4[l3l1,;dgl4-1-0ar*+l3l1,:]s0!<0l3l1,;l4~_1-_3Rar++l3l1,:[1@9 1+,bl1,B]1@9 1+,ZXL4L3L2L1L05C
- No checks for input ranges, all indices start at 0, register #s used for storage start at -999'999'999.
- load(reg, pos, idx, chr) -> byte:
S4S3S2_1@9+dS1,Zl2[[[]l1,S]l2l1,Z-1+X]S0!<0[l1,b1@9 1+,B]l1,Zl2-1-Xl3l1,;gl4[l3l1,;dgl4-1-0ar*+l3l1,:]s0!<0l3l1,;l4%a[1@9 1+,bl1,B]1@9 1+,ZXL4L3L2L1L05C
- Uninitialized cells return 0.
To demonstrate the potential for obfuscation.
-
113ax
- value ofq
-
69 420 128|ax
- peak comedy numbers, found the magic value of 128 using dc:im itself! -
1.562 3tax
- x tan grows rapidly close to xπ2/= -
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzax
- top-of-stack increment -
[6chrs ]19*1-gax
- 6 19*114= -
[qxd]1~1~@8Xx
- chain reaction using[x]
, needs ~10G of memory (@7
for lite version)