section .data
    fizz_str: db "fizz"  ; string for "fizz"
    buzz_str: db "buzz"  ; string for "buzz"
    newline: db 10       ; ascii value for newline
    count_to: equ 100    ; count to 100

section .bss
    num_buffer: resb 3  ; buffer for number

section .text
    global _start
_start:
    xor r8, r8  ; initialize counter to 0

main_loop:
    inc r8          ; increment counter
    xor r9, r9      ; initialize flag to 0
    mov rdx, 0      ; clear remainder register
    mov rax, r8     ; copy counter to dividend register
    mov rcx, 3      ; divisor for fizz
    div rcx         ; divide by 3
    cmp rdx, 0      ; check if remainder is 0
    jnz check_buzz  ; if not, jump to check_buzz
    or r9, 1        ; if yes, set flag to 1

check_buzz:
    mov rdx, 0          ; clear remainder register
    mov rax, r8         ; copy counter to dividend register
    mov rcx, 5          ; divisor for buzz
    div rcx             ; divide by 5
    cmp rdx, 0          ; check if remainder is 0
    jnz process_number  ; if not, jump to process_number
    or r9, 2            ; if yes, set flag to 2

process_number:
    cmp r9, 0          ; check if flag is 0
    jnz process_fizz   ; if not, jump to process_fizz
    mov rax, r8        ; copy counter to dividend register
    call print_number  ; call print_number
    jmp check_end      ; jump to check_end

process_fizz:
    cmp r9, 1         ; check if flag is 1
    jnz process_buzz  ; if not, jump to process_buzz
    call print_fizz   ; call print_fizz
    jmp check_end     ; jump to check_end

process_buzz:
    cmp r9, 2             ; check if flag is 2
    jnz process_fizzbuzz  ; if not, jump to process_fizzbuzz
    call print_buzz       ; call print_buzz
    jmp check_end         ; jump to check_end

process_fizzbuzz:
    call print_fizzbuzz  ; call print_fizzbuzz

check_end:
    cmp r8, count_to   ; check if counter reached count_to
    je exit_program    ; if yes, jump to exit_program
    jmp main_loop      ; if not, jump to main_loop

print_fizzbuzz:
    mov rbx, 1      ; set flag to 1 to indicate fizzbuzz
    jmp print_fizz  ; jump to print_fizz

print_fizz:
    xor rbx, rbx  ; clear flag

print_fizz_continue:
    mov rax, 1         ; syscall number for write
    mov rdi, 1         ; file descriptor 1 is stdout
    mov rsi, fizz_str  ; pointer to fizz_str
    mov rdx, 4         ; string length
    syscall            ; call kernel
    cmp r9, 3          ; check if flag is 3 (fizzbuzz)
    jne print_newline  ; if not, jump to print_newline
    jmp print_buzz     ; if yes, jump to print_buzz

print_buzz:
    mov rax, 1         ; syscall number for write
    mov rdi, 1         ; file descriptor 1 is stdout
    mov rsi, buzz_str  ; pointer to buzz_str
    mov rdx, 4         ; string length
    syscall            ; call kernel
    jmp print_newline  ; jump to print_newline

print_newline:
    mov rax, 1        ; syscall number for write
    mov rdi, 1        ; file descriptor 1 is stdout
    mov rsi, newline  ; pointer to newline
    mov rdx, 1        ; string length
    syscall           ; call kernel
    ret               ; return from subroutine

print_number:
    mov r9, 2  ; set flag to 2

convert_to_ascii:
    mov rdx, 0               ; clear remainder register
    mov rcx, 10              ; divisor for conversion
    div rcx                  ; divide
    add rdx, 0x30            ; convert to ASCII
    mov [num_buffer+r9], dl  ; store in buffer
    dec r9                   ; decrement flag
    cmp rax, 0               ; check if dividend is 0
    jnz convert_to_ascii     ; if not, jump to convert_to_ascii
    mov rax, 1               ; syscall number for write
    mov rdi, 1               ; file descriptor 1 is stdout
    mov rsi, num_buffer      ; pointer to num_buffer
    mov rdx, 3               ; string length
    syscall                  ; call kernel
    call print_newline       ; call print_newline
    ret                      ; return from subroutine

exit_program:
    mov rax, 60   ; syscall number for exit
    xor rdi, rdi  ; exit code 0
    syscall       ; call kernel
 
by

Assembly Online Compiler

Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly and start coding.

About Assembly

Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.

Every assembler may have it's own assembly language designed for a specific computers or an operating system.

Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

Loops are used to iterate a set of statements for a specific number of times.

mov ECX,n
L1:
;<loop body>
loop L1

where n specifies the no of times loops should iterate.

Procedures

Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

procedure_name:
   ;procedure body
   ret