-
Notifications
You must be signed in to change notification settings - Fork 0
/
collatz.asm
201 lines (157 loc) · 1.79 KB
/
collatz.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
SECTION .bss
var resd 255
SECTION .data
msg: db "Please enter a number", 0xa
cal: db "Calculating ...", 0xa
msglen equ $- msg
callen equ $- cal
SECTION .text
global _start
_start:
mov eax, msg
mov edi, 22
call sprintlng
mov edx, 255
mov ecx, var
mov ebx, 2
mov eax, 3
int 80h
mov eax, cal
mov edi, callen
call sprintlng
mov eax, var
call str2int
call collatz
call quit
quit:
mov eax, 1
mov ebx, 0
int 80h
collatz:
mov ebx, 0
mov esi, 0
call intprint
.lp:
cmp eax, 1
jle .done
push eax
call EorO
cmp eax, 1
je .odd
jmp .even
.even:
pop eax
mov edx, 0
mov esi, 2
idiv esi
call intprint
jmp .lp
.odd:
pop eax
mov ebx, 3
imul ebx
add eax, 1
call intprint
jmp .lp
.done:
ret
sprint:
push eax
push ebx
push ecx
push edx
mov edx, 1
mov ecx, eax
mov ebx, 1
mov eax, 4
int 80h
pop edx
pop ecx
pop ebx
pop eax
ret
intprint:
push eax
push ebx
push ecx
push edx
mov ecx, 0
.divlp:
mov edx, 0
mov esi, 10
idiv esi
add edx, 48
push edx
inc ecx
cmp eax, 0
jz .printlp
jmp .divlp
.printlp:
cmp ecx, 0
jz .restore
mov eax, esp
call sprint
pop edx
dec ecx
jmp .printlp
.restore:
call nwln
pop edx
pop ecx
pop ebx
pop eax
ret
nwln:
push eax
mov eax, 0xa
push eax
mov eax, esp
call sprint
pop eax
pop eax
ret
EorO:
mov edx, 0
mov esi, 2
idiv esi
mov eax, edx
ret
str2int:
mov esi, eax
mov ecx, 0
mov eax, 0
.multiplylp:
xor ebx, ebx
mov bl, [esi + ecx]
cmp bl, 48
jl .finaldiv
cmp bl, 57
jg .finaldiv
sub bl, 48
add eax, ebx
mov ebx, 10
mul ebx
inc ecx
jmp .multiplylp
.finaldiv:
cmp ecx, 0
je .restore
mov esi, 10
idiv esi
.restore:
ret
sprintlng:
push eax
push ebx
push ecx
push edx
mov edx, edi
mov ecx, eax
mov ebx, 1
mov eax, 4
int 80h
pop edx
pop ecx
pop ebx
pop eax
ret