-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzbuzz.asm
108 lines (89 loc) · 2.14 KB
/
fizzbuzz.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
;FizzBuzz
global _start
section .text
_start:
mov rdx, 0 ; Zero out rdx. Else we get FPE
add rax, 1 ; Add one. Implicitly we start at 0
push rax ; Save the value
mov rbx, 7 ; Load the dvisor
div rbx ; divide
pop rax ; Restore the current index
push rax ; Hold onto the index
push rdx ; Hold on to remainder
mov rdx, 0 ; Zero out the rdx for clean divides
mov rbx, 9 ; Prepare to divide by 9
div rbx ; divide
push rdx ; Hold on to Remainder
pop rax ; Remainder from 9 operation
pop rbx ; Remainder from 7 operation
cmp rbx, 0 ; if i % 7 == 0
je _true ;Push a 1 for true to the stack
jne _false ;Push a 0 for false to the stack
_loopOrQuit:
pop rax ; Pop our index.
cmp rax,0x64 ; if it's 100, set a flag. This will have to move lower
jne _start
; Convert dword to Ascii would be called here to print the index.
mov rax, 1 ; sys_exit
int 0x80 ; call kernal
_true:
;if rbx is indeed 0, then we only need to make sure rax is too.
cmp rax, 0 ; is rax 0?
je _fizzBuzz ; It is. FIZZBUZZ
jne _fizz ; if it's not, then we are in a FIZZ only situation.
_false:
cmp rax, 0 ; We already know that rbx aint zero. The only thing left
je _buzz
jne _loopOrQuit
_fizz:
push rdx
push rcx
push rbx
push rax
mov rdx, fizzlen
mov rcx, fizzMsg
mov rbx, 1
mov rax, 4
int 0x80
pop rax
pop rbx
pop rcx
pop rdx
jmp _loopOrQuit
_buzz:
push rdx
push rcx
push rbx
push rax
mov rdx, buzzlen
mov rcx, buzzMsg
mov rbx, 1
mov rax, 4
int 0x80
pop rax
pop rbx
pop rcx
pop rdx
jmp _loopOrQuit
_fizzBuzz:
push rdx
push rcx
push rax
push rbx
mov rdx, len
mov rcx, msg
mov rbx, 1
mov rax, 4
int 0x80
pop rax
pop rbx
pop rcx
pop rdx
jmp _loopOrQuit
section .data
msg db 'FizzBuzz', 0xa
len equ $ - msg
fizzMsg db 'Fizz', 0xa
fizzlen equ $ - fizzMsg
buzzMsg db 'Buzz', 0xa
buzzlen equ $ - buzzMsg