linux - Simple way to Get filesize in x86 Assembly Language -


assuming have opened file in assembly , have file handle file in register eax. how go getting size of file can allocate enough buffer space it?

i researched discussion here suggested using sys_fstat(28) system call file stats couldn't implement it...

#my attempt @ getting file size  _test:  movl    filehandle, %ebx  #move filehandle (file descriptor) ebx         movl    $28, %eax         #fstat syscall         int $0x80             # end -14 in here not sure why 

here how implemented in freshlib. wrapper in order provide portability. can simplify of course (see below).

  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 

the minimal version can way (much less readable , not thread safe):

  ; argument: file handle in ebx   ; returns:  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 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -