How to resolve the algorithm FizzBuzz step by step in the X86 Assembly programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm FizzBuzz step by step in the X86 Assembly programming language
Table of Contents
Problem Statement
Write a program that prints the integers from 1 to 100 (inclusive).
But:
The FizzBuzz problem was presented as the lowest level of comprehension required to illustrate adequacy.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm FizzBuzz step by step in the X86 Assembly programming language
Source code in the x86 programming language
; x86_64 linux nasm
section .bss
number resb 4
section .data
fizz: db "Fizz"
buzz: db "Buzz"
newLine: db 10
section .text
global _start
_start:
mov rax, 1 ; initialize counter
loop:
push rax
call fizzBuzz
pop rax
inc rax
cmp rax, 100
jle loop
mov rax, 60
mov rdi, 0
syscall
fizzBuzz:
mov r10, rax
mov r15, 0 ; boolean fizz or buzz
checkFizz:
xor rdx, rdx ; clear rdx for division
mov rbx, 3
div rbx
cmp rdx, 0 ; modulo result here
jne checkBuzz
mov r15, 1
mov rsi, fizz
mov rdx, 4
mov rax, 1
mov rdi, 1
syscall
checkBuzz:
mov rax, r10
xor rdx, rdx
mov rbx, 5
div rbx
cmp rdx, 0
jne finishLine
mov r15, 1
mov rsi, buzz
mov rdx, 4
mov rax, 1
mov rdi, 1
syscall
finishLine: ; print number if no fizz or buzz
cmp r15, 1
je nextLine
mov rax, r10
call printNum
ret
nextLine:
mov rsi, newLine
mov rdx, 1
mov rax, 1
mov rdi, 1
syscall
ret
printNum: ; write proper digits into number buffer
cmp rax, 100
jl lessThanHundred
mov byte [number], 49
mov byte [number + 1], 48
mov byte [number + 2], 48
mov rdx, 3
jmp print
lessThanHundred: ; get digits to write through division
xor rdx, rdx
mov rbx, 10
div rbx
add rdx, 48
cmp rax, 0
je lessThanTen
add rax, 48
mov byte [number], al
mov byte [number + 1], dl
mov rdx, 2
jmp print
lessThanTen:
mov byte [number], dl
mov rdx, 1
print:
mov byte [number + rdx], 10 ; add newline
inc rdx
mov rax, 1
mov rdi, 1
mov rsi, number
syscall
ret
You may also check:How to resolve the algorithm Draw a rotating cube step by step in the Perl programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the PureBasic programming language
You may also check:How to resolve the algorithm 24 game/Solve step by step in the REXX programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Objective-C programming language