It is not necessary to use an std::stringstream as a memory buffer before
touching the file stream. std::ostream does already buffer the data
written to it in memory.
I removed the stringstream and made the assembly generator write
directly to the file stream.
The file stream is now explicitly flushed before assembling.
The hack for making small files work with nasm is no longer
necessary.
The real reason for the misbehaviour was that small
files fit into the file stream's memory buffer and were therefore
not written into the file until an explicit flush.
As the file stream's destruction caused a flush after assembling,
the problem was hardly observable.
I replaced many occurences of std::endl with '\n' because the former
would also flush the file stream. On a stringstream the behaviour is
equivalent.
The code is simpler now and preferring '\n' over endl is widely
considered good practice.
It is not necessary to use an std::stringstream as a memory buffer before touching the file stream. std::ostream does already buffer the data written to it in memory. I removed the stringstream and made the assembly generator write directly to the file stream.
The file stream is now explicitly flushed before assembling.
The hack for making small files work with nasm is no longer necessary. The real reason for the misbehaviour was that small files fit into the file stream's memory buffer and were therefore not written into the file until an explicit flush. As the file stream's destruction caused a flush after assembling, the problem was hardly observable.
I replaced many occurences of std::endl with '\n' because the former would also flush the file stream. On a stringstream the behaviour is equivalent.
The code is simpler now and preferring '\n' over endl is widely considered good practice.