6502.org
http://forum.6502.org/

Would branches on signed comparisons make sense?
http://forum.6502.org/viewtopic.php?f=10&t=2775
Page 1 of 1

Author:  fachat [ Sun Nov 17, 2013 1:38 am ]
Post subject:  Would branches on signed comparisons make sense?

I think somewhere here on the forum once asked whether I was planning branches based on signed comparisons.

After looking at the v-flag primer, I am not sure they make sense: http://www.6502.org/tutorials/vflag.html

The problem I see is that only ADC and SBC modify the V-flag, but NOT the CMP operation!
And as far as I understand the signed number arithmethic, a branch greater or smaller on signed values depends on V.

Basically a set V-flag says, that the operation went out of the 8-bit space. So in principal, you should always use that to
handle error conditions - before actually using C/E for the unsigned greater/smaller branches.
So would a branch on a signed comparison even work?

And if I can't compare two numbers to get a signed comparison result (because CMP does not set V), the value of, say "BGTS" (branch greater than, signed)
would be rather small.

What do you think?

TIA
Andre

Author:  Arlet [ Sun Nov 17, 2013 7:09 am ]
Post subject:  Re: Would branches on signed comparisons make sense?

How about modifying the CMP operation so that it sets V ? Seems sensible anyway.

Author:  BigDumbDinosaur [ Sun Nov 17, 2013 7:13 am ]
Post subject:  Re: Would branches on signed comparisons make sense?

Arlet wrote:
How about modifying the CMP operation so that it sets V ? Seems sensible anyway.

As CMP is really a subtraction that discards the difference, you'd think that V would be appropriately conditioned. It could be a very useful features.

Author:  Rob Finch [ Sun Nov 17, 2013 8:14 am ]
Post subject:  Re: Would branches on signed comparisons make sense?

Another approach might be to have CMP set N and C according to the result of comparison rather than a subtract.

If signed(a) < signed(b) then set N.
If unsigned(a) < unsigned(b) then set C.
If a==b then set Z

Author:  Klaus2m5 [ Sun Nov 17, 2013 10:17 am ]
Post subject:  Re: Would branches on signed comparisons make sense?

I never understood, why the 6502 does not set V on a compare and why there is no compare with carry (borrow). Of course you must use SBC for signed compares! (N xor V) = signed less than if 1, = signed greater or equal if 0

Author:  fachat [ Sun Nov 17, 2013 10:54 am ]
Post subject:  Re: Would branches on signed comparisons make sense?

The problem with modifying CMP is of course compatibility.
I indeed wonder, why, if CMP does a substract internally, the V-flag is not set.
Maybe they only had an unsigned comparison in mind.

Yet, this N xor V makes coding it separately a pain, so maybe a branch on signed
still can make sense, the question is, is it worth the effort?

Author:  Arlet [ Sun Nov 17, 2013 10:59 am ]
Post subject:  Re: Would branches on signed comparisons make sense?

If you don't want to break existing code, you could add an extra 'signed-less-than' flag that gets set by add/subtract/compare, and some extra branch instructions to check for it.

Author:  Klaus2m5 [ Sun Nov 17, 2013 12:31 pm ]
Post subject:  Re: Would branches on signed comparisons make sense?

fachat wrote:
Yet, this N xor V makes coding it separately a pain, so maybe a branch on signed
still can make sense, the question is, is it worth the effort?


Yes, many other processors have it. After all it would be more usefull than many of the extra opcodes in the 65C02.

P.S.: Talking about the pain to do a 16-bit signed compare in the 6502: http://www.6502.org/tutorials/compare_beyond.html#6
Actually not that bad. :shock:

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/