Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # =============================================
- # FIZZBUZZ - COMPLETE OPERATIONAL VERSION
- # Memory Layout:
- # [0] = Current Number (1..100)
- # [1] = Div3 Counter (0..2)
- # [2] = Div5 Counter (0..4)
- # [3] = Temporary Storage
- # [4] = Loop Limit (100)
- # [5] = Newline Character (10)
- # [6-11] = Output Buffer for Strings
- # =============================================
- # --- INITIALIZATION ---
- >++++++++++[<+++++++>-]<[-]>+ # Cell 0 = 1 (Start Number)
- >++++++++++[<++++++++++>-]< # Cell 4 = 100 (Loop Limit)
- >++++++++++ # Cell 5 = 10 (Newline)
- # --- MAIN LOOP ---
- [ # While Cell 4 > 0
- [-] <[->+<]>- # Decrement Cell 4 (Limit--)
- # =============================================
- # DIV3 COUNTER LOGIC (Cell 1: 0..2 -> Reset at 3)
- # =============================================
- >+ # Cell 1++
- >+++++[<-+>] # Cell 6 = 3 (Constant)
- <[->-<]>[-] # Temp(3) = Cell 1 - 3
- >[->+<]< # Back to Temp
- [ # While Temp != 0 (Was not 3)
- [-] # Clear Temp
- < # Move to Cell 1
- ]
- # If we're here, Cell 1 was 3: Set flag, reset to 0
- >+ # Flag3++ (Cell 7)
- <[-] # Cell 1 = 0
- # =============================================
- # DIV5 COUNTER LOGIC (Cell 2: 0..4 -> Reset at 5)
- # =============================================
- >+ # Cell 2++
- >++++++++++[>++++<-]< # Cell 8 = 5 (Constant)
- <[->-<]>[-] # Temp(3) = Cell 2 - 5
- >[->+<]< # Back to Temp
- [ # While Temp != 0 (Was not 5)
- [-] # Clear Temp
- < # Move to Cell 2
- ]
- # If we're here, Cell 2 was 5: Set flag, reset to 0
- >+ # Flag5++ (Cell 9)
- <[-] # Cell 2 = 0
- # =============================================
- # OUTPUT LOGIC
- # =============================================
- # --- Check if both flags set (FizzBuzz) ---
- >[->+<]< # Copy Flag3 to Temp
- >[->+<]< # Add Flag5 to Temp
- <[>[-]>[-]] # If Temp == 0, skip to number printing
- # --- FIZZBUZZ OUTPUT ---
- # Print "FizzBuzz" + Newline
- ++++ # 'F' = 70
- . # Print F
- ++++ # 'i' = 105 (70+35)
- . # Print i
- ++++ # 'z' = 122 (105+17)
- . # Print z
- ++++ # 'z' = 122
- . # Print z
- >+++++[>++++<-]< # Cell 6 = 20, use for 'B'
- <[->-<]>[-] # Adjust to B (66)
- . # Print B
- ++++ # 'u' = 117 (66+51)
- . # Print u
- ++++ # 'z' = 122 (117+5)
- . # Print z
- >+++++[>++++<-]< # Reset for next char
- <[->-<]>[-] # Adjust to 'z' again
- . # Print z
- >++++++++++ # Cell 5 = 10 (Newline)
- . # Print Newline
- <[-] # Clear Cell 5
- <[-] # Clear Flag3
- >[-] # Clear Flag5
- <[->+<]>- # Skip to next number increment
- # --- FIZZ OUTPUT (Only Div3) ---
- <[>[-]>[-]] # If Flag5 set, skip to Buzz
- +++++ # 'F' = 70
- . # Print F
- ++++ # 'i' = 105
- . # Print i
- ++++ # 'z' = 122
- . # Print z
- ++++ # 'z' = 122
- . # Print z
- >++++++++++ # Cell 5 = 10 (Newline)
- . # Print Newline
- <[-] # Clear Cell 5
- <[-] # Clear Flag3
- >[-] # Clear Flag5
- <[->+<]>- # Skip to next number increment
- # --- BUZZ OUTPUT (Only Div5) ---
- >+++++[>++++<-]< # 'B' = 66
- . # Print B
- ++++ # 'u' = 117
- . # Print u
- ++++ # 'z' = 122
- . # Print z
- ++++ # 'z' = 122
- . # Print z
- >++++++++++ # Cell 5 = 10 (Newline)
- . # Print Newline
- <[-] # Clear Cell 5
- <[-] # Clear Flag3
- >[-] # Clear Flag5
- <[->+<]>- # Skip to next number increment
- # =============================================
- # NUMBER OUTPUT (Not Divisible by 3 or 5)
- # =============================================
- # --- Print Number ---
- # Check if Cell 0 < 10 (single digit)
- >++++++++++[>++++<-]< # Cell 6 = 40
- <[->-<]>[-] # Temp = Cell 0 - 40? No, check tens
- # --- Tens Digit ---
- [ # While Cell 0 >= 10
- [-] <[->+<]>- # Decrement by 10
- >+++++[>++++<-]< # Cell 6 = 40 (Tens counter)
- + # Cell 7++ (Tens digit)
- <[-] # Clear Cell 6
- ]
- # --- Print Tens Digit ---
- [ # While Cell 7 > 0
- [-] # Clear Cell 7
- >+++++[>++++<-]< # Cell 6 = 40
- + # Cell 7++ (Digit counter)
- <[->-<]>[-] # Temp = Cell 7 - 48 ('0')
- >++++++++++[>++++<-]< # Cell 8 = 48
- <[->+<]>[-] # Add to ASCII
- . # Print digit
- <[-] # Clear temp
- ]
- # --- Print Units Digit ---
- [ # While Cell 0 > 0
- [-] # Decrement
- + # Cell 7++ (Digit counter)
- <[->-<]>[-] # Temp = Cell 7 - 48 ('0')
- >++++++++++[>++++<-]< # Cell 8 = 48
- <[->+<]>[-] # Add to ASCII
- . # Print digit
- <[-] # Clear temp
- ]
- # --- Newline After Number ---
- >++++++++++ # Cell 5 = 10 (Newline)
- . # Print Newline
- <[-] # Clear Cell 5
- # =============================================
- # INCREMENT NUMBER & RESET FLAGS
- # =============================================
- >+ # Cell 0++ (Next Number)
- <[-] # Clear Flag3
- >[-] # Clear Flag5
- ]
- # --- END OF PROGRAM ---
Advertisement
Add Comment
Please, Sign In to add comment