Printing “Hello, World!” in x86 Assembly language isn’t an easy job compared to any other programming language. Even Assembly Language is the name of fear for many programmers. But everyone wants to try this language for one time.
And for those people, this article was written. In this article, I will show you how you can write a “Hello, World!” assembly code. It’s not that much easy, but if you follow me, you can do it easily.
section .data hello db 'Hello, World!',0 section .text global _start _start: ; write(1, hello, 13) mov edx, 13 ; message length mov ecx, hello ; message to write mov ebx, 1 ; file descriptor (stdout) mov eax, 4 ; system call for sys_write int 0x80 ; call kernel ; exit(0) mov eax, 1 ; system call for sys_exit xor ebx, ebx ; exit code (0) int 0x80 ; call kernel
This is an example of a “Hello, World!” program written in assembly language. It uses the Linux system call interface to write the string “Hello, World!” to the console and then exits.
This code uses the x86 assembly language, it uses the Linux syscall interface, and it is for 32-bit systems. The first section is the data section, where we define the string “Hello, World!” which is called hello. The second section is the text section, where the code is written. The first instruction is the start of the program; the global _start tells the linker that the _start label is the program’s entry point.
The program uses the write syscall to write the string “Hello, World!” to the stdout, the syscall number for write is 4, and it takes three arguments, the file descriptor, the message, and the length of the message.
The last instruction is the exit syscall which is used to exit the program with a status code, the syscall number for exit is one, and it takes one argument, the status code.
Remember that assembly code is highly dependent on the system and architecture, so this code might not work on all systems.
How to run assembly code on Linux?
There are several steps to run assembly code on a Linux system:
- Assemble the code
An assembler needs to convert the assembly code into machine code. The most common assembler for Linux is
NASM (Netwide Assembler). You can use the following command to assemble the code:
nasm -f elf -o program.o program.asm
This will create an object file called
program.o from the assembly source file
- Link the code
The object file needs to be linked with the C library to create an executable binary. You can use the following command to link the code:
ld -s -o program program.o
This will create an executable file called
program from the object file
- Run the code
You can run the code by using the following command:
If you want to debug the assembly code, you can use GDB (GNU Debugger) to step through the assembly code and see the values of registers, memory, and other information.
gdb -q program
Remember that the commands and processes for running assembly code may vary depending on the operating system and architecture.