flat assembler
Message board for the users of flat assembler.
Index
> Main > Is it possible to get the file size of a file at compilation |
Author |
|
scippie 26 Jan 2017, 19:59
I already just thought of a better way, but I'd still prefer a function if there were any.
I just did this: FOO: file 'foo.bin' FOO_end: and in my FAT12 header: dw (FOO / 512 - 33 + 2) dd (FOO_end - FOO) (I still need to add some constants ) |
|||
26 Jan 2017, 19:59 |
|
Tomasz Grysztar 26 Jan 2017, 20:01
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: example file 'example.bin' example_size = $ - example 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: virtual at 0 file "example.bin" example_size = $ end virtual 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: Code: ; WARNING: this snippet works with fasmg only virtual at 0 file 'example.bin' load example_string : $ from 0 end virtual dd lengthof example_string ; size of file db example_string ; contents of file |
|||
26 Jan 2017, 20:01 |
|
scippie 26 Jan 2017, 20:57
Tomasz Grysztar wrote: 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: That's even better! Thanks! Tomasz Grysztar wrote: 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: Cool stuff. Fasm is great |
|||
26 Jan 2017, 20:57 |
|
scippie 26 Jan 2017, 21:22
So, after reading your text, I wonder if it's possible to have the length of a text string as first byte before the string. Something like:
label db <len>,'Hello world!' How could <len> be automated? |
|||
26 Jan 2017, 21:22 |
|
Tomasz Grysztar 26 Jan 2017, 21:27
With fasmg it can be as simple as:
Code: struc str value . db lengthof value, value end struc hello str 'Hello world!' Code: struc str value { local ..length . db ..length,value ..length = $ - (.+1) } hello str 'Hello world!' |
|||
26 Jan 2017, 21:27 |
|
scippie 26 Jan 2017, 22:22
Wow, thanks! As I am using fasm 1, it's the struc.
I had to create an extra struc for strings ending with CR/LF, because the struc wouldn't recognize the extra 13,10 after str as extra data in the string. Or is there a way to make it work with the same struc? I searched the manual for this (it has been very long since I looked at the manual) but couldn't find it. |
|||
26 Jan 2017, 22:22 |
|
l_inc 26 Jan 2017, 22:34
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. _________________ Faith is a superposition of knowledge and fallacy |
|||
26 Jan 2017, 22:34 |
|
scippie 26 Jan 2017, 23:23
l_inc wrote: scippie Thanks! That's perfect. Is it documented? I didn't see it in the docs, but I'm a terrible reader. Thanks for all the quick help guys. I feel welcome already! |
|||
26 Jan 2017, 23:23 |
|
l_inc 26 Jan 2017, 23:38
scippie
Yes, it's mentioned in the chapter 2.3.3 Macroinstructions. The manual is quite concise about many things. So they are admittedly easy to miss and sometimes hard to understand. _________________ Faith is a superposition of knowledge and fallacy |
|||
26 Jan 2017, 23:38 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.