Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
DEAL: For $25 - Add A Second Phone Number To Your Smartphone for life! Use promo code SLASHDOT25. Also, Slashdot's Facebook page has a chat bot now. Message it for stories and more. Check out the new SourceForge HTML5 internet speed test! ×
Programming Hardware IT Technology

6502 Machine Language for Beginners 106

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."
This discussion has been archived. No new comments can be posted.

6502 Machine Language for Beginners

Comments Filter:
  • I own this book (Score:3, Interesting)

    by jvmatthe ( 116058 ) on Sunday March 16, 2003 @03:40PM (#5524585) Homepage
    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.
    • "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.


      Of course, depending on the design of the refrigerator, couldn't he be correct? Sure, you'd need a serial interface, but...
  • The 6502 doesn't have an ADD instruction.
    • Yes, it does; it's called ADC if I remember correctly.
      • But the Amiga had an HCF instruction. Halt and Catch Fire.
      • by mivok ( 621790 )
        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)
        • by Anonymous Coward
          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...
          • Heh.. I've forgotten all about 8-bit architectures (and never knew anything about x86 anyway, which I'd assume is the same idea) - after most recently been readin about ARM assembly it just never occurred to me that it would be more costly than adding one more opcode to the instruction set.
        • What's so weird about preserving the carry bit for the next instruction?
          example: calculating a hash code

          magicNum=(a1+a2+...+a99+a100) mod r1

          adc seems almost ideally suited, especially if r1 = 256
          (in all fairness, my instructors tell me that hash codes are generally implemented as multiplications, so this _could_ be a bit of a stretch!)

          K:|
          • I meant it would be some weird programming that depended on using add (thereby preserving the carry bit from the previous instruction for the next one), as opposed to adc.
            Of course, as soon as I mentioned weird I had a feeling that it had probably already been done, and is probably even common.
      • by Polo ( 30659 )
        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.
        • by Anonymous Coward
          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
          • by Polo ( 30659 )
            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. ;)
            • Interestingly, I think the RISC processor concept was actually based on the 6502. IRC the designers wondered how a processor with 1/8 the clock rate could run the same speed as the Z80. The answer was the low number of clock cycles per instruction, which in turn was related to the simple instructions. RISC just takes this concept to an extreme.
    • The 6502 doesn't have an ADD instruction.

      Ah well, at least it won't get distracted as easily.
      ---
    • by Dahan ( 130247 )
      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 [brighton.ac.uk] didn't have hardware addition. You had to load in a lookup table that contained the answers to "2+2", "2+3", etc...

      • But ADC and ADD are slightly different.

        Still, I'm more impressed with a few others. MIPS has no MOVE instruction in hardware, although it does in the assembly language. Early computers often had a subtract, but no add. Hardware was too expensive, so to add you simply subtracted a negative number.
        • MIPS, being a RISC processor, wouldn't have a MOVE instruction. It does have the normal load and store, though, IIRC.

          You just LOAD one register with the contents of another.
          • You can't do "lw $1 $2" to copy from one reg to another either. You have to do "add $1 $2 $zero". Nertheless,m the assembly language specification does give you the move instruction, and simply substitutes it with the add.

            Well, I thought it was a little odd. Sensible, but odd.
  • by Anonymous Coward on Sunday March 16, 2003 @04:33PM (#5524796)
    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.
    • by Anonymous Coward
      On the other hand, it's hardly a good tool for learning how to design computers that work today. While you can draw a number of parallels between an 8-bit microprocessor and a 64-bit microprocessor, the design challenge of a 64-bit microprocessor is so much greater than the 8-bit, you absolutely must use higher level design techniques, while you could conceivably get away with hand designing an entire 8-bit processor (as was, in fact, done).

      Incidentally, if you want a real learning experience, why not a 4
      • Are we talking about designing software to run on such a processor, or designing such a processor itself? While I agree that designing a modern processor is much more complex, designing software that runs on such a thing seems quite similar, to me at least.
    • Teaching tools (Score:3, Interesting)

      by Paranoid ( 12863 )
      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, whic
    • I think 8-bit processors like the 6502 aren't the greatest learning tools because the instruction sets are somewhat limited and full of idiosyncrasies that will make inexperienced programmers pull out their hair. I'd go for a 16-bit or 32-bit processor with a nice clean instruction set, e.g. 68000, PDP-11, VAX. There are probably newer processors that fit that description too, but I haven't done any serious assembly programming in 10 years.

      I'd also go for a very simple computer to start with, something

    • Good emulators - such as Appler, Dapple ][, AppleWin and most Commode 64 emulators ;) - also allow you to load programs into memory from the local disk (instead of just from within the emulation). (All of these emulate 6502-class CPUs)

      Dapple ][ comes with a simple 65SC02 debugger which offers dump-to-file and local-file-load capability; this can be tied together with any assembler and disassembler, if you have a header-attachment tool (Dapple's BIN2PG2 works well). I personally use this - partially my ha
  • by Tuxinatorium ( 463682 ) on Sunday March 16, 2003 @04:48PM (#5524845) Homepage
    Yeah, that ranks right up there with sorting my sock drawer or beating my head against the window.

    • 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 i
      • Yes, but the 6510 had this cute thing called "zero-page" which meant you had quite a bit of scratch space if you knew what you were doing. It was faster than doing full 16-bit address operations.

        3 registers isn't nearly as bad as you think.
        • This idea of zero-page memory addressing was really smart. This gave you 256 semi-registers. This lead to very compact machine code. I liked the design of the 6502 better than the Z80 another 8-bit processor.
          • The Z80 did have an abundance of registers and the block moves were quite useful and fast.

            And the hidden instructions! when you found you could treat IX and IY as 8-bit registers (XH, XL, YH, YL) made for some fast sprite routines.

            Oh, and the alternate register set was very useful when writing little interrupt handlers - you could just swap register sets to save push/pop'ing everything..
            • Without wanting to start a flame war, but it seems that the 6502 was more of a RISC type of processor (with respect to the instruction set), whereas the Z80 was more SISC like, with all kinds of instructions for special things. I liked the rather orthogonal design of the 6502 instruction set. It was good for making compilers. See also www.cc65.org
              • See also www.cc65.org

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

                In this [cc65.org] 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 softwa
  • Contiki (Score:2, Informative)

    by Frans Faase ( 648933 )
    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 [dunkels.com].
  • 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?"
  • by TheWanderingHermit ( 513872 ) on Sunday March 16, 2003 @08:54PM (#5525904)
    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.
  • A little twang of nostalgy, remembering learning 6502 assembler on an Oric-1 back in 1981 when I was 12... The Basic was so slow and I couldn't wrap my brain around the stupid Forth.

    How did the 6502 evolve ? Does it still have some descendants that can use the same machine code ?

    • by Anonymous Coward
      There is a backwards compatible 16 bit version, the 65816. It was made by Western Design Center. The Apple IIgs had one running at 2.3 MHz. There also was a 20MHz upgrade cartridge for the C64 (SuperCPU), and the SNES used a lower power version, the 65c816, at 2.8 MHz.

      http://www.westerndesigncenter.com/ch816S.html
      • also I believe that that part of the architecture
        was incorporated into the original 68k chips.Could be wrong though? the 68k's eventually
        went out of style for desktops though they are still used in embedded communications as well as
        being the basis for the dragonball line (used in cheap palms, royal pdas')I really don't know wether the assembly works from the 6502/65816 though?
        • also I believe that that part of the architecture was incorporated into the original 68k chips.Could be wrong though?

          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 [wikipedia.org] end

    • 65C816 used in the Super NES and Apple IIgs. Still sold. CPU speed up to 20 MHz.

      -uso.
  • 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 w
    • In hindsight -- why didn't you just load Integer BASIC and use the mini-assembler? I used that MANY times when troubleshooting code or writing a small program/routine. It didn't handle names/labels for addresses, but it did convert the instructions to hex.

      Or did your model come with Intege BASIC?
      • 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!
        • I don't remember exactly. It's been SOOO long since I used it.

          I think, once Integer BASIC is loaded into the language card, you do a CALL -151, and I think a simple exclamation point does it -- like !.

          I'm not sure, though, it's been just too long and I only used it for trouble shooting or writing quick code. There aren't any load/save functions -- you'll have to use BSAVE (is that the right command -- again, I've forgotten some of these) and you can't use labels -- you have to specify addresses directly
      • Didn't have integer basic at the start - though it was mentioned in the book too.

        Clone had 64K of RAM, but by the time I found some software which actually loaded integer basic into the shadow ram (d000-FFFF), I could convert plenty of instructions to hex by myself ;).

        Still remember a number of them and the cycle times too.
  • 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! [nvg.ntnu.no] site. For my money, you couldn;t get a better star


  • This brings back so many memories of 6502 vs. Z80 arguments as vicious as any Linux vs. Windows debate, or Betamax vs. VHS, or .... Food for thought.
  • 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.

    • Where to find an assembler?

      Back in my day, we hand assembled. Actually I did hand-assemble the first month or two. Most of my programs were fairly small. I picked up Buddy / Pal assembler and it changed things.

      Hand assembling gives you a much better understanding of how the code actually works and allows you to understand:

      LDA #$01
      BIT 169*256+0

      which would allow the following:

      LDA #$01

      LDA #$00

      depending on where you jumped.
    • Damn! I knew how were to buy assembler, but didn't have the princly sum of $199 to spend on it. I coded by hand. I never made the machine do much in ML, but it made me understand computers from a whole different perspective.

      Then, as a co-op student at NASA I ran into a mathemetician/programmer on a laser-ranging project who did a lot of ML coding by hand in order to squeeze every last byte out of the legacy systems' memory. His favorite story was about a college grad who came in looking for a job. When th
      • My first job out of college was with a group that did some hard-core transaction processing stuff on IBM System 390s. They did a lot of work in assembly because, "You can't really trust a compiler." I didn't stay in the group very long though so I never had to do any of it myself.

        I just wish I had run into this book in 1984...

  • by CTalkobt ( 81900 ) on Monday March 17, 2003 @11:06AM (#5528867) Homepage
    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).
    • "Bonus question: How can you disable the NMI interrupt on the 6502? (Yes, there is a way)."

      Jumper a wire on the motherboard. IIRC it was NMI to ground or something like that. On the old Apple II systems, that would dump any program to assembler and from there you could do things like copy the uncopyable, etc.
      • Actually, I meant without any hardware modifications - there was a way to do this in software.
        • Okay - I got tired of looking back at this story to see if anybody came up with the answer.

          To disable the NMI interrupt on a 6502 without any hardware modifications do the following:

          1) Trigger an NMI interrupt.
          2) Within the NMI interrupt _do_not_ execute a RTI, instead pop off the contents pushed on the stack when the interrupt occurred and go along your merry way.

          Since an NMI can't occur in the middle of an NMI - you're effectively having one long NMI interrupt and the others are disabled.
    • Didn't the C64 use a 6510, not a 6502?
  • by blancolioni ( 147353 ) on Tuesday March 18, 2003 @04:32AM (#5535211) Homepage
    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 [cmu.edu] 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 ....
  • Back in the day (Score:2, Insightful)

    by pork_spies ( 659663 )
    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!).
  • 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.

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

    (BTW, I think the
  • 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.

Your fault -- core dumped

Working...