Behavior of rel. branch instructions with integer operands

Let's talk about anything related to the 6502 microprocessor.
Post Reply
furrykef
Posts: 5
Joined: 04 Jan 2012
Contact:

Behavior of rel. branch instructions with integer operands

Post by furrykef »

I'm developing a 65816 assembler, and I've noticed an oddity with at least some other assemblers. Suppose we have this code:

bra $12

...what does it mean? Is the $12 relative or absolute? To me it makes sense that $12 is taken as an absolute address and so, assuming a PC of 0 (for the sake of example), this will emit 80 10 (since the absolute address $12 gets converted to the relative address $10). That way labels can simply resolve to absolute addresses as they do in every other context and it should just work. But the assemblers xkas and WLA don't see it that way; they output the operand verbatim, producing 80 12. Thus they behave differently depending on whether the operand is an integer literal or a label. (bass, a later 65816 assembler by the same author as xkas -- rewritten from scratch I believe -- does emit 80 10.)

Is there any 'right' way for this to behave? Or is it just an implementation detail, since (as far as I know) proper assembly code should never need to pass an integer literal to a relative branch instruction anyway?
Last edited by furrykef on Thu Jan 05, 2012 2:58 pm, edited 1 time in total.
teamtempest
Posts: 443
Joined: 08 Nov 2009
Location: Minnesota
Contact:

Post by teamtempest »

I vote for 'just works'. I think it's probably simpler to write an assembler that way anyway, as to have two different behaviors means there has to be additional code to distinguish seperate cases.

But it's your assembler in the end, and you can make it do whatever you want. That's part of the fun.
User avatar
BitWise
In Memoriam
Posts: 996
Joined: 02 Mar 2004
Location: Berkshire, UK
Contact:

Post by BitWise »

My 6502/65816 assembler treats the operand to a relative branch as a absolute address and derives the relative offset from it.

I don't think I've ever come across an assembler for any processor with relative branches that works differently from this.
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Post by GARTHWILSON »

That sounds pretty normal. I think most assemblers would take it as an absolute address, and, if you wanted for example to branch forward past an instruction, without a label, you'd do something like BNE $+2.
furrykef
Posts: 5
Joined: 04 Jan 2012
Contact:

Post by furrykef »

Thanks, guys. I think I posted in the wrong forum, though... perhaps it should be moved to the Programming forum?
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Post by GARTHWILSON »

no big deal-- it's ok.
Post Reply