; Generic procedure to display a string to the screen.
; INDIFFERENTLY PUT BEFORE OR AFTER THE PROGRAM
; String must be NULL terminated.
; Algorithm:
; Count characters in string (excluding NULL)
; Use syscall to output characters
; Arguments:
; 1) address, string
; Returns:
; nothing
global printString
printString:
push rbx
;
; Count characters in string
mov rbx, rdi
mov rdx, 0
;
strCountLoop:
cmp byte [rbx], NULL
je strCountDone
inc rdx
inc rbx
jmp strCountLoop
;
strCountDone:
cmp rdx, 0
je prtDone
;
; Call OS to output string
;
mov rax, SYS_write
mov rsi, rdi          ; address of char's to write
mov rdi, STDOUT       ; standard out
; RDX=count to write, set above
syscall
;
; String printed, return to calling routine.
prtDone:
pop rbx
ret
;
;
;
; screen.msg.asm
;
section .data
LF equ 10                   ; line feed = avanzamento di riga
NULL equ 0                  ; end of string 
TRUE equ 1
FALSE equ 0
EXIT_SUCCESS equ 0
STDIN equ 0                 ; standard input
STDOUT equ 1                ; standard output
STDERR equ 2
SYS_read equ 0              ; read
SYS_write equ 1             ; write
SYS_open equ 2              ; file open
SYS_close equ 3             ; file close
SYS_fork equ 57
SYS_exit equ 60
SYS_creat equ 85            ; file open/create
SYS_time equ 201            ; get time
;
; define some strings
STRLEN equ 50
prmpt db "enter text: ", NULL
new_line db LF, NULL
;
section .bss
_char resb 1
in_line resb STRLEN+2       ; total of 52
;
section .text
global _start
_start:
;display prompt
mov rdi, prmpt
call printString
;
; Read characters from user (one at a time)
mov rbx, in_line
mov r12, 0                  ; char count 
read_Characters:
mov rax, SYS_read           ; system code for read
mov rdi, STDIN              ; 
lea rsi, byte [_char]       ; address of _char
mov rdx, 1                  ; count (how many to read)
syscall
mov al, byte [_char]        ; get character just read
cmp al, LF                  ; if linefeed, input done
je readDone
inc r12                     ; count++
cmp r12, STRLEN             ; if # chars ≥ STRLEN
jae read_Characters          ; stop placing in buffer
mov byte [rbx], al          ; inLine[i] = _char
inc rbx                     ; update tmpStr addr  
jmp read_Characters
readDone:
mov byte [rbx], NULL        ; add NULL termination
;
; Output the line to verify successful read
mov rdi, in_line
call printString
;
mov rax, SYS_exit
mov rdi, EXIT_SUCCESS       ; destination register 
syscall
