Skip to content

Commit f818c72

Browse files
author
kubakerlin
committed
Fizzbuzz in NASM assembly
1 parent 6ee596d commit f818c72

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

fizzbuzz/Zsargul/fizzbuzz

8.24 KB
Binary file not shown.

fizzbuzz/Zsargul/fizzbuzz.asm

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
; FizzBuzz written in 32-bit NASM assembly
2+
;
3+
; To run, type "./fizzbuzz"
4+
;
5+
; To compile, type the following in order:
6+
; nasm -f elf fizzbuzz.asm
7+
; ld -m elf_i386 -s -o fizzbuzz fizzbuzz.o
8+
; ./fizzbuzz
9+
;
10+
section .data
11+
; fizzbuzz and title/end messages
12+
fizz db "Fizz", 0xa
13+
fizz_L equ $-fizz
14+
15+
buzz db "Buzz", 0xa
16+
buzz_L equ $-buzz
17+
18+
fizzBuzz db "FizzBuzz", 0xa
19+
fizzBuzz_L equ $-fizzBuzz
20+
21+
title db "First 100 numbers in fizzbuzz:", 0xa
22+
title_L equ $-title
23+
24+
endMsg db "Finished", 0xa
25+
endMsg_L equ $-endMsg
26+
27+
; message displaying number
28+
cntrMsg db " ", 0xa
29+
30+
; counter variable
31+
cntr db 0
32+
33+
section .text
34+
global _start
35+
36+
_start:
37+
; Print title message
38+
mov eax, 4 ; sys_write (print)
39+
mov ebx, 1 ; sdtout
40+
mov ecx, title ; string
41+
mov edx, title_L ; length of string
42+
int 0x80 ; call kernel
43+
44+
mov [cntr], byte 1
45+
next:
46+
; Divide by 3
47+
mov al, [cntr] ; move counter into al (8-bit eax/rax)
48+
xor ah, ah ; set ah to zero (ah is always equal to itself so the result of this is always 0)
49+
mov bl, 3 ; mov 3 into bl (8-bit rbx/ebx)
50+
div bl ; divide al by bl and put quotient into al and remainder in ah
51+
cmp ah, 0 ; check remainder i.e mod 3
52+
jne not_f ; if the remainder is not 0, jump to not_f
53+
54+
; If remainder is 0, continue, performing division by 5
55+
mov al, [cntr]
56+
xor ah, ah
57+
mov bl, 5
58+
div bl
59+
cmp ah, 0
60+
jne not_fb ; if number is divisible by 3 but not 5, go to not_fb
61+
62+
; If number is divisible by both 3 and 5, print fizzbuzz
63+
mov eax, 4
64+
mov ebx, 1
65+
mov ecx, fizzBuzz
66+
mov edx, fizzBuzz_L
67+
int 0x80
68+
jmp finished_number ; move onto next number
69+
70+
not_f:
71+
; Divide by 5
72+
mov al, [cntr]
73+
xor ah, ah
74+
mov bl, 5
75+
div bl
76+
cmp ah, 0
77+
jne not_f_or_b ; if number isn't divisible by 3 or 5, move to not_f_or_b
78+
79+
; If the number is divisible by 5, print "buzz"
80+
mov eax, 4
81+
mov ebx, 1
82+
mov ecx, buzz
83+
mov edx, buzz_L
84+
int 0x80
85+
jmp finished_number
86+
87+
not_fb:
88+
; Print "fizz", because number is divisible by 3 but not 5
89+
mov eax, 4
90+
mov ebx, 1
91+
mov ecx, fizz
92+
mov edx, fizz_L
93+
int 0x80
94+
jmp finished_number
95+
96+
not_f_or_b:
97+
mov al, [cntr]
98+
xor ah, ah
99+
mov bl, 10
100+
div bl ; divide al by 10
101+
cmp al, 0 ; check remainder to see if we have double-digit decimal number
102+
103+
add al, 48
104+
mov [cntrMsg], al
105+
add ah, 48
106+
mov [cntrMsg+1], ah
107+
108+
; Print number
109+
mov eax, 4
110+
mov ebx, 1
111+
mov ecx, cntrMsg
112+
mov edx, 4
113+
int 0x80
114+
115+
finished_number:
116+
add [cntr], byte 1
117+
cmp [cntr], byte 100
118+
jne next ; If number isn't 100, continue looping
119+
120+
; If loop has reached 100, finish program
121+
mov eax, 4
122+
mov ebx, 1
123+
mov ecx, endMsg
124+
mov edx, endMsg_L
125+
int 0x80
126+
127+
exit:
128+
; exit program
129+
mov eax, 1 ; sys_exit
130+
int 0x80

fizzbuzz/Zsargul/fizzbuzz.o

1.36 KB
Binary file not shown.

0 commit comments

Comments
 (0)