6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 7:01 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Sun Nov 17, 2013 1:38 am 
Offline

Joined: Tue Jul 05, 2005 7:08 pm
Posts: 1043
Location: near Heidelberg, Germany
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 of the GeckOS multitasking operating system, the usb65 stack, designer of the Micro-PET and many more 6502 content: http://6502.org/users/andre/


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 17, 2013 7:09 am 
Offline
User avatar

Joined: Tue Nov 16, 2010 8:00 am
Posts: 2353
Location: Gouda, The Netherlands
How about modifying the CMP operation so that it sets V ? Seems sensible anyway.


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 17, 2013 7:13 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8505
Location: Midwestern USA
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.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 17, 2013 8:14 am 
Offline
User avatar

Joined: Sun Dec 29, 2002 8:56 pm
Posts: 460
Location: Canada
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

_________________
http://www.finitron.ca


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 17, 2013 10:17 am 
Offline

Joined: Sat Jul 28, 2012 11:41 am
Posts: 442
Location: Wiesbaden, Germany
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

_________________
6502 sources on GitHub: https://github.com/Klaus2m5


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 17, 2013 10:54 am 
Offline

Joined: Tue Jul 05, 2005 7:08 pm
Posts: 1043
Location: near Heidelberg, Germany
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 of the GeckOS multitasking operating system, the usb65 stack, designer of the Micro-PET and many more 6502 content: http://6502.org/users/andre/


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 17, 2013 10:59 am 
Offline
User avatar

Joined: Tue Nov 16, 2010 8:00 am
Posts: 2353
Location: Gouda, The Netherlands
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.


Top
 Profile  
Reply with quote  
PostPosted: Sun Nov 17, 2013 12:31 pm 
Offline

Joined: Sat Jul 28, 2012 11:41 am
Posts: 442
Location: Wiesbaden, Germany
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:

_________________
6502 sources on GitHub: https://github.com/Klaus2m5


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: