lundi 20 avril 2015

Why does this code not retrieve a valid file handle?

Complete source:

SECTION .data       ; initialized data
    SEEK_SET    dd  0;
    SEEK_CUR    dd  1;
    SEEK_END    dd  2;

    fname:      db  "d:\asmplus\tsources\s1.txt", 0
    fread:      db  "r", 0

    mopf:       db  "open", 0
    mskf:       db  "seek", 0

    mcld:       db  "[FILE] call [%s]: %d", 10, 0
    mcls:       db  "[FILE] call [%s]: %s", 10, 0
    merr:       db  "[FILE] error opening %s", 10, 0
    mret:       db  "[FILE] ret [%s]: %d", 10, 0

SECTION .text       ; code
    extern _fopen
    extern _fseek
    extern _printf

    global _main

    _main:
        ;   stash base stack pointer
        push    ebp
        mov     ebp,    esp

        push    DWORD   fname
        push    DWORD   mopf
        push    DWORD   mcls
        call    _printf
        add     esp,    12

        ;   open file
        push    DWORD   fread
        push    DWORD   fname
        call    _fopen
        add     esp,    8

        mov     [fh],   eax

        ;   output result
        push    DWORD   [fh]
        push    DWORD   mopf
        push    DWORD   mret
        call    _printf
        add     esp,    12

        push    DWORD   [fh]
        push    DWORD   mskf
        push    DWORD   mcld
        call    _printf
        add     esp,    12

        ;   C:
        ;   fseek(fp, 0L, SEEK_END);    ; set up constants: SEEK_END, SEEK_SET, etc.
        push    DWORD   SEEK_END
        push    DWORD   0
        push    DWORD   [fh]            ; file handle
        call    _fseek                  ; ret [eax]: 0 okay; otherwise nz
        add     esp,    12              ; reset stack pointer

        ;   output result
        push    DWORD   eax
        push    DWORD   mskf
        push    DWORD   mret
        call    _printf
        add     esp,    12

        ;;   NEXT: sz = ftell(fp);             ; result to eax

        .done:
        ;   restore base stack pointer
        mov     esp,    ebp
        pop     ebp

        ret

SECTION .bss        ; uninitialized data
    fh:     resd    1

Output when provided a valid file name:

[FILE] call [open]: d:\asmplus\tsources\s1.txt
[FILE] ret [open]: 2002397536
[FILE] call [seek]: 2002397536
[FILE] ret [seek]: -1

The 2nd & 3rd lines should display a file handle. What I see does not look right and would be why the seek return is -1. What am I doing wrong to open the file?

Aucun commentaire:

Enregistrer un commentaire