Hi guys,  I am developing an assembler program with GAS and I need to know the size of a file
I know that I can use  GetFileSize function, the problem is that I dont know which its parameters are, or maybe I do but I dont understand them.

GetFileSize( HANDLE hFile, LPDWORD FileSizeHigh )


  • HANDLE hFile,  // handle of file to get size of

  • LPDWORD lpFileSizeHigh                  // pointer to high-order word for file size

Can somebody explain me what a handle is? what is its type of data?  Do you have some example to give me?
Thanks in advance,
Posted on 2011-03-11 12:26:59 by massem
Handles are numbers returned by many winapi functions. In this particular example, if you want to get a file's size, you need t first open it (CreateFile) and, after you get its size, close it (CloseHandle). Handles are 32-bit integers on 32-bit Windows and 64-bit integers on 64-bit Windows.
Posted on 2011-03-11 13:04:54 by ti_mo_n
Thanks ti_mo_n ! your explanation was very helpful.  Now I have another doubt related to the same function.  I am using GNU ASSEMBLER (gas) and when I compile my program a message appears in the console:

undefined reference to `GetFileSize'

the question is... which reference do I have to use to allows my program understand the getFileSize function? or are there any other function like this?

Thanx !
Posted on 2011-03-15 13:24:27 by massem
Include these.

You can find these in the win32.hlp file, in the SDK, or online/


Posted on 2011-03-15 14:34:12 by skywalker
undefined reference to `GetFileSize'

Look here: http://msdn.microsoft.com/en-us/library/aa364955(v=vs.85).aspx
It says what lib it needs near the bottom.

Also, pay attention to this tip:
Then use FindFirstFile to do that. Over a network share, CreateFile then GetFileSize is dozens of times slower than just using FindFirstFile to get at the size.
Edit: Or you can use GetFileAttributesEx.
Posted on 2011-03-15 15:57:06 by ti_mo_n
If that doesn't work, also try the naming convention _(Procedure)@(Argument Size) like so:


Posted on 2011-03-15 21:48:48 by Synfire
well, I think that you are going to laugh at me. but i cannot import the library! shoot!  I wrote that

.include "LibraryKernel32.dll"

and the compiler says:

Error: can't open LibraryKernel32.dll for reading: No such file or directory

I am using GNU assembler...

Thanks in advance and thank you for answering me the other doubt.

Posted on 2011-03-18 13:38:23 by massem
Get the masm32 package from here so we can better help you.

Posted on 2011-03-18 18:51:58 by skywalker
mm but i'm working under linux.... programming with GNU assembler.....

Posted on 2011-03-22 13:49:42 by massem
I don't think you're going to find any "GetFileSize" in Linux - definitely not a sys_call, and I don't think it's in libc, either. Try "stat" if the file isn't open, or "fstat" if it is. If it's open, you can also "lseek" to the end of it to get the size (handy if you're going to "mmap" it, otherwise you probably want to "lseek" back to the beginning before you read/write).

"man 2 stat" should give you parameters... (you pass it the address of a buffer, and read the size out of that buffer).


Posted on 2011-03-22 16:25:45 by fbkotler
fopen a file, fseek to the end, ftell the position and then fclose the file. It's the most portable way, AFAIK.
Posted on 2011-03-22 17:22:24 by ti_mo_n

Are you attempting to get a Windows program up and running on Linux using Wine???
Posted on 2011-03-23 10:29:56 by p1ranha
Ok, Thank everybody for the replies. I decided to use stat function from c to get the SIZE OF A PARTICULAR FILE.  Look at my code, I use
GNU assembler (gas) with at&t sintax

file: .asciz"source.txt"
.section .bss
buffer: .space 5
fd: .space 4
.section .text
.globl _start
          movl $5, %eax #service number 5: open
          movl $file, %ebx #copy the path into ebx
          movl $0, %ecx #readonly
          int $0x80 #  SO call
            movl %eax, fd
          pushl $buffer
          pushl $fd
          call fstat
          addl  $8, %esp
            movl $1, %eax
      movl $0, %ebx
      int $0x80

From this code, I understand that buffer has the whole information about the file.  Now, i need to access to the  st_size field.  How I am supposed to do that?  I know that i have to find the offset (which I did with some code c example) but I don't know how to write it! I know that st_size represented 44 bytes of offset... now what?

thanks in advance!!

Posted on 2011-04-07 14:04:03 by massem
Hi marce,

The first thing I see is that your buffer's too small (I think). I guessed that 64 bytes should be "enough". The second thing is that you want to "pushl fd", not "pushl, $fd". The third thing is a question. How'd you come up with 44 for the offset into the structure? I count it as 28, ASSuming 4 bytes per "_t", but 44 appears to be correct... As to "how to write it", I dunno, I'm Nasmist! :) Without the '$', (G)as assumes "contents", so I tried "just do it", and it seems to work...

#file: .asciz"source.txt" # no such file or directory
file: .asciz"filesize.s" # I know this one's there!
fmt: .asciz "%i\n"

.section .bss
buffer: .space 64 #5
fd: .space 4

.section .text
.globl _start
          movl $5, %eax #service number 5: open
          movl $file, %ebx #copy the path into ebx
          movl $0, %ecx #readonly
          int $0x80 #  SO call
# check for success/error?

            movl %eax, fd

          pushl $buffer
          pushl fd # <- contents, not offset!
          call fstat
          addl  $8, %esp
# check for success/error?

movl buffer + 44, %eax
pushl %eax
pushl $fmt
call printf
addl $8, %esp

            movl $1, %eax
      movl $0, %ebx
      int $0x80

I may come up with an "all sys_calls" version, if I'm overcome by an unexpected wave of ambition (possibly in Nasm syntax :) ). I really should look up that structure, rather than "guessing"! But for now, see if that helps...


Posted on 2011-04-07 19:05:40 by fbkotler
            you are, definitely, the best!!!  It worked!!!  Anyway I would like to clear up some things:

You asked me:
How'd you come up with 44 for the offset into the structure?

Well, I wrote some c code to figure it out:

#include <stdio.h>
#include <stddef.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main() {
  printf( "the fffset of st_size is: %d\n", offsetof( struct stat, st_size ) );
  return 0;

Then, I saw in your code corrections like "check for success/error" ----->yes I forgot it!

Besides, you wrote: "#file: .asciz"source.txt" # no such file or directory"  But I needed the size of this exact text file, so, I must say that I didn't pay attention to your correction and it worked hehe... 

Last but NOT LEAST, My question or my doubt was this part: "movl buffer + 44, %eax" I didn't know how to do it and I can't believe that  was so easy.!!!
Thank u so much.  I am a complete beginner with this language and I'm realizing that is really hard but amazing at the same time haha

Posted on 2011-04-09 08:20:17 by massem