NES Programming/Introduction

From Wikibooks, open books for an open world
< NES Programming
Jump to: navigation, search

NES Programming Tutorial 1: Welcome to the machine[edit]

The recommended language for programming the NES is assembler, 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 continous 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 2 times faster. 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 status - keeps 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

LDA #15 ; load the number 15 into the accumulator.

  • Absolute Addressing

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

LDA #196 ; load the number 196 into the accumulator.

STA 2001 ; store the accumulator at memory location 2001.

  • 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.

LDA $00F8 ; Absolute, works but wastes space

LDA $F8 ; Zero Page, saves a byte

There are more addressing modes, but for now stick with these. In case you were wondering about the LDAs and STAs, 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 total instructions. The instructions that are used the most will be described following.

The first instruction you will learn is LDA. It stands for LoaD into 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: LDX. If you guessed LoaD into X, you're right. I don't think I need to explain what's LDY.

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

See also[edit]

6502 Assembly