.text
.globl gcd

gcd:
	push %rbp
	mov %rsp,%rbp

	# params to registers
	mov %edi,%ecx
	mov %esi,%edx
	# allocate space for one 4B local variable
	sub $0x4,%rsp
	
	cmp %ecx,%edx
	jg _test
	# swap the regs to the correct order, using a temporary value on stack
	mov %ecx,(%rsp)
	mov %edx,%ecx
	mov (%rsp),%edx

_test:
	test %edx,%edx
	jz _done
	mov %ecx,%eax
	mov %edx,%ecx

	xor %edx,%edx
	div %ecx   #div %eax by %ecx, remainder goes to %edx (which must be zero)
	jmp _test

_done:
	# save result
	mov %ecx,%eax

	# leaveq would do this all at once:
	mov %rbp,%rsp
	pop %rbp
	retq
