News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: [SNES][SMK][ASM] Where to put new ASM code?  (Read 1049 times)

MattCoalman

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SNES][SMK][ASM] Where to put new ASM code?
« on: March 28, 2021, 12:51:44 pm »
Hello everyone!

I am new to ROM hacking, and I have a question.
I would like to modify something in SMK, so I used the Mesen Debugger to locate the code I would like to modify and I successfully did! Now I need to add two ASM lines in a subroutine.
I know my code is working well because when i overwrite another subroutine to put my code, it is ok, but of course, the code from this other subroutine is damaged.

So, how can I do that? How can I add my two lines (5 bytes)? My plan was to create a subroutine where "there is free space", and to call it.

Thanks a lot for your help.

FAST6191

  • Hero Member
  • *****
  • Posts: 3126
    • View Profile
Re: [SNES][SMK][ASM] Where to put new ASM code?
« Reply #1 on: March 29, 2021, 03:55:53 pm »
It is as you said. Find some space that is doing nothing and jump/branch to it (call if you prefer that parlance but calling a subroutine tends to evoke ideas of you passing data to and from it, which you may even do later, rather than needing space for a couple of instructions and maybe not even a single push and pop).
Some will make the space by optimising the game or something else in it, whether this is out and out optimisation, finding something not used by the hack or taking some space from a deleted function (if you delete all the anti piracy in a game then you probably have some space that is now not doing anything). Some will also come the other way and try to optimise both the thing you are subverting and the code you are adding to get it down small enough to be an in place hack (in place is nice as it makes it more likely to work with other hacks where someone took the first obvious choice for "ooh free space").
Some systems and games this gets a bit harder to find either free space in general or free space in the mapper/bank/segment you are using (or in turn adding something to either detect or swap and swap back and deal with any interrupts).

For the record while it did not matter here it is generally good form to note what the game is at some point -- while SMK is presumably Super Mario Kart and not Super Mahjong Karakuchi... yeah. Or if you prefer is GoW God of War, Gears of War, SMB then Super Mario Brothers or Super Monkey Ball?

MattCoalman

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: [SNES][SMK][ASM] Where to put new ASM code?
« Reply #2 on: March 29, 2021, 04:41:06 pm »
It is as you said. Find some space that is doing nothing and jump/branch to it (call if you prefer that parlance but calling a subroutine tends to evoke ideas of you passing data to and from it, which you may even do later, rather than needing space for a couple of instructions and maybe not even a single push and pop).
Some will make the space by optimising the game or something else in it, whether this is out and out optimisation, finding something not used by the hack or taking some space from a deleted function (if you delete all the anti piracy in a game then you probably have some space that is now not doing anything). Some will also come the other way and try to optimise both the thing you are subverting and the code you are adding to get it down small enough to be an in place hack (in place is nice as it makes it more likely to work with other hacks where someone took the first obvious choice for "ooh free space").
Some systems and games this gets a bit harder to find either free space in general or free space in the mapper/bank/segment you are using (or in turn adding something to either detect or swap and swap back and deal with any interrupts).

For the record while it did not matter here it is generally good form to note what the game is at some point -- while SMK is presumably Super Mario Kart and not Super Mahjong Karakuchi... yeah. Or if you prefer is GoW God of War, Gears of War, SMB then Super Mario Brothers or Super Monkey Ball?
Hi FAST6191. Thanks for your answer. To begin, yes the game is Super Mario Kart :)
Yes I see what you mean, JMP will be better to not take charge of passing data. So I have tried to put my subroutine piece of code where there is twenty lines of
Code: [Select]
BRK #$00, and it work well !
So that's it? The "free space" is the bunch of BRK #$00? Is it safe like this? I mean, these BRK #$00 has no purpose in the game?

Thanks again

FAST6191

  • Hero Member
  • *****
  • Posts: 3126
    • View Profile
Re: [SNES][SMK][ASM] Where to put new ASM code?
« Reply #3 on: March 31, 2021, 01:31:57 am »
Determining the uselessness of a section of data in a game is ever the tricky thing.

If your emulator has a trace logger ( http://fceux.com/web/help/TraceLogger.html , and I am told no$gba might have just got one too) then you can run that and have it note if some piece of code/memory is executed/read. Can do well if you do all the actions (including any idling type things, options menus, cancelling out of things, credits, cutscenes... in an ideal world) but there is the option for some random bit of gameplay to go to it. A more basic break on read/execute or log equivalents thereof will also do something but the full trace logger might give you a better idea of what is executed around it and thus whether it is likely to be hit up at some point.

You can run a disassembly over it and try to see if anything calls it/references it. Sometimes you find something (or indeed nothing) but that does not take into account any pointer maths most of the time. I have not pulled apart enough SNES games to know how much it does with any pointer maths, though on later systems it is not uncommon at all. You also tend not to get as many surprises on older systems where it tends to run through set patterns in code (think levels 1 to 8) where newer ones can ping around between minigames, maps, menus and more all within the same scene. Games are games though rather than straight shot find and replace and write out and exit data conversion programs so always the option for it.

I don't know what the binary encoding of that will be offhand but not all data within a ROM is code -- if that happened to be a nice blank tile you just overwrote...
Runs of actual instructions do have their uses in coding, though probably not here (no point in doing a NOP slide in a SNES game as the original dev and most things don't need that much waiting*).

*if you think you might have a race condition, or are waiting on a slow bus, then you can code it properly with nice indicators and interrupts or, especially if every cycle counts is your mantra, you can add in a bunch of thumb twiddling instructions so you can be reasonably sure you are past the danger zone.

For super mario kart it did seem that the source code was leaked. That might also offer some insights, or might not and comes with the downsides of being leaked code. Might however have some labels and comments that make things have some context.
https://datacrystal.romhacking.net/wiki/Super_Mario_Kart:ROM_map might not be complete but has some data on what goes where, and I am sure whatever passes for the SNES Mario Kart hacking community will have a bit more still.

For the most part if it looks good and a few of the sanity checks above did not reveal anything then give it a go and check that as well. Hard drive space is cheap and ctrl and z works too. Maybe you get to come back in a few years as someone discovers oh no it actually did something after all, or not.