-
-
Save marnixk/3175ce68cf26d5636974162d209539d4 to your computer and use it in GitHub Desktop.
FizzBuzz in Assembler (750 bytes compiled)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .model tiny | |
| .stack 100h | |
| .data | |
| number db '00', 13, 10, '$' | |
| newline db 13, 10, '$' | |
| fizz db 'Fizz.', 13, 10, '$' | |
| buzz db 'Buzz.', 13, 10, '$' | |
| fizzbuzz db 'FizzBuzz.', 13, 10, '$' | |
| .code | |
| mov ax, @data | |
| mov ds, ax | |
| mov es, ax | |
| ; initialise counters | |
| mov ax, 15 | |
| mov bx, 5 | |
| mov cx, 3 | |
| mov dx, 1 | |
| start_loop: | |
| ; decrease counters | |
| dec ax | |
| dec bx | |
| dec cx | |
| ; fizz buzz hit 0? print | |
| cmp ax, 0 | |
| jne test2 | |
| jmp prt_fizzbuzz | |
| test2: | |
| ; buzz hit 0? print | |
| cmp bx, 0 | |
| jne test3 | |
| jmp prt_buzz | |
| test3: | |
| ; fizz hit 0? print | |
| cmp cx, 0 | |
| jne print_number | |
| jmp prt_fizz | |
| print_number: | |
| mov di, offset number | |
| ; larger than 10? print two digits | |
| cmp dx, 10 | |
| jge two_digits | |
| ; otherwise add ' ' and '0'+dl | |
| push ax | |
| mov al, ' ' | |
| stosb | |
| mov al, dl | |
| add al, '0' | |
| stosb | |
| pop ax | |
| jmp print_now | |
| two_digits: | |
| push bx | |
| push ax | |
| ; first digit | |
| mov ax, dx | |
| mov bl, 10 | |
| div bl | |
| add al, '0' | |
| stosb | |
| mov al, ah | |
| add al, '0' | |
| stosb | |
| pop ax | |
| pop bx | |
| print_now: | |
| push ax | |
| push dx | |
| mov dx, offset number | |
| mov al, 24h | |
| mov ah, 09h | |
| int 21h | |
| pop dx | |
| pop ax | |
| ; pop cx | |
| ; pop bx | |
| ; pop ax | |
| ; after printing we end up here | |
| continue_loop: | |
| ; try to reset counters if required | |
| cmp ax, 0 | |
| jne cont2 | |
| mov ax, 15 | |
| cont2: | |
| cmp bx, 0 | |
| jne cont3 | |
| mov bx, 5 | |
| cont3: | |
| cmp cx, 0 | |
| jne cont4 | |
| mov cx, 3 | |
| cont4: | |
| ; increase overall counter (0 -> 100) | |
| inc dx | |
| ; < 101 goto loop | |
| cmp dx, 100 | |
| jne start_loop | |
| ; terminate | |
| mov ah, 4ch | |
| int 21h | |
| prt_fizz: | |
| push dx | |
| push ax | |
| mov dx, offset fizz | |
| mov al, 24h | |
| mov ah, 09h | |
| int 21h | |
| pop ax | |
| pop dx | |
| jmp continue_loop | |
| prt_buzz: | |
| push dx | |
| push ax | |
| mov dx, offset buzz | |
| mov al, 24h | |
| mov ah, 09h | |
| int 21h | |
| pop ax | |
| pop dx | |
| jmp continue_loop | |
| prt_fizzbuzz: | |
| push dx | |
| push ax | |
| mov dx, offset fizzbuzz | |
| mov al, 24h | |
| mov ah, 09h | |
| int 21h | |
| pop ax | |
| pop dx | |
| jmp continue_loop | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment