It is currently Mon Oct 23, 2017 8:10 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Weird Opcode: BPL $FB
PostPosted: Tue Jan 24, 2006 10:23 pm 
Offline

Joined: Tue Jan 24, 2006 10:17 pm
Posts: 2
I've noticed when disassembling some roms that when they do the wait for vblank they'll say BPL $FB. But what does the $FB stand for? Cause I've seen it used like so:

LDX #$12
LDA #$00
STA $00,X
INX
BNE $FB

Which is obviously supposed to be a loop. Can anyone explain this to me?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 10:49 pm 
Offline
Site Admin
User avatar

Joined: Mon Sep 20, 2004 6:04 am
Posts: 3471
Location: Indianapolis
$FB is a signed number, it's the relative address to branch to. In that example, -5 bytes.

Code:
 LDX #$12
 LDA #$00
loop:
 STA $00,X
 INX
 BNE loop


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 11:12 pm 
Offline
User avatar

Joined: Wed Nov 10, 2004 6:47 pm
Posts: 1845
Branches use relative addresses, not absolute addresses like JMP/JSR. The 1-byte operand is a signed value specifying the number of bytes to jump over. If greater than or equal to $80, the signed value is negative, so it jumps backwards the proper number of bytes.

$FF = -1
$FE = -2
$FD = -3
$FC = -4
$FB = -5
and so on


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 8:11 am 
Offline

Joined: Thu Sep 15, 2005 9:23 am
Posts: 1194
Location: Behind you with a knife!
Remember it will branch;

-125/+129

not

-128/+127

as most sites will tell you.

ProgramCounter = ProgramCounter + Value + 2

Or more specifically;

Code:
(char)CPU.Memory[(unsigned short int)(CPU.PC + 1)] + 2

_________________
http://www.jamesturner.de/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 8:27 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10067
Location: Rio de Janeiro - Brazil
This happens when you disassemble stuff. The source code is, most of the time, well organized and everything is labeled. But when the code is assembled, all labels are lost and translated into adreeses, absolute or relative. This makes disassembling stuff a hard thing, and you'd most likely have to follow the program from the start to reconstruct all the labels. Actually, put them in place, because the names can never be restored.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 11:20 am 
Offline
User avatar

Joined: Wed Nov 10, 2004 6:47 pm
Posts: 1845
It actually is -128/+127 ... but from the end of the instruction (after the branch opcode+operand). This is because the PC is auto-incremented after fetching both the opcode byte and operand byte.

But yeah... if you have the PC origin before the opcode (as opposed to after it), you'll have to add an additional 2 with the branch.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Mon Feb 27, 2006 10:27 am 
Offline

Joined: Tue Jan 24, 2006 10:17 pm
Posts: 2
THanks a lot, that makes a lot of sense. I hadn't heard of relative branching before this but now I understand.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 1:00 pm 
Offline
User avatar

Joined: Mon Sep 27, 2004 8:33 am
Posts: 3715
Location: Central Texas, USA
Just to round this out, the reason for having relative addressing in the fist place is that it results in more compact code, since most branches are to a nearby location, and it allows code to be relocated without having to update the addresses of branches (useful in an operating system, but not much in the NES).


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

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 7 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:  
Powered by phpBB® Forum Software © phpBB Group