This page was generated from a source code file. Click here to download the file this page was generated from.
This series of articles on x86 assembly is a set of notes we're making as we learn DOS x86 assembly in a more systematic way than we've done so before. It is placed here in the hope it will be helpful (not least of all to us). If you have questions comments, ideas, or concerns, as always, please let us know!
These articles all use syntax for NASM, which is freely available and even works on DOS!
This file can be compiled with the command nasm -f bin -o x86hello.com x86hello.asm to produce x86hello.com.
These statements are instructions to nasm, rather than the CPU. They setup some assumptions that will help the assembler warn us of issues down the line.
1 | bits 16 ; Assume we're using 16-bit instructions by default. |
2 | org 100h ; Tell the assembler where the program will be loaded. |
3 | cpu 186 ; Tell the assembler not to allow instructions for the 286+. |
4 | start: |
There are four general-purpose 16-bit registers: ax, bx, cx, and dx. Each is divided into high and low bytes, and can be addressed as such e.g. by using ah (OOXXh) and al (XXOOh) (where OO is the byte being accessed).
DOS also provides interrupt 21h as a "catch-all" which performs different functions depending on what the byte in ah is set to when it is called.
In our example here, ah is set to 09h (the equivalent for the full ax register being 0900h). This is the index of the function that prints a string at the pointer placed in the dx register, terminated by a '$' symbol.
5 | mov ax, 900h ; Print-string-until-$-char service. |
6 | mov dx, hello ; Place a pointer to the hello string in the dx register. |
7 | int 21h ; Call function handler interrupt. |
COM programs in DOS should call the DOS "terminate" function when they are finished. This lets DOS effectively clean up after us (to the extent that it does) and return to the command interpreter.
8 | mov ah, 04ch ; Termination service. |
9 | int 21h ; Call function handler interrupt. |
We're building a COM file which, doesn't technically have sections, but nasm will kinda fake that for us. This lets us store e.g. strings in an organized fashion for use above.
10 | SECTION .data |
This is our string. db means "data bytes" and indicates a series of bytes, the string "pointer" is named for the label that precedes it, and it is terminated with a '$' symbol for the benefit of the 0900h function as described above.
11 | hello: db "Hello!$" |