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

0 Members and 1 Guest are viewing this topic.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Self-Modifying Code (SMC)
« on: June 11, 2010, 01:58:45 pm »
I was just curious. How often do you use SMC (self-modifying code) in your ASM programs?
Feel free to list why or how you use it too.
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

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 #1 on: June 11, 2010, 02:07:33 pm »
I use it quite often in my graphics routines. Sometimes I just modify immediate values, other times I will modify the actual instructions being executed (for example, rotating A by any amount can be done in 4 instructions or less). Other than graphics, I don't use much SMC because most of my major projects are now APPs.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

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 #2 on: June 11, 2010, 02:33:31 pm »
I don't program in Asm enough to use it (so I'm not voting yet), but I think I'd use it often when I need speed. :D

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Self-Modifying Code (SMC)
« Reply #3 on: June 11, 2010, 02:35:58 pm »
Yeah, SMC is mainly for speed gains impossible with normal techniques. It is also useful as a tool to prevent people from understanding your programs. :P
Also for forcing instructions to have more options than normal (bit, set, and res, for example)
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

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 #4 on: June 11, 2010, 05:01:57 pm »
It can also be useful if you want to keep your game in one file, even when using save/load options built in the game

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 #5 on: June 11, 2010, 05:41:45 pm »
It can also be useful if you want to keep your game in one file, even when using save/load options built in the game
Though, that's not really self-modifying code, it's modifying data.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

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 #6 on: June 11, 2010, 07:27:26 pm »
Yeah true, just saying, though as it still involves modifying the program file.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Self-Modifying Code (SMC)
« Reply #7 on: June 11, 2010, 07:30:31 pm »
Mmm i like programs that use SMC for highscores ^^ It makes the appvar space less cluttered :P

SirCmpwn

  • Guest
Re: Self-Modifying Code (SMC)
« Reply #8 on: June 11, 2010, 07:44:55 pm »
^++

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 #9 on: June 11, 2010, 08:00:14 pm »
My main worry about appvars is that we're more prone to accidentally delete it or lose it in a RAM clear, and I'm not too fond of the idea of archving my appvars everytime I exit my game, especially if the appvar is very large

SirCmpwn

  • Guest
Re: Self-Modifying Code (SMC)
« Reply #10 on: June 11, 2010, 08:01:47 pm »
That makes sense, especially if it is large.  Obstacle Snake will automatically archive the appvar if it was archived when it was opened.  However, SMC is nice because most gamers archive their games to begin with and you don't have to worry about it getting screwed up (with a couple exceptions).

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Self-Modifying Code (SMC)
« Reply #11 on: June 12, 2010, 09:22:00 am »
Hey, the results kind of look like a bell curve. :P
Not surprising, at any rate.
Yeah, when I said SMC, I wasn't referring to saving data w/in the program itself. The only thing I don't like about that technique (it is, for the most part, good, like others have said) is that I have to have write-back for archived programs turned on. (I try not to use my archive too much, but the programmers are against me! :P)
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

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 #12 on: June 12, 2010, 01:13:20 pm »
yeah that can be annoying with all Garbage Collections, especially if you got an old 83+ x.x

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: Self-Modifying Code (SMC)
« Reply #13 on: June 12, 2010, 05:29:37 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 $9D95). 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.
« Last Edit: June 13, 2010, 10:15:56 am by Galandros »
Hobbing in calculator projects.

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Self-Modifying Code (SMC)
« Reply #14 on: June 12, 2010, 05:47:13 pm »
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.
___Axe_Parser___
Today the calculator, tomorrow the world!