Page 1 of 1

6502 CMP question

Posted: Sun Mar 07, 2004 8:01 am
by Ateneo
Im programming a 6502 emulator for a NES emulator im making. Im using "MOS Microcomputers Programmer Manual" as a reference and its quite decent, but i have the following problem/question regarded de CMP,CPX, CPY Instructions:

The manual says: "This instruction subtracts the contents of memory from the contents of the accumulator", but it says that the simbolic notation is:
A - M.

Becouse im emulating the instructions with real x86 instructions i want to know if "substract memory from acumlator (M - A)", or "substratc acumulator from memory (A - M).

Thanks. :(

Re: 6502 CMP question

Posted: Sun Mar 07, 2004 9:41 am
by Sprow
> Im programming a 6502 emulator for a NES emulator im making.
> I have the following problem/question regarded de CMP,CPX, CPY Instructions

[Not sure which way]

It's (A-M) and (Y-M) and (X-M),
Sprow.

Posted: Sun Mar 07, 2004 9:47 am
by BitWise
The 'A - M' notation in the manual is correct, the contents of the memory (M) is subtracted from the accumulater (A) to set the flags. The result of the subtraction itself is not stored.

The memory value M depends on the addressing mode you use. For example in an immediate compare (CMP #$20) the memory value is the immediate data ($20). If some other form of addressing was used (CMP $70, CMP $1234, CMP ($70),Y, etc.) then CPU resolves the address to locate the memory value.

After the compare the flags tell you the relative sizes of the two values.
  • If A and M are the same (e.g. use BEQ to test for A == B ) then the subtraction will set the zero flag, otherwise it will be clear (e.g. use BNE to test for A != B).
  • If A and M are considered as unsigned values then if the carry flag is clear A was smaller then M (e.g. use BCC to test for A < M). It will be set if A was not smaller (e.g. use BCS to test for A >= M).