FizzBuzz in 6502 assembler

FizzBuzz is an extremely basic competence test for programmers. So I thought I’d write a version extremely in BASIC. Here it is in BBC Basic’s embedded 6502 assembler. For added geek points I used a 6502 emulator written in Perl to develop it on.

Just don’t ask why.

   10 REM FizzBuzz in 6502 assembler
   20 DIM code% 1000
   30 OSWRCH = &FFEE
   40 OSNEWL = &FFE7
   50 work = &70
   60 DIM FizzM 4 : $FizzM = "zziF"
   70 DIM BuzzM 4 : $BuzzM = "zzuB"
   80 FOR pass% = 0 TO 3 STEP 3
   90 P%=code%
  100 [opt pass%
  110
  120 .FizzBuzz  LDA #1
  130            LDX #3
  140            LDY #5
  150 .FB1       SEC
  160            DEX
  170            BNE FB2
  180            JSR Fizz
  190            LDX #3
  200 .FB2       DEY
  210            BNE FB3
  220            JSR Buzz
  230            LDY #5
  240 .FB3       BCC FB4
  250            JSR PrDecimal
  260 .FB4       PHA
  270            JSR OSNEWL
  280            PLA
  290            CLC
  300            ADC #1
  310            CMP #101
  320            BCC FB1
  330            RTS
  340
  350 .Fizz      PHA
  360            LDX #3
  370 .Fizz1     LDA FizzM, X
  380            JSR OSWRCH
  390            DEX
  400            BPL Fizz1
  410            CLC
  420            PLA
  430            RTS
  440
  450 .Buzz      PHA
  460            LDY #3
  470 .Buzz1     LDA BuzzM, Y
  480            JSR OSWRCH
  490            DEY
  500            BPL Buzz1
  510            CLC
  520            PLA
  530            RTS
  540
  550 .PrDecimal STA work
  560            PHA
  570            TXA
  580            PHA
  590            LDA #0
  600            PHA
  610 .PrDec0    LDX #8
  620            LDA #0
  630 .PrDec1    ASL work
  640            ROL A
  650            CMP #10
  660            BCC PrDec2
  670            SBC #10
  680            INC work
  690 .PrDec2    DEX
  700            BNE PrDec1
  710            CLC
  720            ADC #ASC"0"
  730            PHA
  740            LDX work
  750            BNE PrDec0
  760 .PrDec3    PLA
  770            BEQ PrDec4
  780            JSR OSWRCH
  790            JMP PrDec3
  800 .PrDec4    PLA
  810            TAX
  820            PLA
  830            RTS
  840 ]
  850 NEXT

17 Responses to “FizzBuzz in 6502 assembler”

  1. Nick Says:

    An interesting thread you spotted there.

    Working on flight software I’m always amazed when people solve the problem and then waste hours making it smaller/faster/more obfuscated/harder to test & debug.

    “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”

    Brian W. Kernighan

    Still, it would allow me to weed out those programmers who wouldn’t fit into our team.

  2. Andy Says:

    “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”

    Yes, I always liked that quote. It’s only really true if ‘cleverness’ == ‘trick programming’ though. If you use as much cleverness as possible to write code that’s testable, decoupled, modular you’re in good shape.

    I’d be interested to learn how you handle your testing Nick. I’m doing a bit of work on the Perl implementation of the Test Anything Protocol at the moment. TAP started out as Perl’s notation for passing test results to a harness for analysis. We’re trying to popularise its use with other languages at the moment so it’d be useful to find out something about your testing culture.

  3. Nick Says:

    We have use cases that someone manually works through.

    We’re migrating to an XML based method where human readable documents are also the test script.

    BTW we’re the sort of organisation that gets picky about vague requirements so we’d need to be pretty clear whether numbers divisible by five and three should be “fizz-buzz” or “fizzbuzz” - especially as the little detail changes the implementation.

  4. Andy Says:

    XML != human readable - use YAML instead :)

    So you’re not doing unit testing?

  5. Nick Says:

    “XML != human readable”

    It is when used with a style sheet :) That’s how we use it for software configuration/customisation too.

    We don’t do unit testing on this project - which I find kind of freaky - but we do on some of our others.

  6. Nick Says:

    Just out of interest I tried three versions on SunOS5.7 here at work.

    Language:
    Source Code Size:
    Executable Size:
    Time: real -0.0, user 0.0, sys 0.0

    Language: Ada - as small as I could write it
    Source Code Size: 294 bytes
    Executable Size: 1163584 bytes (!) - 571 units included in link closure
    Time: real 0.00, user 0.00, sys 0.00

    Language: C - smallest version I could find on t’internet
    Source Code Size:184 bytes
    Executable Size:4688 bytes
    Time: real -0.00, user 0.00, sys 0.00

    Language: perl - smallest version I could find on t’internet
    Source Code Size:63 bytes
    Executable Size:n/a
    Time: real -0.03, user 0.00, sys 0.03

    One you place value on readability all the source code starts to head towards a similiar size:

    I rewrote the Ada to satisfy RTCA DO-178B and it occupied 439 bytes. K&R C took 290 bytes. Readable Perl just under 200 bytes.

    What is more interesting to me is how the the shortest solutions write “FizzBuzz” explicitly, not using concatenation, so the string operations in perl don’t actually give it an advantage.

    A Perl version using concatenation comes in at 87 bytes, without 63 bytes.

  7. Andy Says:

    I can do 61 bytes in Perl :)

    print$_%3*$_%5?$_:($_%3?Bu:$_%5?Fi:FizzBu).zz,”\n”for 1..100

    Where’s the short one you found Nick?

    And, yes it’s interesting that having a fourth path for ‘FizzBuzz’ is smaller.

  8. Andy Says:

    Sixty bytes actually - it runs without a newline.

  9. Andy Says:

    Actually I can do 55 bytes in Perl:

    print(($_%3?”:Fizz).($_%5?”:Buzz)||$_,”\n”)for 1..100

    And that doesn’t have a fourth route for FizzBuzz.

  10. Andy Says:

    54:

    print+($_%3?”:Fizz).($_%5?”:Buzz)||$_,”\n”for 1..100

  11. Nick Says:

    63 bytes was in one of the comments on your link.

    Your 54 byte solution is more readable than your 61 byte version too.

  12. Andy Says:

    Yeah, I noticed that they were getting more readable. Verbosity != readability :)

  13. Nick Says:

    Yeah, but I could give the 54 byte solution to some guys on military software projects and they wouldn’t be able to tell me what it did. Scary, no?

  14. Andy Says:

    Aye. I guess they have different priorities :)

  15. QuarkBlog » Blog Archive » Resolviendo FizzBuzz Says:

    [...] FizzBuzz resuelto en ensamblador del micro Motorola 6502, porque con 8bits basta [...]

  16. ditdotdat Says:

    I was in such a bad mood this morning and coming across some BBC Micro assembler mnemonics has really cheered me up, thanks. I’m almost considering getting the old Model B out of the loft and trying to get a Token Ring network up and running. Almost.

  17. viz Says:

    PHP comes in at an obese 80 bytes including open and close short php tags(74 without any newlines or open close php tags:

    for($x=1;$x

Leave a Reply

It sounds like SK2 has recently been updated on this blog. But not fully configured. You MUST visit Spam Karma's admin page at least once before letting it filter your comments (chaos may ensue otherwise).

Copyright Andy Armstrong, 2005. Entries (RSS) and Comments (RSS).