📜  nasm hello world (1)

📅  最后修改于: 2023-12-03 15:33:05.437000             🧑  作者: Mango

Nasun Hello World

Introduction

In the field of computer programming, "Hello, World!" is often the first program people write when learning a new language or platform. In this tutorial, we will be using NASM (Netwide Assembler) to create a "Hello, World!" program in x86 assembly language.

Prerequisites

Before we start, make sure you have NASM installed on your computer. If you don't, you can download it from the NASM website.

Writing the Program

The "Hello, World!" program in NASM is pretty simple. Here's the code:

section .data
    msg     db      'Hello, World!',0xa
    len     equ     $-msg

section .text
    global  _start

_start:
    ; write the message to stdout
    mov     eax,4       ; system call for 'write'
    mov     ebx,1       ; file descriptor 1 (stdout)
    mov     ecx,msg     ; message to write
    mov     edx,len     ; message length
    int     0x80        ; call kernel

    ; exit program with status code 0
    mov     eax,1       ; system call for 'exit'
    xor     ebx,ebx     ; return status code 0
    int     0x80        ; call kernel

Let's break it down:

The .data section is where we define our data. In this case, we define a string called msg with the value "Hello, World!" followed by a newline character (0xa). We also define a constant len that is set to the length of the msg string.

The .text section is where we define our code. We begin by declaring _start as a globally visible symbol. This is the entry point for our program.

Inside _start, we use the mov instruction to set the parameters for the write system call. The eax register is set to 4, which is the system call for write. The ebx register is set to 1, which is the file descriptor for stdout. The ecx register is set to the memory location of msg, which is the message we want to write to stdout. The edx register is set to len, which is the length of msg.

After the write system call, we use the mov instruction again to set the parameters for the exit system call. The eax register is set to 1, which is the system call for exit. The ebx register is set to 0, which is the return status code for a successful program.

Finally, we call the kernel with int 0x80 to execute the system calls.

Assembling and Linking the Program

Now that we've written our program, we need to assemble and link it. Here's how:

  1. Save the code to a file called hello.asm.
  2. Open a terminal window and navigate to the directory where hello.asm is located.
  3. Run the following command to assemble the program: nasm -f elf hello.asm.
  4. Run the following command to link the program: ld -m elf_i386 -s -o hello hello.o.
  5. Run the program by entering ./hello in the terminal.
Conclusion

Congratulations, you have successfully written, assembled and linked a "Hello, World!" program using NASM! While this tutorial only scratches the surface of x86 assembly language, it should give you a basic understanding of how to use NASM to create simple programs. Happy coding!