Here I will show how to Allocate memory as needed. Sure, we could create a buffer in the .bss section that hopefully is large enough, but I don't like that. We will learn how to get the size of a file, allocate memory to read the file contents to, close the file and "deallocate" the memory.

This is a straight forward call, put the pointer to the file name to "stat" in ebx, this could be an absolute path and file name, or just a the file name if it is in the directory of the program (relative path). Next in ecx we put the address of the buffer to hold the "stat" info. If all went well, eax should contain 0 and our stat buffer will contain info, otherwise it will contain ERRNO.

Now, the file size will be at offset 20 from our stat pointer base.
We could access the file size like this: mov eax, dword [stat + 20] but I am all for writing code that is "self documenting" so we will access the file size like this: mov eax, [stat + STAT.st_size]

Now that we have the file size, we need to know the address of the end of our .bss section, this is called the Program Break and we get this by calling sys_brk:

To get the address, we set ebx to zero, and on return eax will contain the address of the Program Break. Here I save it to Org_Break so I set the break back to this later. I save it to TempBuf, so I can, well, use this new buffer I will create. After that, I push the address of the break contained in eax onto the stack.

So, how do we extend this break to create a buffer? We take the address of the Program Break, and add to it the number of bytes we want the buffer to be. This will give us a new address that the system will set the new Program Break to:

Another "simple" call, ebx needs a pointer to the file to open, the same rule for sys_stat filename apply here. ecx is the access mode for the file - it MUST contain one of the following:
O_RDONLY, O_WRONLY, or O_RDWR, you could also bitwise OR file creation and file status flags here.
edx contains the file permissions. This is ignored if O_CREAT is not specified in the flags parameter.

If the file was opened eax will contain the File Descriptor. Here we save it to esi since this is one of the few registers that are saved across system calls.

ebx is the File Descriptor of the open file to read from, ecx is the address of the buffer to hold the file contents, edx is the number of bytes to read, here we will read the whole file into our buffer. On return, eax will contain the number of bytes read.