-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdonut.asm
More file actions
289 lines (250 loc) · 4.69 KB
/
Copy pathdonut.asm
File metadata and controls
289 lines (250 loc) · 4.69 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
; donut.asm
; Rotating donut animation for Windows using NASM
section .data
A dd 0.0
B dd 0.0
theta dd 0.0
phi dd 0.0
c dd 0.0
d dd 0.0
e dd 0.0
f dd 0.0
g dd 0.0
h dd 0.0
D dd 0.0
l dd 0.0
m dd 0.0
n dd 0.0
t dd 0.0
x dd 0.0
y dd 0.0
L dd 0.0
x_int dd 0
y_int dd 0
zbuffer times 1760 dd 0.0
output times 1760 db ' '
esc_seq db 1Bh, '[', 'H'
TWO_PI dd 6.28
STEP_T dd 0.07
STEP_P dd 0.02
INC_A dd 0.04
INC_B dd 0.02
FIVE dd 5.0
TWO dd 2.0
EIGHT dd 8.0
THIRTY dd 30.0
FORTY dd 40.0
FIFTEEN dd 15.0
TWELVE dd 12.0
ZERO dd 0.0
chars db ".,-~:;=!*#$@", 0
WIDTH dd 80
HEIGHT dd 22
SLEEP_MS dd 50
hConsole dd 0
bytesWritten dd 0
section .text
extern _GetStdHandle@4
extern _WriteConsoleA@20
extern _Sleep@4
extern _ExitProcess@4
global _main
_main:
push -11
call _GetStdHandle@4
mov [hConsole], eax
main_loop:
; clear buffers
mov edi, output
mov ecx, 1760
mov al, ' '
rep stosb
mov edi, zbuffer
mov ecx, 1760
xor eax, eax
rep stosd
; cursor to top-left
mov eax, output
mov byte [eax], 1Bh
mov byte [eax+1], '['
mov byte [eax+2], 'H'
mov eax, [ZERO]
mov [theta], eax
outer_loop:
fld dword [theta]
fcomp dword [TWO_PI]
fstsw ax
sahf
jae end_outer
mov eax, [ZERO]
mov [phi], eax
inner_loop:
fld dword [phi]
fcomp dword [TWO_PI]
fstsw ax
sahf
jae end_inner
; trig calculations for donut surface
fld dword [phi]
fsin
fstp dword [c]
fld dword [theta]
fcos
fstp dword [d]
fld dword [A]
fsin
fstp dword [e]
fld dword [theta]
fsin
fstp dword [f]
fld dword [A]
fcos
fstp dword [g]
fld dword [d]
fadd dword [TWO]
fstp dword [h]
; distance from camera
fld dword [c]
fmul dword [h]
fmul dword [e]
fld dword [f]
fmul dword [g]
faddp
fadd dword [FIVE]
fld1
fdivrp
fstp dword [D]
fld dword [phi]
fcos
fstp dword [l]
fld dword [B]
fcos
fstp dword [m]
fld dword [B]
fsin
fstp dword [n]
fld dword [c]
fmul dword [h]
fmul dword [g]
fld dword [f]
fmul dword [e]
fsubp
fstp dword [t]
; screen coordinates
fld dword [l]
fmul dword [h]
fmul dword [m]
fld dword [t]
fmul dword [n]
fsubp
fmul dword [D]
fmul dword [THIRTY]
fadd dword [FORTY]
fstp dword [x]
fld dword [l]
fmul dword [h]
fmul dword [n]
fld dword [t]
fmul dword [m]
faddp
fmul dword [D]
fmul dword [FIFTEEN]
fadd dword [TWELVE]
fstp dword [y]
fld dword [x]
fistp dword [x_int]
fld dword [y]
fistp dword [y_int]
; bounds check
mov eax, [x_int]
cmp eax, 0
jl skip_point
cmp eax, 80
jge skip_point
mov ebx, [y_int]
cmp ebx, 0
jl skip_point
cmp ebx, 22
jge skip_point
mov eax, ebx
mov ebx, [WIDTH]
mul ebx
add eax, [x_int]
mov esi, eax
; depth test
fld dword [D]
fcomp dword [zbuffer + esi*4]
fstsw ax
sahf
jbe skip_point
fld dword [D]
fstp dword [zbuffer + esi*4]
; lighting calculation
fld dword [f]
fmul dword [e]
fld dword [c]
fmul dword [d]
fmul dword [g]
faddp
fmul dword [m]
fld dword [c]
fmul dword [d]
fmul dword [e]
fsubp
fld dword [f]
fmul dword [g]
fsubp
fld dword [l]
fmul dword [d]
fmul dword [n]
fsubp
fmul dword [EIGHT]
fstp dword [L]
; pick character based on brightness
fld dword [L]
fcomp dword [ZERO]
fstsw ax
sahf
jae L_positive
xor eax, eax
jmp L_store
L_positive:
fld dword [L]
fistp dword [x_int]
mov eax, [x_int]
cmp eax, 11
jle L_clamped
mov eax, 11
L_clamped:
L_store:
mov bl, [chars + eax]
mov [output + esi], bl
skip_point:
fld dword [phi]
fadd dword [STEP_P]
fstp dword [phi]
jmp inner_loop
end_inner:
fld dword [theta]
fadd dword [STEP_T]
fstp dword [theta]
jmp outer_loop
end_outer:
push 0
push bytesWritten
push 1760
push output
push dword [hConsole]
call _WriteConsoleA@20
push dword [SLEEP_MS]
call _Sleep@4
; rotation angles for next frame
fld dword [A]
fadd dword [INC_A]
fstp dword [A]
fld dword [B]
fadd dword [INC_B]
fstp dword [B]
jmp main_loop
push 0
call _ExitProcess@4