-
Notifications
You must be signed in to change notification settings - Fork 5
/
DINT.ASM
156 lines (141 loc) · 1.92 KB
/
DINT.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
; *******************************************************
; * *
; * Turbo Pascal Run-time Library *
; * Integer Binary/Decimal Routines *
; * *
; * Copyright (c) 1988,92 Borland International *
; * *
; *******************************************************
TITLE DINT
INCLUDE SE.ASM
CODE SEGMENT BYTE PUBLIC
ASSUME CS:CODE
; Publics
PUBLIC Int2Str,Str2Int
; Convert integer to string
; In DX:AX = Value
; ES:DI = String end pointer
; Out CX = String length
; ES:DI = String pointer
Int2Str:
MOV CX,DI
MOV SI,10
MOV BX,DX
OR BX,BX
JNS @@1
NEG BX
NEG AX
SBB BX,0
CALL @@1
DEC DI
MOV ES:[DI].b0,'-'
INC CX
RET
@@1: XOR DX,DX
XCHG AX,BX
DIV SI
XCHG AX,BX
DIV SI
ADD DL,'0'
CMP DL,'0'+10
JB @@2
ADD DL,'A'-'0'-10
@@2: DEC DI
MOV ES:[DI],DL
MOV DX,AX
OR DX,BX
JNE @@1
SUB CX,DI
RET
; Convert string to integer
; In CX = String length
; ES:DI = String pointer
; Out DX:AX = Value
; CX = Remaining characters
; ES:DI = Pointer past string
; CF = 1 if error
Str2Int:
XOR AX,AX
XOR DX,DX
XOR SI,SI
JCXZ @@6
CMP BYTE PTR ES:[DI],'+'
JE @@1
CMP BYTE PTR ES:[DI],'-'
JNE @@2
DEC SI
@@1: INC DI
DEC CX
JE @@6
@@2: CMP BYTE PTR ES:[DI],'$'
JE @@7
@@3: MOV BL,ES:[DI]
SUB BL,'0'+10
ADD BL,10
JNC @@4
TEST DH,0F0H
JNE @@6
PUSH BX
SHL AX,1
RCL DX,1
PUSH DX
PUSH AX
SHL AX,1
RCL DX,1
SHL AX,1
RCL DX,1
POP BX
ADD AX,BX
POP BX
ADC DX,BX
POP BX
XOR BH,BH
ADD AX,BX
ADC DX,0
INC DI
LOOP @@3
@@4: MOV BX,AX
OR BX,DX
JE @@5a
OR SI,SI
JNS @@5
NEG DX
NEG AX
SBB DX,0
@@5: XOR SI,DX
JS @@6
@@5a: RET
@@6: STC
RET
@@7: INC DI
DEC CX
JE @@6
@@8: MOV BL,ES:[DI]
CMP BL,'a'
JB @@9
SUB BL,32
@@9: SUB BL,'0'+10
ADD BL,10
JC @@10
SUB BL,7+16
ADD BL,6
JNC @@4
ADD BL,10
@@10: MOV BH,4
@@11: SHL AX,1
RCL DX,1
JC @@6
DEC BH
JNE @@11
OR AL,BL
INC DI
LOOP @@8
OR SI,SI
JNS @@12
NEG DX
NEG AX
SBB DX,0
@@12: CLC
RET
CODE ENDS
END