Skip to content

Fizzbuzz implementation in x86 Assembly(NASM, Linux, 32bit) #37

Description

@thisisanshgupta

Language

Assembly

Notes

section .data
prompt db "Enter a number: "
prompt_len equ $ - prompt

fizz db "Fizz", 10
fizz_len equ $ - fizz

buzz db "Buzz", 10
buzz_len equ $ - buzz

fizzbuzz db "FizzBuzz", 10
fizzbuzz_len equ $ - fizzbuzz

notfb db "Not Fizz/Buzz", 10
notfb_len equ $ - notfb

section .bss
input resb 16 ; buffer for input

section .text
global _start

_start:
; print prompt
mov eax, 4
mov ebx, 1
mov ecx, prompt
mov edx, prompt_len
int 0x80

; read input
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 16
int 0x80

; convert string to integer
mov esi, input
xor eax, eax      ; result = 0

.convert:
mov bl, [esi]
cmp bl, 10 ; newline?
je .done_convert
sub bl, '0'
imul eax, eax, 10
add eax, ebx
inc esi
jmp .convert

.done_convert:
mov ecx, eax ; store number

; check %15
mov eax, ecx
xor edx, edx
mov ebx, 15
div ebx
cmp edx, 0
je .fizzbuzz

; check %3
mov eax, ecx
xor edx, edx
mov ebx, 3
div ebx
cmp edx, 0
je .fizz

; check %5
mov eax, ecx
xor edx, edx
mov ebx, 5
div ebx
cmp edx, 0
je .buzz

jmp .notfb

.fizzbuzz:
mov ecx, fizzbuzz
mov edx, fizzbuzz_len
jmp .print

.fizz:
mov ecx, fizz
mov edx, fizz_len
jmp .print

.buzz:
mov ecx, buzz
mov edx, buzz_len
jmp .print

.notfb:
mov ecx, notfb
mov edx, notfb_len

.print:
mov eax, 4
mov ebx, 1
int 0x80

; exit
mov eax, 1
xor ebx, ebx
int 0x80

; Compile using:
; nasm -f elf32 fizzbuzz_input.asm
; ld -m elf_i386 fizzbuzz_input.o -o fizzbuzz
; ./fizzbuzz

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions