Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > quick v-flag question
2007-10-13 00:21
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!
2007-10-13 00:56
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.
2007-10-13 08:03
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:
}

2007-10-13 09:09
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.
2007-10-13 09:31
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


2007-10-13 10:17
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.
2007-10-13 10:36
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".
2007-10-13 10:41
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?
2007-10-13 10:50
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.
2007-10-13 10:51
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.
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
MightyAxle
HCL/Booze Design
JEZ
Pantaloon/Fairlight
Freeze/Blazon
.jetan/AI-supported ..
psych
Sulevi/Virtual Dreams
sln.pixelrat
Guests online: 127
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 No Listen  (9.6)
2 Layers  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Censor Design  (9.3)
5 Triad  (9.3)
Top Webmasters
1 Slaygon  (9.6)
2 Perff  (9.6)
3 Sabbi  (9.5)
4 Morpheus  (9.4)
5 CreaMD  (9.1)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.052 sec.