/
equation-solver.asm
463 lines (335 loc) · 7.67 KB
/
equation-solver.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
;;;;
;;
;; This program solves the equation: ax+b=0.
;;
;; Copyright (C) 2009 Efstathios Chatzikyriakidis (stathis.chatzikyriakidis@gmail.com)
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;;;
;;
;; build options.
;;
; name of the bin.
name "equation-solver"
; bin is plain binary format similar to com
; format, but not limited to only 1 segment.
#make_bin#
; set loading address.
#LOAD_SEGMENT=0100H#
#LOAD_OFFSET=0000H#
;;
;; registers' initial value.
;;
; set entry point.
#CS=0100H# ; same as loading segment.
#IP=0000H# ; same as loading offset.
; set segment registers.
#DS=0100H# ; same as loading segment.
#ES=0100H# ; same as loading segment.
; set general registers.
#AX=0000H#
#BX=0000H#
#CX=0000H#
#DX=0000H#
#SI=0000H#
#DI=0000H#
#BP=0000H#
;;
;; library includes.
;;
; include macro definitions
; library for input/output.
INCLUDE "emu8086.inc"
;;
;; working point.
;;
; clear the screen.
CALL CLEAR_SCREEN
; print header message.
CALL print_header_msg
; input data offset.
CALL input_data_offset
; store the data offset.
MOV DI, CX
;;
;; data memory initial values.
;;
; init with zero values the data region.
MOV [0600H+DI], 00H ; 1st byte data offset.
MOV [0601H+DI], 00H ; 2nd byte ^
MOV [0602H+DI], 00H ; 1st byte value a.
MOV [0603H+DI], 00H ; 2nd byte ^
MOV [0604H+DI], 00H ; 1st byte value b.
MOV [0605H+DI], 00H ; 2nd byte ^
MOV [0606H+DI], 00H ; 1st byte quotient.
MOV [0607H+DI], 00H ; 2nd byte ^
MOV [0608H+DI], 00H ; 1st byte remainder.
MOV [0609H+DI], 00H ; 2nd byte ^
; sign flag.
MOV [060AH+DI], 00H
; print new line.
PRINTN
; input equation value a.
CALL input_value_a
; store the value a.
MOV AX, CX
; print new line.
PRINTN
; input equation value b.
CALL input_value_b
; store the value b.
MOV BX, CX
; store a, b values and data
; offset to the data region.
MOV [0600H+DI], DI ; store data offset.
MOV [0602H+DI], AX ; store value a.
MOV [0604H+DI], BX ; store value b.
; print new lines.
PRINTN
PRINTN
; if the value a is zero then the
; equation is probably impossible.
CMP AX, 0000H
JE maybe_impossible
; if the value b is zero then
; the equation has no meaning.
CMP BX, 0000H
JE meaningless
; print calculation message.
PRINT 'Calculating...'
; print new lines.
PRINTN
PRINTN
; do the division and return both
; the quotient and the remainder.
CALL signed_division
; store both the quotient and the
; remainder value to data region.
MOV [0606H+DI], DX ; store the quotient.
MOV [0608H+DI], BX ; store the remainder.
; have the root of the equation by
; doing the complement of quotient.
NEG DX
; print the result message.
PRINT 'The result is: x='
; print the root value.
MOV AX, DX
CALL PRINT_NUM
; print start remainder message.
PRINT ' (Remainder: '
; print the remainder value.
MOV AX, BX
CALL PRINT_NUM
; print end remainder message.
PRINT ').'
; jump to halt and terminate.
JMP halt
;;
;; the following jump regions are used in order to find
;; if the equation is impossible, vague or meaningless.
;;
; equation maybe is impossible. we must
; check also the value of b to be sure.
maybe_impossible:
; if the value b is zero
; the equation is vague.
CMP BX, 0000H
JE vague
; print impossible equation message.
PRINT 'The equation is impossible.'
; jump to halt and terminate.
JMP halt
; equation is vague.
vague:
; print vague equation message.
PRINT 'The equation is vague.'
; jump to halt and terminate.
JMP halt
; equation is meaningless.
meaningless:
; print meaningless equation message.
PRINT 'The equation has no meaning.'
; jump to halt and terminate.
JMP halt
; halt the program.
halt: HLT
;;
;; routines.
;;
; routine which prints header info.
print_header_msg:
; print the header message of the program.
PRINT 'Solving Equation: [ax+b=0].'
; print new line.
PRINTN
; return.
RET
; routine which inputs value a.
input_value_a:
; print input message.
PRINT 'Input value for 'a' (dec): '
; input value from user.
CALL SCAN_NUM
; return.
RET
; routine which inputs value b.
input_value_b:
; print input message.
PRINT 'Input value for 'b' (dec): '
; input value from user.
CALL SCAN_NUM
; return.
RET
; routine which inputs data offset.
input_data_offset:
; loop until the user gets a positive
; decimal value for the data address.
positive_data_offset:
; print new line.
PRINTN
; print input message.
PRINT 'Input data offset (dec>=0): '
; input value from user.
CALL SCAN_NUM
; check if the input is negative.
CMP CX, 0000H
JL positive_data_offset
; return.
RET
; routine which does signed division.
signed_division:
; if value b is negative
; then make it positive.
CMP BX, 0000H
JL complement_b
; key position a.
temp_a:
; if value a is negative
; then make it positive.
CMP AX, 0000H
JL complement_a
; key position b.
temp_b:
; if value b is less than value a.
CMP BX, AX
JL b_less_than_a
; perform divisions using substracts and
; return the quotient and the remainder.
substracts:
; increase quotient by one.
INC DX
; substact b and a values.
SUB BX, AX
; if the result of the substraction is
; above or equal to value a then loop.
CMP BX, AX
JAE substracts
; store to the register the sign flag.
MOV CL, [060AH+DI]
; if sign flag is zero alt
; the sign of the quotient.
CMP CL, 00H
JE complement_quotient
; if sign flag is one change
; the sign of the remainder.
CMP CL, 01H
JE complement_remainder
; if sign flag is three alt
; the sign of both values.
CMP CL, 03H
JE complement_div_vals
; here the division ends.
end_div:
; complement the quotient.
NEG DX
; return the routine.
RET
;;
;; signed division jump regions.
;;
; value b is less than value a.
b_less_than_a:
; store to the register the sign flag.
MOV CL, [060AH+DI]
; if sign flag is one change
; the sign of the remainder.
CMP CL, 01H
JE complement_remainder
; if sign flag is three alt
; the sign of the remainder.
CMP CL, 03H
JE complement_remainder
; end the process.
JMP end_div
; return the complement of remainder.
complement_remainder:
; get the complement of it.
NEG BX
; end the process.
JMP end_div
; return the complement of quotient.
complement_quotient:
; get the complement of it.
NEG DX
; end the process.
JMP end_div
; return the complement of div values.
complement_div_vals:
; get the complement of them.
NEG BX
NEG DX
; end the process.
JMP end_div
; we are in the initial
; state of sign values.
initial_sign_state:
; store to the sign flag value three.
MOV [060AH+DI], 03H
; jump to key position b.
JMP temp_b
; return the complement of value b.
complement_b:
; store to the sign flag value one.
MOV [060AH+DI], 01H
; get the complement of it.
NEG BX
; jump to key position a.
JMP temp_a
; return the complement of value a.
complement_a:
; get the complement of it.
NEG AX
; store to the register the sign flag.
MOV CL, [060AH+DI]
; if sign flag is one then
; go to initial sign state.
CMP CL, 01H
JE initial_sign_state
; store to the sign flag value two.
MOV [060AH+DI], 02H
; jump to key position b.
JMP temp_b
;;
;; i/o routines' defines.
;;
; routine for input signed nums.
DEFINE_SCAN_NUM
; routine for printing signed nums.
DEFINE_PRINT_NUM
; routine for printing unsigned nums.
DEFINE_PRINT_NUM_UNS
; routine to clear the screen.
DEFINE_CLEAR_SCREEN
END