//---------------------------------------------------------------- // noecho.s // // This example shows how to inhibit echoing of user-input. // // programmer: ALLAN CRUSE // written on: 18 SEP 2003 //---------------------------------------------------------------- # manifest constants .equ TERMIOS_SIZE, 60 .equ ECHO, 0x00000008 .equ ECHONL, 0x00000040 .equ TCSANOW, 0x00000000 .equ MAX_INPUT, 40 .equ STDIN_FILENO, 0 .equ STDOUT_FILENO, 1 .section .text #----------------------------------------------------------------- main: call save_termios # save initial settings call echo_inhibit # install modifications call obtain_input # get user's keystrokes call process_data # name gets capitalized call print_output # show sign-off message call echo_enabled # restore initial state ret # return to Linux shell #----------------------------------------------------------------- .data origtty:.space TERMIOS_SIZE # stores 'termios' object .text #----------------------------------------------------------------- save_termios: # # This procedure acquires a copy of current terminal settings. # pushl $origtty # function argument #2 pushl $STDIN_FILENO # function argument #1 call tcgetattr # call C runtime library addl $8, %esp # discard the arguments ret # return to caller #----------------------------------------------------------------- echo_enabled: # # This procedure will reinstall the original terminal settings. # pushl $origtty # function argument #3 pushl $TCSANOW # function argument #2 pushl $STDIN_FILENO # function argument #1 call tcsetattr # call C runtime library addl $12, %esp # discard the arguments ret # return to caller #----------------------------------------------------------------- .data prompt: .ascii "\n\tPlease type in your name: " msglen: .int . - prompt # number of message bytes buffer: .zero MAX_INPUT+1 # insures null-terminated thanks: .string "\n\tThank you, %s \n" # sign-off message-format .text #----------------------------------------------------------------- obtain_input: # prompt the user for keyboard input pushl msglen # function argument #3 pushl $prompt # function argument #2 pushl $STDOUT_FILENO # function argument #1 call write # call C runtime library addl $12, %esp # discard the arguments # read keyboard input into our buffer pushl $MAX_INPUT # function argument #3 pushl $buffer # function argument #2 pushl $STDIN_FILENO # function argument #1 call read # call C runtime library addl $12, %esp # discard the arguments ret # return to caller #----------------------------------------------------------------- process_data: # first letter in the buffer should not be lowercase cmpb $'a', buffer # ascii-code is below 'a'? jb asis # yes, it's not lowercase cmpb $'z', buffer # ascii-code is above 'z'? ja asis # yes, it's not lowercase # ok, it IS lowercase, so we change it andb $0xDF, buffer # else convert to uppercase asis: ret # return to caller #----------------------------------------------------------------- print_output: # display the sign-off message pushl $buffer # function argument #2 pushl $thanks # function argument #1 call printf # call C runtime library addl $8, %esp # discard the arguments ret # return to caller #----------------------------------------------------------------- .data worktty:.space TERMIOS_SIZE # stores 'termios' object .text #----------------------------------------------------------------- echo_inhibit: # # This procedure makes a copy of the initial terminal settings, # modifies its settings for the ECHO and ECHONL bits within the # 'c_lflag' field, and installs the adjusted terminal settings. # # copy the 'origtty' data-structure into 'worktty' movl $origtty, %esi # point %esi to source movl $worktty, %edi # point %edi to dest'n movl $TERMIOS_SIZE, %ecx # number of bytes to copy again: movb (%esi), %al # copy source byte to %al movb %al, (%edi) # copy from %al to dest'n incl %esi # advance source-address incl %edi # advance dest'n-address loop again # copy rest of the bytes # modify some bits in our working copy of the structure movl $12, %edi # c_lflag field's offset andl $~ECHO, worktty(%edi) # turn off the ECHO bit orl $ECHONL, worktty(%edi) # turn on the ECHONL bit # now activate our modified terminal settings pushl $worktty # function argument #3 pushl $TCSANOW # function argument #2 pushl $STDIN_FILENO # function argument #1 call tcsetattr # call C runtime library addl $12, %esp # discard the arguments ret # return to caller #----------------------------------------------------------------- .globl main