6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Sep 21, 2024 11:35 pm

All times are UTC




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Branch instructions
PostPosted: Wed Sep 02, 2020 1:32 am 
Offline

Joined: Wed Aug 12, 2020 2:30 am
Posts: 43
Hey guys.

Does a branch instruction have to come right after a compare?
Or can there be other instructions between them as long as they don't affect the carry or zero flags?

Example...
Code:
lda #$ff
cmp #$ff
; some other code
beq somewhere_if_a_equals_ff


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 1:41 am 
Offline

Joined: Wed Aug 12, 2020 2:30 am
Posts: 43
Looks like perhaps I can push the processor flags, do stuff, then pull them.

Code:
lda #$ff
cmp #$ff
php
; do stuff
plp
beq somethere_if_a_equals_ff


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 2:45 am 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
DanielS wrote:
Looks like perhaps I can push the processor flags, do stuff, then pull them.
Yes, you can -- but it won't always be necessary.

Your previous post has the right idea. There can be other instructions between them as long as they don't affect the carry or zero flags. (But if they *do* affect the flag you're interested in, that's when pushing then later popping the flags may be the best alternative. I say "may" because it's often possible to rearrange the instruction order instead.)

Cheers,
Jeff

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 2:48 am 
Online
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8510
Location: Southern California
The flags will persist as long as the instructions executed don't alter them. For example, if you have something that leaves the V flag for you to branch on later, and the intervening instructions only affect Z, C, and N, there's no reason to push the processor status and then pull it off the stack later.

Note of course that pushing and pulling the status is an automatic part of the interrupt sequence and RTI, so you don't need to bracket an ISR with PHP and PLP. In fact, there are situations where doing so could even cause problems.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 2:55 am 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 702
Location: North Tejas
DanielS wrote:
Does a branch instruction have to come right after a compare?
Or can there be other instructions between them as long as they don't affect the carry or zero flags?


There can be any number of instructions in between the setting of a flag and a test for it.

For example, the code on this post

viewtopic.php?p=77950#p77950

sets the carry flag on line 54 and uses it on line 61.


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 3:02 am 
Offline

Joined: Wed Aug 12, 2020 2:30 am
Posts: 43
Here's an example...

Code:
_validate_1 ; left, right, down
    cpx ch_left
    php
    ldy #0
    plp
    beq _valid
    cpx ch_right
    php
    ldy #2
    plp
    beq _valid
    cpx ch_down
    php
    ldy #4
    plp
    beq _valid
    jmp _invalid

Y needs to be used in the next section. If I do it this way then I can avoid many, seemingly unnecessary, BEQs. The value of Y is based on the value of X.

Maybe this is a hacky way of doing it but it seems to work.


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 3:09 am 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 702
Location: North Tejas
DanielS wrote:
Here's an example...

Code:
_validate_1 ; left, right, down
    cpx ch_left
    php
    ldy #0
    plp
    beq _valid
    cpx ch_right
    php
    ldy #2
    plp
    beq _valid
    cpx ch_down
    php
    ldy #4
    plp
    beq _valid
    jmp _invalid

Y needs to be used in the next section. If I do it this way then I can avoid many, seemingly unnecessary, BEQs. The value of Y is based on the value of X.

Maybe this is a hacky way of doing it but it seems to work.


Can't you just do

Code:
_validate_1 ; left, right, down
    ldy #0
    cpx ch_left
    beq _valid
    ldy #2
    cpx ch_right
    beq _valid
    ldy #4
    cpx ch_down
    beq _valid
    jmp _invalid


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 3:13 am 
Offline

Joined: Wed Aug 12, 2020 2:30 am
Posts: 43
[quote="BillG"]

Can't you just do

Code:
_validate_1 ; left, right, down
    ldy #0
    cpx ch_left
    beq _valid
    ldy #2
    cpx ch_right
    beq _valid
    ldy #4
    cpx ch_down
    beq _valid
    jmp _invalid

Damn. Yes I can. That never occurred to me. I'm still in the C++ mindset I guess so the "simple" things here are, for now, elusive.


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 4:09 am 
Offline

Joined: Sun Apr 26, 2020 3:08 am
Posts: 357
More often than not, you can rearrange the code so that the processor byte never needs to be pushed or pulled.


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 4:29 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8390
Location: Midwestern USA
DanielS wrote:
Hey guys.

Does a branch instruction have to come right after a compare?
Or can there be other instructions between them as long as they don't affect the carry or zero flags?

Example...
Code:
lda #$ff
cmp #$ff
; some other code
beq somewhere_if_a_equals_ff

Short answer is no. The long answer is as long the relevant status register bit (Z in this case) is not affected by intervening instructions your branch will be reliable.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 7:15 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1948
Location: Sacramento, CA, USA
Lemme see if I can recite from memory all the ones that don't affect Z ... nop clc sec cli sei cld sed clv sta stx sty pha php jmp bxx jsr rts txs ... not many. I remember being frustrated at the 68xx for inexplicably messing with Z during a store operation, but not for pula or pulb. It's actually just a minor inconvenience, but so irksome, IMO.

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 12:50 pm 
Offline
User avatar

Joined: Sat Dec 01, 2018 1:53 pm
Posts: 727
Location: Tokyo, Japan
barrym95838 wrote:
Lemme see if I can recite from memory all the ones that don't affect Z ... nop clc sec cli sei cld sed clv sta stx sty pha php jmp bxx jsr rts txs ... not many.

I found it useful to create a little table listing the flags and all the instructions that affect each one:

Code:
C   SEC CLC
    ADC SBC                                         ASL LSR ROL ROR
    BIT CMP CPX CPY

NZ          LDA LDX LDY TAX TXA TAY TYA     TSX PLA
    ADC SBC INC DEC INX DEX INY DEY     AND ORA EOR ASL LSR ROL ROR
    BIT CMP CPX CPY

V       CLV
    ADC SBC
    BIT

D   SED CLD
I   SEI CLI BRK
(4) BRK


Quote:
I remember being frustrated at the 68xx for inexplicably messing with Z during a store operation, but not for pula or pulb. It's actually just a minor inconvenience, but so irksome, IMO.

Yeah, totally irksome. But probably not as inconvenient as not having PSHX and PULX instructions (though the 6801/6803 added those).

_________________
Curt J. Sampson - github.com/0cjs


Top
 Profile  
Reply with quote  
 Post subject: Re: Branch instructions
PostPosted: Wed Sep 02, 2020 5:40 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1948
Location: Sacramento, CA, USA
cjs wrote:
Yeah, totally irksome. But probably not as inconvenient as not having PSHX and PULX instructions (though the 6801/6803 added those).

Oof, yeah, that's a real eye-roller. How many quadrillions of total machine cycles have been spent executing the awkward work-around for that one? The average temperature on Earth is probably .00001° hotter because of it ... :lol:

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: