polslinux
polslinux

Reputation: 1779

assembly x86 (ia-32): how to use struct stat

global _start
section .text
_start:
    mov eax,5 ;open
    mov ebx,filename ;path to file
    mov ecx,0000o ;O_RDONLY
    int 0x80
    mov ebx,eax ;fd
    mov eax,0x1c; fstat
    mov ecx,structvar ;address of struct
    int 0x80
    ;**HERE**
    mov eax,0x1 ;exit
    mov ebx,0x5 ;ret code
    int 0x80

section .data
    filename: db "/home/USER/file.txt"

section .bss
    structvar: resb 88 ;struct stat is 88 bytes in x86

i'm trying to use the fstat syscall but i have a problem where you see ;**HERE** in the code. In fact the value of $eax after the syscall is a negative number (the last time i run this code was -75).
This mean that i am doing something wrong but i cannot understand what. Any hint??

PS: i'm running Gentoo x86

Thanks

EDIT: code update

global _start
section .text
_start:
    mov eax,5 ;open
    mov ebx,filename ;path to file
    mov ecx,0000o ;O_RDONLY
    int 0x80
    mov ebx,eax ;fd
    mov eax,0x6c ;newfstat
    mov ecx,structvar ;address struct
    int 0x80
    mov eax,0x1 ;exit
    mov ebx,0x5 ;ret code
    int 0x80

section .data
    filename: db "/home/USER/file.txt",0x0

section .bss
    structvar: resb 88 ;struct stat in x86 is 88 bytes

Upvotes: 1

Views: 1937

Answers (1)

Jester
Jester

Reputation: 58792

You are using the old fstat system call which indeed has some limitations and is obsolete. You can probably see the message in syslog:

vmunix: VFS: Warning: a.out using old stat() call. Recompile your binary.

You should use the syscall newfstat (0x6c) or fstat64 (0xc5), or better yet, avoid files, i/o and syscalls in asm if possible.

Also note you have forgotten to zero terminate your string. If it happens to be zero terminated it's only because it's accidentally followed by a 0.

Upvotes: 2

Related Questions