If computers were mammals, assembly language would be the limbic system. Wait, that’s a terrible analogy. Never mind.
1. Download the latest version of FASM (“Flat Assembler”), by Tomasz Grysztar. As of this writing, FASM is available at http://flatassembler.net/.
2. Install FASM by uncompressing the archive to the desired directory. Make a note of the directory where the file “fasm.exe” is located.
3. In any convenient location, create a new directory named “HelloWorld”.
4. In the newly created HelloWorld directory, create a new file named “HelloWorld.asm”, containing the following text.
org 0x0100 ; offset the code to where the OS requires it to be Main: mov si,TextHelloWorld ; point the si register at the string to display mov ah,0x0E ; for int 0x10: write chars in teletype mode ForEachChar: ; begin loop lodsb ; load al with what si points to, increment si cmp al,0x00 ; if char is null... je EndForEachChar ; .. then break out of the loop int 0x10 ; call interrupt 0x10 (BIOS: print char) jmp ForEachChar ; jump back to beginning of loop EndForEachChar: ; end of the loop ret ; quit the program ; data to display TextHelloWorld: db 'Hello, world!',0
5. Still in the HelloWorld directory, create a new file called “BuildEnvironmentSetup-Fasm.bat”, containing the following text. Substitute the path of the directory where fasm.exe is located in the indicated place.
set fasmPath=[the directory where fasm.exe is located] for %%* in (.) do (set programName=%%~n*)
6. Still in the HelloWorld directory, create a new file called “ProgramBuildAndRun-Fasm.bat”, containing the following text.
call BuildEnvironmentSetup-Fasm.bat %fasmPath%\fasm.exe %programName%.asm %programName%.com %programName%.com pause
7. Double-click the icon of ProgramBuildAndRun-Fasm.bat to run it. A console window will appear, the HelloWorld.asm file will be assembled, and a file named “HelloWorld.com” will be created and executed. The text “Hello, world!” should be visible in the console window.
- This procedure creates a com file, rather than an exe file. Com files are a legacy of MS-DOS, and, if memory serves, are limited to something like 64 kibibytes in size.
- The program created by this tutorial uses a BIOS interrupt routine to perform the display of a character. DOS and Windows each introduced their own, more complex interrupt routines. Windows actively intervenes to prevent many BIOS interrupts from being called, notably the ones for graphics and disk access.
- An alternate implementation of HelloWorld, featuring a procedure declaration and call, is shown below.
org 0x0100 ; offset the code to where the OS requires it to be Main: ; push TextHelloWorld ; push a parameter to display... call DisplayStringWriteToConsole ; ...and call the display procedure ; ret ; exit the program ; the message to display TextHelloWorld: db 'Hello, world!',0 DisplayStringWriteToConsole: ; (stringToWrite) ; push bp ; save bp before overwriting it... mov bp,sp ; ...with the return address push ax ; save the old values of ax and si... push si ; ...before modifying them ; mov si,[bp+4] ; first parameter: stringToWrite ; mov ah,0x0E ; int 0x10: write character in teletype mode ; ForEachChar: lodsb ; copy what si points to to al, increment si cmp al,0x00 ; if char to display is null... je EndForEachChar ; ... then break int 0x10 ; interrupt 0x10: BIOS draw char jmp ForEachChar EndForEachChar: ; pop si ; return registers to what they were... pop ax ; .. before the procedure was called pop bp ret 2 ; dispose the 2-byte parameter and return to caller