-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sqrt8086.asm
382 lines (278 loc) · 6.52 KB
/
Sqrt8086.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
Name "Sqrt8086"
org 100h
.MODEL small
.STACK 512d
.DATA
readMsg db "Type a number to calculate the SquareRoot of it (Max: 65534):$"
overflowMsg db "The number must be lower than 65534!$"
loadingMsg db "Calculating...$"
resultMsg db "Average SquareRoot: $"
finishMsg db "Press any key to continue | Press ESC to exit:$"
stackCacheLevel1 dw 0
stackCacheLevel2 dw 0
inputArray db 6 DUP(?)
precisionFactor dw 100
.CODE
start:
call clearScreen
call read
mov cx, ax
call verifyNumber
lea dx, loadingMsg
call printString
mov ax, cx
call calcSqrt
mov cx, ax
call printCRLF
lea dx, resultMsg
call printString
mov ax, cx
call printFloat
call finish
ret
;call exit
; Saves registers state
saveState:
pop stackCacheLevel2
push ax
push bx
push cx
push dx
push stackCacheLevel2
ret
; Loads registers state
loadState:
pop stackCacheLevel2
pop dx
pop cx
pop bx
pop ax
push stackCacheLevel2
ret
; Reads a number from Cin with 5 characters max.
;@returns: Number -> AX
read:
pop stackCacheLevel1
lea dx, readMsg
call printString
call printCRLF
mov ah, 02
mov dl, 41
int 21h
xor bx, bx
lea si, inputArray
mov cx, 6
readLoop:
mov ah, 01
int 21h
cmp al, 13
je fnshRead
xor ah, ah
sub al, 48
mov [si], ax
inc si
inc bx ; BX = char length
Loop readLoop:
fnshRead:
lea si, inputArray
mov cx, bx
xor dx, dx
convertLoop:
mov ax, 10
mul dx ; AX = DX * 10
mov bx, [si]
xor bh, bh
inc si
add ax, bx ; AX = (DX * 10) + BX
mov dx, ax
Loop convertLoop
fnshConvert:
mov ax, dx
push stackCacheLevel1
ret
; Calculates the Sqrt of a 16 bit integer
; @args: source -> AX
; @returns: integer result -> AX, decimal result -> BX
calcSqrt:
pop stackCacheLevel1
push 0
push 0
push ax ; Stack = source
; aprox = (source / 200) + 2
xor dx, dx
mov bx, 200
div bx
add ax, 2
mov bx, ax
push ax ; Stack = aprx
; BX -> aprx
; CX -> source
calcLoop:
pop bx ; Stack = aprox(current)
pop cx ; Stack = source
; If aprx == aprx(cache) it means that the squareroot of source
; is a floating point number, which is not supported by the 8068
; and thus, can't be calculated precisely.
pop ax
cmp bx, ax
je fnshAvrgSqrt
; checking if aprx² == source
mov ax, bx ; AX = aprx
xor dx, dx
mul bx
cmp ax, cx
je fnshSqrt
pop dx ; Clearing aprx(decimal) as result was not achieved
push bx ; Store cache of aprx to be compared in the next loop iteration
; aprx(BX) = ((source(CX) / aprx) + aprx) / 2
mov ax, cx ; AX = source
xor dx, dx
div bx
add ax, bx
shr ax, 1
mov bx, ax
push cx ; Stack = source
push bx ; Stack = aprx(current)
; Stack = aprx * 100
mov dx, precisionFactor
mul dx
push ax
; aprox = ((source * 100 / aprx) + aprx * 100) / 2
mov ax, precisionFactor
mul cx ; source * 100
div bx ; (source * 100 / aprx)
mov cx, ax
mov ax, precisionFactor
mul bx
add ax, cx ; (source * 100 / aprx) + aprx * 100)
adc dx, 0 ; (source * 100 / aprx) + aprx * 100) + Carry
mov cx, 2
div cx ; AX = ((source * 100 / aprx) + aprx * 100) / 2
pop bx ; BX = aprx * 100 lower
sub ax, bx ; AX = (((source * 100 / aprx) + aprx * 100) / 2) - aprx * 100
pop bx ; BX = aprx
pop cx ; CX = source
pop dx ; DX = aprx(cache)
push ax ; Stack = aprx(decimal)
push dx
push cx
push bx
JMP calcLoop
; The result is an integer number
fnshSqrt:
mov ax, bx ; AX = aprox(Sqrt)
pop bx
xor bx, bx ; BX = Sqrt(decimal)
push stackCacheLevel1
ret
; The result is a floating point number
fnshAvrgSqrt:
mov ax, bx ; AX = aprox(Sqrt)
pop bx ; BX = Sqrt(decimal)
push stackCacheLevel1
ret
; printString a String in Cout.
; @args: String pointer -> DX
printString:
call printCRLF
mov ah, 09
int 21h
mov dx, 0
ret
; printString a 16Bit-Integer + 8Bit-Precision(decimal) Number
;@args: Number -> AX, Precision -> BX
printFloat:
call printNumber
mov ah, 02
mov dl, 46
int 21h
mov ax, bx
call printNumber
fnshprintFloat:
ret
; printString an Integer number.
;@args: Number -> AX
printNumber:
call saveState
cmp ax, 100
jae printThreeDigits
cmp ax, 10
jae printTwoDigits
; 1 digit:
printOneDigit:
mov ah, 02
mov dl, al
add dl, 48
int 21h
call loadState
ret
; 2 digits:
printTwoDigits:
aam
add ah, 48
add al, 48
mov cx, ax
mov ah, 02
mov dl, ch
int 21h
mov dl, cl
int 21h
call loadState
ret
; 3 digits:
printThreeDigits:
aam
mov dh, al
mov al, ah
mov ah, 0
aam
add ah, 48
add al, 48
add dh, 48
mov cx, ax
mov ah, 02
mov dl, ch
int 21h
mov dl, cl
int 21h
mov dl, dh
int 21h
call loadState
ret
verifyNumber:
cmp ax, 65534
jbe continue
; Overflow:
lea dx, overflowMsg
call printString
call finish
; Continue:
continue:
ret
; Printing CRLF (carriage return + line feed)
printCRLF:
call saveState
mov ah, 02
mov dl, 13
int 21h
mov dl, 10
int 21h
call loadState
ret
clearScreen:
xor ah, ah
mov al, 03h
int 10h
ret
finish:
lea dx, finishMsg
call printString
mov ah, 01
int 21h
cmp al, 27
jne start
ret
exit:
mov ah, 4Ch
int 21h
ret