6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Wed Jun 26, 2024 3:21 am

All times are UTC




Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Sun Apr 25, 2004 1:04 pm 
Offline

Joined: Sun Aug 24, 2003 7:17 pm
Posts: 111
You are right that there are some problems with signed arithmetics in BCD. But if signed arithmetics with N and V flags at all shall be possible $80 has to signify -20 as $20 + $ 80 =$00. Like with binary signed arithmetics $20 signifies -224 because $E0 + $20 =$00. A difference is that with binary arithmetics the sum of the largest positive numbers cannot generate carry, for example $7FFFFFFF + $7FFFFFFF =$FFFFFFFE without carry. Overflow flag set because the sum has the sign bit set while both terms had the sign bit unset. But with analogus signed BCD arithmetics $79999999+$79999999= $59999998 with carry set. The sign bit is unset although there is overflow signifying itself by the carry! A typical overflow with two negative values would be $80000000 + $99999999=$79999999 or $80000000 + $80000000 =$60000000. When adding two negative numbers carry is always set and there is overflow if and only if the sign bit is unset.

A correct rule for setting the V flag could be worked out from this. But this is not how the 6502 sets the V flag as we now that for example $90 + $90 = $80 with the V flag set. But this is not an overflow situation but simply -10 + -10 = -20.


Last edited by Mats on Sun Apr 25, 2004 8:13 pm, edited 5 times in total.

Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Apr 25, 2004 6:02 pm 
Offline

Joined: Sun Aug 24, 2003 7:17 pm
Posts: 111
And dclxvi writes:
Quote:
For ADC in decimal mode, the lower digits are added as though they were BCD, but the upper digits are added as though they are binary, which is why this is such a mess.


Why do you say this?

BCD addition is made according to the following logic:

1
The 4 least significant bits of the one value are added to the 4 least significant bits of the other value. If this sum is larger then $9 then the 4 least significant bits are set to the sum - 10 and an "internal carry" is generated.

2
The 4 most significant bits of the one value are added to the 4 most significant bits of the other value and the "internal carry" is added in addition. If this sum is larger then $9 the 4 most significant bits are set to sum - 10 and the carry flag is set .

Example: $73 + $68

$3 + $8 = $1 with "internal carry"
$7 + $6 +1 = $4 with carry flag set


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Apr 25, 2004 11:36 pm 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
Mats wrote:
You are right that there are some problems with signed arithmetics in BCD. But if signed arithmetics with N and V flags at all shall be possible $80 has to signify -20 as $20 + $ 80 =$00. Like with binary signed arithmetics $20 signifies -224 because $E0 + $20 =$00.

A correct rule for setting the V flag could be worked out from this. But this is not how the 6502 sets the V flag as we now that for example $90 + $90 = $80 with the V flag set. But this is not an overflow situation but simply -10 + -10 = -20.


I would submit that BCD really isn't a signed representation anyway. The whole idea of BCD is that the digits are contained in the hexadecimal value. In the signed BCD method you suggest, where $80 represents -20, the upper digit of $80 is 8, not 2, though of course the 2 can be easily calculated. I would say signed BCD is analogous to floating point. You can write routines for any implemenation you choose, but it is not directly supported on a 6502.

Anyway, ADC and SBC affect the V flag, even in decimal mode, and I was trying to accurately describe what that effect was in decimal mode, even though (a) it's complicated (b) it doesn't make much sense, and (c) it doesn't seem to be useful.

Mats wrote:
Quote:
For ADC in decimal mode, the lower digits are added as though they were BCD, but the upper digits are added as though they are binary, which is why this is such a mess.


Why do you say this?


What I meant was that this was true for the sole purpose of deterimining what the V flag will actually be. I wasn't referring to the resulting accumulator value or carry flag. I should have have stated this more clearly.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: visrealm and 60 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: