Status flags assembler 6502 nes

For a deeper understanding of how branching occurs in programs written in 6502 assembler, you need to delve into the flags and understand which commands affect a particular flag. This will help to avoid many errors associated with non-obvious branching of your program.

The status flags state is one of the six architectural registers of the 6502 processor:

  • Accumulator A register

  • Registers X and Y

  • Status register (status flags)

  • stack pointer

  • Program counter

We are already familiar with the A, X, Y registers, we often use them for data operations and writing to various processor ports. Stack pointer – stores a pointer to the last/first value of the stack. Program counter – always points to the next command to be selected and executed, it is 16 bit and after execution the pointer to the next command is updated. We will consider in more detail the work with these registers in the following articles.

Let’s take a closer look at the flags, they are located in the register as follows NV**DIZC where:

  1. N – Negative – this flag will contain the 7th bit of the result if the command has a result

  2. V – Overflow – overflow flag is set if during addition (ADC) or subtraction (SBC) the result of a signed number is more / less than the range -127 — +127

  3. D – Decimal – flag indicating work with decimal digits in dandy is not used

  4. I – Interrupt disable – flag indicating that all interrupts except NMI are disabled

  5. Z – Zero – flag indicating that the result of the command is 0, often used in the program and branching with BEQ (branch if equal to 0) and BNE (branch if not equal to 0)

  6. C – Carry – the carry flag is set if the result is greater than 255 or less than 0 with unsigned numbers, it is also set if the result is greater than or equal to CMP comparison. Also commonly used are BCC (if cleared) and BCS (if installed)

  7. * – unused bits

Actually, we are more interested in the flags C, Z, since they are often used in the development of games on dandy, so we will talk about them in more detail, while the rest of the flags will not be considered in detail to reduce the volume of the article.

Flag Z – zero

The following commands affect this flag: LDA, LDX, LDY, AND, INX, INY, INC, DEX, DEY, DEC, CMP, EOR, ORA and many others, it is important just to understand that if the execution result is 0, then the Z flag is set, for example

LDA #$00 ; установлен
LDA #$01 ; не установлен
LDX #$00 ; установлен так же и LDY 
DEX   ; как только X станет 0 будет установлен, так же с DEC, DEY
AND #%0000 ; если результат 0 то будет установлен, так же EOR, ORA

LDA #$07
CMP #$07 ; если при сравнение с памятью значение равно то флаг будет установлен

Thus, setting this flag will branch with BEQ if set and BNE if not set or the result is not 0.

Flag C – carry

Commands that affect this flag:

  • ADC – if after addition more than 255

  • SBC – if less than 0

  • CMP – if the value being compared is greater than or equal to the value in the accumulator, an alternate use of this flag

  • ASL, LSR, ROL, ROR – contains the bit that was shifted.

In fact, before ADC and SBC, I often reset the carry flag with the CLC command so that the carry pointer is 0, in which case the ADC will set the carry flag when the result is greater than 255.

When programming a game, I’m more interested in using this flag for the conditions “greater than or equal to” and, accordingly, “less than”, this is such an alternative to using this flag

LDA #$01
CMP #$02 ; флаг C установлен
BCS greatOrEq ; больше либо равно
BCC less ; значение в cmp меньше аккумулятора

As a conclusion

When I started learning the 6502 assembler, I made a big mistake by not understanding the operation of the flags, so it was quite difficult to understand why a BEQ or BNE transition is often used after AND.

A little about plans

In the near future, I plan to write an article about byte arrays and how to use them correctly and why you need to load both the high and low byte of the pointer in zeropage. Not so long ago, I figured out how to make background collisions based on bitmap with loading for each redrawn screen of its own collision map.

useful links

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *