section .bss
X resd 12
Y resd 12
N resd 1
sum_temp resd 1
num_res resd 1
den_res resd 1
section .text
global main
extern io_get_dec, io_print_dec, io_print_char
main:
call io_get_dec
mov [N], eax
cmp eax, 1
jl finish
cmp eax, 12
jg finish
xor ecx, ecx
input_loop:
cmp ecx, [N]
jge compute_lcm
call io_get_dec
mov [X + ecx*4], eax
call io_get_dec
cmp eax, 0
je finish ; запрещаем Yi=0
mov [Y + ecx*4], eax
inc ecx
jmp input_loop
compute_lcm:
mov eax, 1
mov esi, eax
xor ecx, ecx
lcm_loop:
cmp ecx, [N]
jge sum_fraction
mov eax, esi
mov ebx, [Y + ecx*4]
call lcm
mov esi, eax
inc ecx
jmp lcm_loop
sum_fraction:
xor eax, eax
mov [sum_temp], eax
xor ecx, ecx
sum_loop:
cmp ecx, [N]
jge simplify_fraction
mov eax, esi
mov ebx, [Y + ecx*4]
cmp ebx, 0
je finish ; безопасная проверка
xor edx, edx
div ebx
mov ebx, eax
mov eax, [X + ecx*4]
imul eax, ebx
add eax, [sum_temp]
mov [sum_temp], eax
inc ecx
jmp sum_loop
simplify_fraction:
mov eax, [sum_temp]
mov ebx, esi
call gcd
mov ecx, eax
mov eax, [sum_temp]
xor edx, edx
div ecx
mov [num_res], eax
mov eax, esi
xor edx, edx
div ecx
mov [den_res], eax
mov eax, [num_res]
call io_print_dec
mov eax, '/'
call io_print_char
mov eax, [den_res]
call io_print_dec
finish:
ret
gcd:
cmp ebx, 0
je gcd_done
gcd_loop:
xor edx, edx
div ebx
mov eax, ebx
mov ebx, edx
cmp ebx, 0
jne gcd_loop
gcd_done:
ret
lcm:
push eax
push ebx
call gcd
mov ecx, eax
pop ebx
pop eax
imul eax, ebx
xor edx, edx
div ecx
ret