Slashdot Mirror


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

24 of 106 comments (clear)

  1. I own this book by jvmatthe · · Score: 2, Interesting

    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.

  2. Re:Interesting fact... by WolfWithoutAClause · · Score: 2, Informative

    Yes, it does; it's called ADC if I remember correctly.

    --

    -WolfWithoutAClause

    "Gravity is only a theory, not a fact!"
  3. Re:Interesting fact... by PD · · Score: 2, Funny

    But the Amiga had an HCF instruction. Halt and Catch Fire.

  4. Understanding computers by Anonymous Coward · · Score: 4, Insightful

    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.

  5. Re:Interesting fact... by mivok · · Score: 2, Interesting

    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)

  6. Learning old machine languages???? by Tuxinatorium · · Score: 2, Funny

    Yeah, that ranks right up there with sorting my sock drawer or beating my head against the window.

    1. Re:Learning old machine languages???? by Escaholic · · Score: 2, Interesting


      I must be a weirdo, cos I actually find it interesting and quite amusing. I spent my entire childhood on a PC (my dad brought one home in 1983), all my friends back then had Vic-20's, C-64's or Spectrums.

      I find it particularly amazing to see how much instructions sets have progressed. I really do like the ARM instruction set (but how will they make a superscalar ARM CPU with conditional instructions?), the PowerPC is pretty damn good too. MIPS is bearable, but the x86 set with it's ridiculous 8 registers is a horrible patched-then-repatched abortion that should be put to sleep asap!

      Then again, 8 registers is bliss in comparison to the 3 I just found out the 6502 has! :-)

    2. Re:Learning old machine languages???? by blancolioni · · Score: 2, Insightful

      See also www.cc65.org

      Now I shall demonstrate why Ada is a better choice for this sort of thing.

      In this bit of documentation, it is suggested that you get better code by putting an extra cast into an expression which keeps everything nicely eight bitted. The C language definition AFAIK requires that the compiler behave in this (odd) way.

      The Ada equivalent is this:

      A : Octet;
      ...

      if (A and 15) = 0 then

      You don't have to remember to cast, and it's all 8 bit. Ha!

      In the future, all new Commodore 64 software will be written in Ada :-)

  7. Re:Interesting fact... by Frans+Faase · · Score: 2, Informative

    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.

  8. Contiki by Frans+Faase · · Score: 2, Informative

    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.

  9. Re:Interesting fact... by rasteri · · Score: 3, Funny
    The 6502 doesn't have an ADD instruction.

    Ah well, at least it won't get distracted as easily.
    ---
  10. Re:Interesting fact... by Anonymous Coward · · Score: 2, Informative

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

  11. Re:Interesting fact... by Polo · · Score: 2, Interesting

    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.

  12. Re:Interesting fact... by Dahan · · Score: 2, Interesting
    I don't think it's important what the instruction is named... sure, the 6502 doesn't have an instruction named "ADD", but it has add and subtract instructions--"ADC" and "SBC". It's like complaining the 80x86 doesn't have a "LDA" instruction... how are you gonna load your A register without LDA??? Oh, MOV AX... never mind.

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

  13. Those were the days -- Like the first NCC-1701 by TheWanderingHermit · · Score: 3, Interesting

    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.

    I learned almost everything I know about computers from my Apple //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.

    Just like Picard and the Stargazer, I often wish I were programming on my old //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.

  14. Re:Interesting fact... by Dahan · · Score: 2, Informative

    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.

  15. Re:Interesting fact... by Anonymous Coward · · Score: 2, Insightful

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

  16. Teaching tools by Paranoid · · Score: 2, Interesting

    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.
  17. Re:Interesting fact... by Polo · · Score: 2, Interesting

    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. ;)

  18. Re:Two questions? by archeopterix · · Score: 2, Informative
    Is learning one machine language equivilent enough that you can learn them all easily enough?
    Mu.

    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.

  19. Oh sure, now you tell me... by John+Harrison · · Score: 2, Funny
    When I was in 4th and 5th grade I REALLY wanted to learn machine language for the C64. But I could never find an assembler. I had no idea how people found an assembler in order to write ML programs. Now I find out that I could have bought this book and typed in the assembler in the back that was written in BASIC. At this point that isn't helpful information, it is simply frustrating.

    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.

  20. 6502 as a RISK / C64 comments by CTalkobt · · Score: 2, Insightful

    I've always considered the 6502, more or less, a RISC processor due to it's uh, "simpleness".

    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 __, .X is __, .Y is __ and the carry flag is set and the Z flag is off were my normal thoughts while writing assembly.

    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.
  21. Undocumented Instructions still undocumented? by blancolioni · · Score: 2, Interesting

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

  22. Back in the day by pork_spies · · Score: 2, Insightful

    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!).