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.5.1 BIOS 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      ES,AX

          mov      AX,1301h       ;function 1301h prints string
                                  ;and updates cursor
          mov      BX,0007h       ;page 0 attribute 7 (normal)
          mov      DH,12          ;row 12 and column 32 of the
          mov      DL,32          ;display screen
          mov      BP,hello       ;BP points to string
          mov      CX,15          ;CX contains length of string
          int      10h            ;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.

The first three lines of code make sure that the data segment and the extra segment point 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) and the extra segment (mov ES,AX) to ensure your code is looking in the right place for its data. 

The next six lines of code set up for the BIOS call to display a string.  AH contains the function number 13h which prints a string to a given screen coordinate.  AL is a flags register for the function.  Bit 0 of that register is the flag that indicates the cursor will be updated to the end of the string if the flag is true (1).  Since AX can be divided into AH|AL, you can set them both up in one line with mov AX,1301hBH contains the display page number which is 0 by default.  BL is the text attribute where 7 is white text on a black background.  Again, this can be done in one line with mov BX,0007hDH holds the row at which the text will be displayed (mov DH,12) and DL holds the column (mov DL,32).  You could fill both of these with one command, but by using two, the code is much more readable.  BP points to the string that needs to be displayed (mov BP,hello).  Finally, CX holds the number of characters to be displayed (mov CX,15).

Now that the registers are set up, you just call int 10h which holds the BIOS VGA display routines and return (ret) to DOS.

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

www.cynergysoft.com

Email any questions to the author, Nathan Daniels.

Copyright © 2001