Is it possible to get the file size of a file at compilation

I like to use FASM for my own OS development. Not only do I like how it reminds me of assembler when I was 14 years old when I was coding my first OS in Turbo Assembler, but it also has lots of cool features.

For example, to boot my OS, I create a floppy disk image. I don't need special tools for this, because with the file command, I can just combine different files and with the rb command, I can put them in exact places.

So I first compile all my .asm's and then I join all their binaries by compiling disk.asm which is actually a lot of data definitions and bin-file references.

I am now creating a complete FAT12 structure within that file and it works perfectly. The only problem I have is that in a FAT12 structure, I need to have an exact file size to put in there (at this point, I just always read enough from the disk).

So, like there is a file command, is there something like a filesize function or is there a workaround for this? Or can it maybe be added? It would be so cool to be able to create a complete correct disk image just by compiling my disk.asm file.

I could of course create a script that creates a small .inc file for every .bin file I have, just containing dd <filesize>, but that would become such a mess when the number of files grows.

If you use FILE directive to include contents of entire file, you can simply compute the difference in offsets to get the size of that file:

Code:

examplefile'example.bin'example_size = $ - example

And since fasm allows forward-referencing, you can use the value of "example_size" in your headers (before the actual files are included) and fasm is going to resolve these variables so that headers end up containing correct values.

If you do not include entire file anywhere, you can still use the same trick to compute the file size, just use the VIRTUAL block to avoid placing a whole file in the output:

Code:

virtualat0
file"example.bin"example_size = $
endvirtual

The above examples apply to both fasm 1 and fasmg with no difference. But in case of fasmg you can also do some trickier things, like loading the contents of a file into a string:

scippie
You can put an ampersand directly after the argument name in the first line. This will tell fasm to use the whole remainder of an argument line as the last argument. Aside from that passing arguments surrounded by < and > allows to ignore commata as argument separators.

scippie
You can put an ampersand directly after the argument name in the first line. This will tell fasm to use the whole remainder of an argument line as the last argument. Aside from that passing arguments surrounded by < and > allows to ignore commata as argument separators.

Thanks! That's perfect.

Is it documented? I didn't see it in the docs, but I'm a terrible reader.

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum