Algorithms, Blockchain and Cloud

Lost Era, Microsoft DOS, 16-bit Assembly, Echo program revisited


16-bit DOS has been obsolete. But learning 16-bit Assembly helps up understand how underlying system works and it is fun programming using assembly. 16-bit Assembly is similar to 32-bit Assembly in terms of some instructions e.g. mov, cmp, jmp etc.  What differs most is that under DOS, most functions are provided via calling interrupts e.g. int but on Windows, this is no longer supported and should use invoke (which is a macro) to call Win32 APIs.

We all know under DOS shell (even the CMD shell for Win7, Win8), there is a simple command echo that will basically print its parameters from the command line. This command has been enhanced at Windows CMD shell and it supports printing the value of some environment variables such as %PATH%, %SHELL% etc.

Today we are going to make a tiny .COM assembly program (no header or meta data), which has the simple function of printing message given at command line.

echo

As it is small and simple, we don’t actually need an assembler and linker. We just need the powerful tool debug.exe and put assembly instructions one by one. The command line parameters are stored in the PSP (Program Segment Prefix) which is a 256-byte data structure that stores the state of the DOS programs. The length of the parameters is stored at location [0x80h] and from [0x81h] to [0xFFh] it is reserved for the parameter string.

We have learned before that DOS interrupts 02nd prints a single character and 09th interrupt prints a message that ends with dollar sign $ (Register DX holds the address of the string).

Today, we are going to use 40h interrupt which can be used to write a number of characters (count specified in Register CX) to files (file handle given in Register BX). However, we can still write to STDOUT where the file handle is ONE. Register DX as usual, keeps the address of the string to write.

Then the principle is easy. We get the number of characters of the command line parameters at location [0x80h] and we use 40h interrupt to print [0x80h] characters starting at location 0x81h to STDOUT (BX=1).

The source code is 6 lines of assembly code which produces a 17-byte .COM program.

movzx cx, byte ptr[80]
mov bx, 1
mov ah, 40h
mov dx, 81h
int 21h
int 20h

The first line we use movzx, which will copy the value of second operand to the lower part of first operand and extend zeros to the first half of the first operand. It is equivalent to the following two instructions.

mov cl, byte ptr[80]
xor ch, ch

This however, will consume one more byte and is slower than using only 1 instruction.

–EOF (The Ultimate Computing & Technology Blog) —

576 words
Last Post: Lost Era, Microsoft DOS, .COM Assembly, 8 byte program, GetKey
Next Post: Lost Era, MS-DOS 16-bit Assembly, Generate a DOS .COM Message Print using Python, Write Binary Code

The Permanent URL is: Lost Era, Microsoft DOS, 16-bit Assembly, Echo program revisited (AMP Version)

Exit mobile version