A sample programming session in VICMON

Basic and Machine Language

Moderator: Moderators

Merytsetesh
Vic 20 Amateur
Posts: 40
Joined: Sat Mar 02, 2024 8:57 pm
Location: Canada

Re: A sample programming session in VICMON

Post by Merytsetesh »

Mike wrote: Thu Mar 14, 2024 3:22 am That would only be correct with sign-magnitude representation.
Ah yes, silly me again. Thank you for your patience.

So, I just ran a quick test.

Code: Select all

* = 828
eor_test
	CLC
	LDA #$01
	EOR #$FF
	RTS
sbc_test
	CLC
	LDA #$01
	STA $00
	LDA #$00
	SBC $00
	RTS
Both eor_test and sbc_test have the same result, flipping the contents of the accumulator between 1 and 254, or -1. I can't shake the feeling I'm missing something about SBC, else why use it?
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: A sample programming session in VICMON

Post by Mike »

Merytsetesh wrote:[...] why use [SBC]?
If you do the sign inversion in the same context as done in BALLPING (i.e. a given zero page address as source and target), for both methods you'll arrive at (DX := $F9):

Code: Select all

; ** using SBC:
 SEC
 LDA #$00
 SBC $F9
 STA $F9
7 bytes, 10 cycles

Code: Select all

; ** using EOR #$FF and 2s-complement:
 CLC
 LDA $F9
 EOR #$FF
 ADC #$01
 STA $F9
9 bytes, 12 cycles

So there ...

(Also, using SBC has the charme it's independent of the way signed integers are represented)

When the value already is in the Accumulator, then using 2s-complement is shorter and faster - and it can even spare the CLC instruction when the state of the carry flag is known at that point, doing either EOR #$FF/ADC #$01 when C is known clear or EOR #$FF/ADC #$00 when C is known set. See here.
Merytsetesh
Vic 20 Amateur
Posts: 40
Joined: Sat Mar 02, 2024 8:57 pm
Location: Canada

Re: A sample programming session in VICMON

Post by Merytsetesh »

Mike wrote: Thu Mar 14, 2024 1:16 pm When the value already is in the Accumulator, then using 2s-complement is shorter and faster - and it can even spare the CLC instruction when the state of the carry flag is known at that point, doing either EOR #$FF/ADC #$01 when C is known clear or EOR #$FF/ADC #$00 when C is known set. See here.
I read that, and I also read this. I think I get it a bit better.
Post Reply