format PE64 console
entry start

include 'win64a.inc'

section '.text' code readable executable
start:
    ccall [printf], welcome
    ccall [getchar], al
    cmp al, '+'
    je addition
    cmp al, '-'
    je subtraction
    cmp al, '*'
    je multiplication
    cmp al, '/'
    je division
    cmp al, $A
    je exit
    jmp invalid

    invalid:
	ccall [printf], invalid_op
	call [getch]
	jmp start

    addition:
	ccall [printf], add_
	ccall [gets], [operand1]
	mov rcx, operand1
	ccall [gets], [operand2]
	add rcx, operand2
	cinvoke printf, rcx
	call [getch]
	jmp start

    subtraction:
	ccall [printf], sub_
	ccall [gets], operand1
	mov rcx, operand1
	ccall [gets], operand2
	sub rcx, operand2
	ccall [printf], rcx
	call [getch]
	jmp start

    multiplication:
	ccall [printf], mul_
	ccall [gets], operand1
	mov rcx, operand1
	ccall [gets], operand2
	imul rcx, operand2
	ccall [printf], rcx
	call [getch]
	jmp start

    division:
	ccall [printf], div_
	ccall [gets], operand1
	mov rax, operand1
	ccall [gets], operand2
	mov rcx, operand2
	div rcx
	ccall [printf], rcx
	call [getch]
	jmp start

    exit:
	ccall [ExitProcess], 0

section '.data' data readable writeable
    welcome db "Welcome to the Z calculator (beta)", 10,13
	    db "Available options:", 10,13
	    db "[enter] Exit", 10,13
	    db "+       Addition", 10,13
	    db "-       Subtraction", 10,13
	    db "*       Multiplication", 10,13
	    db "/       Division", 10,13,0
    add_ db "[Addition]", 10,13,0
    sub_ db "[Subtraction]", 10,13,0
    mul_ db "[Multiplication]", 10,13,0
    div_ db "[Division]", 10,13,0
    invalid_op db "Invalid option, press any key to continue...", 10,13,0
    operand1 dq ?
    operand2 dq ?

section '.idata' import data readable
    library kernel,'kernel32.dll',\
	msvcrt,'msvcrt.dll'

    import kernel,\
	ExitProcess,'ExitProcess'

    import msvcrt,\
	printf,'printf',\
	getchar,'getchar',\
	gets,'gets',\
	getch,'_getch'\