2015-01-13 4 views
0

이것은 내가 가지고있는 코드 :이 어셈블리 코드가 같은 바이트를 다시 읽지 않고 대신 파일의 끝을 반환하는 이유는 무엇입니까?

section .bss 
    bufflen equ 1024 
    buff: resb bufflen 

    whatread: resb 8 

section .data 

section .text 

global main 

main: 
    nop 
    read: 
     mov eax,3   ; Specify sys_read 
     mov ebx,0   ; Specify standard input 
     mov ecx,buff  ; Where to read to... 
     mov edx,bufflen  ; How long to read 
     int 80h    ; Tell linux to do its magic 

          ; Eax currently has the return value from linux system call.. 
     add eax, 30h  ; Convert number to ASCII digit 
     mov [whatread],eax ; Store how many byte reads info to memory at loc whatread 

     mov eax,4   ; Specify sys_write 
     mov ebx,1   ; Specify standart output 
     mov ecx,whatread ; Get the address of whatread in ecx 
     mov edx,4   ; number of bytes to be written 
     int 80h    ; Tell linux to do its work 

     mov eax,3   ; Specify sys_read 
     mov ebx,0   ; Specify standard input 
     mov ecx,buff  ; Where to read to... 
     mov edx,bufflen  ; How long to read 
     int 80h    ; Tell linux to do its magic 

          ; Eax currently has the return value from linux system call.. 
     add eax, 30h  ; Convert number to ASCII digit 
     mov [whatread],eax ; Store how many byte reads info to memory at loc whatread 

     mov eax,4   ; Specify sys_write 
     mov ebx,1   ; Specify standart output 
     mov ecx,whatread ; Get the address of whatread in ecx 
     mov edx,4   ; number of bytes to be written 
     int 80h    ; Tell linux to do its work 

    mov eax, 1; 
    mov ebx, 0; 
    int 80h 

나는 내용으로 all.txt 라는 파일이 있습니다

[email protected]:~/asm/buffasm$ ./buff < all.txt 
[email protected]:~/asm/buffasm$ 

: 여기

61 62 63 0A 64 65 66 0A (abc - new line - def - new line) 

이 샘플 실행을 그래서 코드의 첫 번째 읽기는 파일에서 1024 바이트를 읽으려고합니다. 파일 자체에는 8 바이트가 있습니다. 콘솔에서 8을 인쇄합니다. 그런데 왜 파일 끝을 의미하는 '0'이 인쇄됩니까? 같은 8 바이트를 다시 읽으 리라고 기대합니까? 왜 다음 1024 바이트를 읽으려고합니까?

+1

왜 다시 읽습니까? 파일 끝을 치고 파일 포인터를 다시 설정하지 않았습니다. 파일에서 현재 위치에 대한 개념을 알지? 그것은 흉자에 특정한 것이 아닙니다. – Jester

+0

@Jester 파일 포인터는 어디에 보관합니까? 내가 아는 한 똑같은 파일을 다시 전달할 것이며이 사실을 알아서는 안됩니다. 포인터를 어떻게 재설정 할 수 있습니까? –

+0

@Jester 또한 왜 9th 바이트로 첫 번째 시도에서 EOF를 읽지 않습니까? –

답변

0

동일한 바이트를 다시 읽는 프로그램 논리가 없습니다. 그들은 이미 읽고 버퍼에 있습니다. 입력 된 버퍼 (리디렉션 된 파일로 채워짐)는 비어 있으므로 더 이상의 바이트를 읽을 수 없습니다. 이것이 sys_read가 0 바이트로 끝나는 이유입니다.

STDIN이 아니라 실제 파일 (물론 이전에 sys_open에 의해 열림) 인 경우에도 동일한 논리가됩니다.

실제로 반품 회수 0은 실제로 EOF라고합니다.

+0

그들이 이미 버퍼에 있다는 것은 무엇을 의미합니까? 버퍼 란 무엇입니까? –

+0

버퍼는 단순히 몇 바이트를 저장하는 데 사용되는 메모리 영역입니다. 대답에는 버퍼 (레이블'buff')와 OS가 할당 한 "입력 버퍼"의 두 가지 버퍼가 있으며 명령 줄에서 입력 리디렉션에 의해 수신 된 STDIN 파일의 내용이 들어 있습니다. – johnfound

+0

은 커널 공간에 할당되어 있습니까? –