Saturday, January 5, 2013

Assemble, Link and Debug assembly language program on Linux

Microsoft macro assembler or MASM is one of the most popular assembler for assembling and linking 8086 programs. It is specially designed to work with DOS operating system as it works well with Windows too. The system calls that MASM executes, work only on DOS based operating system. The Linux does not have such support built-in it.

In order to use DOS facilities on Linux, we need to use emulators or compatibility layer software packages. Some softwares which support this feature are DosBox, FreeDOS and DOSEMU. The DosBox is considered to be one of the popular are useful tool among these all. But, I think DOSEMU is much better than DosBox is many points of view.

The DOSEMU has following capabilities:
It can virtualize all input/output and processor control instructions.
It supports the word size and addressing modes of the x86 processor family's "real mode", while still running within the full protected mode environment.
It can trap all DOS and BIOS system calls and emulate such calls as are necessary for proper operation and good performance.
It can simulate a hardware environment over which DOS programs are accustomed to having control.
It provides DOS services through native Linux services; for example, dosemu can provide a virtual hard disk drive which is actually a Linux directory hierarchy.
It has packet driver API.

Most of these capabilities are not provided by DosBox. Even you cannot debug an .exe file of DOS, but DOSEMU can do!

DosBox is emulator software that simulates an IBM PC compatible computer running the older operating system, MS-DOS. Many IBM PC compatible graphics and sound cards are also emulated. This means that original MS-DOS programs are provided an environment in which they can run correctly on many modern computers running a variety of operating systems. DosBox is especially intended for use with old PC games. DosBox is free software written primarily in C++ and distributed under the GNU General Public License. [Refer: www.dosbox.com ]

DOSEMU, is a compatibility layer software package that enables MS-DOS systems, DOS clones such as FreeDOS, and DOS software to run under Linux on x86-based PCs (IBM PC compatible computers). It is available for x86 32 bit and 64 bit systems. [Refer: www.dosemu.org].

Let’s see the installation and use of DOSEMU.

Getting DOSEMU:

It can be obtained directly from the dosemu website. The link is:


Current stable version 1.4.0 is available here. Downloading and installation information is given on the above.

Or, dosemu can be installed through the terminal by just typing:

sudo apt-get install dosemu  [for Debian/Ubuntu users] or

sudo yum install dosemu      [for Fedora/RedHat users]

See the picture below.  
 
Picture 1. Getting dosemu
Using DOSEMU:

Type dosemu on the terminal and press ‘Enter’. The following window of the DOS prompt will get opened. It shows the ‘C:’ drive on it.  This drive points to Linux directory: ~/.dosemu/drive_c. We may change the drive by typing the following commands. It points to different drives and directories.
A:        Floppy Drive
D:        Linux home directory
E:         CD-ROM

Picture 2. Using dosemu
1.                 First, copy the folder on your Linux home directory named masm containing the masm.exe and link.exe, which are assembler and linker files respectively.
2.                 Change the dosemu drive to D:
3.                 Now, you can use all kind of DOS commands on the files of Linux home directory. By typing DOS command dir, you will get all the files and directories listed on the screen.
4.                 See the picture 3. The directory is changed to masm and listing from it is displayed on the screen.

Picture 3. Assembling a file
5.                 Create any 8086 assembly language program file, put it into the masm folder and then assemble it using the command masm x.asm;. The program will be assembled and errors will be shown on the screen.
6.                 If no errors found, link the program using link x; command. If no linker errors, the .exe file will be created in the same directory. See picture 4.
7.                 Now, execute the file by typing its name, such as x on the command prompt.
8.                 We can debug the program by the DOS command debug x.exe. See the Picture 4.

Picture 4. Linking and Debugging
9.                 We may use the DOS editor also to edit the assembly language program file. To do this, type edit filename on the command prompt. See the picture 5 below.

Picture 5. Using edit command
10.             Few shortcuts are also available to perform several functions with DOSEMU.
Ctrl-Alt-PgDn:   Exit DOSEMU
Ctrl-Alt-F:          Full screen
K:                                 Grab keyboard
Home:                         Grab mouse
Del:                            Reboot          

Enjoy assembly programming!

3 comments:

  1. Can we execute another .EXE files by using DOSEMU???

    ReplyDelete
    Replies
    1. Of course... all kinds of .exe can be executed in it.

      Delete
  2. Thanks for sharing useful information. I always make sure to bookmark pages like this because you know it will be useful in the future too. thanks again.
    Infrastructure as a Service

    ReplyDelete