Author Topic: Self-modifying code  (Read 12692 times)

0 Members and 1 Guest are viewing this topic.

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
Self-modifying code
« on: June 02, 2010, 03:06:30 am »
In a post somewhere in that 3300+ posts sub-forum, Calc84maniac mentionned how is SMC behavior with Axe programs. However, I cannot find it anymore and I wanted to be certain SMC can really be used in Axe and if it's recommended.

If, for example, you store 7 to {GDB1+6}, is the data changed permanently, or is it reverted back to normal after exit?

What would be the behavior in the following?

(RAM prgm) no-stub
(RAM prgm) MirageOs
(RAM prgm) Ion
(RAM prgm) DoorCS
(RAM prgm) DoorCS from home screen (Noshell/Calcutil style)
(archived prgm) MirageOs with writeback to archived programs ON
(archived prgm) MirageOs with writeback to archived programs OFF
(archived prgm) Ion
(archived prgm) DoorCS
(archived prgm) DoorCS from home screen (Noshell/Calcutil style)

I would like to know, because to make a game, I would like to create a map editor and edit the map data directly. However, I am unsure if I want to have to use an APPVAR for that. If I can put the data and permanently modify it directly inside my program, I would rather do that instead (I backup often, anyway).

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
« Reply #1 on: June 02, 2010, 03:33:12 am »
Quite simply, no.  Self modifying code is not possible without disassembly of the generated programs becasue you need to get the address of the instruction to modify.  This is a bad practice becasue future versions and new optimizations change the size and order of the code which means you lose compatibility with newer versions.  That doesn't even include the fact that every time you modify your code, you have to disassemble again to get the address since it changes with how many bytes come before the SMC.  And yet another thing, it doesn't actually save very much space nor speed especially for the effort you would have to go through to implement it.  This does require greater than average assembly knowledge to do.

EDIT: Oops! I misread your post.  If you just want a write back, you can do this with axe commands by copying the running program in ram (address E9D93) into the program itself which you can get via the getcalc() command.  You can also get the size of the amount of data you need to copy with that command by reading the 2 byte number directly before the pointer.
« Last Edit: June 02, 2010, 03:36:32 am by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

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
« Reply #2 on: June 02, 2010, 03:35:53 am »
ah yeah that sounds more complicated than I expected x.x. Thanks for the info.

I guess I'll have to resort to storing to appvars then

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
« Reply #3 on: June 02, 2010, 07:59:48 am »
Quigibo, is there any way to get the name of the program from w/in it? Because I may compile a program as DEMO but someone could rename it to DEMO4. Is there any way to check, or do I have to read OP1 at the beginning of the program?
"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
« Reply #4 on: June 02, 2010, 10:00:38 am »
Quigibo, are you aware that all shell programs automatically write-back? (Well, except the case DJ mentioned where an archived program is run in MirageOS with archive writeback turned off.)
"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
« Reply #5 on: June 02, 2010, 12:41:19 pm »
Now that I think about it my idea wouldn't work for massive maps x.x, because every change would be overwritten when I recompile my program. I don't think very well sometimes x.x

That said it might still be useful to know for people who wants to put their game save data directly inside their program

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
« Reply #6 on: June 02, 2010, 04:45:05 pm »
EDIT: Oops! I misread your post.  If you just want a write back, you can do this with axe commands by copying the running program in ram (address E9D93) into the program itself which you can get via the getcalc() command.  You can also get the size of the amount of data you need to copy with that command by reading the 2 byte number directly before the pointer.
Wow, that's awesome!  I'm pretty sure I'll do this some! ;D I can only think of one problem:
Quigibo, is there any way to get the name of the program from w/in it? Because I may compile a program as DEMO but someone could rename it to DEMO4. Is there any way to check, or do I have to read OP1 at the beginning of the program?
Is there anyway to get the content of OP1 without using hex code?  Thanks! :D

SirCmpwn

  • Guest
Re: Self-modifying code
« Reply #7 on: June 02, 2010, 04:56:09 pm »
I've been thinking of how to use real SMC, and change the executing code, but it would require some h4x.  I have thought of a few possible methods, and will post about them when I figure more out.

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
« Reply #8 on: June 02, 2010, 11:27:47 pm »
EDIT: Oops! I misread your post.  If you just want a write back, you can do this with axe commands by copying the running program in ram (address E9D93) into the program itself which you can get via the getcalc() command.  You can also get the size of the amount of data you need to copy with that command by reading the 2 byte number directly before the pointer.
I just noticed this edit. Nice to know ^^