Author Topic: File open , cannot get the file read - found  (Read 6412 times)

Offline dalfonso01

  • Jr. Member
  • *
  • Posts: 44
File open , cannot get the file read - found
« on: May 30, 2014, 04:11:01 PM »
Hi,
I am trying to translate the code in Wrox book, chapter 16, there is no choice.
I made this, the problem I get is in Read , where I cannot get a successful  int 80h
The original code to get the filehandle from open sys_call , simply says
movl %eax, infile  , that I translated as mov [infile], eax but I cannot get a read. later the call fails, I am missing something.
Could help?
Thanks
Fabio D'Alfonso

Code: [Select]
section .bss  ; Section containing uninitialised data
 
     Buffer: resb 10
     infile: resb 4
     outfile: resb 4
 
 section .data ; Section containing initialised data
 
     size equ 4
 
 section .text ; Section containing code
 
 global _start ; Linker needs this to find the entry point
 
 _start:
         nop   ; This no-op keeps gdb happy   
         mov ebp,esp
         mov eax,5 ; Specify open sys_call
         mov ebx,[ebp+8] ; first argument
         mov ecx,00
         mov edx,0444
         int 80h ; Make kernel call
         test eax,eax
         js BadFile

         mov  [infile],eax
 
 
 ; Open output file  second argument
         mov eax,5
         mov ebx,[ebp+12] ; Second argument
         mov ecx,01101
         mov edx,0644
         int 80h
         test eax,eax
         js BadFile
         mov [outfile],eax
 
 
 Read:
         mov eax,3
Issue WAS here
        ; mov ebx,infile
         mov ebx,[infile]  ; handler is the content.
         mov ecx,Buffer
         mov edx,10
         int 80h
         test eax,eax
         js BadFile
   
      mov [outfile],eax
 
 
 ; Write the file
         mov eax,4
         mov ebx,outfile
         mov ecx,Buffer
         mov edx,size
         int 80h
         test eax,eax
         js BadFile
         jmp Read
 
 Done:
         mov eax,6 ; Specify close sys_call
         mov ebx,outfile
         int 80h
 
         mov eax,6
         mov ebx,infile
         int 80h
 
 BadFile:
         mov ebx,eax
         mov eax,1
         int 80h
 
 ; Exit
         mov eax,1
         mov ebx,0
         int 80h

« Last Edit: May 30, 2014, 07:02:06 PM by dalfonso01 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: File open , cannot get the file read
« Reply #1 on: May 30, 2014, 05:34:27 PM »
You again? Just joking - we're very happy to have questions, or any post.. I've edited your post to add "code tags" - the word "code" in square brackets (like a memory reference) at the beginning of the code and "/code" at the end. This may make it more readable and definitely makes it easier to cut-and paste. Like so:

Code: [Select]
section .bss  ; Section containing uninitialised data
 
     Buffer: resb 10
     infile: resb 4
     outfile: resb 4
 
 section .data ; Section containing initialised data
 
     size equ 4
 
 section .text ; Section containing code
 
 global _start ; Linker needs this to find the entry point
 
 _start:
         nop   ; This no-op keeps gdb happy   
         mov ebp,esp
         mov eax,5 ; Specify open sys_call
         mov ebx,[ebp+8] ; first argument
         mov ecx,00
         mov edx,0444 ; octal? Unlike C the leading 0 doesn't do it. 0444q to get octal in Nasm
         int 80h ; Make kernel call
         test eax,eax
         js BadFile
Issue is here , as passing eax to ebx directly works, I am missing something related to the right way to declare/assign the handle to infile.
         mov  [infile],eax ; this looks okay to me...
 
 
 ; Open output file  second argument
         mov eax,5
         mov ebx,[ebp+12] ; Second argument
         mov ecx,01101 ; binary? 01101b
         mov edx,0644 ; octal? 0644q
         int 80h
         test eax,eax
         js BadFile
         mov [outfile],eax ; this looks okay
 
 
 Read:
         mov eax,3
         mov ebx,infile ; [infile]
         mov ecx,Buffer
         mov edx,10
         int 80h
         test eax,eax
         js BadFile
   
      mov [outfile],eax
 
 
 ; Write the file
         mov eax,4
         mov ebx,outfile ; [outfile]
         mov ecx,Buffer
         mov edx,size
         int 80h
         test eax,eax
         js BadFile
         jmp Read
 
 Done:
         mov eax,6 ; Specify close sys_call
         mov ebx,outfile ; [outfile]
         int 80h
 
         mov eax,6
         mov ebx,infile ; [infile] probably
         int 80h
 
 BadFile:
         mov ebx,eax
     neg ebx ;?
         mov eax,1
         int 80h
 
 ; Exit
         mov eax,1
         mov ebx,0
         int 80h

The "neg ebx" makes the error number positive. The error numbers are defined as positive but what's in eax is -ERRNO. You can read it with "echo $?" and look it up in errno.h.

Once again...

Later,
Frank


Offline dalfonso01

  • Jr. Member
  • *
  • Posts: 44
Re: File open , cannot get the file read
« Reply #2 on: May 30, 2014, 07:01:49 PM »
Hi,
this following, works and I modified myself, but sure will look at yours.

I would give answers not questions! (...) that's when things start to be fun...(and somehow useful)

There were many problems:
- the filehandler needs to be assigned as mov [infile],eax and used as such where is needed, to assigned at the address of the infile.
- set BUFFLEN on "read"
 and
  mov edx,[size] on "write" after mov [size],eax on "read"
 
The availability of a filehandle from I/O ops is a minimal requirement to subsequent hubs between files and the basis to link to mmap like operations.
 
Next I will translate also the mmap example from Wrox book, and put here.

Yes, me again.... As I told, I do not intend to use C libs before being able to know myself how to do it or could do it (for quite large things) in native assembly / NASM / Intel mnemonics.
Ending the party when it could/should begin is not a clever idea, and because I am full time for a while on this, probably will be here frequently.

Code: [Select]
section .bss  ; Section containing uninitialised data

    BUFFLEN equ 100
    Buffer: resb BUFFLEN ; Buffer to read to
    infile: resb 4 ; Input file filehandle
    outfile: resb 4 ; Ouput file filehandle
    size resb 4
   
section .data ; Section containing initialised data


section .text ; Section containing code

global _start ; Linker needs this to find the entry point
       
_start:
        nop   ; This no-op keeps gdb happy   
        mov ebp,esp
        mov eax,5 ; Specify open sys_call
        mov ebx,[ebp+8] ; first argument
        mov ecx,00
        mov edx,0444
        int 80h ; Make kernel call
        test eax,eax
        js BadFile
        ; Issue here in setting the filehandle
        mov dword [infile],eax
       

; Open output file  second argument
        mov eax,5
        mov ebx,[ebp+12] ; Second argument
        mov ecx,01101
        mov edx,0644
        int 80h
        test eax,eax
        js BadFile
        mov dword [outfile],eax


Read:
        mov eax,3
        mov ebx,[infile]
        mov ecx,Buffer
        mov edx,BUFFLEN
        int 80h
        test eax,eax
        jz Done
        js BadFile
        mov [size],eax

; Write the file
        mov eax,4
        mov ebx,[outfile]
        mov ecx,Buffer
        mov edx,[size]
        int 80h
        test eax,eax
        js BadFile
        jmp Read

Done:
        mov eax,6 ; Specify close sys_call
        mov ebx,[outfile]
        int 80h

        mov eax,6
        mov ebx,[infile]
        int 80h

BadFile:
        mov ebx,eax
        mov eax,1
        int 80h

; Exit
        mov eax,1
        mov ebx,0
        int 80h
« Last Edit: May 30, 2014, 07:22:03 PM by dalfonso01 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: File open , cannot get the file read - found
« Reply #3 on: May 30, 2014, 07:12:21 PM »
Square brackets [], not angle brackets. I'll get back to ya.

Later,
Frank


Offline dalfonso01

  • Jr. Member
  • *
  • Posts: 44
Re: File open , cannot get the file read - found
« Reply #4 on: May 30, 2014, 07:14:33 PM »
Square brackets [], not angle brackets. I'll get back to ya.

Later,
Frank

Yes found meanwhile. Thanks