HelloWorld in Assembly Language

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.

Additional Notes

  • 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
Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

6 Responses to HelloWorld in Assembly Language

  1. sid says:

    it was very helpful tutorial. tnx a lot!! 🙂

  2. sam hans says:

    bahul acchha tutorial hai 🙂

  3. Artur says:

    I’m sorry but i can’t run my “ProgramBuildAndRun-Fasm.bat” file.In console I see :”The filename
    ,directory name,or volume label syntax is incorrect”. I replace [the directory where fasm.exe is located] to my path [C:\fasmw168\FASM.exe].Please help me!Thank you very much!

  4. Alvin chang says:

    Is there one flat assembler that is for windows 10 64bit os?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s