No, because that doesn't solve the problem in a good way and because it demonstrates a critical misunderstanding on your part. You forgot to follow my advise and go an online search and find a good article such as
http://blog.llvm.org/2011/05/what-every ... -know.html.
Mind that inline assembly is also UB if you lie to the compiler. That is, if you don't write it in exactly the correct manner (just adding volatiles is not enough, and sometimes they shouldn't even be there) the result is UB and bad things happen. Never write inline assembly, unless you are an expert in inline assembly. It's surprisingly hard to get right. Always reuse community-reviewed inline assembly such as the functions at
http://wiki.osdev.org/Inline_Assembly/Examples or just use a regular assembly file.
The problem with UB is that it happens at the C compile level,
not at the machine level. Unaligned reads are undefined behavior with gcc, even if the machine actually supports it. This is not a problem in practice, unless you write bad code such as your above example. (It's hard to make an unaligned pointer without invoking UB while doing so, well, I can think of a few ways but still). The solution is quite simply to turn your unaligned read into two smaller aligned reads:
Code: Select all
uint8_t* bda = (uint8_t*) 0x400;
uint16_t base_video_port = bda[0x63] << 0 | bda[0x64] << 8;
Notice how this does no unaligned reads and use exclusively well-defined behaviour. When you find yourself writing inline assembly, you should ask yourself whether it is possible to write well-defined C instead (you need to read that above link for information on UB to avoid this minefield). Mind that UB isn't the same as `platform-dependent' or `implementation-specified'. It means that the standard literally says that anything is allowed to happen. Thus a theoretical compiler that follows the standard is allowed to summon Darth Vader and have him destroy the compiler with a lightsaber whenever you do unaligned reads. The idea is that the compiler is allowed to define undefined behavior (if it wants to) as something implementation-specific, but it is not required to, and many C compilers do not do so in most cases.