6502 Machine Language for Beginners
savetz writes "If you've always wanted to learn 6502 assembly language, now's your chance. The full text of the classic, best-selling 1983 book Machine Language for Beginners is now on the Web. It includes examples and program code for Atari 8-bit, Apple ][, PET/CBM, VIC-20, and Commodore 64 computers."
I have the copy of this that I bought back when it came out. Or, rather, my dad bought it for me. I remember showing it to him and having him ask "What? Are you going to start talking to the refrigerator with this?"
Good old dad. Never has understood what "machine language" really means, and still doesn't care.
It's a good book, if you want ot know 6502. Give it a try and then start coding for the Atari 2600 or Commodore 64.
Curmudgeon Gamer: Not happy
Yes, it does; it's called ADC if I remember correctly.
-WolfWithoutAClause
"Gravity is only a theory, not a fact!"But the Amiga had an HCF instruction. Halt and Catch Fire.
If tits were wings it'd be flying around.
An old eight bit processor is the best tool for understanding how computers work. They are simple enough to avoid discouraging the student, but the core concepts are still the same today. There are free simulators, so students don't have to put up with uncomfortable data entry or long loading times.
But wouldnt that be add with carry, and not a straight add? Just being pedantic here mind you, because you do get the carry pretty much for free when making an alu, and stick a multiplexor on the carry line and you have the add instruction almost for free as well.
Come to think of it, is there any advantage to having a straight add instruction over an add with carry? (Aside from preserving the carry bit for the next instruction, but that would be some weird programming)
Yeah, that ranks right up there with sorting my sock drawer or beating my head against the window.
Repeal the DMCA!
Floating point is a completely different matter though. The Acorn Atom (based on the 6502) did have a floating point library in ROM. I never tried calling it from ML, but it shows that it can be implemented.
I learned Machine Language programming on the 6502 from "Atomic Theory and Practice" by David Johnson-Davies. The 6502 is a rather neat processor. I even once wrote a compiler for a self made language that would compile to 6502 machine code. For a proof that some rather impressive things can be done with it, have a look at contiki.
Ah well, at least it won't get distracted as easily.
---
The additional instruction would not be "free" in terms of instruction code space. The 6502 reads one byte for the instruction and 0, 1 or 2 additional bytes as operands. All instructions with their addressing modes and implied operands have to fit into 256 codes. Appendix A of the book is a list of all opcodes (instruction, addressing mode, implied operand). There are 8 opcodes for ADC alone...
Yeah, but you have to clear the carry (CLC), *then* Add with Carry (ADC) to perform what a normal ADD would have done on other processors. Struck me as funny when I first learned 6502.
Now, the IBM 1620 didn't have hardware addition. You had to load in a lookup table that contained the answers to "2+2", "2+3", etc...
When I saw this, my first thought was of the scene in ST:TNG with Scotty and Picard on the hologram bridge of Kirk's Enterprise. Scotty liked the first Enterprise because he could tell the speed by the feel of the deck plates and Picrad said the Enterprise was superior than the Stargazer in terms of numbers, but he still often wished he was on the Stargazer.
//e. I knew the monitor ROM backwards and forwards. I used an amazingly powerful assembler called ORCA/M (known for it's macros and libraries), and learned hardware from books about the Apple //e. It was a wonderful world to learn and play in. I sold my //e to buy an Amiga. I still have the Amiga, but I wish I still had that //e -- it had a FULLY SOCKED motherboard, with a modified ROM that gave me extra features (I did the ROM mods myself), and a few nice accessories -- like a hard drive with a whopping 5 Meg of storage and a memory card that gave me over a megabyte of online memory -- which I used as a ramdrive.
//e (I had even figured out how I could make it multi-task w/ a clock card -- but never got around to programming it) instead of worrying about networking and web pages and relational databases.
I learned almost everything I know about computers from my Apple
Just like Picard and the Stargazer, I often wish I were programming on my old
Of course FP can be implemented on a 6502... AppleSoft BASIC, written by MicroSoft and included in the ROM of all but the earliest Apple ][s (and available on floppy or cassette for the ones that didn't have it in ROM), had floating point routines, including trig functions, log, and square root.
Yeah, but you have to clear the carry (CLC), *then* Add with Carry (ADC) to perform what a normal ADD would have done on other processors. Struck me as funny when I first learned 6502.
This isn't "funny" at all. Remember that when working with an 8 bit value, addition operations on single-bite, not to mention multi-byte, values have a high chance of overflowing the 8 bit register -- thus setting the carry. It's no more odd to CLC (Clear Carry) than to load a register with #$00 to start a loop.
In fact, ADC (Add with Carry) is an abstraction that allows the same instruction to be used for both single- and multi-byte additions. The beauty of the ISA is the small number of operators to fit the small number of registers available.
If ADC didn't work in this way, in what way would you have it work? And what would you like to see happen when you added #$FF to #$02? Forty miles in the slow uphill both ways gravel in my boots kids these days...
I agree that simpler is better, when learning/teaching core computer architecture concepts. However, rather than using old computer simulators, I personally prefer Atmel AVR microcontrollers. With a bare minimum of hardware, you can build a miniature computer from scratch and control every bit of the software that goes into it. It's just a good feeling to know that you aren't relying on anyone else's ROM code to do the dirty work. Plus it's an 8-bit RISC processor with 32 general-purpose registers, which is quite nice when you get too lazy for assembly and break out the gcc cross-compiler. =)
Paranoid
Bwaahahahahaa.
Well, my experience up to the 6502 was with processors that had a lot more opcodes in their instruction set. The 68000, z80/8080, 6809 all had separate ADD and ADC-type instructions. (I won't even touch on 370 BAL, which had one-to-one constructs with fortran code)
;)
But the 6502 was SO minimal. They didn't bother with *any* fluff that could be cut out.
So I found it amusing.
The concepts from 8-bit processors, like instructions, registers and flags are still there and haven't changed much. Of course the new processors brought new concepts - virtual modes, paging, sophisticated memory protection schemes, interrupt hierarchies, numeric coprocessors, hinted jumps, various kinds of SIMD instructions and so on and so on.
On the other hand, most developers are pretty much separated from the processor - the operating system and the compiler are there to manage all of the above. Even if you want a super-optimized piece of code, it's a good idea to start by looking at the compiler output.
This is of course only true if you don't plan to write your own OS or a compiler.
As a side note, I remember being all excited when I found out that we were going to write ML programs in intro to computer theory. Little did I know that the name "ML" is overloaded in the computer languages department. We didn't learn machine language, we learned the "other" ML. Of course I did get really good at recursion. Not that I ever use it now.
Lasers Controlled Games!
I've always considered the 6502, more or less, a RISC processor due to it's uh, "simpleness".
.X is __, .Y is __ and the carry flag is set and the Z flag is off were my normal thoughts while writing assembly.
It has 3 register, 8-bit address lines, 1 accumalator and two index registers. That was pretty much it along with 52(I think) opcodes and variants in terms of addressing modes.
The chip was neat in that it was simple enough for someone programming it to be cognizant of everything going on inside the processor: Ok, at this stage the accumalator is __,
I can't speak for any of the other 6502 computers but the way that it was used on the C-64 was an engineer's wet dream. It was coupled with other chips that were simple in their operation but complex in their innards. There was no filtering of bad input data - if you fed it crap, the chip was free to interpret it however it wanted.
This ability to send "crap" data was pretty neat because once you got the timing down right you could do pretty miraculous things - that weren't even listed as features for those chips.
Bonus question: How can you disable the NMI interrupt on the 6502? (Yes, there is a way).
There's a gorilla from Manilla whose a fella that stinks of vanilla and has salmonella.
The real instructions only used up about a third or so of the available opcodes, and while most of the rest simply froze the processor, there were others that had interesting and predictable effects, and were in fact used by some of the C64 games. See this for the exciting low down.
....
So, I've been writing a 6502 Ada compiler just for the heck of it, and it's much more fun than targeting these new-fangled, regular instruction sets. Clearing space on the stack is great. The fastest way to do it depends on how much space you need; with one or two bytes, a couple of PHPs, three to six or so and you transfer SP to X, decrement the appropriate number of times, then send X back to SP. More than sixish, and you should TSX, TXA, SBC, TAX, TSX.
You'd be astounded at the machinations required for addressing variables on the stack, and mortified at the way a simple CMP instruction has the arrogance to affect the overflow bit. Unfortunately, this comment is to small to go into it.
Meeeeeemmmoooorrrrriiiiieeeeeeeeeees
The earliest religious war I took part in was between the 6502 users (eg PETs) and the good guys, like me, who hacked on the Z80 (eg most/all CP/M machines).
Have to say that the intellectual case for the 6502 looked stronger, but seems to me that the Z80 has lasted the course better (though not as well as the 8080 family which the Z80 was meant to be an improvement on!).