Returning to the line a branch is called from?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
Arc
Posts: 2
Joined: Tue Sep 24, 2019 10:53 pm

Returning to the line a branch is called from?

Post by Arc » Sun Apr 26, 2020 6:56 pm

Hey everyone, I'm new here and not entirely sure how to even phrase my question.

As I understand it, when a BEQ label instruction is carried out following a CMP variable it can be thought of as something like "If variable = 0, JMP label."

Is there a way to cleanly return to the point in your code where the branch was executed like with the JSR and RTS commands?

I was able to figure out a way to do what I want, but the code is a mess:

Code: Select all

  LDA gameStartTimer1
  STA index1

  LDA gameStartTimer2
  CMP index1
  BEQ Add11
calledFrom1:
  STA index2

  LDA gameStartTimer4
  CMP index1
  BEQ Add12
calledFrom3:
  CMP index2
  BEQ Add13
calledFrom4:
  CMP index1
  BEQ Add14
calledFrom5:
  STA index3
;......

Add11:
  CLC
  ADC #$01
  CMP arraySize
  BEQ Set01
calledFrom2:
  JMP calledFrom1

Add12:
  CLC
  ADC #$01
  CMP arraySize
  BEQ Set02
calledFrom6:
  JMP calledFrom3

Add13:
  CLC
  ADC #$01
  CMP arraySize
  BEQ Set03
calledFrom7:
  JMP calledFrom4

Add14:
  CLC
  ADC #$01
  CMP arraySize
  BEQ Set04
calledFrom8:
  JMP calledFrom5

Set01:
  LDA #$00
  JMP calledFrom2

Set02:
  LDA #$00
  JMP calledFrom6

Set03:
  LDA #$00
  JMP calledFrom7

Set04:
  LDA #$00
  JMP calledFrom8
The aim of the code is to obtain 3 unique values not to exceed the size of an array.

The multiple Add1 and Set0 subroutines are essentially the same and it would be a lot more clean and efficient to be able to simply reuse them by having them return to the line following branch. Am I missing something extremely basic that would allow me to do this?

User avatar
rainwarrior
Posts: 7812
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Returning to the line a branch is called from?

Post by rainwarrior » Sun Apr 26, 2020 7:11 pm

Arc wrote:
Sun Apr 26, 2020 6:56 pm
Is there a way to cleanly return to the point in your code where the branch was executed like with the JSR and RTS commands?
JSR is exactly what you use for this.

Just branch over the JSR.

Arc
Posts: 2
Joined: Tue Sep 24, 2019 10:53 pm

Re: Returning to the line a branch is called from?

Post by Arc » Sun Apr 26, 2020 7:29 pm

Wow... You have no idea how long I've been trying to figure this out and how quickly I figured it out after reading your reply. Thanks so much!

Here's what I've got now. Is this what you're referring to?

Code: Select all

  LDA gameStartTimer1
  STA index1

  LDA gameStartTimer2
  JSR Compare1
  STA index2

  LDA gameStartTimer4
  JSR Compare1
  JSR Compare2
  JSR Compare1
  STA index3
  RTS

Compare1:
  CMP index1
  BEQ Add1
  RTS

Compare2:
  CMP index2
  BEQ Add1
  RTS

Add1:
  CLC
  ADC #$01
  CMP arraySize
  BEQ Set0
  RTS

Set0:
  LDA #$00
  RTS

User avatar
rainwarrior
Posts: 7812
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Returning to the line a branch is called from?

Post by rainwarrior » Sun Apr 26, 2020 8:30 pm

Looks like you're doing fine. :beer: :D

Oziphantom
Posts: 825
Joined: Tue Feb 07, 2017 2:03 am

Re: Returning to the line a branch is called from?

Post by Oziphantom » Mon Apr 27, 2020 6:41 am

just to be clear. As far as I can tell Rainwarrior was talking about

Code: Select all

lda Something
beq thing
to

Code: Select all

lda Something
bne +
jsr thing
+ 
when you hit the "branch to far" issue you do

Code: Select all

lda Something
bne +
jmp thing
+ 

Post Reply