|
|||
ASSEMBLY PROGRAMMING AND OS DESIGN
<HOME>
|
CH2.5.1 BIOS 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. 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,1301h. BH 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,0007h. DH 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. |
| www.cynergysoft.com |
Email any questions to the author, Nathan Daniels. |
Copyright © 2001 |