Email or username:

Password:

Forgot your password?
Devil Lu Linvega

I'm working on a project on self-modifying code(SMC), and I'd like to get as many stories of fun things you've done using SMC, papers, articles, videos. Anything but examples of SMC used for obfuscation - stack hacking, ad-hoc quoting, literal injection, branchless conditions, etc.. I want to see it all :maru:

35 comments
Brian Swetland

@neauoire My first exposure to self modifying code was the CHRGET routine which is part of C64 BASIC, but located in zero-page RAM since the 16bit next character pointer (TXTPTR) is the operand of an LDA instruction in the middle of this routine.

c64-wiki.com/wiki/115-138

Devil Lu Linvega

@awwaiid thanks! I hadn't read this. That was very good.

John Nesky

@neauoire My music app beepbox.co/ uses branchless conditions (or conditionally inserting code that then gets JIT compiled on demand depending on your settings, thanks JavaScript) for the different FM synthesis algorithms and enabling effects like echo and reverb.

brett g porter

@neauoire this isn't the paper I was looking for, but there have been people doing smc for audio dsp. A decade or so ago someone tried to commercialize it, but can't find any links to that tonight. patents.google.com/patent/US58

Ward Cunningham

@neauoire

I wrote a program where the cpu's data structure just happened to be a valid display processor instructions so that the live state of the program appeared on the screen. This is not the usual case for self-modifying code but in the spirit none-the-less.

code.fed.wiki.org/maze.html

Devil Lu Linvega

@k9ox Oh that's very cool! I recently made a game, and during the splash when our studio's logo comes up, I stream the game data as an audio stream, makes me think of your maze:

R4_Unit

@neauoire one of my favorite esolangs is ByteByteJump, which is essentially nothing but self modifying code. The only instruction is: copy a byte from location A to location B, then unconditional jump to location C. This is sufficient for Turing completeness. esolangs.org/wiki/ByteByteJump

There is also some belief that ByteByte, a variant that just has a cyclic program counter, is also Turing complete, although that is unproven: esolangs.org/wiki/ByteByte

Fun as minimal examples.

@neauoire one of my favorite esolangs is ByteByteJump, which is essentially nothing but self modifying code. The only instruction is: copy a byte from location A to location B, then unconditional jump to location C. This is sufficient for Turing completeness. esolangs.org/wiki/ByteByteJump

There is also some belief that ByteByte, a variant that just has a cyclic program counter, is also Turing complete, although that is unproven: esolangs.org/wiki/ByteByte

Devil Lu Linvega

@R4_Unit ah yes! I'm well familiar with this one and other OISC that uses modify the state like that.

screwlisp

@neauoire I implemented a method for the 'make-load-form generic for a CLOS lisp class that loaded its last state in 'shared-initialize :after

Basically store your running memory as bulky lisp code and have to dump / recompile it after and before use. It's self-modifying code 'cause from my perspective the code file was the same file, just whatever I was doing last got written into the shared-initialize.

Devil Lu Linvega

@screwtape I guess that counts :) thanks! You've also reminded me that I should probably have a note about metacompilers.

rob pike

@neauoire Well, back in the early '80s we did it for graphics:

9p.io/cm/cs/doc/87/archtr.ps.g

Compiling on the fly, not exactly what you likely mean by self-modifying code, but hey, we generated instructions to put dots on the screen using the parameters of the call to generate near-optimal code in all cases.

Devil Lu Linvega

@robpike Not really what I was looking for, but fascinating details on bitblt which I hadn't come across before. Thanks for sharing. I realize that the later implementations were bound to the hardware by instructions, but are there bitblt emulators around that would let me paint, say, a X11 window with its primitives? Curious to play around with it :)

David JONES

@neauoire not my example but Knuth implements coroutines using self modifying MIX (in The Art of Computer Programming)

jaseg

@neauoire In case you haven't searched for it yet, I'm sure there is a lot of wisdom on this in the demoscene. I wouldn't know where to start looking though.

Elias MÃ¥rtenson

@neauoire it was quite common on the Atari ST. One time I used it was a demo where the screen consisted of a number of tiles that were flipping to reveal various patterns (spelling out the names of the different parts of the demo).

The demo was using overscan, and when you do that on the ST you need to stay in sync with the raster beam at all times, so you want to avoid conditional branches if at all possible.

So, the main loop consisted of about 200 JSR (jump to subroutine) calls, and prior to each frame the jump addresses were rewritten to point to different routines that managed one tile. Each routine was written to take exactly the number of cycles needed to draw one scanline, so it could always stay in sync.

Of coure, a list of addresses could be used as well, but that would have taken more time since you'd have to load the pointer from memory into a register and save it back. If not, the individual routines wouldn't have all registers available.

@neauoire it was quite common on the Atari ST. One time I used it was a demo where the screen consisted of a number of tiles that were flipping to reveal various patterns (spelling out the names of the different parts of the demo).

The demo was using overscan, and when you do that on the ST you need to stay in sync with the raster beam at all times, so you want to avoid conditional branches if at all possible.

xypnox

@neauoire Don't have much actual experience in SMCs but have always wanted to explore it.

The request is, if you would, please do share a list of resources you liked while going through all the stuff about SMCs.

Also fine if you don't, time is of essence.

Devil Lu Linvega

@xypnox It's something I use everyday, and I've been write a post about all sorts of fun things that it allows me to do. The call in the original post was to see if I forgotten anything.

Wait a few days and I'll have a page that goes into detail on all this stuff :)

Earth Notes

@neauoire Back around the early '80s when dinos and Z80s and 6502s still roamed the Earth, a little self-modifing code helped with real-time graphics and servo control. We won't talk about my DEC HL / INC HL thing though.

Csepp 🌢

@neauoire Not a personal example, but I heard that back in the day it was used for reducing code size to fit it into the instruction cache.

Dave Fischer

@neauoire A long time ago I wrote a few little demo programs in LINC machine language on a PDP-12. There's a LINC instruction to sample an analog channel and leave the results in the accumulator, but which channel you're sampling is embedded in the instruction. So if you want to select a channel at runtime, you have to generate the instructions on the fly.

Peter Burka

@neauoire I think the first example of SMC I saw in a product I worked on were polymorphic inline caches (PICs) in the ENVY/Smalltalk JIT.

Exandra

@neauoire I worked on dynamic binary translator projects (like an off-road JIT).

One translated PPC to x86-64 code on the fly, in chunks of 5-30 instructions, writing and optimizing new machine code on the fly and patching together the completed chunks just in time to execute them.

One very confusing week I was translating a music synthesis app but it kept crashing; I came to realize that the audio app itself used self modifying code to efficiently generate the notes.

…

Hart of the Wud

@neauoire I'm pretty sure this is not what you're looking for, but there was an interesting line of research in the 90s to create circuits with FPGAs using self-modifying genetic algorithms. The results were kinda scary since they ended up leveraging inscrutable analog properties of the hardware instead of human-understandable digital logic.

eetimes.com/whatever-happened-

Devil Lu Linvega

@praxeology I mention the paper cited in the article briefly, it's not directly what I'm looking for but still relevant. Thanks :)

scrottie (he/him/they)

@neauoire github.com/scrottie/Code-Splic for injecting inspection, and for in-lining method calls in a certain not-dead-yet language. I wrote a conference lighting talk that I never gave that continuously pulled from github, parsed the diffs, and spliced changes in to the running program. Wasn't able to keep up with maintaining that and afaik no one else really used it for anything so long dead.

Devil Lu Linvega

@scrottie thanks! I'll dive in. Never heard of something like this before

Go Up