Testing for zero after DEC

Basic and Machine Language

Moderator: Moderators

Post Reply
Victim_RLSH
Vic 20 Drifter
Posts: 34
Joined: Thu Jul 15, 2021 10:50 pm
Location: Rapid City, SD
Occupation: Machine Shop Lackey

Testing for zero after DEC

Post by Victim_RLSH »

LDX CollisionCount ; count down the block that was collided with
DEC BlockData+BlockCount,X
LDA BlockData+BlockCount,X
CMP #$00 ; remove block from playfield if it is reduced to 0
BEQ RemoveBlock

The 6502 opcode guide in the Programmer's Reference Manual says DEC affects the zero flag, so is the LDA and CMP here unnecessary? Just trying to shave off a few bytes wherever I can get away with it.
Works in Progress: Gravity Ball, a breakout variant in assembly for the unexpanded vic-20
malcontent
Vic 20 Hobbyist
Posts: 129
Joined: Sun Dec 26, 2010 1:51 pm

Re: Testing for zero after DEC

Post by malcontent »

Yes, it's unnecessary.
tlr
Vic 20 Nerd
Posts: 567
Joined: Mon Oct 04, 2004 10:53 am

Re: Testing for zero after DEC

Post by tlr »

The LDA affects Z as well, so your example uses a double redundant construct. Using an unnecessary CMP has the side effect of overwriting the C-flag which you may not want in a particular case.
User avatar
beamrider
Vic 20 Scientist
Posts: 1452
Joined: Sun Oct 17, 2010 2:28 pm
Location: UK

Re: Testing for zero after DEC

Post by beamrider »

@tlr, assume you mean Z is already set by the DEC so LDA and CMP are both superfluous here?
tlr
Vic 20 Nerd
Posts: 567
Joined: Mon Oct 04, 2004 10:53 am

Re: Testing for zero after DEC

Post by tlr »

beamrider wrote: Thu Jul 29, 2021 5:17 am @tlr, assume you mean Z is already set by the DEC so LDA and CMP are both superfluous here?
Yes. The OP questions if the LDA and CMP is necessary. It would work if you remove just the CMP, but the LDA is redundant too.
And of course: in addition to the C-flag, Acc gets overwritten as well in the original example.
Victim_RLSH
Vic 20 Drifter
Posts: 34
Joined: Thu Jul 15, 2021 10:50 pm
Location: Rapid City, SD
Occupation: Machine Shop Lackey

Re: Testing for zero after DEC

Post by Victim_RLSH »

I found many other instances where I'm testing to set a flag that has already been set by another opcode. My 6502 gears haven't turned in 35+ yrs, so this is definitely a good workout.
Works in Progress: Gravity Ball, a breakout variant in assembly for the unexpanded vic-20
User avatar
bjonte
Vic 20 Hobbyist
Posts: 110
Joined: Sun Jan 22, 2017 5:47 am
Location: Gothenburg

Re: Testing for zero after DEC

Post by bjonte »

Sometimes it's nice to have the CMP for clarity and when using symbols it can keep the code working even if the symbol changes in the future. I made a macro to do the CMP only if the value to compare is non-zero. It looks a bit worse but I rather be safe than sorry.

Code: Select all

lda response_buffer
cmpi(RESPONSE_OK)
bne .response_error

Code: Select all

macro cmpi(.constant)
{
	if (.constant != 0) {
		cmp #.constant
	}
}
A common thing I do that I can get a few bytes back from is ending a subroutine with a JSR followed by an RTS. In some cases the RTS can be removed if the JSR is changed to a JMP.

Code: Select all

jsr something
jsr something_else
rts

Code: Select all

jsr something
jmp something_else
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Testing for zero after DEC

Post by Mike »

bjonte wrote:A common thing I do that I can get a few bytes back from is ending a subroutine with a JSR followed by an RTS. In some cases the RTS can be removed if the JSR is changed to a JMP.
That's a common programming technique that not only applies to 65xx machine language, and which goes by 'tail-call optimization'. :)
Victim_RLSH wrote:I found many other instances where I'm testing to set a flag that has already been set by another opcode. My 6502 gears haven't turned in 35+ yrs, so this is definitely a good workout.
In any case, keeping track of the flags should become second nature when writing 65xx machine code. It's like seeing the operator precedences of a numeric expression at first glance. Sometimes, for clarity, it might be sensible to put subexpressions in brackets. But we don't want to become lost in a labyrinth of parentheses, no?
tlr
Vic 20 Nerd
Posts: 567
Joined: Mon Oct 04, 2004 10:53 am

Re: Testing for zero after DEC

Post by tlr »

Mike wrote:
Victim_RLSH wrote:I found many other instances where I'm testing to set a flag that has already been set by another opcode. My 6502 gears haven't turned in 35+ yrs, so this is definitely a good workout.
In any case, keeping track of the flags should become second nature when writing 65xx machine code. It's like seeing the operator precedences of a numeric expression at first glance. Sometimes, for clarity, it might be sensible to put subexpressions in brackets. But we don't want to become lost in a labyrinth of parentheses, no?
I quite often find myself typing out the state of flags in comments, or even writing out the actual opcode and comment it out when redundant, eg:

Code: Select all

print_hex:
        pha
        lsr
        lsr
        lsr
        lsr
        jsr     ph_skp1
        pla
        and     #$0f
ph_skp1:
        cmp     #10
        bcc     ph_skp2
; C=1
        adc     #"A"-"0"-10-1
; C=0
ph_skp2:
        adc     #"0"
        jmp     $ffd2
;       rts
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: Testing for zero after DEC

Post by chysn »

tlr wrote: Fri Jul 30, 2021 9:04 am I quite often find myself typing out the state of flags in comments, or even writing out the actual opcode and comment it out when redundant
I was going to suggest this as well. Future you will thank you.
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
Post Reply