6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Oct 06, 2024 1:21 am

All times are UTC




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: SBC
PostPosted: Wed Jun 02, 2004 4:54 pm 
Offline

Joined: Fri May 21, 2004 5:23 pm
Posts: 17
Location: Burlington, ON
Hi,

Can anyone tell me what would happen if a call was made to SBC without a call to SEC first?

Thanks,

Andrew

:?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Jun 02, 2004 5:23 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8521
Location: Southern California
The result would just be one less than you were expecting. Occasionally you might even intentionally do a CLC before SBC to save a step if you would otherwise want to subtract one more after the operation. http://www.6502.org/forum/viewtopic.php ... compliment might help.


Top
 Profile  
Reply with quote  
 Post subject: Slightly dodgy coding!
PostPosted: Thu Jun 03, 2004 2:39 pm 
Offline

Joined: Wed Mar 24, 2004 10:54 am
Posts: 38
Well,

The simple answer would be that it would depend on what state the preceeding code has left the carry bit in. If it has been left set the answer will be what you would expect; if it has been left clear, it will be one less.

So, unless are really struggling to save bytes/cycles AND can be 150% certain what state the carry will always be in, you'd be well advised to explicitly put the carry in a known state using SEC (or CLC if you want to subtract an extra 1).

Of course you might be trying to write some very clever code where the result of the subtraction needs to be affected by some previous code leaving the carry in one state or the other, depending upon a port line state for instance. But that could be the slippery slope of hard to follow and fix, not to mention unreliable spaghetti code......


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jun 03, 2004 3:39 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8521
Location: Southern California
> But that could be the slippery slope of hard to follow and fix, not to mention unreliable spaghetti code......

That's where heavy commenting comes in. There should be far more text in the source code explaining than there is of actual instrucions. Unfortunately I seldom see that in others' code. Writing complete comments beside the instructions, I sometimes find bugs before they have shown up in execution, even if the program initially seems to work fine. This comes from having to explain details that I might otherwise think I can gloss over, and make sense of it as if to explain it to someone who is not familiar with the insides (which might be my own situation a year or more later when I need to come back to it to make improvements). It's just part of making reliable, maintainable code. The extra time spent commenting definitely pays for itself in the long run.

Back to the C flag and SBC-- There's a reason the SEC is not integrated into the SBC instruction. If you're dealing with numbers of 16 bits or more, you'll typically start with SEC before subtracting the least-significant byte; but then you leave the C alone for the next-to-least significant byte and so on, or you'll get wrong results sometimes. For example, in $1E49-$1A81, you start with C set and do the $49-$81, getting $C8 and a clear C flag. If you again did SEC before subtracting the high byte, the high byte result would be 4 instead of 3 and your overall result would be $4C8 instead of the correct $3C8. It works just like on paper.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jun 03, 2004 4:47 pm 
Offline

Joined: Fri May 21, 2004 5:23 pm
Posts: 17
Location: Burlington, ON
GARTHWILSON wrote:
> There should be far more text in the source code explaining than there is of actual instrucions.


I completely agree with you Garth.

My code is full of comments with references as to where I got the information. The reason I ask half the questions I ask is because I look at other people's code that's not documented and there's no explaination for what they've done. And, since I have one of these inquisitive minds, if I can't find supporting documentation, I ask.

Thanks again for your help.

Andrew


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 08, 2004 2:48 pm 
Offline

Joined: Sun Aug 24, 2003 7:17 pm
Posts: 111
Look at my contribution under heading "Help with making ..." for all these concepts (SBC BCD etc ) applied to a concrete case!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 08, 2004 5:31 pm 
Offline

Joined: Tue Nov 18, 2003 8:41 pm
Posts: 250
GARTHWILSON wrote:
> But that could be the slippery slope of hard to follow and fix, not to mention unreliable spaghetti code......

That's where heavy commenting comes in. There should be far more text in the source code explaining than there is of actual instrucions.


OK, I'm being pedantic here, but by these dogmas a simple 16 bit subtraction

1) is "unreliable spaghetti code"

2) and needs to be heavily commented to explain what's going on.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 08, 2004 8:13 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8521
Location: Southern California
The context was one of telling what the C flag's meaning from a previous operation was, or why you'd start the subtraction with CLC instead of SEC, or something that might be a little unconventional. I did work one time with a man whose comments sometimes were just reiterating what the mnemonic said, like "Set carry flag." after SEC. That's just wasteful clutter because it does not introduce any new information. But anything that's not immediately clear from the code itself should be explained in the comments.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 08, 2004 10:44 pm 
Offline

Joined: Tue Nov 18, 2003 8:41 pm
Posts: 250
GARTHWILSON wrote:
The context was one of telling what the C flag's meaning from a previous operation was, or why you'd start the subtraction with CLC instead of SEC, or something that might be a little unconventional.


Sure.

My point is that one should probably be circumspect about applying these
rules, they're not absolute.

So I think it's appropriate to note in the coments that the carry flag
will always be set but it strikes me as a little nutty to make it explicit with
and SEC instruction (unless there's some good reason to of course, eg
you're branching here) just because of some rule about what "unreliable
spaghetti code" looks like (because you might some day want to branch
here for some reason).

GARTHWILSON wrote:
That's just wasteful clutter because it does not introduce any new information. But anything that's not immediately clear from the code itself should be explained in the comments.


hmm
As to the above example I'd include the comment even if I thought it
should be immediately clear from the code

The example of 16 bit subtraction was meant as a ridiculous extreme.
So I, (and I assume, you) would consider a full blown tutorial on muti-
byte arithmetic, detailing what happens with the carry flag, as probably
a bit of overkill.

However, part of my dogma is 'there's no such thing as too many comments'
so I certainly wouldn't argue that it would necessarily be too much. :)


Top
 Profile  
Reply with quote  
 Post subject: That darned comment!
PostPosted: Tue Jun 15, 2004 1:52 pm 
Offline

Joined: Wed Mar 24, 2004 10:54 am
Posts: 38
My comment about "slippery slope" etc did of course relate to when the state of the carry before an SBC is depenant upon something a bit more out of the ordinary than multi-byte arithmetic - which is why I put the comment about "possibly a port line state" in.

I'm in complete agreement that it's next to impossible to have too much source code commenting - but as has been said, there's no point if it is no more than STBA commenting (i.e. Stating The B****y Obvious) like "set the carry". But at the same time it mustn't be too abstract from HOW the code does what it does (e.g. "mask off relay control bits so they are not affected when front pane LEDs are updated") by just what concentrating on what it achieves (e.g. "change front Panel LEDs")[/quote]


Top
 Profile  
Reply with quote  
 Post subject: That darned comment!
PostPosted: Tue Jun 15, 2004 2:11 pm 
Offline

Joined: Wed Mar 24, 2004 10:54 am
Posts: 38
My comment about "slippery slope" etc did of course relate to when the state of the carry before an SBC is depenant upon something a bit more out of the ordinary than multi-byte arithmetic - which is why I put the comment about "possibly a port line state" in.

I'm in complete agreement that it's next to impossible to have too much source code commenting - but as has been said, there's no point if it is no more than STBA commenting (i.e. Stating The B****y Obvious) like "set the carry". But at the same time it mustn't be too abstract from HOW the code does what it does (e.g. "mask off relay control bits so they are not affected when front pane LEDs are updated") by just what concentrating on what it achieves (e.g. "change front Panel LEDs")[/quote]


Top
 Profile  
Reply with quote  
 Post subject: Hmmm
PostPosted: Tue Jun 15, 2004 2:14 pm 
Offline

Joined: Wed Mar 24, 2004 10:54 am
Posts: 38
And one of these days my brain and fingers will sort out their sync problem!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 12 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: