Why is the operation address incremented by two?

Posted by Gavin Jones on Stack Overflow See other posts from Stack Overflow or by Gavin Jones
Published on 2012-10-27T03:13:30Z Indexed on 2012/10/27 5:02 UTC
Read the original article Hit count: 127

Filed under:
|

I am looking at a Javascript emulator of a NES to try and understand how it works.

On this line:

    addr = this.load(opaddr+2);

The opcode is incremented by two. However, the documentation (see appendix E) I'm reading says:

Zero page addressing uses a single operand which serves as a pointer to an address in zero page ($0000-$00FF) where the data to be operated on can be found. By using zero page addressing, only one byte is needed for the operand, so the instruction is shorter and, therefore, faster to execute than with addressing modes which take two operands. An example of a zero page instruction is AND $12.

So if the operand's argument is only one byte, shouldn't it appear directly after it, and be + 1 instead of + 2? Why +2?

This is how I think it works, which may be incorrect. Suppose our memory looks like:

-------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | <- index
-------------------------
| a | b | c | d | e | f | <- memory
-------------------------
  ^  
   \ 
     PC

and our PC is 0, pointing to a. For this cycle, we say that the opcode:

var pc= 0; //for example's sake
var opcode= memory[pc]; //a

So shouldn't the first operand be the next slot, i.e. b?

var first_operand = memory[pc + 1]; //b

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about emulator