In this section, we will guide you through writing your first assembly program. We will cover the basic structure of an assembly program, how to write and compile it, and how to run it. By the end of this section, you will have a working assembly program that you can build upon in future lessons.

Key Concepts

  1. Assembly Program Structure: Understanding the basic layout of an assembly program.
  2. Writing the Code: Writing a simple program that prints "Hello, World!" to the console.
  3. Compiling the Program: Using an assembler to convert the assembly code into machine code.
  4. Running the Program: Executing the compiled program.

Assembly Program Structure

An assembly program typically consists of the following sections:

  • Data Section: Defines data elements.
  • BSS Section: Defines uninitialized data.
  • Text Section: Contains the actual code.

Example Structure

section .data
    ; Data section

section .bss
    ; BSS section

section .text
    global _start

_start:
    ; Code section

Writing the Code

Let's write a simple program that prints "Hello, World!" to the console. We will use the x86 architecture for this example.

Code Example

section .data
    hello db 'Hello, World!', 0xA  ; The string to print, followed by a newline character

section .bss

section .text
    global _start

_start:
    ; Write the string to stdout
    mov eax, 4          ; syscall number for sys_write
    mov ebx, 1          ; file descriptor 1 is stdout
    mov ecx, hello      ; pointer to the string
    mov edx, 13         ; length of the string
    int 0x80            ; call kernel

    ; Exit the program
    mov eax, 1          ; syscall number for sys_exit
    xor ebx, ebx        ; exit code 0
    int 0x80            ; call kernel

Explanation

  • section .data: This section contains initialized data. We define a string "Hello, World!" followed by a newline character.
  • section .text: This section contains the code. The _start label marks the entry point of the program.
  • mov eax, 4: This instruction sets the eax register to 4, which is the syscall number for sys_write.
  • mov ebx, 1: This sets the ebx register to 1, which is the file descriptor for stdout.
  • mov ecx, hello: This sets the ecx register to the address of the string we want to print.
  • mov edx, 13: This sets the edx register to the length of the string.
  • int 0x80: This triggers a software interrupt to make a system call.
  • mov eax, 1: This sets the eax register to 1, which is the syscall number for sys_exit.
  • xor ebx, ebx: This sets the ebx register to 0, which is the exit code.
  • int 0x80: This triggers a software interrupt to exit the program.

Compiling the Program

To compile the assembly program, we will use the nasm assembler and the ld linker.

Steps

  1. Save the Code: Save the code in a file named hello.asm.
  2. Assemble the Code: Use nasm to assemble the code.
    nasm -f elf32 hello.asm -o hello.o
    
  3. Link the Object File: Use ld to link the object file and create an executable.
    ld -m elf_i386 hello.o -o hello
    

Running the Program

To run the compiled program, use the following command:

./hello

You should see the output:

Hello, World!

Practical Exercise

Task

Write an assembly program that prints "Welcome to Assembly Programming!" to the console.

Solution

section .data
    welcome db 'Welcome to Assembly Programming!', 0xA

section .bss

section .text
    global _start

_start:
    mov eax, 4
    mov ebx, 1
    mov ecx, welcome
    mov edx, 31
    int 0x80

    mov eax, 1
    xor ebx, ebx
    int 0x80

Explanation

  • The string "Welcome to Assembly Programming!" is defined in the data section.
  • The length of the string is 31 characters.
  • The rest of the code follows the same structure as the "Hello, World!" program.

Common Mistakes and Tips

  • Incorrect String Length: Ensure the length of the string is correctly specified in the mov edx, <length> instruction.
  • Missing Newline Character: Adding a newline character (0xA) at the end of the string ensures the output is properly formatted.
  • File Permissions: Ensure the compiled executable has the correct permissions to run. Use chmod +x hello if necessary.

Conclusion

In this section, you learned how to write, compile, and run a simple assembly program. You now have a basic understanding of the structure of an assembly program and how to use system calls to interact with the operating system. In the next section, we will delve deeper into data representation and instructions.

© Copyright 2024. All rights reserved