-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.asm
354 lines (277 loc) · 7.29 KB
/
utility.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
bits 64
%macro saveRegisters 0
push rax
push rbx
push rcx
push rdx
%endmacro
%macro getBackRegisters 0
pop rdx
pop rcx
pop rbx
pop rax
%endmacro
section .data
numberPrint db 0,0,0,0,0,0,0,0,0,10
NUMBER_LENGTH equ $-numberPrint
sepator db "=--=", 10
SEPARATOR_LENGTH equ $-sepator
hugeSeparator db "-=================================-",10
HUGE_SEPARATOR_LENGTH equ $-hugeSeparator
FLOAT_DISPLAY_DECIMALS equ 4
filename db "lossDataFinish.bin",0
;# Random weights #
RANDOM_PRECISION dq 1000;
randomHalfPart dq 0.1
randomHalf dq 0.5
randomDouble dq 2.0
tweakedSoftMaxRoot dq 0.1
floatTwo: dq 2.0
floatMinusOne: dq -1.0
floatMinusTwo: dq -2.0
randomOddPart dq 1.793
randomTest dq 1.2
dandomTest dq 1.3
floatLimit dq 9999.9998
;# Data Shortkey #;
DOUBLE_SIZE equ 8
INT_SIZE equ 1
section .bss
; CAF ELEMENTS
cafPointer: resq 1
cafElements: resq 1
cafSize: resq 1
; PRINTING ELEMENTS
printedFloatPointer: resq 1
printedIntPointer: resq 1
auxiliaryIntegerPointer: resq 1
; RANDOM ELEMENTS
randomFloatPointer: resq 1
randomIntPointer: resq 1
randomRangePointer: resq 1
randomRange: resq 1
; PSEUDO RANDOM NUMBER GENERATION
randomSeed: resb 1
randomTimePointer: resb 1
section .text
quit:
mov rax, 1
mov rbx,0
int 0x80
ret
print:
mov rax, 4
mov rbx, 1
int 0x80
ret
displayFloat:
saveRegisters
mov qword [printedFloatPointer], rax; move the float to rdx
mov rax, 1
mov rbx, 10
xor rcx, rcx
xor rdx, rdx
offset:
mul rbx
inc rcx
cmp rcx, FLOAT_DISPLAY_DECIMALS
jne offset
mov qword [auxiliaryIntegerPointer], rax
fld qword [printedFloatPointer]
fimul dword [auxiliaryIntegerPointer]
fistp qword [auxiliaryIntegerPointer]
mov rax, [auxiliaryIntegerPointer]
call displayInteger
getBackRegisters
ret
displayInteger:
saveRegisters
xor rdx, rdx
xor rcx, rcx
cmp rax, 0
jge .unsigned
mov [numberPrint], byte "-"
.unsigned:
mov qword rcx, rax
sar rcx, 63
add rax, rcx
xor rax, rcx
mov rcx, NUMBER_LENGTH
sub rcx, 2
mov rbx, 10
.displayDigit:
xor rdx, rdx
cmp rcx,FLOAT_DISPLAY_DECIMALS
jne .addDigit
mov [numberPrint+rcx], byte "."
jmp .done
.addDigit:
div rbx
add rdx, 48
mov [numberPrint+rcx], byte dl
jmp .done
.done:
dec rcx
cmp rax, 0
jnz .displayDigit
cmp rcx, FLOAT_DISPLAY_DECIMALS
jge .displayDigit
mov rcx, numberPrint
mov rdx, NUMBER_LENGTH
call print
call resetPrintedString
getBackRegisters
ret
displayUnity:
saveRegisters
xor rdx, rdx
xor rcx, rcx
mov rcx, NUMBER_LENGTH
sub rcx, 2
mov rbx, 10
.displayUnityDigit:
xor rdx, rdx
div rbx
add rdx, 48
mov [numberPrint+rcx], byte dl
dec rcx
cmp rax, 0
jnz .displayUnityDigit
mov rcx, numberPrint
mov rdx, NUMBER_LENGTH
call print
call resetPrintedString
getBackRegisters
ret
displaySeperator:
saveRegisters
mov rcx, sepator
mov rdx, SEPARATOR_LENGTH
call print
getBackRegisters
ret
displayHugeSeparator:
saveRegisters
mov rcx, hugeSeparator
mov rdx, HUGE_SEPARATOR_LENGTH
call print
getBackRegisters
ret
resetPrintedString:
mov rax, 8
resetPrintedStringLOOP:
mov byte [numberPrint+rax], 0
dec rax
cmp rax, 0
jge resetPrintedStringLOOP
mov byte [numberPrint+9], 10
ret
caf:
mov rax, 45 ; sys_brk
xor rbx, rbx
int 0x80
mov rcx, rax ; save the initial break
mov [cafPointer], rax ; Move pointer to break
mov rbx, [cafSize]
mov rax, [cafElements]
mul rbx ; Set rax as the amount of bytes we need
add [cafPointer], rax ; Move again pointer
mov rbx, [cafPointer]
mov rax, 45
int 0x80
cmp rax, 0
jge noissue
noissue:
;mov rdx, rax ; save the final break
mov rax, rcx
ret
generateRandomFloat:
saveRegisters
xor rax,rax
xor rbx,rbx
xor rdx,rdx
xor rcx,rcx
rdrand rbx
mov byte al, bl
xor rcx, rcx
add rcx, [randomRange]
div rcx
mov rax, rdx
mov rcx, [randomRange]
mov qword [randomIntPointer], rax
mov qword [randomRangePointer], rcx
fild qword [randomIntPointer]
fidiv dword [randomRangePointer]
fstp qword [randomFloatPointer]
getBackRegisters
ret
generateRandomSample:
saveRegisters
push rdi
generateRandomSampleLOOP:
mov qword rcx, [RANDOM_PRECISION]
inc rcx
rdrand rax ; get rdm int
xor rdx, rdx
idiv rcx ; get an int in rang [0;10^n]
dec rcx
mov qword [randomFloatPointer], rdx
fild qword [randomFloatPointer]
mov qword [randomFloatPointer], rcx
fild qword [randomFloatPointer]
fdivp
fld qword [randomHalf]
fsubp
fld qword [randomDouble]
fmulp
fstp qword [rbx]
add rbx, DOUBLE_SIZE
dec rdi
cmp qword rdi, 0
jnz generateRandomSampleLOOP
pop rdi
getBackRegisters
ret
insertListAtIndex:
push rax ; src-list length
push rbx ; src-list pointer
push rcx ; dst-list index
push r8 ; dst-list pointer
; Get the index in bytes
push rax ; save the rax register
mov rax, rcx
mul r9 ; multiply the index by the size of a list element
; Go to index
add qword r8, rax
pop rax ; load back the rax register
; Loading and setting loop
insertListAtIndexLOOP:
mov qword rdi, [rbx] ; load
mov qword [r8], rdi ; set
; Go to next index
add qword rbx, 8
add qword r8, 8
; looping stuff
dec rax
cmp rax,0
jnz insertListAtIndexLOOP
pop r8
pop rcx
pop rbx
pop rax
ret
showPseudoMatrix:
push rax
push rbx
push rcx
showPseudoMatrixLOOP:
mov qword rax, [rbx]
call displayFloat
add rbx, 8
dec rcx
cmp rcx, 0
jnz showPseudoMatrixLOOP
pop rcx
pop rbx
pop rax
ret