global _start

section .data
	sys_read     equ 3
	sys_write    equ 4
	stdin		 equ 0
	stdout       equ 1
	bytesRead    dd  0
	termios:        times 36 db 0
	ICANON:         equ 1<<1
	ECHO:           equ 1<<3

section .bss
    line         resb 11
	index        resb 4

section .text

_start:
	push ebp
	mov ebp, esp
	
	call canonical_off
	call echo_off
	
	call _readLine
	call _printLine	
	
	call canonical_on
	call echo_on
		
	mov esp, ebp
	pop ebp
	jmp	exit

_readLine:
; Reads into line until new line (0xA)
; Number of bytes read will be stored in bytesRead when _readLine returns	
	mov eax, sys_read             ; syscall to read
	mov ebx, stdin                ; stdin
	mov edx, [index]              ; put index into edx
	mov ecx, dword line 		  ; put line addr in ecx
	add ecx, edx	              ; add index to addr in ecx
	mov edx, 1                    ; read one char
	int 0x80	                  ; call kernel to read char
	mov ecx, [index]              ; put index into ecx
	cmp dword [line + ecx], 0xA   ; compare value at line + ecx to new line char
	inc byte [index]              ; increment index
	jne _readLine                 ; if last char is not new line, loop  
	ret

_printLine:
	mov eax, sys_write
	mov ebx, stdout
	mov ecx, line
	mov edx, [index]
	int 0x80
	ret

_printIndex:
	mov eax, sys_write
	mov ebx, stdout
	add byte [index], '0'
	mov ecx, index
	mov edx, 4
	int 0x80
	ret

canonical_off:
        call read_stdin_termios

        ; clear canonical bit in local mode flags
        ;push rax
        mov eax, ICANON
        not eax
        and [termios+12], eax
        ;pop rax

        call write_stdin_termios
        ret

echo_off:
        call read_stdin_termios

        ; clear echo bit in local mode flags
        ;push rax
        mov eax, ECHO
        not eax
        and [termios+12], eax
        ;pop rax

        call write_stdin_termios
        ret

canonical_on:
        call read_stdin_termios

        ; set canonical bit in local mode flags
        or dword [termios+12], ICANON

        call write_stdin_termios
        ret

echo_on:
        call read_stdin_termios

        ; set echo bit in local mode flags
        or dword [termios+12], ECHO

        call write_stdin_termios
        ret

read_stdin_termios:
       ; push rax
       ; push rbx
       ; push rcx
        ;push rdx

        mov eax, 36h
        mov ebx, stdin
        mov ecx, 5401h
        mov edx, termios
        int 80h

        ;pop rdx
       ; pop rcx
        ;pop rbx
        ;pop rax
        ret

write_stdin_termios:
       ; push rax
        ;push rbx
        ;push rcx
       ; push rdx

        mov eax, 36h
        mov ebx, stdin
        mov ecx, 5402h
        mov edx, termios
        int 80h

        ;pop rdx
        ;pop rcx
       ; pop rbx
        ;pop rax
        ret

exit:
	mov		eax, 01h		; exit()
	xor		ebx, ebx		; errno
	int		80h