Easy 6502 – How to get started writing 6502 assembly language
skilldrick.github.io
skilldrick.github.io
This is for those who want another good assembly book, I would recommend Assembly Language and Computer Architecture Using C++ and Java[0]. You will learn a lot from this book and not only assembly language. You'll learn about a theoretical computer H1, how to use a simulator, debugger, how a CPU can be built from scratch, how a compiler generates code by using a stack then slowly improving the instruction set architecture and make it more efficient in generating assembly code. You'll also learn how an object oriented program can be converted into assembly language. You'll learn how a C program is linked to other object files. You'll learn how to implement your own instruction set. You'll learn how to build an assembler... I can go on and on but this book is much more detailed. Just get this book and have fun reading it. I mean it.
[0] - https://www.amazon.com/Assembly-Language-Computer-Architectu...
For simple enough instruction sets it was not unheard of that people memorized the actual opcodes. In school, at the transition from terminals to micros, a friend of mine walked up to one of the new Commodore 6502 machines and entered a simple program in hex directly into the monitor program. For a while I had the common opcodes for the RCA 1802 memorized, due to working with a project board without an assembler (writing assembly on paper, then hand assembling into opcodes, then keying in the opcodes).
There's no returning to those days and I wouldn't if I could, but I can't help but think that I was lucky to have experienced that for the knowledge and perspective.
A9 00 LDA #$00
85 02 STA $02
But what is the intention of the code?> http://oeis.org/eishelp2.html
"The A-number (for example A000108) is the absolute catalogue number of the sequence. It consists of A followed by 6 [not 7!] digits."
Indeed:
"Sorry, the page you requested was not found"
LDIR? ED B0! LD HL,nnnn? 21! ...
Ah... those were the times ;)
A lot of people in my computer organization class five years ago ended up memorizing most of them, since it was sort of a side effect of having to go from opcodes to assembler and back again on quizzes.
My first programming experience happened on an Apple ][ we had in my kindergarten classroom. It's been a long time since I've used one, though, so I didn't remember the on-screen logo.
Ha ha, nice. Early on in my learning programming, I got a book about the Commodore 64. Written by a 16 year old (!) kid from the UK (IIRC), it had, in Commodore BASIC, the complete listing (using POKE statements :) of an assembler program for the C64. I typed the program into a C64 I had access to, over some days (making mistakes with the hex codes in the middle of the listing, a couple of times, and having to start the whole thing over), and finally ended up with a working assembler. Then wrote some 6502 assembly language C64 programs using the assembler, including for simple graphics.
>There's no returning to those days and I wouldn't if I could, but I can't help but think that I was lucky to have experienced that for the knowledge and perspective.
Agreed - and a lot more along the same lines, later on DOS and Unix too :)
https://en.wikipedia.org/wiki/MOS_Technology_6502
under section 1.5, "Computers and games", it says that:
The 6510, a direct successor of the 6502 with a digital I/O port and a tri-state address bus, was the CPU utilized in the best-selling[48][49] Commodore 64 home computer.
Can confirm that the 6502 instruction set is pleasantly simple. On the other hand, a similar experience can be had on some modern CPUs as well. I found the Atmel AVR's instruction set (as found in the Arduino and Raspberry Pi) to be similarly approachable.
Most everybody hated that class, but man did I love it. It was hands-down the most fun class in my studies. Would've been better with a 6502 though.
EDIT: Oh look, I found the machine. Apparently it was a HP 5036A Microprocessor Lab (learning computer): http://www.azur-electronics.com/Pages/hp_5036a_microprocesso...
http://forum.6502.org/viewtopic.php?f=12&t=3286&hilit=acorn+...
(No I haven't built the replica yet - darn work/life balance)
There's absolutely no reason you couldn't constrain yourself to a subset of the instructions available on x86 and have a fairly equivalent experience as coding for a 6502.
I can't wait to get my son into all this stuff. 8-bit Basic first, then assembly. I think he's gonna love it and I love the fact that people still enjoy the 6502.
So much history and memories with the machines it powered..
One of the most challenging projects was designing a floppy disk controller from scratch (complete with the analog circuitry) to read and write to 5-1/4 and 8 inch floppy drives. Of course, I also had to write the driver and every other driver for the thing. In fact, I also had to write my own code editor.
The "pinnacle" was using a dozen of these to run a robot. They talked to each other over a serial loop.
I also had to design and make various peripheral boards, such as: LED numeric display, DC motor drivers, analog and digital I/O, etc.
The entire computer was based on a 44 pin interconnecting bus. The CPU board could then address and talk to up to 16 cards, all memory mapped. No FPGA's or PAL's, all LS-TTL logic.
Sometimes I think it would be neat do put together a Kickstarter project for a kit and lesson plan to that would teach people to build a similar computer from scratch. Start with logic design, on through hand-building boards, bootstrapping in assembly, bringing-up FORTH, bring-up mass storage, writing a text editor and then some applications. Not sure how much interest there might be on such a thing these days. I know I would have been all over something like that way back then.
https://en.wikipedia.org/wiki/Edge_connector
This would be a fun project to work on if I could set aside some time. FIRST robotics build season (http://www.firstinspires.org/) is starting this weekend. I mentor one of the local teams. This sucks-up most of my free time for a few months.
Adding to my to-do list.
No doubt, a number of old stable designs still rely on it (I'd guess medical/scientific equipment).
I learned it at 14 years old after finding out basic wasn't very good for coding graphics. Back then the speed improvement for 6502 was always amazing because there were not high quality C compilers (actually no compilers)
Imagine learning a new language, platform, with no Google or StackOverflow. It didn't seem like a big deal at the time but wow that must have been inefficient.
My bible for 6502 was this book that I think every cmdr programmer had:
http://www.ubbcentral.com/store/item/VIC-20-Programmers-Refe...
Of course there was no assembler, it was just typing in raw instructions and addresses. Anyone else used to leave NOP gaps to make adding instructions easier?
To be fair, 6502 is a small, simple language. I learned 6502 back when I was 11-12 with little more than some .txt files, a lot of time, and a question or two answered by some very RTFM type folks. That one was not too hard overall, despite being my first language. I'd never ask someone to learn some new fancy language and framework without the internet, however.
(Maybe it helped that I was in Toronto and there was a lot interest in Commodore computers)
It was so long ago I've forgotten most of it.
which had appendices that covered the instruction set and opcodes, useful addresses, ROM listings, and all other kinds of useful stuff. What a great manual that was!
That's true, but for a newbie like myself, it would be rather hard to get going.
Also, the current run of the W65C51 is majorly broken, which makes getting the project going even harder (most alternatives to the W65C51 are no longer sold).
Might as well build a Z80 system while I'm at it. And Zilog's UARTs are quite nice, or so I've heard.
I went with a 12V-EEPROM at the time, so I had to write my own emulator to speed up my development cycles :)
Ya. I'm old AF.
But, it laid the groundwork for me so that I have a fundamental understanding of whats really going on with any language, and, processor. Once you learn ML (or assembler), learning new languages becomes an order of magnitude easier because of this understanding.
Shameless plug: https://github.com/DavidBuchanan314/6502-emu
Besides, I wouldn't say x86 is harder than 6502. You don't really 'learn' an assembly language anyway. You learn the most common instructions (loads, saves, arithmetic, branches and tests) and get a feeling for what kind of instructions there are. Now when you write something you use a reference and look up necessary instructions and look for new ones that might be useful.
Wish I'd kept the code for posterity.
Modern assembly x86, or even modern ARM assembly, is significantly more complicated than 6502 assembly.
Starting with the 6502 will let you write working code more quickly and introduce you to the core concepts of assembly language programming.
More than this, even if you never write assembly, being aware of what a CPU instruction set looks like helps you grasp core concepts in C/C++.
If you're only ever going to write high level code (e.g. JavaScript) this probably isn't helpful. But as an aid to understanding how computers work, and what they are capable of, it can be really useful.
(I probably still have a turing-complete set of Z80 opcodes in my head.)
> "Then why 6502? Why not a useful assembly language, like x86? Well, I don’t think learning x86 is useful. I don’t think you’ll ever have to write assembly language in your day job - this is purely an academic exercise, something to expand your mind and your thinking. 6502 was originally written in a different age, a time when the majority of developers were writing assembly directly, rather than in these new-fangled high-level programming languages. So, it was designed to be written by humans. More modern assembly languages are meant to written by compilers, so let’s leave it to them. Plus, 6502 is fun. Nobody ever called x86 fun."
Anyway, 6502 is a super easy processor to program on. Back in the old days (and maybe even still) universities often used non-existent processors for teaching assembly language programming and associated concepts. I learned 6502 as a child on my Apple II and even when I went to university, I always thought it was a much better first starting place simply because it lacks anything complicated at all.
To be honest, even though I have fond memories of 6502, if it were me, I might have chosen 6809 since it gives you a few more facilities and makes a couple of things a bit easier. But it's not crucial...
You can certainly learn using a more modern processor, but there is a lot more stuff to keep track of. The idea of starting with a super simple processor and then graduating to a more modern and complex processor makes sense to me.
Whereas in emulating a complete old system like a BBC micro it is far easier to get to the metal while still being able to use the display and peripherals.
"Assembler" is really quite a portable skill once you've learnt at least two dialects, just as learning German and Latin will open most European languages to you.
If you want to learn low level programming there are better and more recent systems out there (avr, arm)
"Then why 6502? Why not a useful assembly language, like x86? Well, I don’t think learning x86 is useful. I don’t think you’ll ever have to write assembly language in your day job - this is purely an academic exercise, something to expand your mind and your thinking. 6502 was originally written in a different age, a time when the majority of developers were writing assembly directly, rather than in these new-fangled high-level programming languages.
So, it was designed to be written by humans. More modern assembly languages are meant to written by compilers, so let’s leave it to them. Plus, 6502 is fun. Nobody ever called x86 fun".
ARM and AVR may be better and more recent options but they probably won't be good for introduction purposes.
Seriuos question though: does anyone here use any assembly language in contemporary work today? If so, what's that like?
http://www.6502.org/tools/emu/
Someone else will have to provide an actual recommendation.