6502 CMP question

Let's talk about anything related to the 6502 microprocessor.
Post Reply
Ateneo
Posts: 2
Joined: 07 Mar 2004
Location: Argentina

6502 CMP question

Post 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. :(
- Ateneo -
Sprow
Posts: 12
Joined: 23 Oct 2003
Contact:

Re: 6502 CMP question

Post 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.
User avatar
BitWise
In Memoriam
Posts: 996
Joined: 02 Mar 2004
Location: Berkshire, UK
Contact:

Post 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).
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs
Post Reply