I want to thank you for the head start on the version that I copied from you!
; FILE: fizzbuzz.asm
; Author: David Billsbrough
; influenced by Matt_Giuca version of Feb '07
; Revised: Monday, September 18, 2017 at 09:42:39 AM (EDT)
; another weird fizzbuzz example
;
; =====
; Uh, someone ordered an 86_64 assembly version?
; (FreeBSD syscalls/NASM/ELF64)
%macro writestr 2
; Argument 1: char* string to write
; Argument 2: length of string
mov rax, write ; write %1 to stdout
mov rdi, stdout
mov rsi, %1
mov rdx, %2
syscall
%endmacro
; ========================================================================
section .data
; ========================================================================
section .rodata
FIZZ db "Fizz", 0
LENF equ 4
BUZZ db "Buzz", 0
LENB equ 4
NL db 10
; ========================================================================
section .bss
index: resb 1
char: resb 1
tmp: resb 1
; ========================================================================
section .text
global _start
_start:
write: equ 4 ; FreeBSD syscall for write
stdout: equ 1 ; standard output file handle
sysexit: equ 1 ; FreeBSD syscall for exit
maxnum: equ 100 ; count to where?
mov byte [index], 1
forloop:
cmp byte [index], maxnum
jbe not_exit
jmp exit
not_exit:
; if (i % 3 == 0)
mov al, [index]
xor ah, ah
mov cl, 3
div cl
test ah, ah
jnz trybuzz
writestr FIZZ, LENF
; if (i % 5 == 0)
mov al, [index]
mov cl, 5
div cl
test ah, ah
jnz not_buzz
writestr BUZZ, LENB
not_buzz:
jmp continue
trybuzz:
; if (i % 5 == 0)
mov al, [index]
xor ah, ah
mov cl, 5
div cl
test ah, ah
jnz printnum
writestr BUZZ, LENB
jmp continue
printnum:
; Write the number i
mov al, [index]
xor ah, ah
cmp al, 100
jl tens
mov cl, 100
div cl ; ah = al % 100
add al, '0'
mov [char], al
mov [tmp], ah
writestr char, 1
mov al, [tmp]
xor ah, ah
tens:
mov al, [index]
cmp al, 10
jl ones
mov cl, 10
div cl ; ah = al % 10
add al, '0'
mov [char], al
mov [tmp], ah
writestr char, 1
mov al, [tmp]
xor ah, ah
ones:
add al, '0'
mov [char], al
writestr char, 1
continue:
writestr NL, 1
inc byte [index]
jmp forloop
exit:
mov rax, sysexit ; exit program
xor rdi, rdi ; set a zero exit code (I think)
syscall
; vim: ts=8: nowrap:
; OH my God, that took two ages!!
regards,
David