; Compile and execute with: ; $ cat > main.c << EOF ; int asm_main(); ; int main() { return asm_main(); } ; EOF ; $ nasm -f elf fizzbuzz.asm && gcc -m32 -Wall -o fizzbuzz main.c fizzbuzz.o && ./fizzbuzz ; %define MAX_ITER 100 segment .data Fizz db "Fizz", 0 Buzz db "Buzz", 0 segment .text global asm_main asm_main: enter 0,0 push ebx mov ecx, MAX_ITER ; initialize loop counter dec ecx ; skip zero iteration Fizz_Loop: mov eax, MAX_ITER sub eax, ecx ; current iteration: MAX_ITER - loop count xor ebx, ebx ; clear ebx xor edx, edx ; clear edx push ebx mov ebx, 3 div ebx ; edx = edx:eax % 3 pop ebx cmp edx, 0 jne No_Fizz inc ebx ; indicate Fizz usage mov eax, Fizz call print_string No_Fizz: mov eax, MAX_ITER sub eax, ecx ; current iteration: MAX_ITER - loop count xor edx, edx ; clear edx push ebx mov ebx, 5 div ebx ; edx = edx:eax % 5 pop ebx cmp edx, 0 jne No_Buzz inc ebx ; indicate Buzz usage mov eax, Buzz call print_string No_Buzz: cmp ebx, 0 jne Got_Fizzed_or_Buzzed mov eax, MAX_ITER sub eax, ecx ; current iteration: MAX_ITER - loop count call print_int Got_Fizzed_or_Buzzed: call print_nl loop Fizz_Loop mov eax, 0 pop ebx leave ret ; ; Various output helpers using C library printf, putchar functions ; %define NL 10 ; 10 == ASCII code for \n segment .data int_format db "%i", 0 string_format db "%s", 0 segment .text extern printf, putchar print_int: enter 0,0 pusha pushf push eax push dword int_format call printf pop ecx pop ecx popf popa leave ret print_nl: enter 0,0 pusha pushf push NL call putchar pop ecx popf popa leave ret print_string: enter 0,0 pusha pushf push eax push dword string_format call printf pop ecx pop ecx popf popa leave ret