-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot_set.asm
244 lines (179 loc) · 2.95 KB
/
mandelbrot_set.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
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
; preudo-algorithm
;
; x = 0, y = 0
; while (iteration < 100 and x^2 + y^2 < 4)
; t = x^2 - y^2 + ix
; y = 2xy + iy
; x = t
; iteration += 1
; asm notes
;
; x, y are 32-bit integers with 8-bit fraction
; 1.0 is 0x00000100 (256 decimal)
;
; 0x0100 * 0x0100 = 0x010000 (so need to devide by 256 (1.0))
cpu 8086
org 0x100
v_a equ 0xfa00 ; y-coordinate
v_b equ 0xfa02 ; x-coordinate
v_x equ 0xfa04 ; x 32-bit for fraction
v_y equ 0xfa08 ; y 32-bit for fraction
v_s1 equ 0xfa0c ; temp s1
v_s2 equ 0xfa10 ; temp s2 (48-bit)
start:
mov ax, 0x13 ; vga 320*200*256 mode
int 0x10 ;
mov ax, 0xa000 ; setup video segment
mov ds, ax ;
mov es, ax ;
; 199 and 319 gets by formuls
m4:
mov ax, 199
mov [v_a], ax
m0:
mov ax, 319
mov [v_b], ax
m1:
xor ax, ax
mov [v_x + 0], ax ;
mov [v_x + 2], ax ; x = 0.0
mov [v_y + 0], ax ;
mov [v_y + 2], ax ; y = 0.0
mov cx, 0 ; iteration = 0
m2:
push cx ; save iterator
mov ax, [v_x + 0] ; get x and square it
mov dx, [v_x + 2] ;
call square32 ;
push dx ; save squared x
push ax ;
mov ax, [v_y + 0] ; get y and square it
mov dx, [v_y + 2] ;
call square32 ;
pop bx ; x^2 + y^2
add ax, bx ;
pop bx ;
adc dx, bx ;
pop cx ; restore iterator
cmp dx, 0 ; result is >= 4.0 ?
jne m3 ;
cmp ax, 4 * 256 ;
jnc m3 ;
;
; t = x^2 - y^2 + ix
;
push cx ; save iterator
mov ax, [v_y + 0] ; get x and square it
mov dx, [v_y + 2] ;
call square32 ;
push dx ; save squared x
push ax ;
mov ax, [v_x + 0] ; get y and square it
mov dx, [v_x + 2] ;
call square32 ;
pop bx ; x^2 - y^2
sub ax, bx ;
pop bx ;
sbb dx, bx ;
add ax, [v_b]
adc dx, 0
add ax, [v_b]
adc dx, 0
sub ax, 480
sbb dx, 0
push ax
push dx
;
; y = 2xy + iy
;
mov ax, [v_x + 0] ; get x
mov dx, [v_x + 2] ;
mov bx, [v_y + 0] ; get y
mov cx, [v_y + 2] ;
call mul32
shl ax, 1
rcl dx, 1
add ax, [v_a]
adc dx, 0
add ax, [v_a]
adc dx, 0
sub ax, 250
sbb dx, 0
mov [v_y + 0], ax
mov [v_y + 2], dx
pop dx
pop ax
mov [v_x + 0], ax
mov [v_x + 2], dx
pop cx
inc cx
cmp cx, 100
je m3
jmp m2
m3:
mov ax, [v_a]
mov dx, 320
mul dx
add ax, [v_b]
xchg ax, di
add cl, 0x20
mov [di], cl
dec word[v_b]
jns m1
dec word[v_a]
jns m0
mov ah, 0
int 0x16
mov ax, 2
int 0x10
int 0x20
;
; square 32-bit number
; ax:dx = (ax:dx * ax:dx) / 256
;
square32:
mov bx, ax
mov cx, dx
mul32:
xor dx, cx
pushf
xor dx, cx
jns mul32_2
not ax
not dx
add ax, 1
adc dx, 0
mul32_2:
test cx, cx
jns mul32_3
not bx
not cx
add bx, 1
adc cx, 0
mul32_3:
mov [v_s1 + 0], ax
mov [v_s1 + 2], dx
mul bx
mov [v_s2 + 0], ax
mov [v_s2 + 2], dx
mov ax, [v_s1 + 2]
mul cx
mov [v_s2 + 4], ax
mov ax, [v_s1 + 2]
mul bx
add [v_s2 + 2], ax
add [v_s2 + 4], dx
mov ax, [v_s1]
mul cx
add [v_s2 + 2], ax
add [v_s2 + 4], dx
mov ax, [v_s2 + 1]
mov dx, [v_s2 + 3]
popf
jns mul32_1
not ax
not dx
add ax, 1
adc dx, 0
mul32_1:
ret