From 21f616b5593d87d6381c1f3d67a3df0c442dba44 Mon Sep 17 00:00:00 2001 From: scratko Date: Wed, 27 Mar 2024 22:38:51 +0300 Subject: Initial commit --- number_conversion.asm | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 number_conversion.asm (limited to 'number_conversion.asm') diff --git a/number_conversion.asm b/number_conversion.asm new file mode 100644 index 0000000..af6452f --- /dev/null +++ b/number_conversion.asm @@ -0,0 +1,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 + -- cgit v1.2.3