don't click here

How to fix those nasty branch-out-of-range errors

Discussion in 'Engineering & Reverse Engineering' started by Destructiox, Aug 9, 2009.

Thread Status:
Not open for further replies.
  1. Destructiox

    Destructiox

    Masochistic Maniac, Raving Lunatic Member
    237
    0
    0
    East Midlands, England
    Sonic 1 Lunacy.
    So, you've written some awesome code, for the most awesome shit ever to be hacked into sonic, you're sure it all works fine, go to build...........but wait? What's this?

    [​IMG]

    Seems like something's wrong. But you didn't even touch that part of the code, what's going on?

    What happened, is when you implemented your new code, you pushed this branch out of it's range. To be more specific, you've pushed the branch at line 899 out of range, by 556652 bytes. There's three ways to fix this, plain and simple.

    1) Take it to the next level!

    if the branch in question that is out of range is a bxx.s, then try replacing it with a bxx.w. IF you're wondering why, it's because bxx.w has a greater range than bxx.s (Wherein the x's used are wildcards and practically any form of branch can be substituted into bxx)

    2) I'll have to give myself a prrrrromotion!

    if the branch in question is a bra or bsr, then simply replace them with a jmp or jsr respectively. This will solve most of your problems, as jmp and jsr have unlimited range.

    3) SnooPING AS usual I see!

    So the above two methods didn't work or weren't eligible, so what do you do now? Work around the problem.

    Say, for example, you have the following code

    Code (Text):
    1. Obj01_RAGE:
    2.         cmpi.b    #1,($FFFFFFFE).w
    3.         beq.w    SonicRAAAAGE
    4.         move.w    #$1800,($FFFFF760).w; change Sonic's top speed
    5.         move.w    #$40,($FFFFF762).w; change Sonic's acceleration
    6.         move.w    #$60,($FFFFF764).w; change Sonic's deceleration
    7.         rts
    8.  
    9. [30,000 lines of code here]
    10.  
    11. SonicRAAAAGE:
    12.         jsr        KillSonic
    (Yes, I know this code is completely random and makes no sense, it's not supposed to :V)

    So, you go to build, but oops! It's out of range. You can't make the branch any bigger, and you can't change it to jsr or jmp, that will make it mandatory, so what do we do?

    What we can do, is set up a mini-chain of branches, which will ensure the code will work the way you want it to. It's ridiculously easy.

    Code (Text):
    1. Obj01_RAGE:
    2.         cmpi.b    #1,($FFFFFFFE).w
    3.         beq.w    SonicRAAAAGEJump
    4.         move.w    #$1800,($FFFFF760).w; change Sonic's top speed
    5.         move.w    #$40,($FFFFF762).w; change Sonic's acceleration
    6.         move.w    #$60,($FFFFF764).w; change Sonic's deceleration
    7.         rts
    8.  
    9. SonicRAAAAGEJump:
    10.         jmp        SonicRAAAAGE
    11.  
    12. [30,000 lines of code here]
    13.  
    14. SonicRAAAAGE:
    15.         jsr        KillSonic
    And that should work :D Give it a try on your latest out-of-branch errors, I guarantee it'll work, as long as you use the right labelling.





    Comments & Amendments welcome. Written by Destructiox.
     
  2. MarkeyJester

    MarkeyJester

    Nothing's Impossible Resident Jester
    2,183
    394
    63
    Japan
    A very good guide there, this is sure to come at some extreme use to someone who hasn't understood the diversity of certain branches and jumps, do you plan to create any other guides as useful as this?
     
  3. Destructiox

    Destructiox

    Masochistic Maniac, Raving Lunatic Member
    237
    0
    0
    East Midlands, England
    Sonic 1 Lunacy.
    I'd be lying if I said I had considered it, but I'd also be lying if I said that I definitely won't. So, watch this space?
     
  4. Selbi

    Selbi

    The Euphonic Mess Member
    1,497
    48
    28
    Northern Germany
    Sonic ERaZor
    As I already said here, I made this some time ago as well. Good guide though!

    @MarkeyJester: I think instead of making new guides, he should edit this into a basic error fixing guide.
     
  5. Destructiox

    Destructiox

    Masochistic Maniac, Raving Lunatic Member
    237
    0
    0
    East Midlands, England
    Sonic 1 Lunacy.
    Yeah selbi, I was writing this as you were posting, I didn't notice until afterwards :x But thanks for the compliment.
     
  6. Hayate

    Hayate

    Tech Member
    I know this is a big bump, but the instruction after SonicRAAAAGEJump should be a "jmp", not a "jsr".
     
  7. Destructiox

    Destructiox

    Masochistic Maniac, Raving Lunatic Member
    237
    0
    0
    East Midlands, England
    Sonic 1 Lunacy.
    Ah. Well that's what I get for rushing a guide >.> But ta for the fix! =)
     
  8. TmEE

    TmEE

    Master of OPL3-SA2/3 Tech Member
    1,726
    2
    18
    Estonia, Rapla City
    T-04YBSC-A !
    I like BRAs more so I rather move my code and/or data instead :P

    EDIT: 777 posts..... 666 is nicer :P
     
Thread Status:
Not open for further replies.