Jump to content
IGNORED

The Z Flag


Recommended Posts

Hi, I'm having a problem with a m/c program I'm writing but no matter how many times I've looked at it I just can't see what the problem is, I would put it up here for your perusal but it's quite a large program (over 400 bytes), perhaps if I don't get anywhere with this thread I will put it up. What I'm querying specifically is the Z flag. If I use an LDA #number (with no CMP) then if the number = 0 then Z = 1, 0 otherwise, correct? What I'd like to be sure of is, is this the only result for Z, what I mean is if the #number = any other value will the flag still work as given? So, if I use LDA #128 then Z should still be 0, yes?

Edited by ac.tomo
Link to comment
Share on other sites

17 minutes ago, Irgendwer said:

Yes.

 

Edit: Extra points for letting me think this thread is about some "special operation"...

lol, thanks, just wanted to make sure because I have seen somewhere (on this forum, where I can't remember) that there's something about the number #128. Does it matter with the N flag, I mean I know that when a register reversely decrements from 0 to 255 the N flag = 1, does the same apply from going from 255 to 0? and does the number #128 come into this flags use at all? Thanks in advance.

Link to comment
Share on other sites

5 minutes ago, ac.tomo said:

lol, thanks, just wanted to make sure because I have seen somewhere (on this forum, where I can't remember) that there's something about the number #128. Does it matter with the N flag, I mean I know that when a register reversely decrements from 0 to 255 the N flag = 1, does the same apply from going from 255 to 0? and does the number #128 come into this flags use at all? Thanks in advance.

Counting up 128 is the first negative number, so the first one that will set N.

Link to comment
Share on other sites

2 hours ago, slx said:

Counting up 128 is the first negative number, so the first one that will set N.

loop  ldx   #120

        dex

        bmi  done

        jmp loop

done continue...

 

Am I right in saying this will bmi when x goes below #0, but if the number in the ldx was #130 then program flow will bmi when x goes below 128? If so, this is one of the bugs in my program.

 

Edited by ac.tomo
Link to comment
Share on other sites

The rule for the N flag is simple for almost all instructions: it's a copy of bit 7 of the result. Doesn't matter whether the operation was a load, add, shift, or whatever, it grabs bit 7 of the result. That means that any N-affecting operation that gives you $FF (255) as the result will also set N=1. Doesn't matter how you got there, whether it was by loading 255, decrementing 0, incrementing 254, adding, subtracting, or shifting.

 

You can interpret this as "negative number", but only if you take wrapping within 8-bits into account (signed overflow) -- as in (-128) - 1 = 127 and 127 + 1 = -128. So yeah, 130 is 'negative', because it has the same bit pattern as -126. Which means that in most cases you will need to use BEQ/BNE instead of BPL/BMI when you want a loop to count more than 128 times.

 

There are two exceptions to the way N is set. One is the BIT instruction, and the other is decimal mode. You probably won't have to worry about these yet.

 

Z is similar. For instructions that affect Z, it's set if the resulting byte is 0 and cleared otherwise. Doesn't matter what operation got you got there. Doesn't matter if you actually added 1 + 255 = 256 and pushed a bit off the left side, if the resulting 8 bits are zero then Z=1.

 

  • Like 4
Link to comment
Share on other sites

8 hours ago, phaeron said:

The rule for the N flag is simple for almost all instructions: it's a copy of bit 7 of the result. Doesn't matter whether the operation was a load, add, shift, or whatever, it grabs bit 7 of the result. That means that any N-affecting operation that gives you $FF (255) as the result will also set N=1. Doesn't matter how you got there, whether it was by loading 255, decrementing 0, incrementing 254, adding, subtracting, or shifting.

 

You can interpret this as "negative number", but only if you take wrapping within 8-bits into account (signed overflow) -- as in (-128) - 1 = 127 and 127 + 1 = -128. So yeah, 130 is 'negative', because it has the same bit pattern as -126. Which means that in most cases you will need to use BEQ/BNE instead of BPL/BMI when you want a loop to count more than 128 times.

 

There are two exceptions to the way N is set. One is the BIT instruction, and the other is decimal mode. You probably won't have to worry about these yet.

 

Z is similar. For instructions that affect Z, it's set if the resulting byte is 0 and cleared otherwise. Doesn't matter what operation got you got there. Doesn't matter if you actually added 1 + 255 = 256 and pushed a bit off the left side, if the resulting 8 bits are zero then Z=1.

 

 

Many thanks for that phaeron, to be honest I actually haven't read a 6502 manual from cover to cover, the only flags I've never used or understood fully are the decimal flag and the N flag and it's this N flag that I didn't know about regarding bit-7 (#128) and because of this this was one of my oversights in the program that I'm doing, so that's one bug sorted, only another 2 to do., thanks again.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...