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
|
global result_to_string
global input_to_string
;
;local variables of result_to_string
;
%define ten ebp-4
%define count ebp-8
%define digit ebp-12
section .text
;
; converts float number to string
; [ebp+8] is value of sine/cosine/tangent/cotangent
; [ebp+12] is pointer to buffer
; returns eax is string size
;
result_to_string:
push ebp
mov ebp, esp
sub esp, 12 ; memory allocation
mov dword [ten], 10
mov dword [count], 0
mov edi, [ebp+12] ; string address
cld
finit
fld dword [ebp+8] ; load number
ftst ; compare with 0.0
fstsw ax ; save flags
sahf ; load flags
jnc .positiv
mov al, '-'
stosb
inc dword [count]
fchs ; modulus
.positiv: fld1 ; load 1
fld st1 ; copy number to top
fprem ; fraction part
fsub st2, st0 ; integer
fxch st2
xor ecx, ecx ; count for integer part
.lp1: fidiv dword [ten] ; number /10
fxch st1 ; st0 = 1, st1 = number
fld st1 ; st0 = number, st1 = 1, st2 = number
fprem
fsub st2, st0 ; number - fract
fimul dword [ten] ; 0,fraction * 10
fistp dword [digit]
push dword [digit] ; save integer part
inc ecx ; increment integer count
inc dword [count] ; incement total count
fxch st1 ; st0 = number, st1 = 1
ftst ; integer part = 0.0
fstsw ax
sahf
jne .lp1
.extract: pop eax
add al, '0'
stosb
loop .extract
fstp st0 ; remove integer part
fxch st1 ; st0 = fraction part, st1 = 1
ftst ; compare with 0.0
fstsw ax
sahf
je .quit
mov al, '.'
stosb
inc dword [count]
mov ecx, 5 ; length fract part
.lp2: fimul dword [ten]
fxch st1 ; st0 = 1, st1 = number
fld st1 ; st0 = number, st1 = 1, st2 = number
fprem
fsub st2, st0 ; st2 = integer part
fxch st2 ; st0 = int part, st1 = 1, st2 = frac
fistp dword [digit] ; st0 = 1, st1 = frac
mov al, [digit]
add al, '0'
stosb
inc dword [count]
fxch st1 ; st0 = number, st1 = 1
ftst
fstsw ax
sahf
je .quit ; number = 0.0
loop .lp2 ; else reduce ecx
.quit: ffree st0 ; clear stack
fincstp
ffree st0
fincstp
mov eax, [count] ; return count
mov esp, ebp
pop ebp
ret
;
; angle translation from integer to string
; [ebp+8] is number
; [ebp+12] is pointer to buffer
; return eax is pointer to the beggining of string
; and ecx is character count
;
input_to_string:
push ebp
mov ebp, esp
sub esp, 4
push edi
mov dword [ebp-4], 10
xor ecx, ecx ; count
mov edi, [ebp+12] ; buffer
add edi, 7 ; by the end of buffer
std ; from right to left
mov eax, [ebp+8] ; number
.lp: xor edx, edx
div dword [ebp-4]
xchg al, dl
add al, '0'
stosb
inc ecx
xchg al, dl
cmp eax, 0
jne .lp
mov eax, edi ; address of string
inc eax
pop edi
mov esp, ebp
pop ebp
ret
|