程序集x86(ia-32):如何使用struct stat

时间:2014-02-14 10:38:18

标签: assembly x86 system-calls ia-32

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

我正在尝试使用fstat系统调用,但我遇到问题,您在代码中看到;**HERE**。事实上,系统调用后$eax的值为负数(上次运行此代码的时间为-75)。
这意味着我做错了什么,但我无法理解。有任何暗示吗?
PS:我正在运行Gentoo x86

由于

编辑:代码更新

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

1 个答案:

答案 0 :(得分:1)

您正在使用旧的fstat系统调用,它确实有一些限制并且已过时。您可以在syslog中看到该消息:

  

vmunix:VFS:警告:a.out使用旧的stat()调用。重新编译你的   二进制的。

你应该使用syscall newfstat(0x6c)或fstat64(0xc5),或者更好的是,尽可能避免asm中的文件,i / o和系统调用。

另请注意,您忘记了零终止字符串。如果它恰好是零终止,那只是因为它意外地跟着0。