Author Topic: Self-Modifying Code (SMC)  (Read 14910 times)

0 Members and 1 Guest are viewing this topic.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Self-Modifying Code (SMC)
« Reply #15 on: June 12, 2010, 05:47:19 pm »
Thanks for the clarification Galandros! :D
userMem (address $95D5 if memory serves well).
It's $9D95. :)

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Self-Modifying Code (SMC)
« Reply #16 on: June 12, 2010, 11:52:55 pm »
I will only start using SMC when I need speed.
Not using SMC keeps compatibility with APPS and is much more reusable code for other projects (like OSes or other APPS).


I see some confusion between SMC (Self Modifying Code) and write-back on Omnimaga forums. Not only on this topic. I will try to differentiate them.

SMC is when code will simply modify some other code.
Only possible when the code is in RAM so it is easily changed like normal data. The modified code is temporary. (true for 99% of cases) So very rarely if ever SMC needs write-back for the program to work as expected.

Write-back is writing the "copy" back to the "original" program.
This happens because the assembly programs you run is not the original but a copy in a address equated as userMem (address $95D5 if memory serves well). For example, in the end of the program data resides the save game data. In order to the program work as expected and your precious progress be kept you need some additional code (it finds out the position of the original program and copies the data back to it) or a shell with write-back option that does the job for the coder.

Note that SMC and write-back are independent. But generally data needs write-back and SMC do not.
If I made a mistake or was not clear, please point out, no one is perfect.

EDIT: bonus example
Mmm i like programs that use SMC for highscores ^^ It makes the appvar space less cluttered :P
It is correctly write-back and not self modifying code. You are modifying some data of a highscore, not code and you need that is copied back to the original program.
(curiously you can hard code high score data into code but is very rare and not the best way to store this kind of data)
EDIT: Oh, but if you are talking about Axe Parser, it is possible you are using both SMC and write-back.
aAH thanks for the carification Galandros ^^

Offline mapar007

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 550
  • Rating: +28/-5
  • The Great Mata Mata
    • View Profile
Re: Self-Modifying Code (SMC)
« Reply #17 on: June 13, 2010, 06:39:20 am »
I very rarely use SMC, because it spoils readability. If I can get a decent speed, memory or code elegance advantage, I consider using it, but even then I write it in 'normal code ' first, for the sake of debugging.

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: Self-Modifying Code (SMC)
« Reply #18 on: June 13, 2010, 10:22:37 am »
Thanks for the clarification Galandros! :D
userMem (address $95D5 if memory serves well).
It's $9D95. :)
aAH thanks for the carification Galandros ^^
Edited the post. I was doing from memory and I did not have the time to check it.
Thank you both. It is good to know that it was clarifying. ^^ I hope BuilderBoy got it, too.
Maybe we can link the post for other time or even make a topic with only it, if it is needed.

I occasionally use it.  Sometimes, its for speed considerations, sometimes for size, other times for simplicity.  Its not too difficult to copy small code segments from apps into ram and then execute them there so compatibility is usually not an issue.  One smc I really like to use is modifying (ix+0) and change the 0 to other values to really quickly read and write to the nth element of a list without using any extra registers.  I did this in Pyoro for the "closest block" algorithm.
That is a use I haven't seen often for SMC. And is really useful.
I documented quite some examples SMC here: http://wikiti.brandonw.net/index.php?title=Z80_Optimization#Self_Modifying_Code
I will add that one too.
Hobbing in calculator projects.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Self-Modifying Code (SMC)
« Reply #19 on: June 13, 2010, 10:27:59 am »
Quigibo, I must have missed that example.  Changing the offset is a really cool idea.  Nice job! ;D

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Self-Modifying Code (SMC)
« Reply #20 on: June 13, 2010, 11:54:13 am »
Yeah, I use the (ix+nn) trick sometimes in my variable-height masked sprite routines
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: Self-Modifying Code (SMC)
« Reply #21 on: June 13, 2010, 12:13:14 pm »
Yeah, I use the (ix+nn) trick sometimes in my variable-height masked sprite routines
Yeah, that is the smaller and faster way in size to have in count the variable height.

Not using SMC in this case is much harder to code.
« Last Edit: June 13, 2010, 12:13:58 pm by Galandros »
Hobbing in calculator projects.