6502 CMP instruction doesn't compare as expected
Clash 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.
assembly 6502
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.
add a comment |Â
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.
assembly 6502
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
add a comment |Â
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.
assembly 6502
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
assembly 6502
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.
edited 11 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
233
233
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
add a comment |Â
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
add a comment |Â
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.
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; somehowCPY
had mutated into an implied operation in my head. You're right, it's not, it's exactly likeCMP
but forY
. So I don't think you can directly compare the two registers.
â Tommy
5 hours ago
add a comment |Â
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 :)))
add a comment |Â
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.
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; somehowCPY
had mutated into an implied operation in my head. You're right, it's not, it's exactly likeCMP
but forY
. So I don't think you can directly compare the two registers.
â Tommy
5 hours ago
add a comment |Â
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.
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; somehowCPY
had mutated into an implied operation in my head. You're right, it's not, it's exactly likeCMP
but forY
. So I don't think you can directly compare the two registers.
â Tommy
5 hours ago
add a comment |Â
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.
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.
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; somehowCPY
had mutated into an implied operation in my head. You're right, it's not, it's exactly likeCMP
but forY
. So I don't think you can directly compare the two registers.
â Tommy
5 hours ago
add a comment |Â
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; somehowCPY
had mutated into an implied operation in my head. You're right, it's not, it's exactly likeCMP
but forY
. 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
add a comment |Â
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 :)))
add a comment |Â
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 :)))
add a comment |Â
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 :)))
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 :)))
edited 3 hours ago
answered 4 hours ago


Raffzahn
37.8k485151
37.8k485151
add a comment |Â
add a comment |Â
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.
user115898 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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