flat assembler
Message board for the users of flat assembler.

Index > Linux > Reading works from console, but not from a file (cat n | pro

Author
Thread Post new topic Reply to topic
Ton



Joined: 06 Jan 2005
Posts: 22
Ton
If I run the following program, it just runs fine:

Code:
$ ./prog
12           input
24           output
34
68
56
112
    


but not if

Code:
$ cat n
12
34
56

$ cat n | ./prog
460766
460766
460766
    


How come, because in both cases stdin is read from.


Best Regards,
Ton


Code:
format elf executable
entry main

use32

main:

   xor eax,eax
.a:
   cmp eax,3
   jge exit
   push eax

   mov eax, 3               ; sys_read
   mov ebx, 0             ; stdin
   mov ecx, buf
   mov edx, 0x20
   int 0x80 

; chop nl
   dec eax
   mov [buf+eax],0


   mov ebx,10
   mov edi,buf
   call StrToInt
   shl eax,1
   mov  ebx,10              ; \
   mov  [base],ebx          ;   configure dot
   xor  edx,edx            
   call dot
   
   pop eax
   inc eax
   jmp .a


;  mov ecx, buf
;  mov ebx, 1           ; stdout
;  mov eax, 4           ; sys_write
;  int 80h              ; Linux syscall

exit:
   xor ebx,ebx  ; exit 0
   mov eax, 1
   int 80h



StrToInt:
        ; edi = buffer, ebx = base
        xor     eax,eax
        xor     edx,edx
     .loop:
        mov     dl,byte [edi] ; get char
        test    dl,dl             ; empty?
        jz      .end
        imul    eax,ebx             ; multiply with base
        sub     dl,'0'                ; 'n' - '0'
        cmp     dl,9         ; hex number?
        jle     .ok
        sub     dl,7            ; substract 7 on top
      .ok:
        add     eax,edx               ; add result
        inc     edi
        jmp     .loop
      .end:
        ret




dot:    ; print a number
   ; eax = number, ebx = base, edi = buffer
   push eax
   push ebx
   push ecx
   push edx
   mov     ebx, [base]
   cmp  ebx,10
   jne  udot
   mov     edi,buffer
   or      eax,eax         ; negative?
   jns     udot
   push eax
   mov     al,'-'          ; eax is altered
   stosb                   ; only al is stored
   mov  edx, 1
   mov     ecx, buffer
   mov     ebx, 1          ; stdout
   mov     eax, 4          ; sys_write
   int     80h             ; Linux syscall
   pop  eax
   neg  eax
udot:   ;  print a unsigned number
   ; eax = number, ebx = base, edi = buffer
   mov  ebx, [base]
   mov     edi,buffer
   xor     ecx,ecx
.new:
   xor  edx,edx
   div  ebx
   push    edx          ; push remainder
   inc     ecx          ; loop cnt, see below
   test    eax,eax
   jnz     .new
   mov  edx,ecx         ; for type above
.loop:
   pop     eax
   add     al,30h
   cmp     al,'9'
   jng     .ok
   add     al,27h       ; lowercases
.ok:
   stosb                ; mov edi,[al], and inc edi, inc edx
   loop    .loop        ; loop bails out on eci=0
   mov     al,10        ; terminate with nl
   inc  edx
   stosb
type:
                        ; edx = count
   mov ecx, buffer
   mov ebx, 1           ; stdout
   mov eax, 4           ; sys_write
   int 80h              ; Linux syscall
   pop  edx
   pop  ecx
   pop  ebx
   pop  eax
   ret

buffer  rb 33           ; 32 + 1
base    rd 1    


buf      rb      0x20 
a       rd     20
    
Post 02 Feb 2009, 08:53
View user's profile Send private message Reply with quote
lsa



Joined: 10 Aug 2006
Posts: 17
Location: Denmark
lsa
The console is line buffered.
This is not the case when reading from stdin (unless the data is sent as such (like when using a console)).

So if you have this:
20
30
40

In some file (say 'n') you read 20 chars (everything, not 20 chars on a single line).
You need to split the lines youself.

Maybe this is your problem.
It`s not like <STDIN> in perl (Which as i remember always reads a line).
Post 02 Feb 2009, 16:12
View user's profile Send private message Reply with quote
Ton



Joined: 06 Jan 2005
Posts: 22
Ton
Isa, you're right.

Code:
   mov eax, 3           ; sys_read
   mov ebx, 0           ; stdin
   mov ecx, buf
   mov edx, 0x4       ; read exactly four characters
   int 0x80


$ more n
123
234
345
$ cat n | ./prog
246
468
690
    
[/code]
Post 02 Feb 2009, 19:39
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.