assemblyfizzbuzzdcpu-16

Optimizing DCPU-16 FizzBuzz


I tried to implement FizzBuzz in DCPU-16. I use this web emulator: http://mappum.github.com/DCPU-16/ (repository: https://github.com/mappum/DCPU-16).

It stops before the end of the loop. Why?

How can I optimize it? I'm a high level language programmer who don't know anything about asm. Have you some basic resource on this?

More infos on DCPU-16: official specs: http://0x10c.com/doc/dcpu-16.txt - unofficial specs: http://0x10.cc/

The code:

; FizzBuzz in DCPU-16

SET Z, 0x8000
SET I, 1


:loop
    JSR print
    ADD I, 1
    IFN I, 100
        SET PC, loop

    BRK


:print
    ADD Z, 1
    SET [Z], 10

    SET A, I
    MOD A, 3
    IFE A, 0
        JSR printFizz

    SET B, I
    MOD B, 5
    IFE B, 0
        JSR printBuzz

    SET C, A
    IFG B, A
        SET C, B
    ADD A, B
    IFG A, C
        JSR printNumber

    SET PC, POP


:printNumber
    SET A, I

    IFG I, 9
        DIV A, 10

    JSR printDigit

    IFG 10, I
        SET PC, POP

    SET A, I
    MOD A, 10

    JSR printDigit
    SET PC, POP

:printDigit
    ADD A, 48
    ADD Z, 1
    SET [Z], A
    SET PC, POP

:printFizz
    ADD Z, 1
    SET [Z], 70
    ADD Z, 1
    SET [Z], 105
    ADD Z, 1
    SET [Z], 122
    ADD Z, 1
    SET [Z], 122
    SET PC, POP

:printBuzz
    ADD Z, 1
    SET [Z], 66
    ADD Z, 1
    SET [Z], 117
    ADD Z, 1
    SET [Z], 122
    ADD Z, 1
    SET [Z], 122
    SET PC, POP

Solution

  • The console space on that emulator is only 0x8000 - 0x8180.

    Looks like you're hitting the end of the console, so your output cuts off mid-letter and you end up writing to unrelated memory.

    If you look in the debugger: the register I is 0x64 at compeletion, which is 100 in base-10 (so your loop finished).