This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Flags?

Flags?
by on (#138461)
My main question is why on earth do these processors have flags? Zero flag, carry flag, negative flag and so on. Why can't just the code be followed?
Re: Flags?
by on (#138463)
Flags represent the state of a comparison and are used to implement "if-then" logic with branches.
Re: Flags?
by on (#138468)
but cant you do if then logic without flags

example:

load 55 into x register
compare with value of 20
since they are not equal then do something
if they are equal then don't do something
Re: Flags?
by on (#138469)
If the processors didn't have flags, you couldn't branch conditionally. So the code would just go straight down. There could be no changes in behavior based on input. They're what allow you to choose whether to jump or fall down a pit, whether to move left, right or stand still. Any choice at all would be impossible without them.

Quote:
but cant you do if then logic without flags

Well... no? I mean, the flag is exactly what tells you if they're equal or not after the compare. If the flag wasn't there you could only do one thing. The branch based on the flag is what allows you to potentially not do that thing. Without flags, there could be no branches based on them.
Re: Flags?
by on (#138470)
Erockbrox wrote:
load 55 into x register
compare with value of 20
since they are not equal then do something
if they are equal then don't do something


like this?
Code:
ldx #55
cpx #20
bne "do something"
beq "don't do something"


Edit: you can also test flags and the status register with this small routine in a simulator.. like easy6502

Code:
start:
; things to try out goes here, for example sec, sei, lda #$ff or whatever
 
php
tsx
lda $101,x
sta $0

; in $0 now you have a copy of status register, and will be able to analyze it

loop:
jmp loop
Re: Flags?
by on (#138473)
Erockbrox wrote:
but cant you do if then logic without flags

example:

1. load 55 into x register
2. compare with value of 20
3. since they are not equal then do something
4. if they are equal then don't do something

Between step 2 and 3, the processor needs to remember the comparison result, since these are separate instructions. That's what the flags are for.

Kasumi wrote:
If the processors didn't have flags, you couldn't branch conditionally.

There are some CPU architectures that don't have flags, like MIPS for example. A comparison and branch is one instruction.

Code:
beq $r1, $r2, offset   # branch if r1==r2
bne $r1, $r2, offset   #   r1!=r2
bgtz $r1, offset       #   r1>0
bgez $r1, offset       #   r1>=0
bltz $r1, offset       #   r1<0
blez $r1, offset       #   r1<=0
                       # and other oddities, like:
slt $r1, $r2, $r3      #   r1 = r2<r3? 1: 0
Re: Flags?
by on (#138474)
Of course you can do branching logic without flags, see the MIPS family of processors for example. However, that's just not how the 6502 family was designed.

Asking why there is flags is as weird as asking why there is an accumulator and two index registers. It was just an arbitrary choice made when the processor was designed.

Also this should probably go in the newbie section rather than in NESdev (even though it's not up to me to judge).

EDIT : I was out-speeded by Loopy, but our point was the exact same :beer: :wink:
Re: Flags?
by on (#138483)
Cool! In the languages I've looked at, I've never seen no flags. Always good to learn a thing.
Re: Flags?
by on (#138484)
Another way of thinking about flags is that they save the result of the "if" instruction for use in the "then" part.
Re: Flags?
by on (#138498)
I like the design decision of using flags, because most operations set multiple flags, so you get more information about the result of each operation, which allows you to make successive decisions after a single operation.

Back when I started learning assembly, the flags were the most mystical thing about it. It felt like magic that decisions were completely disconnected from comparisons, and it took a while for me to realize that the flags were responsible for that, and that you could take advantage of flag persistence to optimize the code.
Re: Flags?
by on (#138503)
Erockbrox: Is this your first processor with assembler?

As far as flags, I thought the concept was fairly simple. The Z flag is the easiest flag to understand. The result of an action is either zero or non-zero. C flag is a little more complicated than the Z flag, but definitely nothing overly complex. I mean, on the simplest level it's just 'borrow/carry'. Don't get intimidated by Z or C flags. N and V, well.. that's something else entirely. With enough exposure and experience, you'll come to understand them.
Re: Flags?
by on (#138512)
Most CPUs are just giant state machines, and don't have a "short-term memory" like you or I do. They don't continue directly from the condition to the result in a single step,

Thus, the CPU checks the flags on every instruction (that uses it) to remind itself what it had just done.
Re: Flags?
by on (#138528)
Thanks guys for explaining this!

I've taken a few computer courses, but as far as I remember they never mentioned flags before so the idea was strange at first. While I'm new at learning these old processors I'm not entirely new to programming. Here is my background:

Classes

Computer logic and design - wrote pseudo code and simple programs
Visual Basic - online course, hardly learned anything
C Programming - Wrote some programs in C, for loops, do while loops, nested loops, stings, arrays
Software Applicatoins - learned Matlab and a little of Mathematicia

So I'm familiar with some things. I've taken an interest in making NES and SNES games and so that's why I'm here trying to learn these old processors and hopefully make a game!

And now thanks to you guys I think I kind of understand these flags LOL!
Re: Flags?
by on (#138538)
You should not confuse assembly and high level languages, as those are very different things.

Quote:
Cool! In the languages I've looked at, I've never seen no flags. Always good to learn a thing.

I know it's weird, but I don't think it's correct to call different assembly language "languages". Even if they are all completely different, they're still assembly language : The different processors are usually referred to as "architectures" or "processor family".

Most RISC does not have flags, exept ARM which does have them. I am not sure about PowerPC and MSP430 series, but I'm sure MIPS series lacks them.


Erockbrox wrote:
load 55 into x register
compare with value of 20
since they are not equal then do something
if they are equal then don't do something


Quote:
like this?
Code:
ldx #55
cpx #20
bne "do something"
beq "don't do something"



The ARM family can do exactly that, not only branches are conditional but all other instructions too. This is a huge advantage in an if-then-else of a single instruction, as it avoids flushing the pipeline in one of both cases. When there is more than 2 instrucitons, typically it's better to take the risk to flush the pipeline, rather than loosing fetching many useless instructions, although it depends on applications.

In non-pipelined processors such as the 6502 it does not matter, since we never loose time flushing the pipeline since there is none. So it makes more sense not wasting 4 bits per instructions just for the conditional flags, and have only the branches who are conditional.