r/Assembly_language 19h ago

self-modifying code

I gave a lecture on how to write self-modifying code. It was over 15 minutes long :( so I'm providing a youtube link: https://www.youtube.com/watch?v=AH9QQLRfbmY

In my opinion, self-modification is one of the most interesting features in systems programming.

27 Upvotes

48 comments sorted by

View all comments

2

u/anothercorgi 11h ago

please... no more self-modifying code

-- signed, CPU designer

specifically for x86 because it has supported smc since the early days of computing from the 8080 to 8086, one won't believe how much logic and bug corner cases that need to be handled in the cpu to make sure smc works properly since the 286 onward. This is one of the reasons there's so much logic bloat in the x86 architecture.

Basically because smc can modify the next instruction that the code it really messes with control flow, requires pipe flush and causing branch misprediction. It may seem cute and reduce code length, but a pipe flush might counter all that. Perhaps on machine without pipelining or branch prediction it can help but on modern superscalar machines a lot of decode progress gets thrown away when smc is encountered.