NASM Forum > Example Code

Accepting keyboard input to implement 'Password' program

<< < (2/2)

S Mahen:
Hi,
Now I want to work on 64-bit code.
Using syscall "ioctl" (16).

The objective is same...

Please guide me.

fredericopissarra:

--- Quote from: S Mahen on January 29, 2021, 12:41:04 AM ---Hi,
Now I want to work on 64-bit code.
Using syscall "ioctl" (16).
--- End quote ---

Here's the same excellent code from Gunner, but for x86-64 (with tiny corrections):

--- Code: ---  bits        64
  default     rel

ICANON      equ 1<<1
ECHO        equ 1<<3

; NOTE: syscalls #s are different for SysV x86-64 ABI.
sys_exit    equ 60
sys_read    equ 0
sys_write   equ 1
sys_ioctl   equ 16
stdin       equ 0
stdout      equ 1

  ; CHANGE: don't need to keep these bytes in a writable section!
  section .rodata

szAsterisk  db  "*"
szLF        db  `\n`

  ; NOTE: Beware that .bss isn't zeroed automatically, as in C.
  section     .bss

lpBufIn     resb    2
lpPassIn    resb    24
termios     resb    36

  section .text

  global _start
_start:
  call    echo_off
  call    canonical_off

  ; NOTE: Changed from edi to ebx because SysV ABI for x86-64 uses RSI.
  xor     ebx, ebx
.GetCode:   
  cmp     ebx, 24
  je      .PassTooBig
   
  call    GetKeyCode

  mov     al, byte [lpBufIn]
  cmp     al, "Q"
  je      .Done
  cmp     al, 10
  je      .Continue

  mov     byte [lpPassIn + rbx], al
  call    PrintAsterisk
  inc     ebx
  jmp     .GetCode
 
.Continue:
  mov     byte [lpPassIn + rbx], 0    ; FIX
   
  ;~ for testing purposes
  ;~ #####################################
  inc ebx                             ;~ #
                                      ;~ #
  mov     edx, 1                      ;~ #
  lea     rsi, [szLF]                 ;~ #
  mov     eax, sys_write              ;~ #
  mov     esi, stdout                 ;~ #
  syscall                             ;~ #
                                      ;~ #
  mov     edx, ebx                    ;~ #
  lea     rsi, [lpPassIn]             ;~ #
  mov     eax, sys_write              ;~ #   
  mov     esi, stdout                 ;~ #
  syscall                             ;~ #
  ;~ #####################################
       
  ;~ Compare passowrds here!!!
  jmp     .Done
   
.PassTooBig:
  ;~ Entered password too big!
   
.Done:
  call    echo_on
  call    canonical_on

  mov     eax, sys_exit
  xor     edi, edi
  syscall
   
;~ #########################################
GetKeyCode:
  mov     eax, sys_read
  mov     edi, stdin
  lea     rsi, [lpBufIn]
  mov     edx, 1
  syscall
  ret
 
;~ #########################################
PrintAsterisk:
  mov     edx, 1
  lea     rsi, [szAsterisk]
  mov     eax, sys_write
  mov     edi, stdout
  syscall     
  ret        
   
;~ #########################################
canonical_off:
  call read_stdin_termios

  ; clear canonical bit in local mode flags
  and dword [termios+12], ~ICANON

  call write_stdin_termios
  ret
     
;~ #########################################
echo_off:
  call read_stdin_termios

  ; clear echo bit in local mode flags
  and dword [termios+12], ~ECHO

  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:
  mov eax, sys_ioctl
  mov edi, stdin
  mov esi, 0x5401
  lea rdx, [termios]
  syscall
  ret

;~ #########################################
write_stdin_termios:
  mov eax, sys_ioctl
  mov edi, stdin
  mov esi, 0x5402
  lea rdx, [termios]
  syscall
  ret
--- End code ---

S Mahen:
Thank you so much for the sample code... I am modifying the same as per problem statement. Will share the modified code...

Navigation

[0] Message Index

[*] Previous page

Go to full version