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
The 6502 doesn't have an ADD instruction.
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.
Yeah, that ranks right up there with sorting my sock drawer or beating my head against the window.
Repeal the DMCA!
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.
One of the best programmers I know personally, considered this book irrelevant, even when it was still relevant.
"The ability to program the 6502 is innate. It only has three registers. How hard can it be?"
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
How did the 6502 evolve ? Does it still have some descendants that can use the same machine code ?
Non-Linux Penguins ?
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.
First is: how relavent is this to modern machine languages? I studied some MIPS assembly in college but I'm not sure if that's even still being used. Not sure how much it's going to be used in the new Itanium chips or in my next cell phone. Is learning one machine language equivilent enough that you can learn them all easily enough?
Second question is, are there any good emulators and programs for this available to accompany the book?
Hope someone here can shed some light on these, thanks.
When I got my apple II+ clone about 2 decades ago, it came with a book that was 3 books stuck together. I think the taiwanese clone company lumped 3 useful books together.
There was a section about BASIC. A section about the Apple II.
It had all sorts of technical info on it - the IO addresses of the Apple II (keyboard, sound, graphics, disk, etc)and what they did, a list of 6502 opcodes, and even how many cycles they take (which doesn't seem to be in the online book being discussed). The number of cycles was useful when I was modifying the DOS disk code for my purposes.
From that book and the Apple II clone, I learnt to write 6502 machine language programs without an assembler - just type the hex in. After all there wasn't a built-in assembler. Also modifed a few games - added features even.
Unfortunately I don't know where that tattered book is now. It was pretty good - I don't recall it having many if any mistakes in the technical info.
Basically I honed the very useful skill of RTFM from that book at an early age, because the manual was indeed really fine.
IMHO, the ultimate 6502 based home computer had to be the BBC Micro; those of us educated in the UK during the 80's will almost certainly remember these ubiquitous machines sitting in virtally every computer lab in every school up and down the country.
One of the great things about this system was that it's BASIC interpreter contained a full 6502 assembler, and they produced some excellent documentation. Check out the Advanced User Guide from The BBC Lives! site. For my money, you couldn;t get a better start to 6502 development.
Life is like a sewer; what you get out of it depends on what you put into it...
This brings back so many memories of 6502 vs. Z80 arguments as vicious as any Linux vs. Windows debate, or Betamax vs. VHS, or
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.
Where have you gone, Joe Dimaggio?
The nation turns its lonely its lonely eyes to you.
Anyway, Jim was the 6502 guru who disassembled the C64 and PET ROMs and started a million kids on their path to programming fame and fortune. Where is he at these days?
First you trigger the NMI, and then you run the rest of the program inside the NMI handler.
Actually, you're confusing a few there. The 6502 was from MOS Technologies. The 6800 series, on the other hand, was from Motorola. That is the line that was the little brother to the 68k series.
MOS Technolgies was mainly former Motorola people from the 68k group, so it looked a little similar. However after the problems with their 6501, they did stay different enough.
And the 6809 ended up being a very nice microprocessor, with fewer of the 6502 limitations (16-bit addressing, two stack pointers, more generic opcodes with fewer register limitations), and being closer to the 68k in use. That is, going from the 6809 to the 68000 was much easier than from the 6502 to the 68000.
Theres an assembler with interger basic?! Please tell me how to start/utilize it...is it a keyword like INITIALIZE, LOAD, SAVE ? or am i thinking of DOS 3.1 ? shoot me a link!
GENERATION 26: The first time you see this, copy it into your sig on any forum and add 1 to the generation.
How could that be?<br> := 0
;
<pre>
;start psuedocode
; get binary # A
; get binary # b
; sum
; while b != 0
; if (right bit of b on)
; add A to sum
; shift A one bit left
; shift b one bit right
; output sum
;might want to check flags before trusting result
;finish</pre>
start real code<pre>
lod 0, sp, r1 ; Grab operands from stack
lod -1, sp, r2
mov r0, r3 ; Initialize result to zero
or r2, r0, r2 ; check if we're at zero to begin with
startMultiply
bz doneMultiplying
and r2, 1, r0
bz nextColumn ; if the last bit of multiplier is 1,
add r1, r3,r3 ; add 'product', (check for overflow too)
bc overflowed
nextColumn ; move one column over
shl r1, 1 ; shr moght set zero flag
shr r2, 1 ; so we don't have to repeat 'or r2, r0, r2'
br startMultiply ; (saves one step every time through the loop)
united states nuclear device terrorist bioweapon encryption cocaine korea syria iran iraq columbia cuba
[damn twitchy submit finger sorry]
1 82/barker/
:= 0 ;might want to check flags before trusting result
;
Am I missing something about the 6502 set? How could that take so horking long with only 8 bits?
hopefully this bit from the (16 bit) craps virtual assembly language won't be _too_ obtuse:
http://www.cs.camosun.bc.ca/courses/comp
(this is my code, not my instructors. mens culpa.)
;start psuedocode
; get binary # A
; get binary # b
; sum
; while b != 0 {
; if (right bit of b on)
; add A to sum
; shift A one bit left
; shift b one bit right
; }
; output sum
;
;finish</pre>
;start real code
lod 0, sp, r1 ; Grab operands from stack
lod -1, sp, r2
mov r0, r3 ; Initialize result to zero
or r2, r0, r2 ; check if we're at zero to begin with
startMultiply
bz doneMultiplying
and r2, 1, r0
bz nextColumn ; if the last bit of multiplier is 1,
add r1, r3,r3 ; add 'product', (check for overflow too)
bc overflowed
nextColumn ; move one column over
shl r1, 1 ; shr moght set zero flag
shr r2, 1 ; so we don't have to repeat 'or r2, r0, r2'
br startMultiply ; (saves one step every time through the loop)
overflowed
; set err flags
doneMultiplying
worst case for 8 bit: 4 + (8*8) = 68 (except you're gonna have an overflow)
best case: 4 + 1 = 5
average: 4 + (4*6) + (4*8) = 60
--
compsci finals coming up, why should i moderate?
united states nuclear device terrorist bioweapon encryption cocaine korea syria iran iraq columbia cuba
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
But IRC the inner loop of the multiply needed something like 5 or 6 instructions, and each instruction takes 2-5 cycles to execute. Worst case you need to go around the loop up to 8 times; so it's easy to add up to over a hundred cycles.
-WolfWithoutAClause
"Gravity is only a theory, not a fact!"The 6502 processor is one of the most challanging to program. You have limited memory, limit cyles before you screen sync falls apart. I person am programming demos for the Atari 2600 and that use a 6507. Which is just a cost-reduced 18 pin version, same instruction set. The limitation on the processor lend you to write faster and more optimized code.
Early processors didn't have ROR.
"In any event, a talent for conserving bytes, like skill at trapping wild game, will likely become a victim of technology. It will always be a skill, but it seems as if it will not be an everyday necessity."
10-4
I sit corrected and edified. I forgot about the multiple-cycle instructions.
united states nuclear device terrorist bioweapon encryption cocaine korea syria iran iraq columbia cuba
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!).
it doesn't hurt nothing.
Learning 6502 was my first foray into assembly language programming in 1988/89 on the BBC Micro. It was so basic that even I could understand it at the time as a youngster.
;DEFINE LABLE "TWO" AS 2. I do think this book is way too Atari specific though. I had a 6502 book that was platform independent.
:-))
The 6502 was beautiful in its simplicity, and I have to second the comments above about it almost bieng like a RISC chip.
And after all this time, there's still a spelling error in the book:
(BTW, I think the NES also used the 6502. So this book should be handy for people coding NES games for the emulators
mogorific carpentry experiments
I stole a Comodore 64 from my grandma back in like 4th grade, God bless it, that got me into coding... gotta love Eliza programs.
>> Of course, depending on the design of the refrigerator, couldn't he be correct? Sure, you'd need a serial interface, but...
:)
More importantly, does the fridge speak 'Bocce'??
I just found this article on slashdot ( havent been here for a while ) and was most surprised by the fact that no one mentioned a few things about the 6502 that are really worth mentioning. Heck, this message could become a new topic/article itself. Follow the list:
1) The 6502 is still manufactured by a company called Western Design Center ( http://www.westerndesigncenter.com ) . They even produce a 6502 based microcontroller, as well as the 16 bit processor used in the Apple 2 GS ( on microcontroller version too ).
2) The 6502 is a dream microprocessor for people trying to learn digital electronics. There are lots of people designing home made computers based on this chip, and it is a great self-learning project. http://www.6502.org is the place to go to get information on this subject. Some of them are very easy to build and there is lots of software available. People are also building IDE interfaces, PC keyboard adaptors, etc.
3) My preferred project can be found on http://65c02.tripod.com . It is built using a really cool program to layout printed circuit boards, that you can design on your PC and order through Internet. Check it out.
I guess this is the kind of information that should get the slashdot crowd really excited...