-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.asm
436 lines (365 loc) · 7.8 KB
/
main.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#ARKO project - Bezier Curve v1.0
#by Jakub Nietupski
#developed: april 2015
#note: for all fixed point operations precision is 16,16
.data
output: .asciiz "out.bmp"
width_prompt: .asciiz "podaj szerokosc: "
height_prompt: .asciiz "\npodaj wysokosc: "
points_prompt: .asciiz "\npodaj 5 punktow kotrolnych (wartosci procentowe):"
x_prompt: .asciiz "\n\nx: "
y_prompt: .asciiz "y: "
debug: .asciiz "\n-------------------------------------\n"
control_points: .word 0
.word 0
.word 0
.word 0
.word 0
.word 0
.word 0
.word 0
.word 0
.word 0
bm_data_start: .word 0 # addres in heap
triple_w: .word 0 # used in painting computation
# bitmap buffer (54 bytes for header)
bitmap_size: .word 0
.half 0
# BMP header
bitmap: .byte 'B' # bfType
.byte 'M'
.word 0 # bfSize;
.half 0 # bfReserved1
.half 0 # bfReserved2
.word 54 # bfOffBits
.word 40 # biSize
bw: .word 0 # biWidth
bh: .word 0 # biHeight
.half 0 # biPlanes
.half 24 # biBitCount
.word 0 # biCompression
.word 0 # biSizeImage
.word 0 # biXPelsPerMeter
.word 0 # biYPelsPerMeter
.word 0 # biClrUsed
.word 0 # biClrImportant
#bitmap_data: .space 57600
#bitmap_end: .word 0
.text
#################################################
#getting input starts here
#!!!
#readInput.asm
#gathering resolution information
#t1 - width
#t2 - height
li $v0, 4 #print string
la $a0, width_prompt
syscall
li $v0, 5 #read int
syscall
move $t1, $v0
li $v0, 4 #print string
la $a0, height_prompt
syscall
li $v0, 5 #read int
syscall
move $t2, $v0
#gathering control points information
#t3 - array pointer
la $t3, control_points
li $v0, 4 #print string
la $a0, points_prompt
syscall
#1st point
li $v0, 4 #print string
la $a0, x_prompt
syscall
li $v0, 5 #read int
syscall
mul $v0, $v0, 655 #0.01 x 2^16
sw $v0, ($t3)
li $v0, 4 #print string
la $a0, y_prompt
syscall
li $v0, 5 #read int
syscall
mul $v0, $v0, 655 #0.01 x 2^16
sw $v0, 4($t3)
#2nd point
li $v0, 4 #print string
la $a0, x_prompt
syscall
li $v0, 5 #read int
syscall
mul $v0, $v0, 655 #0.01 x 2^16
sw $v0, 8($t3)
li $v0, 4 #print string
la $a0, y_prompt
syscall
li $v0, 5 #read int
syscall
mul $v0, $v0, 655 #0.01 x 2^16
sw $v0, 12($t3)
#3rd point
li $v0, 4 #print string
la $a0, x_prompt
syscall
li $v0, 5 #read int
syscall
mul $v0, $v0, 655 #0.01 x 2^16
sw $v0, 16($t3)
li $v0, 4 #print string
la $a0, y_prompt
syscall
li $v0, 5 #read int
syscall
mul $v0, $v0, 655 #0.01 x 2^16
sw $v0, 20($t3)
#4th point
li $v0, 4 #print string
la $a0, x_prompt
syscall
li $v0, 5 #read int
syscall
mul $v0, $v0, 655 #0.01 x 2^16
sw $v0, 24($t3)
li $v0, 4 #print string
la $a0, y_prompt
syscall
li $v0, 5 #read int
syscall
mul $v0, $v0, 655 #0.01 x 2^16
sw $v0, 28($t3)
#5th point
li $v0, 4 #print string
la $a0, x_prompt
syscall
li $v0, 5 #read int
syscall
mul $v0, $v0, 655 #0.01 x 2^16
sw $v0, 32($t3)
li $v0, 4 #print string
la $a0, y_prompt
syscall
li $v0, 5 #read int
syscall
mul $v0, $v0, 655 #0.01 x 2^16
sw $v0, 36($t3)
#!!!
#!!!
#hardcoded, converted to fixed binary values for testing can be put here
#!!!
#assighning correct size
sw $t1, bw
sw $t2, bh
sll $t4, $t1, 1
add $t4, $t4, $t1 #width x 3
and $t5, $t4, 3 #division by 4 check
#beqz $t5, no_correction
li $t6, 4
sub $t5, $t6, $t5
and $t5, $t5, 3
add $t4, $t4, $t5
#no_correction:
sw $t4, triple_w
#mul $t4, $t1, $t2 #width x heigth x 3(bytes per pixel) + 54 (bitmap header)
#sll $t5, $t4, 1
#add $t5, $t5, $t4
mul $t5, $t4, $t2
addi $t4, $t5, 54
sw $t4, bitmap_size
li $v0, 9 #sbrk
move $a0, $t5
syscall
sw $v0, bm_data_start
#################################################
#painting starts here
#fill background
lw $t1, bm_data_start
add $t2, $t1, $t5
li $t3, 0x0
fill_next_byte:
sb $t3, ($t1)
addi $t1, $t1, 1
blt $t1, $t2, fill_next_byte
#beginning of casteljau algorithm
li $t0, 0x80
lw $s2, bw
lw $s3, bh
casteljau_loop:
li $t2, 0x10000
sub $t1, $t2, $t0
#point calculations
#1st column
lw $t2, control_points
lw $t3, control_points + 4
lw $t4, control_points + 8
lw $t5, control_points + 12
mul $t2, $t2, $t1
srl $t2, $t2, 16
mul $t6, $t4, $t0
srl $t6, $t6, 16
add $t2, $t2, $t6
mul $t3, $t3, $t1
srl $t3, $t3, 16
mul $t7, $t5, $t0
srl $t7, $t7, 16
add $t3, $t3, $t7 #"10"
lw $t6, control_points + 16
lw $t7, control_points + 20
mul $t4, $t4, $t1
srl $t4, $t4, 16
mul $t8, $t6, $t0
srl $t8, $t8, 16
add $t4, $t4, $t8
mul $t5, $t5, $t1
srl $t5, $t5, 16
mul $t9, $t7, $t0
srl $t9, $t9, 16
add $t5, $t5, $t9 #"11"
lw $t8, control_points + 24
lw $t9, control_points + 28
mul $t6, $t6, $t1
srl $t6, $t6, 16
mul $s0, $t8, $t0
srl $s0, $s0, 16
add $t6, $t6, $s0
mul $t7, $t7, $t1
srl $t7, $t7, 16
mul $s1, $t9, $t0
srl $s1, $s1, 16
add $t7, $t7, $s1 #"12"
lw $s0, control_points + 32
lw $s1, control_points + 36
mul $t8, $t8, $t1
srl $t8, $t8, 16
mul $s0, $s0, $t0
srl $s0, $s0, 16
add $t8, $t8, $s0
mul $t9, $t9, $t1
srl $t9, $t9, 16
mul $s1, $s1, $t0
srl $s1, $s1, 16
add $t9, $t9, $s1 #"13"
#2nd column
mul $t2, $t2, $t1
srl $t2, $t2, 16
mul $s0, $t4, $t0
srl $s0, $s0, 16
add $t2, $t2, $s0
mul $t3, $t3, $t1
srl $t3, $t3, 16
mul $s0, $t5, $t0
srl $s0, $s0, 16
add $t3, $t3, $s0 #"20"
mul $t4, $t4, $t1
srl $t4, $t4, 16
mul $s0, $t6, $t0
srl $s0, $s0, 16
add $t4, $t4, $s0
mul $t5, $t5, $t1
srl $t5, $t5, 16
mul $s0, $t7, $t0
srl $s0, $s0, 16
add $t5, $t5, $s0 #"21"
mul $t6, $t6, $t1
srl $t6, $t6, 16
mul $s0, $t8, $t0
srl $s0, $s0, 16
add $t6, $t6, $s0
mul $t7, $t7, $t1
srl $t7, $t7, 16
mul $s1, $t9, $t0
srl $s1, $s1, 16
add $t7, $t7, $s1 #"22"
#3rd column
mul $t2, $t2, $t1
srl $t2, $t2, 16
mul $s0, $t4, $t0
srl $s0, $s0, 16
add $t2, $t2, $s0
mul $t3, $t3, $t1
srl $t3, $t3, 16
mul $s0, $t5, $t0
srl $s0, $s0, 16
add $t3, $t3, $s0 #"30"
mul $t4, $t4, $t1
srl $t4, $t4, 16
mul $s0, $t6, $t0
srl $s0, $s0, 16
add $t4, $t4, $s0
mul $t5, $t5, $t1
srl $t5, $t5, 16
mul $s0, $t7, $t0
srl $s0, $s0, 16
add $t5, $t5, $s0 #"31"
#4th column
mul $t2, $t2, $t1
srl $t2, $t2, 16
mul $t4, $t4, $t0
srl $t4, $t4, 16
add $t2, $t2, $t4
mul $t3, $t3, $t1
srl $t3, $t3, 16
mul $t5, $t5, $t0
srl $t5, $t5, 16
add $t3, $t3, $t5 #"40" - final point
#drawing point
# (x0, y0) =
# addr + 3 * width * (y0 - 1) + 3 * x0
# addr + 3 * width * (y0 - 1) + 3 * x0 + 1
# addr + 3 * width * (y0 - 1) + 3 * x0 + 2
#lw $t4, bw
#lw $t5, bh
mul $t2, $t2, $s2 #x0
srl $t2, $t2, 16
mul $t3, $t3, $s3 #y0
srl $t3, $t3, 16
lw $t6, bm_data_start #addr
lw $t4, triple_w #3 * width
subi $t3, $t3, 1 #y0 - 1
mul $t3, $t3, $t4 #3 * width * (y0 - 1)
sll $t7, $t2, 1
add $t2, $t7, $t2 #3 * x0
add $t6, $t6, $t3
add $t6, $t6, $t2
#li $t3, 0x0 #putting pixel
#sb $t3, ($t6)
#addi $t6, $t6, 1
#li $t3, 0x0
#sb $t3, ($t6)
#addi $t6, $t6, 1
addi $t6, $t6, 2
li $t3, 0xef
sb $t3, ($t6)
addi $t0, $t0, 0x80
blt $t0, 0x10000, casteljau_loop
#################################################
#saving stuff starts here
#save bitmap from buffer to file
li $v0, 13 #open file
la $a0, output #file name
li $a1 1 #write mode
syscall
move $t1, $v0
#write header
li $v0, 15 #write to file
move $a0, $t1 #file desc
la $a1, bitmap #bitmap buffer
li $a2, 54 #buffer length
syscall
#write pixels
li $v0, 15 #write to file
move $a0, $t1 #file desc
lw $a1, bm_data_start #bitmap buffer
lw $t5, bitmap_size
subi $t5, $t5, 54
move $a2, $t5 #buffer length
syscall
li $v0, 16 # close file
move $a0, $t1
syscall
exit:
li $v0, 10
syscall