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 
  29 
  30 ; args [...]
  31 ;
  32 ; Show all ARGumentS given, one per line.
  33 
  34 
  35 stdout equ 1
  36 sys_write equ 1
  37 sys_exit equ 60
  38 line_feed equ 10
  39 
  40 section .text
  41 
  42 global _start
  43 
  44 _start:
  45 
  46 ; rcx = argc
  47 pop rcx
  48 
  49 ; skip argv[0]
  50 pop rbx
  51 ; get argv[1]
  52 pop rbx
  53 
  54 ; find length of byte-slice using the arg-count, replacing each null byte
  55 ; with a line-feed byte along the way
  56 
  57 ; for (rdx = 0, rcx = rcx - 1; rcx != 0; rcx--, rbx++, rdx++)
  58 mov rdx, 0
  59 dec rcx
  60 args_loop:
  61     ; quit loop when rcx == 0
  62     cmp rcx, 0
  63     je args_loop_done
  64 
  65     ; check current byte
  66     mov al, [rbx]
  67     cmp al, 0
  68     jne not_null
  69         ; if (byte @ rbx == 0) { byte @ rbx = '\n'; rcx--; }
  70         mov al, line_feed
  71         mov [rbx], al
  72         dec rcx
  73     not_null:
  74 
  75     inc rbx
  76     inc rdx
  77     jmp args_loop
  78 
  79 args_loop_done:
  80 
  81 ; make rbx point to the start of the multi-line byte-slice, which had all
  82 ; nulls replaced by line-feed bytes
  83 sub rbx, rdx
  84 
  85 ; write(stdout, message = rbx, message_length = rdx)
  86     mov rax, sys_write
  87     mov rdi, stdout
  88     mov rsi, rbx
  89     syscall
  90 
  91 ; exit(0)
  92     mov rax, sys_exit
  93     mov rdi, 0
  94     syscall