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