Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: DJ Omnimaga on June 02, 2010, 03:06:30 am

Title: Self-modifying code
Post by: DJ Omnimaga 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).
Title: Re: Self-modifying code
Post by: Quigibo 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.
Title: Re: Self-modifying code
Post by: DJ Omnimaga 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
Title: Re: Self-modifying code
Post by: calcdude84se 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?
Title: Re: Self-modifying code
Post by: calc84maniac 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.)
Title: Re: Self-modifying code
Post by: DJ Omnimaga 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
Title: Re: Self-modifying code
Post by: ztrumpet 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
Title: Re: Self-modifying code
Post by: SirCmpwn 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.
Title: Re: Self-modifying code
Post by: DJ Omnimaga 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 ^^