-
Notifications
You must be signed in to change notification settings - Fork 0
/
fib.asm
874 lines (775 loc) · 14.7 KB
/
fib.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
;提示输入字符串
inNum segment
db 'please input a number: ','$' ;length=23
inNum ends
;警告
warn segment
w_range db 'the number is out of range...','$' ;length=29
w_below_zero db 'the number must be positive!','$' ;length=28
w_restart db 'press any key to continue...','$';length=28
is_warn dw 0
warn ends
;设置输入数字字符栈
num_input segment
db 32 dup(0)
top dw 0;数字栈顶
fib_length dw 0;序列长度
num_input ends
;设置栈段
stk segment
db 200h dup(0)
stk ends
;显示字符串
;设置16进制的数据段
radix_16_stk segment
tmp_16 dd 0
f1_16 dd 0
f2_16 dd 0
radix_16_stk ends
;设置10进制的数据段
radix_10_stk segment
tmp_10 db 5 dup(0)
f1_10 db 5 dup(0)
f2_10 db 5 dup(0)
radix_10_stk ends
code segment
main proc
start:
;初始化各段
mov ax,stk
mov ss,ax
mov sp,0
call clean_screen
to_get_num: ;重开
mov ax,num_input
mov ds,ax
mov bx,0
mov word ptr ds:[32],bx;栈顶置0
call cancel_warn
call show_inNum
call get_num
call do_16
mov dx,1
jmp short restart
pattern:
cmp dx,1
je to_print_16
call print_10
jmp short restart
to_print_16:
call print_16
;要不要重开,小老弟?
restart:
mov ah,07h
int 21h
call clean_screen
cmp al,'q' ;exit
je to_end
cmp al,'Q' ;exit
je to_end
cmp al,' ' ;切换16进制或者10进制表达
jne short to_get_num
mov bx,warn
mov ds,bx
mov bx,offset is_warn
cmp word ptr [bx],1
je short to_get_num
cmp dx,1
je ok1
mov dx,1
jmp short pattern
ok1:
mov dx,0
jmp short pattern
to_end:
call end_pro
main endp
clean_screen proc
; push ax
; mov ax,3
; int 10h
; pop ax
push ax
mov ah,0fh
int 10h
mov ah,0
int 10h
pop ax
;下面这段在emu8086特别慢,不推荐使用
; push bx
; push es
; push cx
; push ax
;
; mov bx,0b800h
; mov es,bx
; mov bx,0
; mov cx,4000
;clean:
; mov ax,0
; mov es:[bx],ax
; add bx,2
; loop clean
;
; pop ax
; pop cx
; pop es
; pop bx
ret
clean_screen endp
;提示输入数字
show_inNum proc
push ax
push ds
push dx
push bx
push cx
push di
;转移光标位置
mov ah,02h
mov bh,0
mov dh,12
mov dl,10
int 10h
;显示字符串
mov ax,inNum
mov ds,ax
mov dx,0h
mov ah,09h
int 21h
mov ax,0b800h
mov ds,ax
mov di,160*12+21
mov cx,22
show_s:
mov ds:[di],11110010b
inc di
inc di
loop show_s
pop di
pop cx
pop dx
pop ds
pop ax
pop bx
ret
show_inNum endp
;处理输入的数字,可回退数字
get_num proc
push ax
push ds
push dx
mov ax,num_input
mov ds,ax
mov si,0
call get_num_
pop dx
pop ds
pop ax
ret
get_num endp
get_num_ proc
push ax
_get_num_:
mov ah,0
int 16h;INT16H 00H读取键盘字符
cmp al,30h ;数字?
jb no_digit
mov ah,0
call num_stk
mov ah,2
call num_stk
jmp _get_num_
pop ax
get_num_ endp
;以ah为选项,ah=0、1、2分别代表入栈、出栈、显示字符串
num_stk proc
jmp short num_start
num_start:
push bx
push dx
push di
cmp ah,0
je num_push
cmp ah,1
je num_pop
cmp ah,2
je num_show
jmp sret
num_push:
mov bx,top
mov [bx],al
mov [bx+1],'$'
inc top
jmp sret
num_pop:
cmp top,0
je sret
dec top
mov bx,top
mov al,[bx];将出栈字符放入al中
mov [bx],'$'
jmp sret
num_show:
call clean_screen
call show_inNum
cmp top,0
je sret
;移动光标位置
mov ah,02h
mov bh,0
mov dh,12
mov dl,10+23
int 10h
;显示字符串
mov ax,num_input
mov ds,ax
mov dx,0h
mov ah,09h
int 21h
sret:
pop di
pop dx
pop bx
ret
num_stk endp
;输入非数字的处理方法
no_digit proc
cmp ah,0ch
je minus
cmp ah,0eh
je backspace
cmp ah,1ch
je enter
jmp _get_num_
minus:
mov ah,0
call num_stk
mov ah,2
call num_stk
jmp _get_num_
backspace:
mov ah,1
call num_stk
mov ah,2
call num_stk
jmp _get_num_
enter:
mov ah,0
mov al,'$'
call num_stk
pop ax
ret
no_digit endp
end_pro proc
mov ax,4c00h
int 21h
end_pro endp
;处理16进制fibonacci
do_16 proc
call clean_screen
cmp top,0
je do_16_ret
push ax
push ds
push si
push bx
push cx
push dx
mov ax,num_input
mov ds,ax
mov ax,0
mov si,0
mov bx,0
mov dx,0
mov cx,top
dec cx
;获取十进制数并放在ax中
cmp byte ptr ds:[si],'-'
je below_zero_10
cmp byte ptr ds:[si],'$'
je do_16_ret
get_num_10:
mov bl,byte ptr ds:[si]
sub bl,30h
add al,bl
adc ah,0
mov bx,10
mul bx
inc si
loop get_num_10
mov bx,10
div bx
;ax的值应小于等于49
cmp ax,31h
ja out_range_10
cmp dx,0
ja out_range_10
cmp ax,0
je do_16_ret
mov cx,ax
push si
mov si,offset fib_length
mov [si],cx
pop si
call print_16
jmp short do_16_ret
out_range_10:
call set_warn
call out_range
jmp short do_16_ret
below_zero_10:
call set_warn
call below_zero
jmp short do_16_ret
do_16_ret:
pop dx
pop cx
pop bx
pop si
pop ds
pop ax
ret
do_16 endp
;数字越界?
out_range proc
call clean_screen
push ax
push bx
push dx
push ds
push di
push cx
;移动光标位置
mov ah,02h
mov bh,0
mov dh,12
mov dl,25
int 10h
;显示字符串
mov ax,warn
mov ds,ax
mov dx,offset w_range
mov ah,09h
int 21h
mov ax,0b800h
mov ds,ax
mov di,160*12+51
mov cx,29
show_out0:
mov ds:[di],11110100b
inc di
inc di
loop show_out0
;显示press to continue
mov ah,02h
mov bh,0
mov dh,13
mov dl,25
int 10h
mov ax,warn
mov ds,ax
mov dx,offset w_restart
mov ah,09h
int 21h
mov ax,0b800h
mov ds,ax
mov di,160*13+51
mov cx,28
show_out1:
mov ds:[di],11110100b
inc di
inc di
loop show_out1
pop cx
pop di
pop ds
pop dx
pop bx
pop ax
ret
out_range endp
;数字小于0?
below_zero proc
call clean_screen
push ax
push bx
push dx
push ds
push di
push cx
;移动光标位置
mov ah,02h
mov bh,0
mov dh,12
mov dl,25
int 10h
;显示字符串
mov ax,warn
mov ds,ax
mov dx,offset w_below_zero
mov ah,09h
int 21h
mov ax,0b800h
mov ds,ax
mov di,160*12+51
mov cx,28
show_out2:
mov ds:[di],11110100b
inc di
inc di
loop show_out2
;显示press to continue
mov ah,02h
mov bh,0
mov dh,13
mov dl,25
int 10h
mov ax,warn
mov ds,ax
mov dx,offset w_restart
mov ah,09h
int 21h
mov ax,0b800h
mov ds,ax
mov di,160*13+51
mov cx,28
show_out3:
mov ds:[di],11110100b
inc di
inc di
loop show_out3
pop cx
pop di
pop ds
pop dx
pop bx
pop ax
ret
below_zero endp
;设置警告
set_warn proc ;set warning
push ax
push ds
push di
mov ax,warn
mov ds,ax
mov di,offset is_warn
mov word ptr [di],1
pop di
pop ds
pop ax
ret
set_warn endp
;撤销警告
cancel_warn proc ;cancel warning
push ax
push ds
push di
mov ax,warn
mov ds,ax
mov di,offset is_warn
mov word ptr [di],0
pop di
pop ds
pop ax
ret
cancel_warn endp
;radix_16_stk segment
; tmp_16 dd 0
; f1_16 dd 0
; f2_16 dd 0
;radix_16_stk ends
;重置16进制数据段
reset_16 proc
push bx
push cx
push ds
push di
mov bx,radix_16_stk
mov ds,bx
mov di,offset tmp_16
mov byte ptr [di],0
mov cx,3
inc di
reset_16_loop1:
mov byte ptr [di],0
inc di
loop reset_16_loop1
mov di,offset f1_16
mov byte ptr [di],0
mov cx,3
inc di
reset_16_loop2:
mov byte ptr [di],0
inc di
loop reset_16_loop2
mov di,offset f2_16
mov byte ptr [di],1
mov cx,3
inc di
reset_16_loop3:
mov byte ptr [di],0
inc di
loop reset_16_loop3
pop di
pop ds
pop cx
pop bx
ret
reset_16 endp
;打印16进制序列
print_16 proc
push di
push si
push cx
push es
push bx
push ax
call clean_screen
mov ax,num_input
mov ds,ax
mov si,offset fib_length
mov cx,[si]
call reset_16
mov bx,radix_16_stk
mov ds,bx
mov bx,0b800h
mov es,bx
mov si,8
mov bx,0
final_loop_16:
push cx
;输出f2_16,并使得tmp=f2 f2=f1+f2 f1=tmp
mov ax,offset f2_16
add ax,3
mov di,ax
mov cx,4
show_f2_16:
mov al,byte ptr ds:[di]
push bx
mov bx,ax
shr al,4
mov ah,al;存高4位
mov al,bl
and al,00001111b;存低4位
pop bx
cmp ah,9
jbe change_to_char_16_h
add ah,41h
sub ah,10
sub ah,30h
change_to_char_16_h:
add ah,30h
mov byte ptr es:[si],ah
mov ah,00000010b
mov byte ptr es:[si+1],ah
add si,2
cmp al,9
jbe change_to_char_16_l
add al,41h
sub al,10
sub al,30h
change_to_char_16_l:
add al,30h
mov byte ptr es:[si],al
mov al,00000010b
mov byte ptr es:[si+1],al
add si,2
dec di
loop show_f2_16
add si,2 ;往后两个字节,为了保证数据间有间隔
push si
mov di,offset f2_16
mov si,offset tmp_16
mov cx,2
swap_tmp_16:
mov ax,word ptr [di]
mov word ptr [si],ax
add di,2
add si,2
loop swap_tmp_16
mov di,offset f1_16
mov si,offset f2_16
mov cx,2
clc;清除进位
add_f2_16:
mov ax,word ptr [di]
adc ax,word ptr [si]
pushf
mov word ptr [si],ax
add di,2
add si,2
popf
loop add_f2_16
mov di,offset tmp_16
mov si,offset f1_16
mov cx,2
swap_f1_16:
mov ax,word ptr [di]
mov word ptr [si],ax
add di,2
add si,2
loop swap_f1_16
pop si
pop cx
inc bx
cmp bx,8
jne ok2
add si,16
mov bx,0
ok2:
loop final_loop_16
pop ax
pop bx
pop es
pop cx
pop si
pop di
ret
print_16 endp
;radix_10_stk segment
; tmp_10 db 5 dup(0)
; f1_10 db 5 dup(0)
; f2_10 db 5 dup(0)
;radix_10_stk ends
;重置十进制数据段
reset_10 proc
push bx
push cx
push ds
push di
mov bx,radix_10_stk
mov ds,bx
mov di,offset tmp_10
mov byte ptr [di],0
mov cx,4
inc di
reset_10_loop1:
mov byte ptr [di],0
inc di
loop reset_10_loop1
mov di,offset f1_10
mov byte ptr [di],0
mov cx,4
inc di
reset_10_loop2:
mov byte ptr [di],0
inc di
loop reset_10_loop2
mov di,offset f2_10
mov byte ptr [di],1
mov cx,4
inc di
reset_10_loop3:
mov byte ptr [di],0
inc di
loop reset_10_loop3
pop di
pop ds
pop cx
pop bx
ret
reset_10 endp
;打印十进制数据
print_10 proc
push di
push si
push cx
push es
push bx
push ax
call clean_screen
mov ax,num_input
mov ds,ax
mov si,offset fib_length
mov cx,[si]
call reset_10
mov bx,radix_10_stk
mov ds,bx
mov bx,0b800h
mov es,bx
mov si,2
mov bx,0
final_loop_10:
push bx
push cx
mov ax,offset f2_10
add ax,4
mov di,ax
mov cx,5
show_f_10:
mov al,byte ptr ds:[di]
push bx
mov bx,ax
shr al,4
mov ah,al;存高4位
mov al,bl
and al,00001111b;存低4位
pop bx
add ah,30h
mov byte ptr es:[si],ah
mov ah,00000010b
mov byte ptr es:[si+1],ah
add si,2
add al,30h
mov byte ptr es:[si],al
mov al,00000010b
mov byte ptr es:[si+1],al
add si,2
dec di
loop show_f_10
add si,2
push si
mov di,offset f2_10
mov si,offset tmp_10
mov cx,5
swap_tmp_10:
mov al,byte ptr [di]
mov byte ptr [si],al
inc di
inc si
loop swap_tmp_10
mov di,offset f1_10
mov si,offset f2_10
mov cx,5
clc
add_f2_10:
mov al,byte ptr [di]
adc al,byte ptr [si]
daa
pushf
mov byte ptr [si],al
inc di
inc si
popf
loop add_f2_10
mov di,offset tmp_10
mov si,offset f1_10
mov cx,5
swap_f1_10:
mov al,byte ptr [di]
mov byte ptr [si],al
inc di
inc si
loop swap_f1_10
pop si
pop cx
pop bx
inc bx
cmp bx,7
jne ok3
add si,6
mov bx,0
ok3:
loop final_loop_10
pop ax
pop bx
pop es
pop cx
pop si
pop di
ret
print_10 endp
code ends
end start