I am currently coping with a devious RISC architecture (ahem) and wrote some assembly for it, so I thought what the hell lets write something in x86(_64). I’ve done that in the past in a kernel driver I developed as part of my master thesis, but it wasn’t the real deal. I merely wrote around 10 lines of inline assembly, so I guess that, it doesn’t count at all.
This is not the typical howto article I write, this is merely a rant about how crappy the x86 assembly is. It has almost no registers (compared with current RISC architectures) and its few registers are not exactly general purpose. I remember when I nagged in the office during the first days of work of how crappy the RISC architecture is. Well I take that back. x86 is crappier. Segmented memory? Why isn’t my memory flat? And to leave real mode and enter protected mode? I almost wrote a encyclopedia to get me going (thing which makes me wonder If I had skipped classes in CEID - or aren’t we taught real mode, protected mode and x86? I mean ok in theory I know the whereabouts but what about the exact procedures?)
To tell a long story short, in order to boot a bochs instance and to print something comprehensible like “x86 is a mess” outside real mode, took me almost half a day, most of which was getting acquainted with the language (assembly), but mostly the architecture. Some tips to those that intend to do the same (bad idea):
- Bootable sector are the first 512 bytes of a floppy disk, and needs to be crafted carefully in order to be able to boot (no spoilers
) - If the criteria are met the sector is loaded @ 0×7C00 and the system is in real mode
- While you prepare to go to protected mode choose wisely: do use paging (it will save you from many trouble later on) ie load CR3 with PDPR
- Always have valid selectors on the data segment registers (on interrupt you get GP exception if not setup properly
) - At some point you will understand that unreal mode is all you wanted (so skip previous steps and go find out about this little sucker)
- To use the 64bit arithmetic, long mode can be used (and it is easier to get from real mode to long mode than from real to protected - NO MORE MEMORY SEGMENTATION FFS - that’s what I call upgrade
) - enable long mode doesn’t mean that you have entered long mode. You need a little more work to use those 64bit registers
- returning from long mode to protected mode? it is easier to reset
to sum up don’t try any of the above at home. It is not funny. Anything is better than hacking with x86’s assembly
Leave a Comment