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