Friday, October 6, 2017

How to start writing Assembly 16-bit code on your 64-bit machine

Hello there. The first thing to came into anyone's mind after deciding to learn Assembly is: Why can't I run my 16-bit assembly code on my PC? Why there is still Assembly 16-bit while all the processors and OSs nowadays are 64 bits?

The answer is...History! let me explain.

Do you know what BIOS is? It's the first thing to run when you press the power button on your PC/Laptop. BIOS is an abbreviation for The Basic Input Output System. BIOS is a software, stored on a small memory chip on the motherboard.

What is BIOS used for?
BIOS instruct your PC on how to perform a number of basic functions that are pretty important for booting process (loading your OS) like identifying the hard drive, floppy drive, optical drive, memory, etc..
And it does some other stuff like POST process or Power-on Self-test.

In order to not getting bogged down in nitpicky details, let's back to the track.
BIOS must run and the processor has to be in the proper mode to run it. From now onwards, things gonna make sense.

The very first commercial computer released, had an 8088 processor, which was a variant of the 8086 processor with an 8-bit data bust. It was able to deal with 16-bit hardware drivers but back then, the hardware designers were centered around the 8-bit processors. So creating a 16-bit processor with 8-bit data bus will help the processor to fit in the hardware market thus will make it more successful.

But there is another detail that will help the processor to fit in the markets. The ability to run the pre-existing software(like BIOS). 8086 and 8088 processor were designed to be assembly-language compatible with the earlier 8080 8-bit processors to make it easier to port the existing software. And this became a recurrent theme. Each processor in the family has to be compatible somehow with what comes before.

After that, when the 80386 came out, the first truly 32-bit processor, the designers had a problem. The processor must be able to run the existed 16-bit software or it's not gonna success. The solution was to make the processor starts at the 16-bit compatible mode (real mode) then they programmed a piece of software to switch it to the 32-bit mode (protected mode) to make use of the other features that 32-bit processors offer like memory and so on.

So the existing BIOS, which was written for 16-bit machines, could still work on 386 machines. MS-DOS, which was written for 16-bit machines, could still work on 386 machines.

And that's why you can run 16-bit assembly on a 32-bit OS. But what happened after? What about 64-bit?

The 64-bits processors offered a new mode which is the long mode and the designers changed the time-honored tradition. The BIOS was replaced by UEFI (Unified Extensible Firmware Interface) that allows the computers to boot directly into the protected mode with the ability to switch into the long mode. The software leaders like Microsoft removed the 16-bit subsystems from Windows because it's no longer useful. Bye bye, real mode.

Now that you know the story, how to get over that? How to learn the easiest and the most supported assembly and then make your way up to other variants?

There are many solutions you can go through, I'm going to mention some of them.

If you are on Windows, you can get emu8086. As its name implies, its an 8086 emulator and debugger.
You can download it here

You can also get DOSBox, Its a DOS emulator that will allow you to use DOS on your modern OS, so you will be able to use any assembler you need (unlike emu8086 that uses FASM by default).
Get DOSBox here

The third option is to download a VM like Virtual Box or VMWare workstation and install any DOS-like OS on it, like Free-DOS

If you are on Linux, you still can use DOSBox because its ported to run under Linux. And if you  wanna use emu8086, you can download WINE and install emu8086 through it. It runs perfect, I've tried it under Linux. And you also can get a VM because you know, VMWare workstation and Virtual Box are ported to Linux.

No comments:

Post a Comment