extern exit
extern putchar
extern getchar
extern fflush

global main

%define _START main

%macro EXIT_ 0
      	xor  rdi, rdi ; return code of 0
        call exit
%endmacro

%macro GETCHAR 0
        push rdi
        push rsi
        call getchar
        pop  rsi
        pop  rdi
        mov  rbx, 0xffffffff
        sub  rbx, rax
        jnz  L0
        mov  rbx, 1
        mov  rdx, eof_reached ; TODO use [eof_reached] directly
        mov  [rdx], rbx
        mov  rax, -1
        ret
L0:
        and  rax, 255 ; mask out char value
%endmacro

%macro PUTCHAR 0
        push rdi
        push rsi
        mov  rdi, [rdi]
        call putchar
        mov  rdi, 0
        call fflush
        pop  rsi
        pop  rdi
%endmacro
