This tutorial is somewhat of a Hello World, but not really. It's main purpose is to familiarize you with 6502asm.com, the tool we will be using throughout our tutorials.
A "Hello World" tutorial is a traditional way to introduce someone to a new programming language. However, it's simply not possible with 6502 assembly language. Before you can possibly try to get the 6502 to display the letters in the words "Hello World" to the screen, you need to learn a bit more than I can teach you in a simple introductory tutorial. But don't worry, you'll find it's all very easy when you get there.
Therefore, instead of a "Hello World", I'm going to teach how to make a "Hello Dot."
It's extremely handy. Because it's a web page you don't have to download anything. It also has something most 6502 simulators lack —- a graphics screen.
If you are serious about learning to program the 6502, bookmark it now. You'll be using it a lot. In the top bar you will see a pull-down menu labeled 6502asm.com. There you will find a link marked Launch. Clicking on this link will launch 6502asm.com in a new browser window. Do this now.
Playing with 6502asm.com
6502asm.com has examples and you're just going to have to play with them before you can move on with this tutorial. If the thought hadn't occurred to you — then do it anyway. It will give you some idea of how 6502asm.com works. Here's how to load, compile, and play with the examples.
- First, click on the box that says "Examples". It's a pull down menu.
- Choose one of the examples. It doesn't matter which one.
- This will load the example into the code editor window.
- The code you will see is assembly code, and very similar to what you will be coding shortly. (Don't be afraid).
- Click on the button marked compile. This will compile the assembly into machine language that the 6502 understands.
- Finally, click on the button marked run and watch the magic.
- Press the button labeled "Stop" to stop the 6502 emulator. You will have to do this before choosing and compiling another example.
Be sure to try spacer.asm. It is a are video game of a sort — exactly the kind of thing you will be learning to program by doing the tutorials in this wiki.
The Hello Dot
Ok. Ready to get started?
Delete all the code in the editor window and copy this code into it. You should already know how to compile and run it by now :
LDX #$01 STX $200
All this does is put a dot in the upper/left corner of the window. It's not very impressive, but it's a good start.
The X register is the right hand of the 6502. You can pick up things, hold them, put them down and even "stack" them using a method called relative addressing (more on that in later tutorials). By loading a value into X, we are putting a number in the right hand of the 6502. Let's look a bit closer at the entire line :
LDX is the instruction, #$01 is the operand — or in less technical language — the number we are putting into X. There's several ways to get a number into X but this time we are just giving it a number. Whenever you use ( # ), that means you are just putting a number straight into X.
The number we are putting into X is $01. $ signifies that this is a hexadecimal number, but don't worry, this is the same number as 1 in the familiar 10-base number system. So, therefore :
puts the number 1 directly into X. When you do this, it is called immediate addressing.
Now that we have given the 6502 something to work with, we need to do something with it. STX stands for Store X. In the "Hello Dot" code we use it put the number in X ( $01) into memory location $200. Notice that there is no #. This means that we are using a memory address as the operand rather than a number. This is known as absolute addressing.
$200 is the memory location that is linked (or "mapped") to the video output. When you put something in $200, it changes the color of the first pixel of the video display. If you wanted to change the second pixel of the video display, you would use $201.
And there it is. A Hello Dot.