Testing for zero after DEC
Moderator: Moderators
-
- 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
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.
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
-
- Vic 20 Hobbyist
- Posts: 128
- Joined: Sun Dec 26, 2010 1:51 pm
Re: Testing for zero after DEC
Yes, it's unnecessary.
Re: Testing for zero after DEC
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.
Re: Testing for zero after DEC
@tlr, assume you mean Z is already set by the DEC so LDA and CMP are both superfluous here?
Re: Testing for zero after DEC
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.
-
- 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
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
Re: Testing for zero after DEC
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.
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
lda response_buffer
cmpi(RESPONSE_OK)
bne .response_error
Code: Select all
macro cmpi(.constant)
{
if (.constant != 0) {
cmp #.constant
}
}
Code: Select all
jsr something
jsr something_else
rts
Code: Select all
jsr something
jmp something_else
- Mike
- Herr VC
- Posts: 5129
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
Re: Testing for zero after DEC
That's a common programming technique that not only applies to 65xx machine language, and which goes by 'tail-call optimization'.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.

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?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.
Re: Testing for zero after DEC
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:Mike wrote: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?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.
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
- chysn
- Vic 20 Scientist
- Posts: 1204
- 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
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
WIP: MIDIcast BASIC extension
he/him/his