6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Wed Nov 13, 2024 6:56 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue Mar 04, 2003 7:01 pm 
Offline

Joined: Tue Mar 04, 2003 6:57 pm
Posts: 1
We're being taught Assembly language in school right now using a 6502 emulator and I'm having real trouble grasping the concepts of Carry and Overflow flags...

I had a quick browse over the site but couldn't find any information on these... could anyone give me a quick description and maybe an example or 2? Cheers!

P.s. My teacher is pretty useless... he doesnt even know what they are for either!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Mar 04, 2003 8:55 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8540
Location: Southern California
The carry (C) flag is the much simpler of the two. For addition, it works pretty much like a carry works when you add on paper. You'll normally start with C clear. If the first column (in the paper analogy) generates a carry, C gets set, which adds one to the next colum.

For subtraction, C=1 just means you haven't borrowed. Pretty simple. The examples below cover C. Now on to the V flag.

WDC's programming manual says that to get the V flag, the 6502 XOR's the carry out of bit 6 with the carry out of bit 7 (which ends up in the C flag). In other words, V will reflect the result of the XOR of these two carry values.

I decided to actually try it. Sometimes the rules are harder to understand without examples. Here's what I got (all numbers in hex):

First with ADC, starting with C clear: (1st number, 2nd number, then the resulting number and flags)
Code:
1st     2nd    result      N        V        C
-----------------------------------------------
 7      -2        5        0        0        1
 7       2        9        0        0        0
 7      80       87        1        0        0
 7      -9       -2        1        0        0
 7      7A       81        1        1        0
80      90       10        0        1        1
F0      F0       E0        1        0        1
F8      0A        2        0        0        1


Now with SBC, starting with C set:

F8      0A       EE        1        0        1
81       7       7A        0        1        1
 7       2        5        0        0        1
 7      -2        9        0        0        0
 7       9       FE        1        0        0
 7      90       77        0        0        0
10      90       80        1        1        0
10      91       7F        0        0        0


Again, the book says that to determine the correct overflow flag value, the processor XORs the carry out of bit 6 with the carry out of bit 7 (which winds up in the carry flag), and transfers the result to the overflow flag.

More simply, V=1 means the sign of the result is wrong. Take for example the second-to-last line of the table above. 10 minus 90 is, in signed numbers, 10 minus -70, or 10+70. The result has to be positive; but 80 with its bit 7 set looks negative, so the sign is wrong and the V flag is set to indicate that fact.

Garth Wilson

_________________
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?


Last edited by GARTHWILSON on Sat Jul 19, 2008 1:39 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Thanks!
PostPosted: Wed Apr 02, 2003 8:17 pm 
Offline

Joined: Wed Mar 05, 2003 8:07 pm
Posts: 4
I didn't post this originally, but I am attempting to write an emulator for the 6502 on my graphing calculator (TI-89) because I take it to school every day. THis will help me quite a bit in making to C and V logic.

Just a little question: Is the C flag set if any bit caused a carry, the last bit did, or the first bit did? I'm using logic (regarding a+b) somewhat like:
Code:
If (a and b)=0
 c=0;
else
 c=1;
Fi

That is, if any bit produced a carry, set the carry flag, otherwise clear it. Is that correct?

_________________
-Slimey


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Apr 02, 2003 9:50 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8540
Location: Southern California
> That is, if any bit produced a carry, set the carry flag, otherwise clear it. Is that correct?

No, the carry flag is set only if an addition results in an answer that has more bits than the normal register size, which in this case is 8 bits (numbered 0-7, bit 0 being least significant). For example, $F4+4 will not set the carry flag because the result ($F8) can still be held in 8 bits, even though the addition of the 4's in the bit-2 positions carried into bit 3. C reflects a carry at the byte level, not the bit level. The carry flag is really bit 8 of the result, where the register can only hold bits 0 through 7. If you begin the addition with the carry set (typically from a previous operation), then it gets added to the bit-0 position. Any 65xx programming manual or tutorial should cover this quite clearly.

BTW, you're writing a simulator, not an emulator. An emulator plugs into the microprocessor socket in your board and does the uP's job but allows you to see into the internal registers and record the steps taken. A simulator is software only. Even the I/O is simulated in software, instead of using the actual hardware I/O on the board like an emulator does.

Garth


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Apr 03, 2003 8:27 pm 
Offline

Joined: Wed Mar 05, 2003 8:07 pm
Posts: 4
Let me get this right: If the carry flag is set, the overflow flag is also always set, but the overflow flag can be set

Never mind, I found an explanation of this elsewhere. Sorry for bothering you.

http://www.obelisk.demon.co.uk/6502/registers.html

(Update) Thanks for telling me that about the emulator concept v. simulator concept. I'll change my source code some day. :wink:

_________________
-Slimey


Top
 Profile  
Reply with quote  
 Post subject: Confusing but useful
PostPosted: Sun May 11, 2003 4:19 pm 
Offline

Joined: Sat May 10, 2003 4:03 am
Posts: 25
Location: Haslett, Michigan, USA
The V flag offers many uses.

Yes it is true that its primary use was originally to indicate arithmentic overflows for 7-bit signed operations. If one is doing simple signed 7-bit add/subtract, the V flag is set to indicate that the result cannot be represented properly.

It may be most valuable in 6502 programming when used in conjuction with the BIT instruction however. BIT does several things. It tests the addressed memory location ANDed with the accumulator without altering the accumulator, setting Z appropriately. It also sets N to bit 7 of the memory value and V to bit 6 of the memory value.

One can load the accumulator (A) with a mask prior to BIT for straight forward mask testing of memory locations (or I/O registers of course). One can leave A alone and use BIT to test the values of bits 6 and or 7 of a memory location (or I/O register). This latter use is quite powerful.

The 6502 native "lazy" Boolean values become True = $80 (-128 signed) and False = $00 (0). But the more powerful (memory saving) Boolean values are True = [bit7 = 1, bits6-0 preserved] and False = [bit7 = 0, bits6-0 preserved]. This retains other bits of the memory byte for various other purposes, or allows shift/rotate options to treat a byte as a vector of 8 Boolean values.

One can also find uses for Tristate data items, where bit7 = 1 means True, bit7 = 0 means false, and bit6 = 1 means the third state (often something like "use default" or "not selected").

But a very big use for the V flag in 6502 systems is to allow easy testing of two flags or status bits in an I/O register. Many 65XX-family I/O chips will put important flags into bits 7 and 6 of their registers. BIT along with tests of V and N are quite powerful operations that go a long way. This is especially true when writing interrupt-service routines and trying to keep them short and responsive.


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 23 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: