Getting started with MASM 8086 assembly

Akshay Anand
3 min readFeb 22, 2020

--

Why suddenly 8086 assembly using MASM?
Well, partly because I love exploring random legacy system programming and partly because I stumbled upon this book called “C using Assembly Language” by Steven Holzner (1989). Author has used Quick C and Microsoft Micro Assembler to demonstrate use cases. So here I am, experimenting with stuffs.

Setup
To get started with MASM, we need to have DOSBOX (an IBM PC compatible DOS emulator) and MASM 8086 assembler files to go along with it.

I use a Mac, so my instructions will be for MacOS. However, these steps are same for the most part for other platforms too, as our tool of choice, DOSBOX, is available for other OSes too.

Download DOSBOX from here.
Download 8086 assembler from here.

Copy DOSBOX to Applications from the disk image file.
Then make a folder named “dosbox” in your home directory.
Extract 8086.zip files to ~/dosbox.

Now start DOSBOX. You should see the prompt to be z:\>
Type “mount c ~/dosbox/8086”
Z:\> mount c ~/dosbox/8086
Then type “c:” to change drive.
Z:\> C:
C:\>

Now you are ready to go. You can use “edit” utility to create and edit new asm files.
For example: “edit hello.asm”
Once you are done writing the code (You can try the code snippet from below), you need to assemble and link before executing.

Type “masm hello.asm”
(Keep pressing Enter to bypass the prompts and to use default values for other filenames)
Then “link hello”
(Again press Enter a couple of times)
Then type “hello.exe” and press Enter to execute the program.

Now coming to the assembly code.

MASM programs usually contains two segments: CODE and DATA.
CODE segment contains the opcodes. DATA segment contains the variables.

In this code, we are going to define a variable named MSG which will store the string to be printed. DB (Define Byte) is a directive that is used to reserve byte(s) of memory locations in available memory. “$” at the end of the variable declaration is used to signify termination of the string.

In the code segment, the following two statements (line12, 13) are used to initialise DATA segment:

MOV AX, DATA
MOV DS, AX

These two statements must be used in all the MASM codes.

Lines 15 to 17 are used to print the string.
To output a string in 8086 assembly, we load the address of a '$'-terminated string into DX and then call the interrupt with function code 09h in AH. MS-DOS provides many interrupt services through INT 21H. Function code 09h is just one of them (i.e., to output a string).

Lines 20, 21 are used to exit the program. In MS-DOS on 8086, we save the return code for a program in AL register. Then we call the interrupt with function code 4Ch in AH. In line 20 we are directly moving 4C00h into AX, signifying normal exit.

Resources

http://www.shsu.edu/~csc_tjm/fall2002/cs272/intro_to_asm.html
https://www.csc.depauw.edu/~bhoward/asmtut/asmtut12.html#topic11
http://alpbyrs.blogspot.com/2015/06/assembly-language-programming.html

--

--

Akshay Anand

Programmer | Bibliophile | Pencil artist | Photographer | Media creator | Polyglot