【问题标题】:Simple way to Get filesize in x86 Assembly Language在 x86 汇编语言中获取文件大小的简单方法
【发布时间】:2014-04-06 20:17:47
【问题描述】:

假设我已经在程序集中打开了一个文件,并且在寄存器 eax 中有该文件的文件句柄。我将如何获取文件的大小以便为它分配足够的缓冲区空间?

我在这里研究了另一个讨论,建议使用sys_fstat(28) 系统调用来获取文件统计信息,但无法实现它...

#My attempt at getting the file size

_test:  movl    filehandle, %ebx  #move filehandle (file descriptor) into ebx
        movl    $28, %eax         #fstat syscall
        int $0x80             # always end up with -14 in here not sure why

【问题讨论】:

  • 系统调用将其结果放在哪里?
  • 我的印象是除非出现错误,否则结果应该进入 eax?
  • 整个调用的返回值在那里,是的,但是sys_fstat 函数不返回给定文件的大小。它需要第二个参数,一个指向 __old_kernel_stat 结构的指针,它填充了许多信息 - 其中只有一个是文件大小。
  • 文件统计是整个结构的数据价值。这不适合 EAX。
  • 使用stat系统调用。

标签: linux file assembly


【解决方案1】:

我的解决方案——只需使用 .lcomm 为所有命名变量创建位置

movl    inputfile, %ebx         #Move file handler into ebx for system call
movl    $0x6c, %eax          #Stat Sys Call into eax
leal    statlocation, %ecx      #Move reserved location for stat structure into
int     $0x80                    #Execute System Call
movl    20(%ecx), %eax          #20 to location of size variable, size is now in eax

【讨论】:

    【解决方案2】:

    这是 FreshLib 中的实现方式。它是一个包装器,以提供可移植性。您当然可以简化它(见下文)。

      struct STAT
        .st_dev     dw  ?     ; ID of device containing file
        .pad1       dw  ?
        .st_ino     dd  ?     ; inode number
        .st_mode    dw  ?     ; protection
        .st_nlink   dw  ?     ; number of hard links
        .st_uid     dw  ?     ; user ID of owner
        .st_gid     dw  ?     ; group ID of owner
        .st_rdev    dw  ?     ; device ID (if special file)
        .pad2       dw  ?
        .st_size    dd  ?     ; total size, in bytes
        .st_blksize dd  ?     ; block size
        .st_blocks  dd  ?
    
        .st_atime   dd  ?     ; time of last access
        .unused1    dd  ?
    
        .st_mtime   dd  ?     ; time of last modification
        .unused2    dd  ?
    
        .st_ctime   dd  ?     ; time of last status change
        .unused3    dd  ?
        .unused4    dd  ?
        .unused5    dd  ?
      ends
    
      sys_newfstat =  $6c
    
      proc FileSize, .handle
      .stat STAT
      begin
              push    edx ecx ebx
    
              mov     eax, sys_newfstat
              mov     ebx, [.handle]
              lea     ecx, [.stat]
              int     $80
    
              cmp     eax, -1
              jle     .error
    
              mov     eax, [.stat.st_size]
              clc
              pop     ebx ecx edx
              return
    
      .error:
              neg     eax       ; error code
              stc
              pop     ebx ecx edx
              return
      endp
    

    最小版本可以这样看(可读性差很多,而且不是线程安全的):

      ; argument: file handle in ebx
      ; returns:  the size in EDX; error code in EAX
      FileSize:
              mov     eax, $6c
              mov     ecx, file_stat
              int     $80
              mov     edx, [file_stat+$14]
              retn
    
      file_stat rd $10
    

    【讨论】:

    • 谢谢...我也想通了,为了有 AT&T 语法,我会发布:
    • @user2499298 - 尝试退出 AT&T 语法。它确实不适合严肃的汇编编程。 :)
    • 相信我,我完全同意,但我使用它是因为我想在 gdb 中调试时看到符号/行 cmets。
    • @user2499298 - 也退出 gdb。 ;) 请改用EDB。
    猜你喜欢
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    相关资源
    最近更新 更多