-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizzbuzz-shift.asm
169 lines (132 loc) · 2.17 KB
/
fizzbuzz-shift.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
.loop:
mov eax, esi
mov ebx, 0xffff0000
and ebx, eax
jz .next_1
shr ebx, 16
and eax, 0x0000ffff
add eax, ebx
mov ebx, 0x00010000
and ebx, eax
shr ebx, 16
and eax, 0x0000ffff
add eax, ebx
.next_1:
mov bx, 0xff00
and ebx, eax
jz .next_2
shr ebx, 8
and ax, 0x00ff
add eax, ebx
mov bx, 0x0100
and ebx, eax
shr ebx, 8
and ax, 0x00ff
add eax, ebx
.next_2:
mov bx, 0x00f0
and ebx, eax
shr ebx, 4
and ax, 0x000f
add eax, ebx
mov bx, 0x0010
and ebx, eax
shr ebx, 4
and ax, 0x000f
add eax, ebx
cmp al, 0x0f
jne .next_3
mov ecx, fizz
mov edx, fizzbuzz_len
call print_string
jmp .next_another
.next_3:
mov edx, eax
mov bx, 0x000c
and ebx, eax
shr ebx, 2
and ax, 0x0003
add eax, ebx
mov ebx, 0x0004
and ebx, eax
shr ebx, 2
and ax, 0x0003
add eax, ebx
cmp al, 3
jnz .next_4
mov ecx, fizz
mov edx, fizz_len
call print_string
jmp .next_another
.next_4:
mov eax,edx
mov bx, 0x000c
and ebx, eax
shr ebx, 2
and eax, 0x0003
cmp eax, ebx
jne .next_5
mov ecx, buzz
mov edx, buzz_len
call print_string
jmp .next_another
.next_5:
mov eax, esi
call print_number
.next_another:
mov ecx, separator
call print_symbol
inc esi
cmp esi, ebp
jng .loop
mov ecx, newline
call print_symbol
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