File: args-linux-amd64.asm
   1 ; The MIT License (MIT)
   2 ;
   3 ; Copyright (c) 2026 pacman64
   4 ;
   5 ; Permission is hereby granted, free of charge, to any person obtaining a copy
   6 ; of this software and associated documentation files (the "Software"), to deal
   7 ; in the Software without restriction, including without limitation the rights
   8 ; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   9 ; copies of the Software, and to permit persons to whom the Software is
  10 ; furnished to do so, subject to the following conditions:
  11 ;
  12 ; The above copyright notice and this permission notice shall be included in
  13 ; all copies or substantial portions of the Software.
  14 ;
  15 ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16 ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17 ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18 ; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19 ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20 ; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21 ; SOFTWARE.
  22 
  23 
  24 ; To compile this app for linux/amd64, use the commands
  25 ;
  26 ; nasm -f elf64 -o args-linux-amd64.o args-linux-amd64.asm
  27 ; ld -n -s args-linux-amd64.o -o args
  28 ; rm args-linux-amd64.o
  29 
  30 
  31 ; args [...]
  32 ;
  33 ; Show all ARGumentS given, one per line.
  34 
  35 
  36 stdout equ 1
  37 sys_write equ 1
  38 sys_exit equ 60
  39 line_feed equ 10
  40 
  41 section .text
  42 
  43 global _start
  44 
  45 _start:
  46 
  47 ; rcx = argc
  48 pop rcx
  49 
  50 ; skip argv[0]
  51 pop rbx
  52 ; get argv[1]
  53 pop rbx
  54 
  55 ; find length of byte-slice using the arg-count, replacing each null byte
  56 ; with a line-feed byte along the way
  57 
  58 ; for (rdx = 0, rcx = rcx - 1; rcx != 0; rcx--, rbx++, rdx++)
  59 mov rdx, 0
  60 dec rcx
  61 args_loop:
  62     ; quit loop when rcx == 0
  63     cmp rcx, 0
  64     je args_loop_done
  65 
  66     ; check current byte
  67     mov al, [rbx]
  68     cmp al, 0
  69     jne not_null
  70         ; if (byte @ rbx == 0) { byte @ rbx = '\n'; rcx--; }
  71         mov al, line_feed
  72         mov [rbx], al
  73         dec rcx
  74     not_null:
  75 
  76     inc rbx
  77     inc rdx
  78     jmp args_loop
  79 
  80 args_loop_done:
  81 
  82 ; make rbx point to the start of the multi-line byte-slice, which had all
  83 ; nulls replaced by line-feed bytes
  84 sub rbx, rdx
  85 
  86 ; write(stdout, message = rbx, message_length = rdx)
  87     mov rax, sys_write
  88     mov rdi, stdout
  89     mov rsi, rbx
  90     syscall
  91 
  92 ; exit(0)
  93     mov rax, sys_exit
  94     mov rdi, 0
  95     syscall