Author Topic: Console Input using SYS_READ and STDIN  (Read 4314 times)

Offline TightCoderEx

  • Full Member
  • **
  • Posts: 103
Console Input using SYS_READ and STDIN
« on: May 08, 2012, 05:52:09 AM »
This is basically the counterpart of my previous post.  As of this version, nothing special is done with overflow other than being passed to the next input.  I'm sure most programmers using this will have a reasonably sized buffer, but if someone wants to add buffer flushing on overflow, that would be cool.

Code: [Select]

; ========================================================================================================
; Read keyboard entry until EOF (return) or overflow ENTRY > RCX.

; ENTRY: RCX = maximum characters
; RDX = Pointer to input buffer

; LEAVE: RAX = Actuall size of entry
;       All other registers unmodified.

; FLAGS: ZR = 1 if null input, otherwise 0

; NOTE: Routine terminates strings with null to be "C" style conformant.
;     24H - 36 Bytes
; ---------------------------------------------------------------------------------------------------------

Keyboard: push rbx
push rcx ; Preserve those not to be altered
push rdx

xor rax, rax ; Less code than move RAX, 0
mov rbx, rax
inc rbx ; STDIN
xchg rdx, rcx ; RCX = Pointer & RDX = Maximum input
mov al, READ ; SYS_READ
int 0x80 ; Get users input
or eax, eax ; I think we can do this, surely entry wasn't that big
jz .Done ; Don't bother doing anything else if NULL string

; Calculate where end of string is

dec rax ; Adjust for carriage return
add rcx, rax ; Bump to last character of string

; Post terminator to end of string, cleanup and leave

  .Done mov byte [rcx], 0 ; Set terminating byte
 
  pop rdx
  pop rcx ; Restore preserved registers
  pop rbx
 
  ret