;screen_input.asm
section .data
LF equ 10
NULL equ 0
TRUE equ 1
FALSE equ 0
EXIT_SUCCESS equ 0
STDIN equ 0
STDOUT equ 1
STDERR equ 2
SYS_read equ 0
SYS_write equ 1
SYS_open equ 2
SYS_close equ 3
SYS_fork equ 57
SYS_exit equ 60
SYS_creat equ 85
SYS_time equ 201

STRLEN equ 9    ; 8+(1=end of line) 

testo db "salve, puoi scrivere qui sotto:", LF, NULL
newline db LF, LF, NULL

section .bss
chr resb 1
inputline resb STRLEN;   

section .text
global _start
_start:

; display prompt

mov rdi, testo
call printstring

; Read characters from user (one at a time)

mov rbx, inputline       ; inputline address in register
mov r12, 0               ; char count 

ReadCharacters:
mov rax, SYS_read    ; microchip reads from console
mov rdi, STDIN       ; input in microchip from keyboard
lea rsi, byte[chr]
mov rdx, 1
syscall

mov al, byte[chr]     ; al is the low of low of low of rax
                      ; at extreme right of rax
cmp al, LF            ; if linefeed, input done         
je ReadDone

inc r12
cmp r12, STRLEN
jae ReadCharacters    ; 0-8
mov byte[rbx], al     ; inputline[i] = chr
inc rbx               ; update tmp string address

jmp ReadCharacters

ReadDone:
mov byte[rbx], NULL

mov rdi, inputline
call printstring

mov rax, SYS_exit
mov rdi, EXIT_SUCCESS
syscall




global printstring
printstring:

push rbx
mov rbx, rdi
mov rdx, 0

stringcountloop:
cmp byte [rbx], NULL
je stringcountdone
inc rdx
inc rbx
jmp stringcountloop
stringcountdone:
cmp rdx, NULL
je printdone

mov rax, SYS_write  ; to output characters to the console
mov rsi, rdi        ; address of inputline in rsi
mov rdi, STDOUT     ; File Descriptor to write characters in the console
syscall

printdone:
pop rbx
ret

