It is currently Sat Dec 16, 2017 6:11 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Greater then comparison?
PostPosted: Mon Mar 13, 2017 10:16 am 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 303
Location: Colorado USA
I want to have a money system in my game, but I can only branch if equal (BEQ), and branch if not equal(BNE). However, I am aware that there are other branch commands such as Branch on Carry Clear(BCC), and Branch on Carry Set (BCS) as well as branch on plus (BPL), and Branch on Minus (BMI). What are these all used for? I understand that BPL and BMI are used when testing a bit, but what is Carry clear, or better yet, what is Carry set?


Top
 Profile  
 
PostPosted: Mon Mar 13, 2017 10:21 am 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1868
Location: DIGDUG
I kind of explain it here, near the bottom...

https://nesdoug.com/2016/03/13/28-asm-part-3/

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
PostPosted: Mon Mar 13, 2017 11:40 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10165
Location: Rio de Janeiro - Brazil
DementedPurple wrote:
I understand that BPL and BMI are used when testing a bit

Well, not really. BMI and BPL will branch (or not) based on the N flag, which is just a copy of bit 7 of the result of the latest CPU operation (that happens to affect the N flag, refer to an opcode documentation to see which operations affect which flags). In 2's complement, negative numbers have bit 7 set, so that's why the flag is called N(egative). You might have gotten the impression that these were meant for testing bits because some NES registers return state information (such as the vblank flag) in bit 7, so you can conveniently use these branch instructions, even though in this particular case this has nothing to do with positive or negative values.

If you're working with signed numbers (-128 to 127 for 8 bit, -32768 to 32767 for 16-bit, and so on), use BMI and BPL to test the results of additions/subtractions/comparisons. If you subtract a number from another and the result is negative, this means that the amount being subtracted is bigger.

Quote:
but what is Carry clear, or better yet, what is Carry set?

The carry is meant for multi-byte operations and for testing unsigned results (0 to 255 for 8-bit, 0 to 65535 for 16-bit, and so on). In additions, the carry is set whenever the result doesn't fit in the number of bits you're working with, it represents an overflow. For example, 254 + 3 will result in 1 and the carry will be set, since 257 doesn't fit in 8 bits. In subtractions the carry acts as a borrow value. It must be set before subtractions (and is implicitly set before comparisons) and if a borrow is necessary (e.g. 4 - 6 will require a borrow, since 6 is larger than 4) the carry flag will be cleared. From there you can use the value of the carry flag to propagate the carry or the borrow to higher bytes in multi-byte operations or to detect if the operation caused an overflow or an underflow. An underflow (carry clear) after a subtraction/comparison means that the value being subtracted is larger.

There's also the V flag, which is meant for testing signed overflows and underflows, but let's not get into that right now.


Top
 Profile  
 
PostPosted: Mon Mar 13, 2017 2:39 pm 
Offline

Joined: Wed Nov 30, 2016 4:45 pm
Posts: 93
Location: Southern California
Beyond 8-bit Unsigned Comparisons by Bruce Clark http://6502.org/tutorials/compare_beyond.html
6502 Compare Instructions from 6502 Software Design, Updated by Bruce Clark http://6502.org/tutorials/compare_instructions.html
The Overflow (V) Flag Explained by Bruce Clark http://6502.org/tutorials/vflag.html

and I can never pass up the opportunity to recommend the excellent programming manual, "Programming the 65816 including the 6502, 65C02, and 65802" by David Eyes and Ron Liechty. This is a .pdf file of a rather large book that is well laid out and is much better than the description there lets on. Note: There were many problems with the earlier .pdf version that were not in the original paper manual; but in late March 2015, WDC scanned and OCR'ed the paper manual and posted the new, repaired .pdf. No 6502 programmer should be without it!

_________________
http://WilsonMinesCo.com/ lots of 6502 resources


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

All times are UTC - 7 hours


Who is online

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