-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagefilter_assemb_optimised.s
221 lines (172 loc) · 6.3 KB
/
imagefilter_assemb_optimised.s
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
# An image processing program.
# This program blurs an eight-bit grayscale image by averaging a pixel
# in the image with the eight pixels around it. The average is computed
# by (CurCell*8 + other 8 cells)/16, weighting the current cell by 50%.
.xlist:
.include "stdlib.a"
.includelib "stdlib.lib"
.list:
.286:
dseg segment para public 'data'
# integer variables:
.bss
h word ?
i word ?
j word ?
k word ?
l word ?
sum word ?
iterations word ?
# Files names:
.data
InName byte "roller1.raw",0
OutName byte "roller2.raw",0
dseg ends
# input data we manipulate.
.bss
InSeg segment para public 'indata'
DataIn byte 251 dup (256 dup (?))
InSeg ends
# output array to hold the result.
.bss
OutSeg segment para public 'outdata'
DataOut byte 251 dup (256 dup (?))
OutSeg ends
cseg segment para public 'code'
.text
.globl Main
Main: proc
movw dseg, %ax
movw %ax, %ds
meminit
movw $0x3d00, %ax #Open input image to be read.
leaw InName, %dx
int $0x21
jnc GoodOpen
print
byte "Could not open input file.",cr,lf,0
jmp Quit
GoodOpen: movw %ax, %bx #File handle.
movw InSeg, %dx #Where to put the data.
movw %dx, %ds
leaw DataIn, %dx
movw $256*251, %cx #Size of data file to read.
movb $0x3F, %ah
int $0x21
cmpw $256*251, %ax # check if we can read data.
je GoodRead
print
byte " Impossible to read ",cr,lf,0
jmp Quit
GoodRead: movw dseg, %ax
movw %ax, %ds
print
byte "Enter number of iterations: ",0
getsm
atoi
free
movw %ax, iterations
print
byte "Computing Result",cr,lf,0
# for h= 1 to iterations
movl $1, h
hloop:
# Copying input data to output buffer.
# Optimization1:Use movsd instruction rather than a loop to copy data from DataOut back to DataIn.
pushw %ds
movw OutSeg, %ax
movw %ax, %ds
movw InSeg, %ax
movw %ax, %es
leaw DataOut, %si
leaw DataIn, %di
movw (251*256)/4, %cx
rep
movsl
popw %ds
# Optimization2: Use a repeat-until
# for i = 1 to 249
movl $1, i
iloop:
# for j= 1 to 254
movl $1, j
jloop:
# Optimization3:Unroll the innermost two loops
mov bh, byte ptr i
mov bl, byte ptr j
pushb %ds
movw InSeg, %ax #Get access to InSeg.
movw %ax, %ds
movw $0, %cx #Calculate sum.
movb %ch, %ah
movb ds:DataIn[bx-257], %cl #DataIn[i-1][j-1]
movb ds:DataIn[bx-256], %al #DataIn[i-1][j]
addw %ax, %cx
movb ds:DataIn[bx-255], %al #DataIn[i-1][j+1]
addw %ax, %cx
movb ds:DataIn[bx-1], %al #DataIn[i][j-1]
addw %ax, %cx
movb ds:DataIn[bx+1], %al #DataIn[i][j+1]
addw %ax, %cx
movb ds:DataIn[bx+255], %al #DataIn[i+1][j-1]
addw %ax, %cx
movb ds:DataIn[bx+256], %al #DataIn[i+1][j]
addw %ax, %cx
movb ds:DataIn[bx+257], %al #DataIn[i+1][j+1]
addw %ax, %cx
movb ds:DataIn[bx], %al #DataIn[i][j]
shlw $3, %ax #DataIn[i][j]*8
addw %ax, %cx
shrw $4, %cx #Division by 16
movw OutSeg, %ax
movw %ax, %ds
movb %cl, ds:DataOut[bx]
popb %ds
incl %ds
cmpl $254, j
jbe jloop
incl %ds
cmpl $249, i
jbe iloop
incl %ds
movw h, %ax
cmpw Iterations, %ax
jnbe Done
jmp hloop
Done: print
byte "Writing result",cr,lf,0
#Writing data to the output file:
movb $0x3c, %ah #Create output file
movw $0, %cx
leaw OutName, %dx
int $0x21
jnc GoodCreate
print
byte " Impossible to create output file.",cr,lf,0
jmp Quit
GoodCreate: movw %ax, %bx # handling file
pushw %bx
movw OutSeg, %dx # data is placed here
movw %dx, %ds
leaw DataOut, %dx
movw $256*251, %cx # amount of words to write at once
movb $0x40, %ah # Writing task
int $0x21
popw %bx # Retrieve handle for close.
cmpw $256*251, %ax # check if writing operation succeeded
je GoodWrite
print
byte "Did not write the file properly",cr,lf,0
jmp Quit
GoodWrite: movb $0x3e, %ah #Closing operation.
int $0x21
Quit: ExitPgm
Main ENDP
cseg ends
sseg segment para stack 'stack'
stk byte 1024 dup ("stack ")
sseg ends
zzzzzzseg segment para public 'zzzzzz'
LastBytes byte 16 dup (?)
zzzzzzseg ends
end Main