linux - file read buffer is empty in nasm -
i managed build nasm tutorial code dealing files. outputs contents of file stdout fine, when try access data buffer, contains zeros. example in code below in middle loop ebx set 0, when should contain file bytes.
section .data bufsize dw 1024 section .bss buf resb 1024 section .text ; declaring our .text segment global _start ; telling program execution should start _start: ; code starts getting exec'ed ; filename in ebx pop ebx ; argc pop ebx ; argv[0] pop ebx ; first real arg, filename ; open file mov eax, 5 ; open( mov ecx, 0 ; read-only mode int 80h ; ); ; read file mov eax, 3 ; read( mov ebx, eax ; file_descriptor, mov ecx, buf ; *buf, mov edx, bufsize ; *bufsize int 80h ; ); mov ecx, 20 loop: mov eax, 20 sub eax, ecx mov ebx, [buf+eax*4] loop loop ; write stdout mov eax, 4 ; write( mov ebx, 1 ; stdout, mov ecx, buf ; *buf int 80h ; ); ; exit mov eax, 1 ; exit( mov ebx, 0 ; 0 int 80h ; );
for example in code below in middle loop ebx set 0, when should contain file bytes.
how determining this? (running under debugger, perhaps?)
your code has unfortunate bug:
; read file mov eax, 3 ; read( mov ebx, eax ; file_descriptor,
you're overwriting eax (which contains file descriptor returned open
syscall, if open
succeeded) value 3, before gets moved ebx file descriptor argument read
.
normally, process start out file descriptors 0, 1 , 2 assigned stdin
, stdout
, stderr
, , first file descriptor explicitly open
3, you'll away it!
but might not lucky if you're running debugger. file descriptor 3 might else, , read
might fail (you don't check see if returned value negative error code), or read unexpected...
Comments
Post a Comment