Discovery Zone: Keeping Knowledge Free!
Search:
Keywords:
In Association with Amazon.com
Another great reference
cover 80X86 IBM PC and Compatible Computers: Assembly Language, Design and Interfacing Vol. I and II

ASSEMBLY PROGRAMMING AND OS DESIGN


2. Intro to Assembly
2.1 Data Types
2.2 Registers
2.3 DOS Int Calls
2.4 Basic Commands
2.4.1 DOS Hello World
2.5 BIOS Int Calls
2.5.1 BIOS Hello World
2.6 String Routines

<HOME>

 

 

 

Need a good reference?
cover
Assembly Language Step-by-Step: Programming with DOS and Linux, 2nd Edition
CH2.4.1 DOS Hello World Source

<HOME><TOC><PREV><UP><NEXT>

Regardless of your experience, you haven't really learned a language until you've written the ritual program of "Hello World."  Below is the source that prints hello world in the center of the text screen.

;=================================
;Hello World by Nathan Daniels
;Freely distributable, all rights
;waved by author.
;=================================
          org      100h           ;DOS executables all start at 100h
          jmp      START          ;jump to start of code

hello     db       "Hello World!!",13,10,"$"

START:    mov      AX,CS          ;Make sure all segments
          mov      DS,AX          ;point to this code

          mov      AH,9           ;function 9 prints string
                                  ;and updates cursor
          mov      DX,hello       ;DX points to string
          int      21h            ;call BIOS Video Routine
          ret

Every DOS executable has the basic format of having 256 bytes reserved at the beginning of it's memory.  Thus, when you write a DOS executable, you will always begin with org 100h to tell the compiler to start the code after 100h or 256 bytes of memory.

In typical fashion, the very next command is a jump to the start of the actual program (jmp START).  This allows you to declare all of your variables at the top like you would do in a higher level language.  It also makes the code easier to read because the reader has seen all the variables before they see the code that uses them.

The Hello World string is given the variable name hello.  It is declared in typical string format using the db or DATA BYTE type.  The string ends with carriage return - line feed pair (CR - LF) of ASCII 13 and 10 respectively.  To let DOS know the string is done, you also have to tack on a "$" at the end.

The first two lines of code make sure that the data segment points to the same segment where the code is.  DOS should do this by default, but it is bad programming to assume something is done.  In assembly, this can produce bugs that are nearly impossible to find.  As the saying goes, if you want something done right....do it yourself.  The AX register is the only register which can be used to move data into a segment register.  This is done for protection to ensure that errant code has a minimal chance of damaging segments that don't belong to it.  You know that the code segment is correct since your code is executing, so you use it as a source to fill the other two registers by moving the data from CS to AX (mov AX,CS).  Then you copy it into the data segment (mov DS,AX) to ensure your code is looking in the right place for its data. 

The next six lines of code set up for the DOS call to display a string.  AH contains the function number 09h which prints a string and is set up with mov AH,09hDX points to the string that needs to be displayed (mov DX,hello).

Now that the registers are set up, you just call int 21h which holds the DOS general functions and return (ret) to DOS.

<HOME><TOC><PREV><UP><NEXT>
 

www.cynergysoft.com

Email any questions to the author, Nathan Daniels.

Copyright © 2001