Time in Assembly Language

; Our first program will print the classic "hello world" message.
; Here's the full source code.

section .data
    message db 'hello world', 0x0A   ; Define the message with a newline character

section .text
    global _start

_start:
    ; Print the message
    mov eax, 4          ; System call number for sys_write
    mov ebx, 1          ; File descriptor 1 is stdout
    mov ecx, message    ; Address of the message
    mov edx, 12         ; Length of the message (including newline)
    int 0x80            ; Call kernel

    ; Exit the program
    mov eax, 1          ; System call number for sys_exit
    xor ebx, ebx        ; Return 0 status
    int 0x80            ; Call kernel

To assemble and run the program, you’ll need to use an assembler like NASM and a linker. Here are the steps:

$ nasm -f elf hello_world.asm
$ ld -m elf_i386 -o hello_world hello_world.o
$ ./hello_world
hello world

In Assembly, we don’t have the concept of a package or importing libraries like in high-level languages. Instead, we directly interact with the system through syscalls.

The program is divided into two sections:

  1. .data: This section is used to declare initialized data or constants. In this case, we define our “hello world” message.
  2. .text: This section contains the actual code.

The _start label is the entry point of the program. We use two system calls:

  1. sys_write (system call number 4) to print the message to stdout.
  2. sys_exit (system call number 1) to exit the program.

Assembly doesn’t have built-in functions like Println. Instead, we directly manipulate registers and make system calls to achieve the desired output.

To create an executable, we need to assemble the code into an object file and then link it. The resulting binary can be executed directly.

While Assembly provides low-level control, it requires more code and understanding of the system architecture compared to high-level languages. However, it can be very efficient when optimized correctly.