-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizzbuzz.asm
123 lines (91 loc) · 1.49 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
BITS 32
global _start
section .data
fizz db "Fizz"
fizz_len equ $-fizz
buzz db "Buzz"
buzz_len equ $-buzz
fizzbuzz_len equ $-fizz
separator db 0x20
newline db 0xa
section .text
_start:
mov eax, 100; natural integer number
call fizzbuzz
;exit
mov eax, 1
xor ebx, ebx
int 0x80
fizzbuzz:
push ebp
mov ebp, eax
mov esi, 1
push 0
push 0
.loop:
mov eax, [esp]
inc eax
mov [esp], eax
xor ebx, ebx
cmp eax, 3
jnz .next_1
xor eax, eax
mov [esp], eax
mov ecx, fizz
mov edx, fizz_len
call print_string
mov ebx, 1
.next_1:
mov eax, [esp+4]
inc eax
mov [esp+4], eax
cmp eax, 5
jnz .next_2
xor eax, eax
mov [esp+4], eax
mov ecx, buzz
mov edx, buzz_len
call print_string
mov ebx, 1
.next_2:
test ebx,ebx
jnz .next_3
mov eax, esi
call print_number
.next_3:
mov ecx, separator
call print_symbol
inc esi
cmp esi, ebp
jng .loop
mov ecx, newline
call print_symbol
pop eax
pop eax
pop ebp
ret
print_symbol:
mov edx, 1 ; length of the string
print_string:
mov eax, 4 ; system call number (sys_write)
mov ebx, 1 ; file descriptor (stdout)
int 0x80
ret
print_number:
mov ecx, esp
sub esp, 12 ; reserve space for the number string
mov edi, 0
mov ebx, 10 ; base-10
.loop:
xor edx, edx
div ebx
add dl, '0'
dec ecx
inc edi
mov [ecx],dl
test eax, eax
jnz .loop
mov edx, edi ; length of the string
call print_string
add esp, 12 ; release space for the number string
ret