File: echo-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 echo-linux-amd64.o echo-linux-amd64.asm
  27 ; ld -n -s echo-linux-amd64.o -o echo
  28 ; rm echo-linux-amd64.o
  29 
  30 
  31 ; echo [words...]
  32 ;
  33 ; Emit a line with all the words given as command-line arguments.
  34 
  35 
  36 stdout equ 1
  37 sys_write equ 1
  38 sys_exit equ 60
  39 
  40 line_feed equ 10
  41 
  42 section .rodata
  43     empty_line: db line_feed
  44 
  45 section .text
  46 
  47 global _start
  48 
  49 _start:
  50 
  51 ; rcx = argc
  52 pop rcx
  53 
  54 ; skip argv[0]
  55 pop rbx
  56 ; get argv[1]
  57 pop rbx
  58 
  59 ; find length of byte-slice using the arg-count, replacing each null byte
  60 ; with a space along the way
  61 
  62 ; for (rdx = 0, rcx = rcx - 1; rcx != 0; rcx--, rbx++, rdx++)
  63 mov rdx, 0
  64 dec rcx
  65 args_loop:
  66     ; quit loop when rcx == 0
  67     cmp rcx, 0
  68     je args_loop_done
  69 
  70     ; check current byte
  71     mov al, [rbx]
  72     cmp al, 0
  73     jne not_null
  74         ; if (byte @ rbx == 0) { byte @ rbx = ' '; rcx--; }
  75         mov al, ' '
  76         mov [rbx], al
  77         dec rcx
  78     not_null:
  79 
  80     inc rbx
  81     inc rdx
  82     jmp args_loop
  83 
  84 args_loop_done:
  85 
  86 ; if (rdx == 0) { rbx = &empty_line; rdx = 1; } else byte @ rbx - 1 = '\n';
  87 cmp rdx, 0
  88 je got_no_words
  89     mov al, line_feed
  90     mov [rbx-1], al
  91     jmp got_words
  92 got_no_words:
  93     mov rbx, empty_line
  94     inc rbx
  95     mov rdx, 1
  96 got_words:
  97 
  98 ; make rbx point to the start of the multi-line byte-slice, which had all
  99 ; nulls replaced by spaces, except for the final null, which was replaced
 100 ; by a line-feed
 101 sub rbx, rdx
 102 
 103 ; write(stdout, message = rbx, message_length = rdx)
 104     mov rax, sys_write
 105     mov rdi, stdout
 106     mov rsi, rbx
 107     syscall
 108 ; exit(0)
 109     mov rax, sys_exit
 110     mov rdi, 0
 111     syscall