Lost Era, Microsoft DOS, .COM Assembly, Print Letters using Loop


Microsoft DOS has declined but to review those knowledge of writing DOS programs (starting from the simplest .COM binary) can help us learn better how computers work in the low level aspect. Previous articles [here] and [here] describe the experiments using DOSBox to write .COM programs using debug.exe 

Today, we are going to explore a little bit more. We will show you how to print 26 upper case letters in using .COM assembly in just 15 bytes! Through this tutorial, you will learn how to use the powerful tool debug.exe to write assembly steps by steps. Also, you will be familiar with the simplest loop statement for creating an iteration.

dosbox-print-letter-debug Lost Era, Microsoft DOS, .COM Assembly, Print Letters using Loop 16 bit assembly language implementation MSDOS 16-bit programming languages windows command shell

Start from the shell prompt C:\ or anywhere as long as the PATH variable stores the correct path to look for the file debug.exe.

Type debug letters.com and this will give a message that the file does not exist. If it does exist, it will load the program into 0x100 location (the first 256 bytes stores the Program Segement Prefix, that is a data structure to represent the state of a program).

We use command to set the filename for writing later with the command w. Then we can use a 100 to start writing assembly at address 100.

After we finish the program, we can press Enter twice that will return to the command mode of debug.exe. We can then use r cx to write value to the register CX that stores the length of code to write. Finally, the command writes the code to disk (prior to this, and r cx should be in place).

The source code is just 15 bytes, a few lines only and it iterates printing the upper case letter (starting with ‘A’, ASCII = 65, hexadecimal is 41h)  then each iteration, increase the character by one and print it. After 26 times, the program terminates by using int 20h .COM exit interrupt.

  mov cx, 1a
  mov dl, 41
rep:
  mov ah, 2 
  int 21h
  inc dl
  loop rep
  int 20h

The register CX stores the loop counter. Each time when loop is executed, it decrements the CX by one and checks whether CX = 0. If yes, it doesn’t jump but move forward one instruction. If no, jumps to the specified label. It can be translated into C code, which is more clear.

1
if (--cx > 0) goto label;
if (--cx > 0) goto label;

The command in debug.exe will run the .COM program at address 100.  Instruction inc dl increments the register DL by one. similarly, dec will decrement the specified register by one.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
605 words
Last Post: Lost Era - Microsoft DOS, 16 bit .COM Assembly
Next Post: Emerging Era, Win32 Assembly, MASM, Hello World!

The Permanent URL is: Lost Era, Microsoft DOS, .COM Assembly, Print Letters using Loop

4 Comments

  1. Sammy
  2. Emerson Lara

Leave a Reply