Skip to content

Instantly share code, notes, and snippets.

@mtornwall
Created March 28, 2013 13:01
Show Gist options
  • Save mtornwall/5262940 to your computer and use it in GitHub Desktop.
Save mtornwall/5262940 to your computer and use it in GitHub Desktop.
Fizzbuzz on the PDP-8 I'm not exactly a PDP-8 hacker, so I suspect this code is far from optimal, but it does get the job done.
/ To assemble:
/ $ palbart -l fizzbuzz.pal
/ To run, install the SimH emulator collection and run:
/ $ pdp8
/ sim> load fizzbuzz.bin
/ sim> go 200
*10
X1, 0 / Autoincrement register for TTY printing
*20
DIVSOR, 0 / Divisor for MOD operation
DIVSON, 0 / Negated divisor for MOD operation
DIVDND, 0 / Dividend for MOD operation
NUMBER, -145 / Negation of the current number (-145 oct = -101 dec)
*200
LOOP0, ISZ NUMBER / Done?
JMP CONT / Nope? Continue
HLT / Yep; halt
CONT, CLA / Clear accumulator
TAD (17) / Load the constant 17 oct = 15 dec
DCA DIVSOR / Put it where the MOD procedure can find it
TAD NUMBER / Load the first number
TAD (145) / Normalize (add 101 dec)
JMS MOD / Compute n % 15
TRY15, SZA / Skip next if zero (remainder 0)
JMP TRY3
JMS FIZBUZ / Print FizzBuzz
JMP LOOP0 / Next number
TRY3, CLA / Clear accumulator
TAD (3) / Load the constant 3
DCA DIVSOR / Put it where the MOD procedure can find it
TAD NUMBER / Load the first number
TAD (145) / Normalize (add 101 dec)
JMS MOD / Compute n % 3
SZA / Skip next if zero (remainder 0)
JMP TRY5
JMS FIZZ / Print Fizz
JMP LOOP0 / Next number
TRY5, CLA / Clear accumulator
TAD (5) / Load the constant 5
DCA DIVSOR / Put it where the MOD procedure can find it
TAD NUMBER / Load the first number
TAD (145) / Normalize (add 101 dec)
JMS MOD / Compute n % 5
SZA / Skip next if zero
JMP LOOP0 / Next number
JMS BUZZ / Prints Buzz
JMP LOOP0 / Next number
FIZZ, 0
CLA / Clear accumulator
TAD (FIZZS) / Load Fizz string
JMS TTYPRT / Print to terminal
JMP I FIZZ / Return
BUZZ, 0
CLA / Clear accumulator
TAD (BUZZS) / Load Buzz string
JMS TTYPRT / Print to terminal
JMP I BUZZ / Return
FIZBUZ, 0
CLA / Clear accumulator
TAD (FZBZS) / Load FizzBuzz string
JMS TTYPRT / Print to terminal
JMP I FIZBUZ / Return
FIZZS, "F;"i;"z;"z;12;15;0
FZBZS, "F;"i;"z;"z;"B;"u;"z;"z;12;15;0
BUZZS, "B;"u;"z;"z;12;15;0
TTYPRT, 0
TAD (-1) / Back up pointer to before string start
DCA X1 / Store pointer to autoincrement register
NXTCHR, CLA / Clear AC (redundant first time)
TAD I X1 / AC = M[++X1]
TLS / Print character on TTY
TSF / Skip next if done
JMP .-1 / Repeat until done
SZA / Break if AC = 0
JMP NXTCHR / Next char
JMP I TTYPRT / Return
MOD, 0
DCA DIVDND / Store dividend for now
TAD DIVSOR / Load the divisor
CMA IAC / Negate divisor
DCA DIVSON / Put it back
TAD DIVDND / Get the dividend back
LOOP, TAD DIVSON / Subtract divisor
SMA / Skip next if accumulator < 0
JMP LOOP / Not done yet; continue
TAD DIVSOR / Undo the last subtraction, yielding remainder
JMP I MOD / Return to caller
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment