ca65 range error when calculating address

Building your first 6502-based project? We'll help you get started here.
Post Reply
reesey-spoon
Posts: 6
Joined: 01 Sep 2021

ca65 range error when calculating address

Post by reesey-spoon »

I am using calculated addresses to push onto the stack and return from, but when I use

Code: Select all

addressTableH
    .byte >address00, etc
addressTableL
    .byte <address00-1, etc
I get a range error (not within 255) whenever the lowbyte of an address is 0 and causes an underflow. I am sure there is a simple way around this, I can't find it though.
hoglet
Posts: 367
Joined: 29 Jun 2014

Re: ca65 range error when calculating address

Post by hoglet »

reesey-spoon wrote:
I get a range error (not within 255) whenever the lowbyte of an address is 0 and causes an underflow. I am sure there is a simple way around this, I can't find it though.
Try this:

Code: Select all

addressTableH
    .byte >(address00-1), etc
addressTableL
    .byte <(address00-1), etc
reesey-spoon
Posts: 6
Joined: 01 Sep 2021

Re: ca65 range error when calculating address

Post by reesey-spoon »

hoglet wrote:
Try this:

Code: Select all

addressTableH
    .byte >(address00-1), etc
addressTableL
    .byte <(address00-1), etc
Thank you! I'll try it tonight
User avatar
BigDumbDinosaur
Posts: 9425
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: ca65 range error when calculating address

Post by BigDumbDinosaur »

hoglet wrote:
Try this:

Code: Select all

addressTableH
    .byte >(address00-1), etc
addressTableL
    .byte <(address00-1), etc

Yuh beat me to it! :D
x86?  We ain't got no x86.  We don't NEED no stinking x86!
reesey-spoon
Posts: 6
Joined: 01 Sep 2021

Re: ca65 range error when calculating address

Post by reesey-spoon »

hoglet wrote:
Try this:

Code: Select all

addressTableH
    .byte >(address00-1), etc
addressTableL
    .byte <(address00-1), etc

I'm guessing the -1 NEEDS to be on both the High and Low address. I have been hunting down a bug for the past few days and I think its because, after reading your advice, I only but it on the low byte side.
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: ca65 range error when calculating address

Post by drogon »

reesey-spoon wrote:
hoglet wrote:
Try this:

Code: Select all

addressTableH
    .byte >(address00-1), etc
addressTableL
    .byte <(address00-1), etc

I'm guessing the -1 NEEDS to be on both the High and Low address. I have been hunting down a bug for the past few days and I think its because, after reading your advice, I only but it on the low byte side.
Indeed - so say for example, the target address is $8000, then >$8000 is $80 and <$8000 is $00, but by just taking 1 from the low byte you get: >$8000 = $80 and <$7FFF = $FF, so the resulting address is $80FF which isn't what you want, so >$7FFF = $7F and <$7FFF is $FF which is what you want.

So you have an address that lands on a page boundary $xx00 and it crashes. This is a tricky thing to fix as simply adding in debug code can change the target address which may then cause it to work! it's only when address00 is $xx00 that you'll have issues.


-Gordon
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
reesey-spoon
Posts: 6
Joined: 01 Sep 2021

Re: ca65 range error when calculating address

Post by reesey-spoon »

drogon wrote:

Indeed - so say for example, the target address is $8000, then >$8000 is $80 and <$8000 is $00, but by just taking 1 from the low byte you get: >$8000 = $80 and <$7FFF = $FF, so the resulting address is $80FF which isn't what you want, so >$7FFF = $7F and <$7FFF is $FF which is what you want.

So you have an address that lands on a page boundary $xx00 and it crashes. This is a tricky thing to fix as simply adding in debug code can change the target address which may then cause it to work! it's only when address00 is $xx00 that you'll have issues.


-Gordon
Yes I found this out the hard way. My program was hanging at "random" intervals. Took me quite some time to realize it was unrelated to what I was working on at the moment. Thank you for all your help
Post Reply