Reversing compiled code

Discussion of hardware and software development for Super NES and Super Famicom.

Moderator: Moderators

Forum rules
  • For making cartridges of your Super NES games, see Reproduction.
Post Reply
Oziphantom
Posts: 825
Joined: Tue Feb 07, 2017 2:03 am

Reversing compiled code

Post by Oziphantom » Fri Jul 05, 2019 5:43 am

On my path to ExHiROM I decided to stop by HiROM first, so I've extended my code to handle HiROM. I chose a fun small project Super Piccross to test this HiROM functionality upon, its pic-cross how hard can it be right? Also to learn the Lua extensions so I can modify/augment gameplay.

However the code was weird, once I worked out the bank register changes it started to make a little more sense but then still weird.

Then I noticed things like this..

Code: Select all

jC07A04                          lda #$7000 
                                 tax  
                                 lda #$0000 
                                 bra bC07A11 

bC07A0D                          sta p7F6000,x 
bC07A11                          dex  
                                 dex  
                                 bpl bC07A0D 
which screams "this was done by a compiler", then the weirdness made sense. What it does is, sets Y to the "struct" pointer, and Bank to 7e and then DP to the other structure. This way all the code is

Code: Select all

lda $0024,y
ora $0036,y
clc
adc $7e08775
sta $0005,x 
Which is maddening, as I work out a variable, but even if I change the label for it to it, it doesn't help because the code references it with a lda $XXXX,r since it can shift the X and Y around depending upon what the code needs they don't even always have the same offsets when used in different code.

Anybody got tips for this kind of thing? Do you see it often is worth adding "set offset" and look up into stuct systems to my regenerator?

User avatar
dougeff
Posts: 2673
Joined: Fri May 08, 2015 7:17 pm
Location: DIGDUG
Contact:

Re: Reversing compiled code

Post by dougeff » Fri Jul 05, 2019 6:08 am

Not necessarily done by a compiler. Humans program like this too.

Tips for what? Figuring out what every RAM address does? That is probably not possible.
nesdoug.com -- blog/tutorial on programming for the NES

Oziphantom
Posts: 825
Joined: Tue Feb 07, 2017 2:03 am

Re: Reversing compiled code

Post by Oziphantom » Fri Jul 05, 2019 6:44 am

Sure, but a human that can't see that this

Code: Select all

jC07A04                          lda #$7000
                                 tax 
                                 lda #$0000
                                 bra bC07A11

bC07A0D                          sta p7F6000,x
bC07A11                          dex 
                                 dex 
                                 bpl bC07A0D
should be

Code: Select all

jC07A04                          ldx #$6FFF
                                 lda #$0000
bC07A0D                          sta p7F6000,x
                                 dex 
                                 dex 
                                 bpl bC07A0D
I doubt is going to go hardcore into Stack, DP, DB relocation, with offset strats.

Tips on how to track/manage/comment lessen the pain.

User avatar
rainwarrior
Posts: 7803
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Reversing compiled code

Post by rainwarrior » Fri Jul 05, 2019 7:37 pm

Oziphantom wrote:Which is maddening, as I work out a variable, but even if I change the label for it to it, it doesn't help because the code references it with a lda $XXXX,r since it can shift the X and Y around depending upon what the code needs they don't even always have the same offsets when used in different code.
I think at this point every reference to a variable could become a two-part expression:

Code: Select all

sta variable-base, X
So you'd have "variable" labels that can be an absolute address, and "base" labels that give a name to a temporary frame of reference. The variable would remain identified and searchable with different base combinations this way.

Post Reply