; define some macros
%define O_RDONLY    00000000o
%define O_WRONLY    00000001o
%define O_RDWR      00000002o
%define O_CREAT     00000100o
%define O_TRUNC     00001000o
%define S_IRUSR     00000400o
%define S_IWUSR     00000200o


section .data
    path: db "/home/user/nachricht", 0
    greeting: db "hidad!", 0

    ; https://stackoverflow.com/a/45386640
    length equ $ - greeting ; Length of greeting message


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md#x86-32_bit;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section .text
global _start
_start:
    call _open              ; open file
    call _write             ; write to file
    call _close             ; close file
    call _exit              ; exit without errors

; https://www.man7.org/linux/man-pages/man2/open.2.html
_open:
    mov eax, 0x05           ; syscall number for 'open'
    mov ebx, path           ; pointer to the path string
    mov ecx, O_CREAT        ; open the file if it doesn't exist
    xor ecx, O_WRONLY       ; open the file in write-only mode
    mov edx, S_IRUSR        ; permissions for the file
    int 0x80                ; call kernel

; https://man7.org/linux/man-pages/man2/write.2.html
_write:
    mov ebx, eax            ; file descriptor returned by 'open'
    mov eax, 0x04           ; syscall number for 'write'
    mov ecx, greeting       ; pointer to the greeting message
    mov edx, length         ; length of the greeting message
    sub edx, 0x01           ; length of the greeting message without null byte
    int 0x80                ; call kernel

; https://man7.org/linux/man-pages/man2/close.2.html
_close:
    mov eax, 0x06           ; syscall number for 'close'
    int 0x80                ; call kernel

; https://man7.org/linux/man-pages/man2/exit.2.html
_exit:
    mov eax, 1              ; syscall number for 'exit'
    mov ebx, 0              ; exit status
    int 0x80                ; call kernel
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
