| |
Conrad
Registered: Nov 2006 Posts: 849 |
quick v-flag question
Hey guys, just a quick dirty question if you care to answer...
I just learned something new that the overflow (V) flag is modified when you use the opcodes ADC and SBC (as well as CLV and BIT of course)... however I'm not sure what values to put in the accumulator and the ADC parameter to get V to equal to 1 (or WHY is V set at such a state?).
Can someone give me some example values to make V = 1 ?
cheers! |
|
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
AFAIK the V bit is used when working with 2's complement numbers. These numbers are used to represent signed numbers in binary form. 0-127 is positive as u are used to it, but 255 counts as -1 downto 128 which is -127. now 100+64 gives us 164 which is not a positive number anymore in this world, and the V bit will turn into 1 to notify the poor programmer that he has an overflow. |
| |
Slammer
Registered: Feb 2004 Posts: 416 |
As Oswald writes you can use the overflow flag when adding or subtracting signed numbers. Its kind of a carry flag for the last bit. If the result of the adc or sbc destroys the last bit (the sign bit) the overflow flag is set.
It's quite useful too. I used it to cut the bobs in the full screen bob rot zoomer in the 'Kick Ass Easter Egg'-demo (If adding your rotation vector to a point inside the screen gives overflow, then the resulting point is outside the screen and you dont have to print any more bobs).
I just found this little test program in some old code. It adds 2 numbers and checks the result of the overflow flag. :ovtest_c asserts that the result is a cleared overflowflag and :ovtest_s asserts that its set. If the assertion fails the sideborder will shift its colors. (so 1+3 clears the overflow flag, so does -2+1 and so forth.)
.pc =$0801 :BasicUpstart($2000)
.pc = $2000
:ovtest_c #1 ; #3
:ovtest_c #-2 ; #1
:ovtest_c #-2 ; #4
:ovtest_c #-1 ; #120
:ovtest_c #126 ; #1
:ovtest_c #0 ; #127
:ovtest_s #1 ; #127
:ovtest_s #127 ; #127
:ovtest_s #126 ; #2
:ovtest_c #-1 ; #-127
:ovtest_s #-1 ; #-128
inc $d021
rts
.pseudocommand ovtest_s a ; b {
lda a
clc
adc b
bvs ov1
lo: inc $d020
jmp lo
ov1:
}
.pseudocommand ovtest_c a ; b {
lda a
clc
adc b
bvc ov1
lo: inc $d020
jmp lo
ov1:
}
|
| |
doynax Account closed
Registered: Oct 2004 Posts: 212 |
Quote: Hey guys, just a quick dirty question if you care to answer...
I just learned something new that the overflow (V) flag is modified when you use the opcodes ADC and SBC (as well as CLV and BIT of course)... however I'm not sure what values to put in the accumulator and the ADC parameter to get V to equal to 1 (or WHY is V set at such a state?).
Can someone give me some example values to make V = 1 ?
cheers!
The overflow flag is often used to perform signed comparisons which can be a bit tricky since you have to check both the sign flag and the overflow flag, and most introductory 6502 material neglects to mention how to perform such an operation.
lda a
cmp b
bmi negative
bvs less
greater_equal ;; a >= b
.
.
.
negative bvs greater_equal
less ;; a < b
.
.
.
Honestly though once things starts getting complicated you're often better of with something simpler, like biased numbers, instead. Keeping the flags straight when doing two's complement arithmetic can be a real pain. |
| |
yago
Registered: May 2002 Posts: 333 |
Very nice to read so much accurate Information about a single bit :-)
"Can someone give me some example values to make V = 1 ?"
There is also the BIT-Command, which copies bit 6 of the address to the V-Flag.
bit setvflag
rts
setvflag: .byte %01000000
|
| |
Conrad
Registered: Nov 2006 Posts: 849 |
Thank you for your answers guys! :)
@yago: I know there is the BIT opcode which puts M6 bit into the V-flag... I just wanted to see if it's possible to do V-flag branching WITHOUT using it, for experimental purposes. |
| |
JackAsser
Registered: Jun 2002 Posts: 2014 |
May I recommend this excellent tutorial I often use myself when I get a temporary brain loss: http://www.6502.org/tutorials/compare_instructions.html
And especially the table "Use of Branch Instructions with Compare". |
| |
doynax Account closed
Registered: Oct 2004 Posts: 212 |
Quote: May I recommend this excellent tutorial I often use myself when I get a temporary brain loss: http://www.6502.org/tutorials/compare_instructions.html
And especially the table "Use of Branch Instructions with Compare".
Except those tables are just plain wrong for signed numbers. Just try checking if -2 is less than -1 with a BMI..
edit: Hold on a second, that example works. But it can't really work can it? |
| |
Conrad
Registered: Nov 2006 Posts: 849 |
@JA: don't you mean this page ? :)
http://www.6502.org/tutorials/vflag.html#2.4
anyway, thanks again for the offer. |
| |
doynax Account closed
Registered: Oct 2004 Posts: 212 |
Quote: Except those tables are just plain wrong for signed numbers. Just try checking if -2 is less than -1 with a BMI..
edit: Hold on a second, that example works. But it can't really work can it?
I was confused as usual it seems.. Anyway, it still doesn't hold for -128 < 127 ($80 - $7f > 0).
Also I had completely forgotten that CMP doesn't touch the V flag. Nasty detail that. |