Re: simple read char app return wrong value?



naunetr wrote:

[snip code]
is everything correct here?

Yeah... but maybe sys_read doesn't work quite as we'd prefer.

but when i run this everything is okay when i enter single character

Right... and "enter"...

or i press crtl-d for end of file,

??? Okay, I don't know about that one. "EOT" in ascii. In my modified program, ctrl-j or ctrl-m bails out, for me. crtl-d doesn't seem to do anything special - indicates "the char" is zero (???)

but when i enter several characters after my app exits bash somehow reads them and complains. here is some examples.

Annoying, but normal.

....
bash: kjalskj: command not found
#

what is happened here?

Extra characters the user types stay in the system's input buffer.

how can avoid input to readc from being read by bash?

We'll have to flush the buffer!

also the exit code for example 1 is 1 and for example 2 is 0 but for example 3 it is 127. does anyone know what 127 means? 1 and 0 seem to be correct as said in man 2 read.

Well, I think 127 is what bash is returning after complaining, no longer the return code from your program. Damned if I know what it means! I would expect an "error" indication - a "negative" number, which "echo $?" should display as >128 (since it doesn't do "signed") I can confirm that it *does* return 127, but I don't know why.

also Duntemann's book mentions setting up "stack frame". is that only for c library or do i have to do that for above program also?

No, you don't need to do it here. PGU gets into it in the section about "functions". Too much to get into here...

So how do we flush the system's buffer? We know that sys_read doesn't return until the user hits "enter" (We would prefer, perhaps, that it did. This can be arranged. "sys_ioctl" can be used to modify the behavior of the "file"... but it isn't "simple". Another post.), so we can watch for that. In spite of only wanting one key, I've modified your program to read *two*. If the second one is the "enter" (10, although we might expect it to be 13), we're all set - the user entered just one key. If the second character *isn't* the "enter" key, we want to gobble up any excess. If the user hit *just* enter, it won't be in the second position, so we have to check for that, too. If we need to "flush" some, we read into a different buffer (so we don't overwrite the "good" buffer), one at a time, until we find the "enter" key. We ignore the "dummy" buffer, and the second byte of the "good" buffer - we're only interested in the one key. Rather than return the number of bytes read (which would include the "enter" if we read enough to include it), I've modified the program to return (as an exit code) the (first) character entered. We'll want to make this a subroutine, probably, and return the character in al, but "from the Ground Up", "Step by Step"... This attempts to solve just the one immediate problem.

Best,
Frank

; readc.asm - read a character from standard input and exit.
section .text

global _start

_start:
mov ebx, 0h ; standard input
mov ecx, the_char ; input array
mov edx, 02h ; 1 character to read (+ "enter")
mov eax, 03h ; system call number for read
int 080h

; user just hit "enter"?

cmp eax, 1
je done ; begone

; if user obediently hit just one key,
; and "enter", we're done

cmp byte [the_char + 1], 10
je done

; pesky user entered more than one key
; get rid of excess

mov ecx, dummy ; buffer
mov edx, 1 ; one at a time
flush:
mov eax, 3 ; __NR_read - as C calls it
int 80h

; finally got our "enter"?

cmp byte [dummy], 10
jne flush ; no, do more.

; yes, "fall through" to...

done:

; mov ebx, eax ; byte read
; rather than return number of bytes read, return the char

mov bl, [the_char]
mov eax, 01h ; system call number for exit
int 080h

section .bss

the_char resb 02h ; array for input
dummy resb 1 ; extra buffer
.



Relevant Pages

  • Re: simple read char app return wrong value?
    ... Extra characters the user types stay in the system's input buffer. ... So how do we flush the system's buffer? ... We know that sys_read doesn't return until the user hits "enter" (We would prefer, perhaps, that it did. ... mov ecx, the_char; input array ...
    (alt.lang.asm)
  • Re: 8031 question
    ... AUXBUF EQU 0; TRICK - BAFER ... BUFFER EQU 100H; PREPARE FOR OUTPUT ... MOV R0,#TEMPDIV; TEMPERATURU CITA SAMO U KRUGU 1 ... JMP EQSEC; ...
    (sci.electronics.design)
  • Re: Fastcode PosIEx B&V 1.1.1
    ... I2, SubStrLength, StrLength: Integer; ... Exit; ... mov edi,edx ...
    (borland.public.delphi.language.basm)
  • Re: HLA Lib
    ... The are all based around readln. ... // as there is still additional data in the input buffer. ... mov(InputIndex, ebx); ... mov(CharsInBuf, ecx); ...
    (alt.lang.asm)
  • Re: Completely confused. Please send help. :)
    ... I was backward in my thinking on which was the start of my buffer. ... mov ebx, %1 ... mov eax, SYSCODE_write ... mov ebx, EXIT_FAILURE ...
    (comp.lang.asm.x86)