6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Sep 20, 2024 9:26 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Mar 25, 2019 12:45 am 
Offline

Joined: Fri Jan 25, 2019 5:40 am
Posts: 346
Location: Knoxville, TN
Really simple question. Reading this it seems that branch instructions index off of the next instruction. For instance:

Code:
LABEL INA ; lets say our label address is 0000 for simplicity
BRA LABEL ; Here we'll be branching to the previous line, so -1 from the current address, but -3 from the next address.


When assembled, would BRA LABEL be BRA -1 or BRA -3 (80 FF or 80 FD)?

The document I mentioned shows the case of:

Code:
LABEL BRA LABEL+2 ; 3 cycles

Now, if this were to assemble to BRA 0 we'd be on top of the next instruction, as it seems to imply, but if it assembles to BRA -2 since the label is 2 addresses behind the next instruction, we'd be infinite looping on our current instruction. Which way is correct?


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 25, 2019 1:17 am 
Offline

Joined: Sat Jul 28, 2012 11:41 am
Posts: 442
Location: Wiesbaden, Germany
Quote:
When assembled, would BRA LABEL be BRA -1 or BRA -3 (BRA FE or BRA FC)?
It would be BRA -3 or BRA $FD. The 6502 uses two's complement for negative numbers.

Quote:
Code:
LABEL BRA LABEL+2 ; 3 cycles

Now, if this were to assemble to BRA 0 we'd be on top of the next instruction, as it seems to imply, but if it assembles to BRA -2 since the label is 2 addresses behind the next instruction, we'd be infinite looping on our current instruction. Which way is correct?
BRA 0 is correct (-2+2=0). You could see it as a 3 cycle 2 byte NOP.

_________________
6502 sources on GitHub: https://github.com/Klaus2m5


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 25, 2019 1:22 am 
Offline

Joined: Fri Jan 25, 2019 5:40 am
Posts: 346
Location: Knoxville, TN
Klaus2m5 wrote:
Quote:
When assembled, would BRA LABEL be BRA -1 or BRA -3 (BRA FE or BRA FC)?
It would be BRA -3 or BRA $FD. The 6502 uses two's complement for negative numbers.

Quote:
Code:
LABEL BRA LABEL+2 ; 3 cycles

Now, if this were to assemble to BRA 0 we'd be on top of the next instruction, as it seems to imply, but if it assembles to BRA -2 since the label is 2 addresses behind the next instruction, we'd be infinite looping on our current instruction. Which way is correct?
BRA 0 is correct (-2+2=0). You could see it as a 3 cycle 2 byte NOP.


Thank you for the quick response.

I knew about two's complement, I messed up my numbering, I corrected it but it seems you caught it before I got to it :)

This is what I had thought, and why I didn't use the code that I got out of my assembler, as I figured this would be a stumbling block. Many thanks, now I can make this change and get on with some more features that are badly needed.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 32 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: