NES Programming/Introduction

NES Programming Tutorial 1: Welcome to the machine
The recommended language for programming the NES is assembly, because the slow 8-bit processor demands it. The processor is similar to the early Intel chips, but lacks certain functions.

The 6502 processor can address up to 64 kilobytes of memory. Think of this as a continuous space that has 65536 "cells". Each "cell", or byte, can store a number from 0 to 255. The first 256 bytes of this space is known as the zero page, which the 6502 can access about slightly faster and in one less byte. The next 256 bytes are the stack. The 6502 contains 3 general-purpose registers:

Accumulator - all data movement, mathematical, and logical operations go through here.

X index register - used for general counting and pre-index memory offsetting.

Y index register - used for general counting and post-index memory offsetting.

All of these general-purpose registers can be used for any reason, but the reasons above are what they are usually used for.

These are special-feature registers.

Stack pointer - keeps track of the stack.

Processor flags / status - holds flags containing information about the result of previous instructions.

Program Counter - keeps track of where the program is executing.

There are several ways to access memory:


 * Immediate

Here, the exact value of the 8-bit number is written in the instruction. No memory accessing occurs. To denote an immediate, place a  before the number, for example


 * Absolute Addressing

The 16-bit address is stored in the instruction. Whatever at the specified memory address will be the number. For example,


 * Zero Page Addressing

Similar to absolute addressing, this method accesses an absolute address. However, this method saves a byte by only writing the low byte in the instruction. The high byte is assumed to be zero. Therefore, this method can only be used for the zero page. The following example shows the same instruction using absolute and zero page addressing.

There are more addressing modes, but for now stick with these. In case you were wondering about the s and  s, those are just names of instructions. On to the instructions!

The instructions are certain commands that tell the processor to do something. This may be move something into memory, add some numbers, jump to a different location, etc. There are 56 instructions. The instructions that are used the most will be described following.

The first instruction you will learn is. It stands for LoaD A, which copies data into register A. The data can be a number, an address, or indexed address (more on this addressing mode next tutorial). See if you figure out this one:. If you guessed LoaD X, you're right. I don't think I need to explain what's.

, the second most common instruction, stands for STore A, which stores the value of A into memory. would store the current value of A into memory address. Its counterparts include  and.