|
|||
ASSEMBLY PROGRAMMING AND OS DESIGN
<HOME>
|
CH2.4.1 DOS Hello World
Source
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. ;================================= 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,09h. DX 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. |
| www.cynergysoft.com |
Email any questions to the author, Nathan Daniels. |
Copyright © 2001 |