
	format PE console
	entry start

	include 'win32a.inc'

	section '.code' code readable executable
start:
	push intro
	call dword [printf]
	pop eax
	
	push prompt
	call dword [printf]
	pop eax
	
	push rat_den
	push rat_num
	push fmt_2d
	call dword [scanf]
	add esp, 12

	;+
	push rat_den
	push rat_num
	call reduce
	;+

	push dword [rat_den]
	push dword [rat_num]
	push result
	call dword [printf]
	add esp, 12
	
	push 0
	call dword [exit]
	
; Gets addresses of numertor and denominator on stack.
; No error checking done. They should be non-negative.
reduce:
pnum equ ebp+8
pden equ ebp+12
	enter 0, 0
	mov esi, [pnum]
	mov edi, [pden]
	mov eax, [esi]
	mov ebx, [edi]
	cmp eax, ebx
	jge .test_R
	xchg eax, ebx
	jmp .test_R
.do:
	xor edx, edx
	div ebx
	mov eax, ebx
	mov ebx, edx
.test_R:
	test ebx, ebx
	jnz .do
	mov ebx, eax
	mov eax, [esi]
	;xor edx, edx
	div ebx
	mov [esi], eax
	mov eax, [edi]
	;xor edx, edx
	div ebx
	mov [edi], eax
	leave
	ret 8
	
	section '.data' data readable writable
rat_num dd 0	; Rational number numerator.
rat_den dd 0	; Rational number denominator.
intro	db 13,10,"Reduce a Rational Number.",13,10,13,10,0
prompt	db "Enter numerator followed by denominator: ",0
result	db 13,10,"Reduced Rational Number: %d / %d",13,10,13,10,0
fmt_2d db "%d %d",0
	
	section '.idata' import data readable writable

library msvcrt,'msvcrt.dll'
import	msvcrt,\
	scanf,'scanf',\
	printf,'printf',\
	exit,'exit'
	