Jump to content
IGNORED

Assembler Jump target to far away.


Recommended Posts

There seems to be limit in Assembler how far away the target adress can be when jumping or using sub routines. My solution is to make sort of a "stepping stone"

so that i first jump to this stepping stone that is located in between and then jump to the target from here avoid error messages like "target to far way.. out of bounds etc.."

 

Is this the normal way of avoiding problems with adresses to far away ?

Link to comment
Share on other sites

Branching instructions (BNE, BMI, etc) are the ones with the range limit (127 bytes forward, 128 back). JSR and JMP suffer from no such range limit on the 6502. Sure you can use "chains" of branching instructions, but it can get out of hand. One way is to replace branches with jumps which use the opposite logic:

 

BNE label

 

becomes:

 

BEQ label2

JMP label

label2:

 

It's also often possible to juggle your code around a bit so that those branch targets fall within range.

  • Like 1
Link to comment
Share on other sites

One way is to replace branches with jumps which use the opposite logic:

 

I think that that is the straight forward logic, and the other way around feels for me as the opposite logic ;-)

 

INIT LDX #$30

LOOP AN INCREDIBLE

AMOUNT OF CODE

DOING THE MOST AMAZING

EFFECTS!

DEX

BEQ READY

JMP LOOP

READY RTS

 

 

This is (For me) the common way of using branches.

Edited by ProWizard
Link to comment
Share on other sites

Branching instructions (BNE, BMI, etc) are the ones with the range limit (127 bytes forward, 128 back). JSR and JMP suffer from no such range limit on the 6502. Sure you can use "chains" of branching instructions, but it can get out of hand. One way is to replace branches with jumps which use the opposite logic:

 

BNE label

 

becomes:

 

BEQ label2

JMP label

label2:

 

It's also often possible to juggle your code around a bit so that those branch targets fall within range.

 

That is exactly how it done. CA65 has a macro "jXX" for this. And I bet MADS or other assemblers have them too. If not shouldn't be hard to implement if the assembler supports anonymous labels.

As soon as ca65 tells me a branch is too far away, I use the "j-version" aka longbranch. And later, short for the release of the prod I check all longbranches and try to optimize them away with code shuffling.

 

 

Sidenote:

what I mean by anonymous labels. You can do this in ca65 for example:

        beq        +
        jmp        far_away
+:
        nop

I would be surprised if the macro package "longbranch" implements it differently. Haven't checked it.

Edited by Creature XL
Link to comment
Share on other sites

Thank you . It helps to know that jmp and jsr don't have the same limitations as beq bne etc. This will help specialy on where to place certain pieces of code in my program.

 

I use a text editor and compile with atasm. i prefer this way of writing assembler.

Link to comment
Share on other sites

 

I think that that is the straight forward logic, and the other way around feels for me as the opposite logic ;-)

 

INIT LDX #$30

LOOP AN INCREDIBLE

AMOUNT OF CODE

DOING THE MOST AMAZING

EFFECTS!

DEX

BEQ READY

JMP LOOP

READY RTS

 

 

This is (For me) the common way of using branches.

this takes some additional cycles over the other method though, so if it's a loop that is relatively small and runs for many iterations, it can make a difference that matters.

Link to comment
Share on other sites

If you want your code to be relocatable, you really have to use the 'stepping stone' method.

 

I once did it with several step stones like this:

 

...regular code

clc

bcc skip ; jump over stepping stone code

stepstone2

clc

bcc stepstone1 ; which is further up

skip

continue regular code...

....

....

bxx stepstone2 ; here I start branching back

.....

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...