Hello World Program in x86 Assembly Language

x86 Assembly programming language


In this article, we show how to create a hello world program in x86 assembly language.

So a hello world program in x86 assembly language is much different than anything you would encounter in most other languages.

This is because x86 is assembly language and deals directly with underlying registers.

These registers can only hold 1 or 2 bytes at a time.

When dealing with the AX register and breaking down into the high and low registers, which are ah and al, ah and al can only hold one byte at a given time.

A character is a byte. Therefore, in order to print 'Hello World', we can only pass one character at a time into the al register. The program reads one character at a time and then prints out the character. It can then proceed to the next character, read that character, and output it, until it has output all the characters in the data byte, 'Hello World'

This differs dramatically from other high-level programming languages, where you just pass in the whole string at once with no regard for the amount of data that can be read at once.

In order to do this program, we make use of many assembly programming concepts.

We will explain this after we show the code and go through each of its lines.



So the first thing we do is we create the statement, jmp main

We create this statement because the x86 emulator will run a program from the top of the code to the bottom.

We don't want this because we have a message label underneath that contains the string, 'Hello World'

If we didn't jump to main, then the program would try to execute the message label and it would give an error and the program wouldn't run because the x86 doesn't have enough bytes to process an entire string at once. Instead, we must read the string one byte (which is one character) at a time so that the program can't process and output the string without causing an error.

So we have this jump statement so that the program won't fail due to the message label.

So the program jumps to the main label.

Within this main label, we have the statement, mov si, message

The si register is a register that stands for Source Index register (SI). It is used as a pointer to a source in stream operations. Because we're streaming from this data byte string, 'Hello World', we utilize this register.

Next we have the statement, call print

This goes to the print label

There we have the statement, mov ah, 0eh

This allows us to print out each statement to the emulator screen one character at a time.

Underneath, we have the lodsb operation, which stands for load string byte, meaning it loads one byte at a time, which is one character.

Next, we have the statement, cmp al, 0

This statement checks to see if the byte loaded in is 0

If it is 0, this means that the string is ended. This is why there is a 0 at the end of 'Hello World'

If this is a match, then the program will jump to the .done label

If it is not 0, then the program runs the statement, int 10h

With this statement, the x86 program outputs each byte to the emulator screen as output.

Then the program jumps back to the ._loop label

It then reads in the next byte.

It checks to see whether this byte is 0. If it is, we have reached the end of the string and the program is complete.

If it is not 0, then we output the byte to the emulator screen.

This repeats until the byte read in is 0, signaling that the string is fully read and we have reached the end.

At the end, the program outputs the 'Hello World' string, as can be seen below.



Hello World Program output in x86 assembly language



So, there we can see the output on the emulator screen of this Hello World program.

To get this, make sure you click on the 'run' button of the 8086 emulator to get this output.

So this is how to create a Hello World program in the x86 assembly language.

Related Resources



HTML Comment Box is loading comments...