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

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: 6502 Math Limits
PostPosted: Thu Mar 06, 2014 11:47 pm 
Offline

Joined: Mon Mar 03, 2014 7:19 pm
Posts: 7
What does the 6502 do if I keep adding values to the accumulator (ADC)? I was trying to test this on visual6502.org and I see that the after the value goes over 0XFF the C bit is set. The accumulator value with the carry bit makes sense until it gets to above 0X0200. I'm not sure of what the value in the accumulator means after this point. I also noticed that the negative bit gets set during this addition. I would appreciate any help with my understanding on this.


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 Math Limits
PostPosted: Fri Mar 07, 2014 12:41 am 
Offline
User avatar

Joined: Sat Dec 07, 2013 4:32 pm
Posts: 246
Location: The Kettle Moraine
The accumulator cannot contain a 16 bit number such as $0200. The accumulator is 8 bits, and can contain the numbers $00 to $FF. The 8th bit of the accumulator (bit 7) denotes a negative sign in this case.

Think of addition in binary.

Code:
 C 76543210
 0 11111111
+0 00000001
=1 00000000


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 Math Limits
PostPosted: Fri Mar 07, 2014 1:36 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
I cannot pass up an opportunity to recommend again the excellent programming manual linked at the top of the page at http://6502.org/documents/datasheets/wdc/ . Actually though, any 65-family programming manual should cover this. [Edit, 4/9/15: I was recommending the Liechty & Eyes manual based on my paper copy, not realizing there have been a lot of problems with the .pdf version. WDC just scanned the paper version and OCR'ed it to get rid of all the problems of the previous .pdf version (Where did they get it from??) and they have it on their site now for free download at http://65xx.com/Products/Programming_an ... e-Manuals/ .)

The processor only deals with 8 bits at a time; but you will have wider numbers in memory. Say you have two 3-byte (ie, 24-bit) variables in memory, and call them VAR_A_L, VAR_A_M, and VAR_A_H, for variable A low, middle, and high bytes, and the same for variable B. To add the two together and leave the result in variable B, you would do:

Code:
        CLC
        LDA  VAR_A_L
        ADC  VAR_B_L
        STA  VAR_B_L

        LDA  VAR_A_M
        ADC  VAR_B_M
        STA  VAR_B_M

        LDA  VAR_A_H
        ADC  VAR_B_H
        STA  VAR_B_H

At the end, the 24-bit sum will be in variable B. The carry flag will tell if the three bytes were not enough to hold it all, and the overflow flag will tell if the high bit is valid as a sign byte, if indeed you're using signed numbers. (The addition works the same whether they're signed or not; but the interpretation will be different, depending on the application.)

Subtraction works similarly. This website's source-code repository has routines for multiplication and division, and as you can imagine, you just keep putting together building blocks into higher and higher level program components until you're doing complex math.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 Math Limits
PostPosted: Fri Mar 07, 2014 3:58 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8506
Location: Midwestern USA
It should be noted that most microprocessors behave in a similar fashion when addition overflows the register holding the sum. Also, the basic procedure for handling larger numbers is similar to that described above by Garth.

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


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 Math Limits
PostPosted: Fri Mar 07, 2014 4:50 am 
Offline

Joined: Sun Nov 08, 2009 1:56 am
Posts: 411
Location: Minnesota
Quote:
What does the 6502 do if I keep adding values to the accumulator (ADC)?


It calculates the sum of three terms: that value, whatever value the accumulator already holds and the carry bit.

The accumulator is set to the sum modulo 256 ($100).

The carry flag is set to zero if the sum is less than 256, otherwise it is set to one.

The negative flag is set to zero if bit seven of the sum is zero, otherwise it is set to one, ie., it is the same as the value of the most significant bit of the accumulator result.

The overflow flag is set to zero if the sum, considered as a signed number, did not overflow, otherwise it is set to one.


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 Math Limits
PostPosted: Mon Mar 10, 2014 4:11 am 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 679
Here's what happens when you start at zero and add 1 more than $200 times:
Code:
$00 + 1 = $01, carry clear
$01 + 1 = $02, carry clear
...
$FE + 1 = $FF, carry clear
$FF + 1 = $00, carry SET
$00 + 1 = $01, carry clear
$01 + 1 = $02, carry clear
...
$FE + 1 = $FF, carry clear
$FF + 1 = $00, carry SET
$00 + 1 = $01, carry clear


After rollover occurs, there's no "$100" or "$101", it's just back to plain old $00 and $01. The carry lets you know that something *external* to the addition should note the rollover, if you're working with larger numbers.

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


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

All times are UTC


Who is online

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