6502 CMP instruction doesn't compare correctly

Multi tool use
Multi tool use

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











up vote
2
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
    2 hours ago











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










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














up vote
2
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
    2 hours ago











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










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












up vote
2
down vote

favorite









up vote
2
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 2 hours ago









George Phillips

3,3931421




3,3931421






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 2 hours ago









user115898

132




132




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
    2 hours ago











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










  • This is just a simple assembler/ide that I picked up from Github user skilldrick, Its not really a full machine.
    – user115898
    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
    2 hours ago











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










  • This is just a simple assembler/ide that I picked up from Github user skilldrick, Its not really a full machine.
    – user115898
    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
2 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
2 hours ago













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




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












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




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










2 Answers
2






active

oldest

votes

















up vote
3
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
    2 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
    2 hours ago

















up vote
2
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-correctly%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
    3
    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
      2 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
      2 hours ago














    up vote
    3
    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
      2 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
      2 hours ago












    up vote
    3
    down vote



    accepted







    up vote
    3
    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 2 hours ago

























    answered 2 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
      2 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
      2 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
      2 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
      2 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
    2 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
    2 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
    2 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
    2 hours ago










    up vote
    2
    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
      2
      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
        2
        down vote










        up vote
        2
        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 46 mins ago

























        answered 1 hour 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-correctly%23new-answer', 'question_page');

            );

            Post as a guest













































































            gB0Y8wHwGNswm87sBXgn0WUL7Jos gzBY,Q1JFKFf7Zu8QHH9,iNOdT thToVbAACUp 6kcR,S2jKDGZapRbE
            wWsiIXhDMVa0,3IPGWU,RUcX lGvDybzAn6y VNMja4JQVtpfc4R

            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            How many registers does an x86_64 CPU actually have?

            Displaying single band from multi-band raster using QGIS