不用多说了,就直接代码吧
1
assume
cs:
codesg,
ds:
data
2 data segment
3 ; 00 is filler a
4 ; 317ah is 12666 hex value
5 ; 0000h is high bits. if the number is big and hex value is f317ah, then here 0000h will be 0000fh
6 ; 000Ah is hex value of 10.
7 ; rest of 0008h are just filler to make the debug view eaiser
8 dw 00 ,317ah,0000h,000Ah,0008h,0008h,0008h,0008h
9
10 ; here will hold decimal text
11 db ' '
12 data ends
13 codesg segment
14 start:
15 mov ax, data ; set data address
16 mov ds, ax
17 mov bx, 16 ; set decial text text offset in bx, because declare 8 words, so here is 16
18 mov cx, 0
19 push cx ; push a zero in bottom of stack, and it will be terminator of text
20 divNext:
21 CALL DivDW ; return remainder in [0] position, and quotient is still in [2] for next divide
22 mov cx, ds: [ 0 ] ; set remainder in cx
23 add cx, 30h ; add decimal ascii
24 push cx ; save the decimal in stack
25
26 mov ax, ds: [ 2 ] ; using bit OR to merge Divident H to Divident Low
27 OR ax, ds: [ 4 ] ; Divident H is ds:[4], Divident L is ds:[2]
28 MOV cx, ax ; Then check whether ds:[2] is 0 or not
29 jcxz EXIT_DN ; if quotient is 0, exit divNext
30
31 jmp divNext ; continue get next decimal value
32
33 EXIT_DN: ; When run to here, the stack contain all decimal ascii
34 pop cx ; get decimal ascii from stack
35 jcxz Exit_DT ; if ascii value in stack is 0, then exit.
36 ; This zero is the terminator pushed at line 19
37 mov ds: [bx+ 16 ], cx ; move the ascii to data section declared at line 11
38 add bx, 1
39 jmp EXIT_DN
40
41 Exit_DT:
42 Call Show_Str ; Show_Str show text from memory declared at line 11
43
44 mov ax, 4c00h
45 int 21h
46
47 ; ********************************************************************
48 ; CONVERT to DECIMAL
49 ; divisor 除数
50 ; divisor 除数 can 8 bits or 16 bits and can be in register or memory
51 ; dividend 被除数 , have to be AX or DX and AX,
52 ; divisor is 8 bits, then dividend is in AX. | the result is AL = quotient 商, AH = remainder 余
53 ; divisor is 16 bits, then dividend is in DX(H) and AX(L) | the result is AX = quotient 商, DX = remainder 余
54
55 divDW: ; chapter: 8.7 Page 157
56 pop BP
57 push bx
58 MOV BX, ds: [ 2 ] ; 4240 Divident(L)
59 MOV AX , ds: [ 4 ] ; 000f Divident(H)
60 MOV CX , ds: [ 6 ] ; 000A Divisor
61 MOV DX, 0
62 DIV CX ; (DX(0) + AX(000f)) / CX
63 mov ds: [ 4 ], ax
64 MOV AX, BX ; move Divident (L) in AX
65 DIV CX ; (DX(0005) + AX (4240)) / CX
66 mov ds: [ 2 ], ax
67 mov ds: [ 0 ], dx
68 pop bx
69 PUSH BP ; SET IP back to stack
70 ret
71
72 ; ***********************************************************
73 ; ****** Show Text
74 show_str:
75 pop bp
76 mov ax, 0b86eh
77 mov ss, ax ; row
78 mov si, 40 ; Column
79 mov bx, 0 ; String stard postion
80 mov ah, 00000010B ; Font
81 push ss
82 push si
83 push ds
84 push bx
85 main:
86 mov cl, ds: [bx+ 16 ] ; ds:[bx] is the character
87 mov al, cl
88 mov ch, 0
89 jcxz EXIT_SS
90 mov ss: [si], ax ; this ss:[si+bx] is character position
91 inc bx
92 add si, 2
93 jmp short main
94
95 EXIT_SS:
96 pop bx
97 pop ds
98 pop si
99 pop ss
100 push bp
101 ret
102 codesg ends
2 data segment
3 ; 00 is filler a
4 ; 317ah is 12666 hex value
5 ; 0000h is high bits. if the number is big and hex value is f317ah, then here 0000h will be 0000fh
6 ; 000Ah is hex value of 10.
7 ; rest of 0008h are just filler to make the debug view eaiser
8 dw 00 ,317ah,0000h,000Ah,0008h,0008h,0008h,0008h
9
10 ; here will hold decimal text
11 db ' '
12 data ends
13 codesg segment
14 start:
15 mov ax, data ; set data address
16 mov ds, ax
17 mov bx, 16 ; set decial text text offset in bx, because declare 8 words, so here is 16
18 mov cx, 0
19 push cx ; push a zero in bottom of stack, and it will be terminator of text
20 divNext:
21 CALL DivDW ; return remainder in [0] position, and quotient is still in [2] for next divide
22 mov cx, ds: [ 0 ] ; set remainder in cx
23 add cx, 30h ; add decimal ascii
24 push cx ; save the decimal in stack
25
26 mov ax, ds: [ 2 ] ; using bit OR to merge Divident H to Divident Low
27 OR ax, ds: [ 4 ] ; Divident H is ds:[4], Divident L is ds:[2]
28 MOV cx, ax ; Then check whether ds:[2] is 0 or not
29 jcxz EXIT_DN ; if quotient is 0, exit divNext
30
31 jmp divNext ; continue get next decimal value
32
33 EXIT_DN: ; When run to here, the stack contain all decimal ascii
34 pop cx ; get decimal ascii from stack
35 jcxz Exit_DT ; if ascii value in stack is 0, then exit.
36 ; This zero is the terminator pushed at line 19
37 mov ds: [bx+ 16 ], cx ; move the ascii to data section declared at line 11
38 add bx, 1
39 jmp EXIT_DN
40
41 Exit_DT:
42 Call Show_Str ; Show_Str show text from memory declared at line 11
43
44 mov ax, 4c00h
45 int 21h
46
47 ; ********************************************************************
48 ; CONVERT to DECIMAL
49 ; divisor 除数
50 ; divisor 除数 can 8 bits or 16 bits and can be in register or memory
51 ; dividend 被除数 , have to be AX or DX and AX,
52 ; divisor is 8 bits, then dividend is in AX. | the result is AL = quotient 商, AH = remainder 余
53 ; divisor is 16 bits, then dividend is in DX(H) and AX(L) | the result is AX = quotient 商, DX = remainder 余
54
55 divDW: ; chapter: 8.7 Page 157
56 pop BP
57 push bx
58 MOV BX, ds: [ 2 ] ; 4240 Divident(L)
59 MOV AX , ds: [ 4 ] ; 000f Divident(H)
60 MOV CX , ds: [ 6 ] ; 000A Divisor
61 MOV DX, 0
62 DIV CX ; (DX(0) + AX(000f)) / CX
63 mov ds: [ 4 ], ax
64 MOV AX, BX ; move Divident (L) in AX
65 DIV CX ; (DX(0005) + AX (4240)) / CX
66 mov ds: [ 2 ], ax
67 mov ds: [ 0 ], dx
68 pop bx
69 PUSH BP ; SET IP back to stack
70 ret
71
72 ; ***********************************************************
73 ; ****** Show Text
74 show_str:
75 pop bp
76 mov ax, 0b86eh
77 mov ss, ax ; row
78 mov si, 40 ; Column
79 mov bx, 0 ; String stard postion
80 mov ah, 00000010B ; Font
81 push ss
82 push si
83 push ds
84 push bx
85 main:
86 mov cl, ds: [bx+ 16 ] ; ds:[bx] is the character
87 mov al, cl
88 mov ch, 0
89 jcxz EXIT_SS
90 mov ss: [si], ax ; this ss:[si+bx] is character position
91 inc bx
92 add si, 2
93 jmp short main
94
95 EXIT_SS:
96 pop bx
97 pop ds
98 pop si
99 pop ss
100 push bp
101 ret
102 codesg ends