-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|269|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Article Home -> Coding Basics


 
cthug
Created : 18 February 2010
 
System : Amstrad CPC
Language : PHP

DOS Assembly Introduction

A basic introduction in DOS assembly

This tutorial assumes knowledge of DOS commands and x86 Assembly. Also before reading this tutorial please install FASM, and have a pc or virtual machine running DOS.

What IS this tutorial

  • A basic introduction in DOS assembly.

What ISN'T this tutorial

  • A complete introduction in DOS assembly.
  • A tutorial in assembly.
  • A tutorial for VGA or other Graphics programming on DOS.
  • A reference for DOS assembly/programming.

Now some code

Don't worry if you do not understand this I will explain it later.



Save to disk as 'test.asm', and type these command:


Make sure you are in the same directory as your source code. You will notice when you run it that it will do nothing, but really this small program is showing us a very important part of programming: INTERRUPTS. Interrupts are the way for programs to interface with the BIOS, and also how we interface with DOS.

Breaking down the code

'format binary'

This is just a directive to the assembler to output flat binary machine code, not in any format like ELF or PE.

'org 100h'

This piece of code is telling the assembler that the codes origin is 100h (the 'h' means hexadecimal). The origin is where the code is loaded into memory. DOS loads the program code to the memory address 100h hence 'org 100h'.

'mov ax,4C00h'

This code moves the 4Ch into ah , and 0 into al. As we know ah is the high order byte and al is the low order byte of ax. This is just a little optimisation trick.

'int 21h'

This causes a interrupt number 21h (here is a full list of interrupts, including DOS interrupts). This is interrupt number is handled by DOS, and is used to call functions provided by the OS. Registers are used to pass the arguments to these functions. So when we moved 4C to ah, that was the function number to terminate the program. The 0 in al is the return code.

Hello, World!

Ok here is a simple hello world program using DOS:


'mov dx, _msg
mov ah, 9
int 21h'

Another interrupt function call which prints a string the the standard output (default is the screen). Register dx contains the address of the string to print, ds holds the segment the string is in, but our string is in the same segment as our code so do not worry about this. ah again holds the function number.

'_msg db 'Hello, World!', 0Ah, 0Dh, '$''

This is a directive to the assembler to define a number of bytes (db means define byte). The bytes 0Ah and 0Dh represent the ASCII characters carriage return and line feed respectfully. This is just the stupid way that DOS encodes new lines (Also how Window encodes them). Now the $ character is equally as stupid, because this is the way DOS terminates a string (like C uses 0), there are other ways to print text but they will not be covered in this tutorial (Check out Int 21/AH=02h, or VGA).

Command Line Arguments

As this is just a introduction tutorial I will NOT show how to parse command line arguments.

Now if you are used to programming in HLL's (or assembly programming for other OS's), you will be used to nicely parsed command line arguments however DOS does not do this. DOS places the length of the command line at 80h. At 81h is the command line, everything after the executable name is placed here.

Simple Example:


Try it:


Notice that we read from 82h instead of 81h like I originally said, well that's because DOS doesn't remove the space between the executable name and the arguments, so.

The code is pretty self explanatory, it definitely not the best code but is easy to understand.

Notes

I plan to write a tutorial dedicated to VGA programming on DOS in the future, but I wouldn't hold my breath.

If you have any questions, please visit here first, before emailing me.

 

Comments