6502 CMP instruction doesn't compare as expected

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












I am trying to code in 6502 assembly and for some reason the CMP instruction doesn't work. For example:



 CLD
LDY #$03
LDA #$00
LDX #$05
CMP Y
BEQ Equal
STX $0200
Equal:
BRK


This should place a green square on the screen if I am correct but it doesn't, it skips right over the STX line because apparently the equal flag is raised. I'm new to 6502 assembly so I could just be making a mistake.










share|improve this question









New contributor




user115898 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    It would help if you could also say which assembler and machine you're trying to code for. (regardless I suspect the answer that's been given is correct)
    – PeterI
    5 hours ago











  • Which 6502-based machine has screen (colour?) memory at $0200?
    – berendi
    5 hours ago










  • This is just a simple assembler/ide that I picked up from Github user skilldrick, Its not really a full machine.
    – user115898
    5 hours ago










  • Unless Y is declared as a label somewhere (which is probably not a good label name to use, but for the sake of argument), that should not even assemble. Sounds like it's not a very mature assembler. Update: It's definitely not. Tried LDA X, STA Y at skilldrick.github.io/easy6502 and that assembles without errors (to an address operand of 0xFF 0xFF, as does any unknown labels).¯_(ツ)_/¯
    – Cumbayah
    2 hours ago















up vote
4
down vote

favorite












I am trying to code in 6502 assembly and for some reason the CMP instruction doesn't work. For example:



 CLD
LDY #$03
LDA #$00
LDX #$05
CMP Y
BEQ Equal
STX $0200
Equal:
BRK


This should place a green square on the screen if I am correct but it doesn't, it skips right over the STX line because apparently the equal flag is raised. I'm new to 6502 assembly so I could just be making a mistake.










share|improve this question









New contributor




user115898 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    It would help if you could also say which assembler and machine you're trying to code for. (regardless I suspect the answer that's been given is correct)
    – PeterI
    5 hours ago











  • Which 6502-based machine has screen (colour?) memory at $0200?
    – berendi
    5 hours ago










  • This is just a simple assembler/ide that I picked up from Github user skilldrick, Its not really a full machine.
    – user115898
    5 hours ago










  • Unless Y is declared as a label somewhere (which is probably not a good label name to use, but for the sake of argument), that should not even assemble. Sounds like it's not a very mature assembler. Update: It's definitely not. Tried LDA X, STA Y at skilldrick.github.io/easy6502 and that assembles without errors (to an address operand of 0xFF 0xFF, as does any unknown labels).¯_(ツ)_/¯
    – Cumbayah
    2 hours ago













up vote
4
down vote

favorite









up vote
4
down vote

favorite











I am trying to code in 6502 assembly and for some reason the CMP instruction doesn't work. For example:



 CLD
LDY #$03
LDA #$00
LDX #$05
CMP Y
BEQ Equal
STX $0200
Equal:
BRK


This should place a green square on the screen if I am correct but it doesn't, it skips right over the STX line because apparently the equal flag is raised. I'm new to 6502 assembly so I could just be making a mistake.










share|improve this question









New contributor




user115898 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am trying to code in 6502 assembly and for some reason the CMP instruction doesn't work. For example:



 CLD
LDY #$03
LDA #$00
LDX #$05
CMP Y
BEQ Equal
STX $0200
Equal:
BRK


This should place a green square on the screen if I am correct but it doesn't, it skips right over the STX line because apparently the equal flag is raised. I'm new to 6502 assembly so I could just be making a mistake.







assembly 6502






share|improve this question









New contributor




user115898 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user115898 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 4 mins ago









Thorbjørn Ravn Andersen

1657




1657






New contributor




user115898 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 5 hours ago









user115898

232




232




New contributor




user115898 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user115898 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user115898 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2




    It would help if you could also say which assembler and machine you're trying to code for. (regardless I suspect the answer that's been given is correct)
    – PeterI
    5 hours ago











  • Which 6502-based machine has screen (colour?) memory at $0200?
    – berendi
    5 hours ago










  • This is just a simple assembler/ide that I picked up from Github user skilldrick, Its not really a full machine.
    – user115898
    5 hours ago










  • Unless Y is declared as a label somewhere (which is probably not a good label name to use, but for the sake of argument), that should not even assemble. Sounds like it's not a very mature assembler. Update: It's definitely not. Tried LDA X, STA Y at skilldrick.github.io/easy6502 and that assembles without errors (to an address operand of 0xFF 0xFF, as does any unknown labels).¯_(ツ)_/¯
    – Cumbayah
    2 hours ago













  • 2




    It would help if you could also say which assembler and machine you're trying to code for. (regardless I suspect the answer that's been given is correct)
    – PeterI
    5 hours ago











  • Which 6502-based machine has screen (colour?) memory at $0200?
    – berendi
    5 hours ago










  • This is just a simple assembler/ide that I picked up from Github user skilldrick, Its not really a full machine.
    – user115898
    5 hours ago










  • Unless Y is declared as a label somewhere (which is probably not a good label name to use, but for the sake of argument), that should not even assemble. Sounds like it's not a very mature assembler. Update: It's definitely not. Tried LDA X, STA Y at skilldrick.github.io/easy6502 and that assembles without errors (to an address operand of 0xFF 0xFF, as does any unknown labels).¯_(ツ)_/¯
    – Cumbayah
    2 hours ago








2




2




It would help if you could also say which assembler and machine you're trying to code for. (regardless I suspect the answer that's been given is correct)
– PeterI
5 hours ago





It would help if you could also say which assembler and machine you're trying to code for. (regardless I suspect the answer that's been given is correct)
– PeterI
5 hours ago













Which 6502-based machine has screen (colour?) memory at $0200?
– berendi
5 hours ago




Which 6502-based machine has screen (colour?) memory at $0200?
– berendi
5 hours ago












This is just a simple assembler/ide that I picked up from Github user skilldrick, Its not really a full machine.
– user115898
5 hours ago




This is just a simple assembler/ide that I picked up from Github user skilldrick, Its not really a full machine.
– user115898
5 hours ago












Unless Y is declared as a label somewhere (which is probably not a good label name to use, but for the sake of argument), that should not even assemble. Sounds like it's not a very mature assembler. Update: It's definitely not. Tried LDA X, STA Y at skilldrick.github.io/easy6502 and that assembles without errors (to an address operand of 0xFF 0xFF, as does any unknown labels).¯_(ツ)_/¯
– Cumbayah
2 hours ago





Unless Y is declared as a label somewhere (which is probably not a good label name to use, but for the sake of argument), that should not even assemble. Sounds like it's not a very mature assembler. Update: It's definitely not. Tried LDA X, STA Y at skilldrick.github.io/easy6502 and that assembles without errors (to an address operand of 0xFF 0xFF, as does any unknown labels).¯_(ツ)_/¯
– Cumbayah
2 hours ago











2 Answers
2






active

oldest

votes

















up vote
8
down vote



accepted










The code you've posted:



  • loads the immediate value 0 into A;

  • loads the immediate value 3 into Y;

  • then compares the 0 in A to whatever is in memory at the address you've given the label Y.

There are no register-to-register comparisons on the 6502.






share|improve this answer






















  • How would I do that with CPY, from what I know about CPY you have to have a memory address or immediate as a source. I never gave any memory address the label Y but I think I get what you are saying here. So I guess the question I should be asking here is how I would compare the accumulator register to the Y register? If you would be willing to post the answer to this question it would be really helpful!
    – user115898
    5 hours ago







  • 1




    I've suffered a major memory failure here; somehow CPY had mutated into an implied operation in my head. You're right, it's not, it's exactly like CMP but for Y. So I don't think you can directly compare the two registers.
    – Tommy
    5 hours ago

















up vote
3
down vote













There are no register/register operations on the 6502 (except for transfer). The 6502 follows a strict accumulator/memory scheme (with a few extensions for index-register/memory)



Your example is a bit useless, as comparing two constants doesn't make sense, one needs to be a variable at least, right? Lets assume the first (#$03) is a memory location called VALUE instead. So a useful check for VALUE holding 00 would work like this:



 LDX #$05 * Prepare X
LDA VALUE * Value to compare
CMP #$00 * Compare with this
BEQ Equal * Do not store X when equal
STX $0200 * Store X
Equal:
BRK


In a real life 6502 programm the CMP instruction can be left out at all, as loading a value already performs a test for Zero.



(Further, CLD is not needed, as decimal mode has no influence beside addition/subtractions and there is no need to use X, as storing a constant value (or moving one) can be done by using A after the BEQ - same programm length but fewer cycles when not needed. And so on :)))






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "648"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    user115898 is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fretrocomputing.stackexchange.com%2fquestions%2f7977%2f6502-cmp-instruction-doesnt-compare-as-expected%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    8
    down vote



    accepted










    The code you've posted:



    • loads the immediate value 0 into A;

    • loads the immediate value 3 into Y;

    • then compares the 0 in A to whatever is in memory at the address you've given the label Y.

    There are no register-to-register comparisons on the 6502.






    share|improve this answer






















    • How would I do that with CPY, from what I know about CPY you have to have a memory address or immediate as a source. I never gave any memory address the label Y but I think I get what you are saying here. So I guess the question I should be asking here is how I would compare the accumulator register to the Y register? If you would be willing to post the answer to this question it would be really helpful!
      – user115898
      5 hours ago







    • 1




      I've suffered a major memory failure here; somehow CPY had mutated into an implied operation in my head. You're right, it's not, it's exactly like CMP but for Y. So I don't think you can directly compare the two registers.
      – Tommy
      5 hours ago














    up vote
    8
    down vote



    accepted










    The code you've posted:



    • loads the immediate value 0 into A;

    • loads the immediate value 3 into Y;

    • then compares the 0 in A to whatever is in memory at the address you've given the label Y.

    There are no register-to-register comparisons on the 6502.






    share|improve this answer






















    • How would I do that with CPY, from what I know about CPY you have to have a memory address or immediate as a source. I never gave any memory address the label Y but I think I get what you are saying here. So I guess the question I should be asking here is how I would compare the accumulator register to the Y register? If you would be willing to post the answer to this question it would be really helpful!
      – user115898
      5 hours ago







    • 1




      I've suffered a major memory failure here; somehow CPY had mutated into an implied operation in my head. You're right, it's not, it's exactly like CMP but for Y. So I don't think you can directly compare the two registers.
      – Tommy
      5 hours ago












    up vote
    8
    down vote



    accepted







    up vote
    8
    down vote



    accepted






    The code you've posted:



    • loads the immediate value 0 into A;

    • loads the immediate value 3 into Y;

    • then compares the 0 in A to whatever is in memory at the address you've given the label Y.

    There are no register-to-register comparisons on the 6502.






    share|improve this answer














    The code you've posted:



    • loads the immediate value 0 into A;

    • loads the immediate value 3 into Y;

    • then compares the 0 in A to whatever is in memory at the address you've given the label Y.

    There are no register-to-register comparisons on the 6502.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 5 hours ago

























    answered 5 hours ago









    Tommy

    12.8k13264




    12.8k13264











    • How would I do that with CPY, from what I know about CPY you have to have a memory address or immediate as a source. I never gave any memory address the label Y but I think I get what you are saying here. So I guess the question I should be asking here is how I would compare the accumulator register to the Y register? If you would be willing to post the answer to this question it would be really helpful!
      – user115898
      5 hours ago







    • 1




      I've suffered a major memory failure here; somehow CPY had mutated into an implied operation in my head. You're right, it's not, it's exactly like CMP but for Y. So I don't think you can directly compare the two registers.
      – Tommy
      5 hours ago
















    • How would I do that with CPY, from what I know about CPY you have to have a memory address or immediate as a source. I never gave any memory address the label Y but I think I get what you are saying here. So I guess the question I should be asking here is how I would compare the accumulator register to the Y register? If you would be willing to post the answer to this question it would be really helpful!
      – user115898
      5 hours ago







    • 1




      I've suffered a major memory failure here; somehow CPY had mutated into an implied operation in my head. You're right, it's not, it's exactly like CMP but for Y. So I don't think you can directly compare the two registers.
      – Tommy
      5 hours ago















    How would I do that with CPY, from what I know about CPY you have to have a memory address or immediate as a source. I never gave any memory address the label Y but I think I get what you are saying here. So I guess the question I should be asking here is how I would compare the accumulator register to the Y register? If you would be willing to post the answer to this question it would be really helpful!
    – user115898
    5 hours ago





    How would I do that with CPY, from what I know about CPY you have to have a memory address or immediate as a source. I never gave any memory address the label Y but I think I get what you are saying here. So I guess the question I should be asking here is how I would compare the accumulator register to the Y register? If you would be willing to post the answer to this question it would be really helpful!
    – user115898
    5 hours ago





    1




    1




    I've suffered a major memory failure here; somehow CPY had mutated into an implied operation in my head. You're right, it's not, it's exactly like CMP but for Y. So I don't think you can directly compare the two registers.
    – Tommy
    5 hours ago




    I've suffered a major memory failure here; somehow CPY had mutated into an implied operation in my head. You're right, it's not, it's exactly like CMP but for Y. So I don't think you can directly compare the two registers.
    – Tommy
    5 hours ago










    up vote
    3
    down vote













    There are no register/register operations on the 6502 (except for transfer). The 6502 follows a strict accumulator/memory scheme (with a few extensions for index-register/memory)



    Your example is a bit useless, as comparing two constants doesn't make sense, one needs to be a variable at least, right? Lets assume the first (#$03) is a memory location called VALUE instead. So a useful check for VALUE holding 00 would work like this:



     LDX #$05 * Prepare X
    LDA VALUE * Value to compare
    CMP #$00 * Compare with this
    BEQ Equal * Do not store X when equal
    STX $0200 * Store X
    Equal:
    BRK


    In a real life 6502 programm the CMP instruction can be left out at all, as loading a value already performs a test for Zero.



    (Further, CLD is not needed, as decimal mode has no influence beside addition/subtractions and there is no need to use X, as storing a constant value (or moving one) can be done by using A after the BEQ - same programm length but fewer cycles when not needed. And so on :)))






    share|improve this answer


























      up vote
      3
      down vote













      There are no register/register operations on the 6502 (except for transfer). The 6502 follows a strict accumulator/memory scheme (with a few extensions for index-register/memory)



      Your example is a bit useless, as comparing two constants doesn't make sense, one needs to be a variable at least, right? Lets assume the first (#$03) is a memory location called VALUE instead. So a useful check for VALUE holding 00 would work like this:



       LDX #$05 * Prepare X
      LDA VALUE * Value to compare
      CMP #$00 * Compare with this
      BEQ Equal * Do not store X when equal
      STX $0200 * Store X
      Equal:
      BRK


      In a real life 6502 programm the CMP instruction can be left out at all, as loading a value already performs a test for Zero.



      (Further, CLD is not needed, as decimal mode has no influence beside addition/subtractions and there is no need to use X, as storing a constant value (or moving one) can be done by using A after the BEQ - same programm length but fewer cycles when not needed. And so on :)))






      share|improve this answer
























        up vote
        3
        down vote










        up vote
        3
        down vote









        There are no register/register operations on the 6502 (except for transfer). The 6502 follows a strict accumulator/memory scheme (with a few extensions for index-register/memory)



        Your example is a bit useless, as comparing two constants doesn't make sense, one needs to be a variable at least, right? Lets assume the first (#$03) is a memory location called VALUE instead. So a useful check for VALUE holding 00 would work like this:



         LDX #$05 * Prepare X
        LDA VALUE * Value to compare
        CMP #$00 * Compare with this
        BEQ Equal * Do not store X when equal
        STX $0200 * Store X
        Equal:
        BRK


        In a real life 6502 programm the CMP instruction can be left out at all, as loading a value already performs a test for Zero.



        (Further, CLD is not needed, as decimal mode has no influence beside addition/subtractions and there is no need to use X, as storing a constant value (or moving one) can be done by using A after the BEQ - same programm length but fewer cycles when not needed. And so on :)))






        share|improve this answer














        There are no register/register operations on the 6502 (except for transfer). The 6502 follows a strict accumulator/memory scheme (with a few extensions for index-register/memory)



        Your example is a bit useless, as comparing two constants doesn't make sense, one needs to be a variable at least, right? Lets assume the first (#$03) is a memory location called VALUE instead. So a useful check for VALUE holding 00 would work like this:



         LDX #$05 * Prepare X
        LDA VALUE * Value to compare
        CMP #$00 * Compare with this
        BEQ Equal * Do not store X when equal
        STX $0200 * Store X
        Equal:
        BRK


        In a real life 6502 programm the CMP instruction can be left out at all, as loading a value already performs a test for Zero.



        (Further, CLD is not needed, as decimal mode has no influence beside addition/subtractions and there is no need to use X, as storing a constant value (or moving one) can be done by using A after the BEQ - same programm length but fewer cycles when not needed. And so on :)))







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 3 hours ago

























        answered 4 hours ago









        Raffzahn

        37.8k485151




        37.8k485151




















            user115898 is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            user115898 is a new contributor. Be nice, and check out our Code of Conduct.












            user115898 is a new contributor. Be nice, and check out our Code of Conduct.











            user115898 is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fretrocomputing.stackexchange.com%2fquestions%2f7977%2f6502-cmp-instruction-doesnt-compare-as-expected%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery