Skip to content

Instantly share code, notes, and snippets.

@gengwg
Created March 14, 2025 17:30
Show Gist options
  • Save gengwg/b2358312879644e8a4c768f3fa1c082a to your computer and use it in GitHub Desktop.
Save gengwg/b2358312879644e8a4c768f3fa1c082a to your computer and use it in GitHub Desktop.
"Writing and Running a Simple 'Hello, World!' Program in x86-64 Assembly on Fedora Linux"

Below is a simple "Hello, World!" program written in x86-64 assembly language for Linux. I'll also provide instructions on how to assemble, link, and run it on Fedora Linux.

Hello World Assembly Code (x86-64)

Save the following code in a file named hello.asm:

section .data
    hello db 'Hello, World!', 0x0A  ; 'Hello, World!' followed by a newline (0x0A)

section .text
    global _start

_start:
    ; Write 'Hello, World!' to stdout
    mov eax, 1          ; syscall: sys_write
    mov edi, 1           ; file descriptor: stdout
    lea rsi, [hello]     ; address of the string to write
    mov edx, 14          ; length of the string (13 characters + 1 newline)
    syscall              ; invoke the system call

    ; Exit the program
    mov eax, 60          ; syscall: sys_exit
    xor edi, edi         ; exit code: 0
    syscall              ; invoke the system call

Instructions to Assemble, Link, and Run on Fedora Linux

  1. Install the necessary tools (if not already installed): You need nasm (Netwide Assembler) and ld (GNU linker). Install them using dnf:

    sudo dnf install nasm binutils
  2. Assemble the code: Use nasm to assemble the hello.asm file into an object file:

    nasm -f elf64 hello.asm -o hello.o

    This generates an object file named hello.o.

  3. Link the object file: Use ld to link the object file into an executable:

    ld hello.o -o hello

    This creates an executable file named hello.

  4. Run the program: Execute the program using:

    ./hello

    You should see the output:

    Hello, World!
    

Explanation of the Code

  • .data section: Contains the string Hello, World! and a newline character (0x0A).
  • .text section: Contains the program's instructions.
  • _start: The entry point of the program.
  • System calls:
    • sys_write (eax = 1): Writes the string to stdout.
    • sys_exit (eax = 60): Exits the program with a status code of 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment