-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfib2.asm
1119 lines (995 loc) · 18.6 KB
/
fib2.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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;提示输入字符串
inNum segment
in_n_count db 0
entered db 0
in_n db 'please input a number(N): ','$' ;length=26
in_i db 'please input a number(I): ','$' ;length=26
in_m db 'please input a number(M): ','$' ;length=26
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
w_end db 'are you kidding me? see you~','$';length=28
is_warn dw 0
w_count dw 0
warn ends
;设置输入数字字符栈
num_input segment
db 32 dup(0)
top dw 0;数字栈顶
fib_length dw 0;序列长度
num_input ends
;保存上一次的初始数据
pre_data segment
p_tmp_16 dw 5 dup(0)
p_f1_16 dw 5 dup(0)
p_f2_16 dw 5 dup(0)
p_tmp_10 dw 5 dup(0)
p_f1_10 dw 5 dup(0)
p_f2_10 dw 5 dup(0)
pre_data ends
;设置栈段
stk segment
db 100h dup(0)
stk ends
;显示字符串
;设置16进制的数据段
radix_16_stk segment
tmp_16 dw 5 dup(0)
f1_16 dw 5 dup(0)
f2_16 dw 5 dup(0)
radix_16_stk ends
code segment
main proc
start:
;初始化各段
mov ax,stk
mov ss,ax
mov sp,0
call clean_screen
to_get_num:
mov ax,inNum
mov ds,ax
mov in_n_count,0
mov entered,0
mov ax,num_input
mov ds,ax
mov top,0;栈顶置0
call reset_16
call clean_screen
call cancel_warn
call show_inNum
call get_num
push ax
push bx
push ds
call solve_num
mov bx,radix_16_stk
mov ds,bx
mov f1_16,ax
mov bx,pre_data
mov ds,bx
mov p_f1_16,ax
pop ds
pop bx
pop ax
call clean_screen
mov top,0;栈顶置0
call cancel_warn
call show_inNum
call get_num
push ax
push bx
push ds
call solve_num
mov bx,radix_16_stk
mov ds,bx
mov f2_16,ax
mov bx,pre_data
mov ds,bx
mov p_f2_16,ax
pop ds
pop bx
pop ax
call clean_screen
mov top,0;栈顶置0
call cancel_warn
call show_inNum
call get_num
push ax
call solve_num
mov fib_length,ax
pop ax
call print_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 ah,0fh
int 10h
mov ah,0
int 10h
pop ax
ret
clean_screen endp
show_inNum proc
push ax
push ds
push dx
push bx
push cx
push di
;显示字符串
mov ax,inNum
mov ds,ax
mov di,160*9+21
cmp in_n_count,0
jne in_n_ok1
;转移光标位置
mov ah,02h
mov bh,0
mov dh,10
add dh,in_n_count
mov dl,10
int 10h
mov dx,offset in_n
add di,160
mov ah,09h
int 21h
call show_num_style
in_n_ok1:
cmp in_n_count,1
jne in_n_ok2
;转移光标位置
mov ah,02h
mov bh,0
mov dh,10
add dh,in_n_count
mov dl,10
int 10h
mov dx,offset in_i
add di,160*2
mov ah,09h
int 21h
call show_num_style
in_n_ok2:
cmp in_n_count,2
jb in_n_ok3
;转移光标位置
mov ah,02h
mov bh,0
mov dh,10
add dh,in_n_count
mov dl,10
int 10h
mov dx,offset in_m
add di,160*3
mov ah,09h
int 21h
call show_num_style
in_n_ok3:
pop di
pop cx
pop bx
pop dx
pop ds
pop ax
ret
show_inNum endp
show_num_style proc
push ax
push ds
push cx
push di
mov ax,0b800h
mov ds,ax
mov cx,25
show_s:
mov byte ptr ds:[di],01110010b
inc di
inc di
loop show_s
pop di
pop cx
pop ds
pop ax
ret
show_num_style 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
;下面两步控制是否支持输入除数字外的可打印字符读入num_input
cmp al,39h
ja 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
mov ax,inNum
mov ds,ax
mov dh,10
add dh,in_n_count
cmp entered,1
jne n_s_ok1
mov entered,0
inc in_n_count
; cmp in_n_count,3
; jne n_s_ok1
; mov in_n_count,0
n_s_ok1:
cmp top,0
je sret
;移动光标位置
mov ah,02h
mov dl,10+26
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:
push ds
mov ax,inNum
mov ds,ax
mov entered,1
pop ds
mov ah,0
mov al,'$'
call num_stk
mov ah,2
call num_stk
pop ax
ret
no_digit endp
end_pro proc
mov ax,4c00h
int 21h
end_pro endp
;将输入的数字转成10进制并放入ax
solve_num proc
call clean_screen
cmp top,0
je solve_num_ret
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 solve_num_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的值应小于等于100
cmp ax,64h
ja out_range_10
cmp dx,0
ja out_range_10
cmp ax,ax
je solve_num_ret
out_range_10:
mov bx,inNum
mov ds,bx
cmp in_n_count,3
jne solve_num_ret
call set_warn
call out_range
jmp far to_get_num
below_zero_10:
call set_warn
call below_zero
jmp far to_get_num
solve_num_ret:
pop dx
pop cx
pop bx
pop si
pop ds
ret
solve_num 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
mov ah,08h
int 21h
pop cx
pop di
pop ds
pop dx
pop bx
pop ax
ret
out_range endp
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
mov ah,08h
int 21h
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 is_warn,1
inc w_count
cmp w_count,3
jne set_warn_ret
call exit_show
set_warn_ret:
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 is_warn,0
pop di
pop ds
pop ax
ret
cancel_warn endp
;延迟时间
delay proc
push cx
mov cx,0ffffh
end_loop:
push cx
mov cx,66h
end_loop_:
nop
loop end_loop_
pop cx
loop end_loop
pop cx
ret
delay endp
exit_show proc
call clean_screen
;移动光标位置
mov ah,02h
mov bh,0
mov dh,12
mov dl,25
int 10h
;显示字符串
mov ax,warn
mov ds,ax
mov dx,offset w_end
mov ah,09h
int 21h
mov ax,0b800h
mov ds,ax
mov di,160*12+51
mov cx,28
show_out4:
mov ds:[di],11110100b
inc di
inc di
loop show_out4
call delay
call clean_screen
call end_pro
exit_show endp
reset_16 proc
push bx
push cx
push ds
push di
push es
push si
mov bx,radix_16_stk
mov ds,bx
mov bx,pre_data
mov es,bx
mov si,offset p_tmp_16
mov di,offset tmp_16
mov cx,9
reset_16_loop1:
mov bl,byte ptr es:[si]
mov byte ptr [di],bl
inc di
inc si
loop reset_16_loop1
mov di,offset f1_16
mov si,offset p_f1_16
mov cx,9
reset_16_loop2:
mov bl,byte ptr es:[si]
mov byte ptr [di],bl
inc di
inc si
loop reset_16_loop2
mov di,offset f2_16
mov si,offset p_f2_16
mov cx,9
reset_16_loop3:
mov bl,byte ptr es:[si]
mov byte ptr [di],bl
inc di
inc si
loop reset_16_loop3
pop si
pop es
pop di
pop ds
pop cx
pop bx
ret
reset_16 endp
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
cmp fib_length,0
ja short p_16_ok
pop ax
pop bx
pop es
pop cx
pop si
pop di
jmp far restart
p_16_ok:
mov cx,fib_length
call reset_16
mov bx,radix_16_stk
mov ds,bx
mov bx,0b800h
mov es,bx
mov si,4
mov bx,0
final_loop_16:
push cx
;输出f2_16,并使得tmp=f2 f2=f1+f2 f1=tmp
mov ax,offset f2_16
add ax,8
mov di,ax
mov cx,9
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,9
swap_tmp_16:
mov al,byte ptr [di]
mov byte ptr [si],al
inc di
inc si
loop swap_tmp_16
mov di,offset f1_16
mov si,offset f2_16
mov cx,9
clc;清除进位
add_f2_16:
mov al,byte ptr [di]
adc al,byte ptr [si]
pushf
mov byte ptr [si],al
inc di
inc si
popf
loop add_f2_16
mov di,offset tmp_16
mov si,offset f1_16
mov cx,9
swap_f1_16:
mov al,byte ptr [di]
mov byte ptr [si],al
inc di
inc si
loop swap_f1_16
pop si
pop cx
inc bx
cmp bx,4
jne ok2
add si,8
mov bx,0
ok2:
loop final_loop_16
16_ret:
pop ax
pop bx
pop es
pop cx
pop si
pop di
ret
print_16 endp
reset_10 proc
push ax
push ds
push si
push cx
mov ax,pre_data
mov ds,ax
lea si, p_tmp_10
mov ax,0
mov cx,5
r_10_loop1:
mov ds:[si],ax
add si,2
loop r_10_loop1
lea si, p_f1_10
mov ax,0
mov cx,5
r_10_loop2:
mov ds:[si],ax
add si,2
loop r_10_loop2
lea si, p_f2_10
mov ax,0
mov cx,5
r_10_loop3:
mov ds:[si],ax
add si,2
loop r_10_loop3
pop cx
pop si
pop ds
pop ax
ret
reset_10 endp
change_radix proc
push ax
push ds
push si
push di
push bx
push cx
mov ax,pre_data
mov ds,ax
mov cx,10
lea si,p_tmp_16
lea di,p_tmp_10
add bx,0
c_r_loop1:
mov al,byte ptr [si]
adc al,0
daa
mov byte ptr [di],al
pushf
inc si
inc di
popf
loop c_r_loop1
mov cx,3
lea si,p_f1_16
lea di,p_f1_10
mov ax,word ptr [si]
c_r_loop2:
mov dx,0
mov bx,10
div bx
mov bx,0
mov bl,dl
shl bx,4
push bx
mov bx,10
mov dx,0
div bx
pop bx
mov bh,dl
shr bx,4
mov byte ptr [di],bl
inc di
loop c_r_loop2
mov cx,3
lea si,p_f2_16
lea di,p_f2_10
mov ax,word ptr [si]
c_r_loop3:
mov dx,0
mov bx,10
div bx
mov bx,0
mov bl,dl
shl bx,4
push bx
mov bx,10
mov dx,0
div bx
pop bx
mov bh,dl
shr bx,4
mov byte ptr [di],bl
inc di
loop c_r_loop3
pop cx
pop bx
pop di
pop si
pop ds
pop ax
ret
change_radix endp
print_10 proc
push di
push si
push cx
push es